Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Do you have any clue how i could achieve this ?

Here is my building process, i'm using gulp with browserify and babelify :

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. Would there be a way to achieve something like that ?

Thanks,

Regards

like image 753
Samuel Maisonneuve Avatar asked Jun 11 '15 09:06

Samuel Maisonneuve


People also ask

How do I add presets to Babel config?

Using a Preset Within a Babel config, if the preset is on npm, you can pass in the name of the preset and Babel will check that it's installed in node_modules already. This is added to the presets config option, which takes an array. Otherwise, you can also specify a relative or absolute path to your presets.

What is Babel choose the correct option?

Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.

Does Babel preset order matter?

Ordering matters for each visitor in the plugin. This means if two transforms both visit the "Program" node, the transforms will run in either plugin or preset order. Plugins run before Presets. Plugin ordering is first to last.


1 Answers

This has changed recently in 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.

like image 148
mik01aj Avatar answered Sep 18 '22 18:09

mik01aj