kentcdodds / babel-plugin-macros

🎣 Allows you to build simple compile-time libraries
https://npm.im/babel-plugin-macros
MIT License
2.62k stars 135 forks source link

Feature: Support configuration passed through the macro plugin #112

Closed conartist6 closed 5 years ago

conartist6 commented 5 years ago

Currently it is possible to configure macros through a configuration file. I propose additionally that it be possible to configure a macro through the babel configuration itself.

I would like to have this functionality because I am using babel plugin macros to split certain files into two similar but subtly different versions. Just for clarity, the versions are sync and async. This means that I always need to run the plugin twice, once per version. Previously I was using process.env to pass the sync/async flag, but now I wish to be able to transpile inside a watcher callback, which makes it unsafe to use a global.

Currently the only data which I can access from the transpile call is the filename, which I am loth to repurpose.

I think what should happen is that a macro should receive a slice of the babel-plugin-macros settings:

babel.transformFileSync(templateFilename, {
  plugins: [
    [ 'macros', { 'my-macro': { setting: true} }],
  ],
});

This would presumably be merged with the contents of the cosmiconfig settings file for the macro.

I also looked into using the new caller setting to sneak past babel-plugin-macros entirely, but it turns out this is not possible because it is only possible to access caller prior to the start of the traversal, and babel-plugin-macros only instantiates its plugins inside its traversal.

kentcdodds commented 5 years ago

Assuming it doesn't complicate things too much, I'm in favor and would accept a poll request for this.

kentcdodds commented 5 years ago

:tada: This issue has been resolved in version 2.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: