Skip to content
This repository has been archived by the owner on Oct 15, 2019. It is now read-only.

Babel throws an error when a preset is passed options but doesn't accept them #6

Open
ericf opened this issue Aug 19, 2016 · 9 comments · May be fixed by #8
Open

Babel throws an error when a preset is passed options but doesn't accept them #6

ericf opened this issue Aug 19, 2016 · 9 comments · May be fixed by #8

Comments

@ericf
Copy link

ericf commented Aug 19, 2016

I ran into an issue using this lib because it passes options ({modules: false}) to all presets; e.g. babel-preset-react, even if that preset doesn't accept options. When this happens Babel errors out.

@deavial
Copy link

deavial commented Aug 27, 2016

same issue and have not been able to compile code

@sebinsua
Copy link

sebinsua commented Sep 4, 2016

Same issue here. :/

This seems to fix it:

function mapPreset(preset, options) {
  var info = getPresetInfo(preset);

  if (!info) {
    return preset;
  }

  if (options.findRollupPresets && hasRollupVersionOfPreset(info.name, options.resolve || resolve.sync)) {
    return [info.name + '-rollup', info.options];
  } else if (options.addModuleOptions) {
    const options = info.name === 'es2015' ? _extends({}, info.options, { modules: false }) : []
    return [info.name].concat(options);
  } else {
    return preset;
  }
}

Not sure which other modules accept options? I just made the change above temporarily so I could publish.

@gabrielcorado
Copy link

I'm having the same issue. Any updates?

@brabeji
Copy link

brabeji commented Nov 4, 2016

To disable that behavior:

...
    plugins: [
        babel(babelrc({ addModuleOptions: false })),
    ],
...

@vitorfox
Copy link

@brabeji where do you put that config?

@ramingar
Copy link

In your rollup.config.js.

@rhysburnie
Copy link

rhysburnie commented Apr 16, 2017

Hi @eventualbuddha,

I think the fix described by @sebinsua is much better.
Currently it just blindly adds { "modules": false } to every preset even though most don't have that option and will complain.

I don't consider babelrc({ addModuleOptions: false }) to be a fix...

In that situation you will then get an error (babel plugin) It looks like your Babel configuration specifies a module transformer. Please disable it.

ie. you have to add in manually to your .babelrc, however in many cases (for instance using AVA test suite) the preset needs to be "es2015" or ["es2015", { "modules": true}]" so the test runner can interpret import.

Simply put the mapPreset function should only add modules: false if the preset is one that actually has the option modules as suggested by @sebinsua (whether that includes more presets than es2015 I don't know).

I was having issues because I need the following for AVA (and any other normal es w/ modules)

{
  "presets": [
    "es2015",
    "stage-2"
  ]
}

but this plugin incorrectly adds ["stage-2", { "modules": false }]

For the time being I have to loop over the presets returned by babelrc() and remove "modules": false from all but es2015. before passing it to the rollup babel plugin.

UPDATE

In addition to this it turns out some presets can also refuse any options at all! :(

So in the case of 'stage-2' I was getting an error babel-preset-stage-2/lib/index.js which does not accept options..

So even if modules option was only added if es2015, the following will still add an option to some presets that don't accept any options (because it returns the array).

  } else if (options.addModuleOptions) {
    return [info.name, { ...info.options, modules: false }];
  }

This is my hacky fix in my config to undo any problematic preset option transformation.

const rc = babelrc();
const doesNotAcceptOptions = ['stage-2'];
const acceptsOptionModules = ['es2015'];
rc.presets.forEach((preset, i) => {
  if (doesNotAcceptOptions.indexOf(preset[0]) > -1) {
    rc.presets[i] = preset[0];
  } else if (acceptsOptionModules.indexOf(preset[0]) === -1) {
    delete preset[1].modules; // eslint-disable-line
  }
});
// console.log(JSON.stringify(rc, null, 2));

in config...

  plugins: [
    babel(rc),
  ],

@eventualbuddha
Copy link
Owner

I'm happy to let someone else take over this project, or to review a PR that fixes this.

@robinvdvleuten
Copy link

You can quite easily pass custom options to babel and still have a global .babelrc file;

import babel from 'rollup-plugin-babel';

export default {
  plugins: [
    babel({
      presets: [['env', { modules: false }], 'stage-2'],
      exclude: 'node_modules/**',
      babelrc: false
    }),
  ],
};

Hopefully it will help anyone 😄

@verkholantsev verkholantsev linked a pull request Apr 18, 2018 that will close this issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants