创建和使用Babel插件而无需使其成为npm模块 [英] Create and use Babel plugin without making it a npm module
问题描述
在我的项目中,我将Babel 6与require挂钩一起使用.我需要加载我编写的自定义babel插件.但是我真的需要先使用npm发布插件,然后在主项目的.babelrc
中包含插件名称吗?
In my project, I'm using Babel 6 with the require hook. I need to load a custom babel plugin that I wrote. But do I really need to publish my plugin using npm first, and then include the plugin name in my main project's .babelrc
?
有什么方法可以直接加载插件代码?换句话说,我可以直接加载以下内容吗?
Is there any way to just directly load the plugin code? In other words, can I just load the following directly?
export default function({types: t }) {
return {
visitor: {
...
}
};
}
推荐答案
在.babelrc中列出插件的位置,请提供插件路径,而不是标准的已发布插件名称.>
Where you list your plugins in your .babelrc, provide the path to your plugin instead of your standard published plugin name.
"plugins": ["transform-react-jsx", "./your/plugin/location"]
导出插件功能时,您可能需要使用module.exports =
而不是export default
,因为ES2015模块尚未在Node中完全实现.
When exporting your plugin function, you'll probably need to use module.exports =
instead of export default
, since ES2015 modules haven't been fully implemented in Node yet.
这篇关于创建和使用Babel插件而无需使其成为npm模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!