插件参数
插件和 preset 都可以接受参数,参数由插件名和参数对象组成一个数组,可以在配置文件中设置。
如果不指定参数,下面这几种形式都是一样的:
babel.config.json
{
"plugins": ["pluginA", ["pluginA"], ["pluginA", {}]]
}
要指定参数,请传递一个以参数名作为键(key)的对象。
babel.config.json
{
"plugins": [
[
"transform-async-to-module-method",
{
"module": "bluebird",
"method": "coroutine",
moduleName: 'name',
}
]
]
}
preset 的设置参数的工作原理完全相同:
babel.config.json
{
"presets": [
[
"env",
{
"loose": true,
"modules": false
}
]
]
}
插件开发
export default function() {
return {
visitor: {
Identifier(path, state) {
const name = path.node.name;
const { moduleName } = state.opts;
// reverse the name: JavaScript -> tpircSavaJ
path.node.name = name
.split("")
.reverse()
.join("");
},
},
};
}
其中 Identifier
为节点类型, 也可以是 ImportDeclaration
和 Program
等。