[英]Is there a way to give custom options to a babel plugin?
I might be wrong but i couldn't find any solution to give some custom options to my custom babel plugin. 我可能错了,但我找不到任何解决方案,为我的自定义babel插件提供一些自定义选项。 Do you have any clue how i could achieve this ? 你有什么线索我能做到这一点吗?
Here is my building process, i'm using gulp with browserify and babelify : 这是我的构建过程,我正在使用浏览器和babelify gulp:
gulp.task("scripts", function() {
return browserify({
entries: "myFile.js"
})
.transform(babelify.configure({
plugins: ["./lib/myPlugin:after"]
}))
.bundle()
.pipe(source("all.js"))
.pipe("build/");
});
I would like to give some custom data to my plugin, doing something like this : 我想给我的插件提供一些自定义数据,做这样的事情:
gulp.task("scripts", function() {
return browserify({
entries: "myFile.js"
})
.transform(babelify.configure({
myCustomOptions: {
rootPath: "some path",
customInfo: "..."
}
plugins: ["./lib/myPlugin:after"]
}))
.bundle()
.pipe(source("all.js"))
.pipe("build/");
});
Then in my plugin, i would like to retrieve the customOptions object i just declared. 然后在我的插件中,我想检索刚刚声明的customOptions对象。 Would there be a way to achieve something like that ? 有没有办法实现这样的目标?
Thanks, 谢谢,
Regards 问候
This has changed recently in Babel 6 . 最近在Babel 6中发生了变化。 From the docs : 来自文档 :
Plugins can specify options. 插件可以指定选项。 You can do so in your config by wrapping it in an array and providing a options object. 您可以通过将其包装在数组中并提供选项对象来在配置中执行此操作。 For example: 例如:
{ "plugins": [ ["transform-async-to-module-method", { "module": "bluebird", "method": "coroutine" }] ] }
Plugin Options documentation in the Babel plugin handbook. Babel插件手册中的插件选项文档。
I found a way out but i'm pretty sure it's not a proper way to go : 我找到了出路,但我很确定这不是一个正确的方法:
Reading babel code, it seems there is a hidden option called "extra". 阅读巴贝尔代码,似乎有一个名为“额外”的隐藏选项。 I used that option to push my custom options : 我使用该选项来推送自定义选项:
gulp.task("scripts", function() {
return browserify({
entries: "myFile.js"
})
.transform(babelify.configure({
extra: {
myPlugin: {
// here i put my custom data
}
},
plugins: ["./lib/myPlugin:after"]
}))
.bundle()
.pipe(source("all.js"))
.pipe("build/");
});
Then in my plugin i can retrieve my custom options like this : 然后在我的插件中,我可以检索我的自定义选项,如下所示:
var myPlugin = function(babel) {
var t = babel.types;
var pluginConf;
return new babel.Transformer("babel-platform", {
CallExpression(node, parent, scope, config) {
pluginConf = pluginConf || config.opts.extra["myPlugin"] || {};
// Doing some stuff here on the CallExpression
}
});
}
I know this is definitely not a proper way. 我知道这绝对不是一个正确的方法。 Would you have any alternatives ? 你有其他选择吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.