babel-plugin-feature-flags icon indicating copy to clipboard operation
babel-plugin-feature-flags copied to clipboard

Support multiple modules.

Open rwjblue opened this issue 8 years ago • 5 comments

Ember is migrating from import isEnabled from 'ember-metal/features' to import { isFeatureEnabled } from 'ember-metal';. During the transition (while the code is being updated piece by piece) it would be nice to instruct this plugin to simply process both modules. Unfortunately, that doesn't seem possible today.

One possible solution is something like:

{
  "plugins": [["feature-flags", [
    {
      "import": {
          "module": "ember-metal/features"
      },
      "features": {
          "new-feature": "disabled"
      }
    },
    {
      "import": {
          "module": "ember-metal",
          "name": "isFeatureEnabled"        
      },
      "features": {
          "new-feature": "disabled"
      }
    }
  ]]
}

@mmun - Thoughts?

rwjblue avatar Sep 03 '16 04:09 rwjblue

I have not explicitly tried adding the same plugin twice in Babel 6, but in Babel 5 (v2.x branch) doing so results in an error.

I tried:

  var applyFeatureFlags = require('babel-plugin-feature-flags');

  plugins.push(applyFeatureFlags({
    import: { module: 'ember-metal/features' },
    features: features
  }));

  plugins.push(applyFeatureFlags({
    import: { module: 'ember-metal', name: 'isFeatureEnabled' },
    features: features
  }));

Which results in:

File: container/container.js
The Broccoli Plugin: [Babel: /ember.debug.js] failed with:
ReferenceError: The plugin "babel-plugin-feature-flags" collides with another of the same name
    at PluginManager.validate (/Users/rjck/Source/emberjs/emberjs-build/node_modules/babel-core/lib/transformation/file/plugin-manager.js:159:13)
    at PluginManager.add (/Users/rjck/Source/emberjs/emberjs-build/node_modules/babel-core/lib/transformation/file/plugin-manager.js:213:10)
    at File.buildTransformers (/Users/rjck/Source/emberjs/emberjs-build/node_modules/babel-core/lib/transformation/file/index.js:237:21)
    at new File (/Users/rjck/Source/emberjs/emberjs-build/node_modules/babel-core/lib/transformation/file/index.js:139:10)
    at Pipeline.transform (/Users/rjck/Source/emberjs/emberjs-build/node_modules/babel-core/lib/transformation/pipeline.js:164:16)
    at Babel.transform (/Users/rjck/Source/emberjs/emberjs-build/node_modules/broccoli-babel-transpiler/index.js:105:21)
    at Babel.processString (/Users/rjck/Source/emberjs/emberjs-build/node_modules/broccoli-babel-transpiler/index.js:204:25)

rwjblue avatar Sep 03 '16 04:09 rwjblue

I was going to suggesting adding it twice but I beat me to it. I'd hope that it is supported in Babel 6! We can definitely tweak the options for the Babel 5 version though.

mmun avatar Sep 03 '16 06:09 mmun

I think I'd prefer attaching the plugin only once and replacing the import option with an imports option that supports multiple import styles.

Turbo87 avatar Sep 03 '16 19:09 Turbo87

@Turbo87 I mildly disagree, but if you code it I'll merge it.

Having the plugin support multiple feature flags seems overly coupled to me. It would be as if Object.keys took an array of objects like Object.keys([obj1, obj2]) and returned an array of arrays of keys: [keysOfObj1, keysOfObj2]. There's no point in doing that when you can use composition: [obj1, obj2].map(Object.keys).

There might be a performance implication of adding more plugins, but I'm pretty sure Babel would compress the two plugins into one pass.

mmun avatar Sep 03 '16 19:09 mmun

I like @Turbo87's implementation where the feature flags are shared among all the imports.

This has landed on the babel-5 branch but I'll leave it open until it lands on master.

mmun avatar Sep 11 '16 00:09 mmun