Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addBabelPlugin not inserting the plugin #4

Closed
jgoux opened this issue Oct 3, 2018 · 6 comments
Closed

addBabelPlugin not inserting the plugin #4

jgoux opened this issue Oct 3, 2018 · 6 comments

Comments

@jgoux
Copy link

jgoux commented Oct 3, 2018

Hello,

the current implementation doesn't insert the plugin (in my case I tried with addBabelPlugin(['emotion'])(config)).

This doesn't match the babel-loader :

Array.isArray(r.use) && r.use.some(u => u.options && u.options.babelrc != void 0)

because the babel-loader doesn't have a use key.

The two characteristics I use to spot the right babel-loader (there are 2 of them) are :

  • The loader name contains "babel-loader"
  • It has a "customize" key
function addBabelPlugin(plugin) {
  return function(config) {
    const babelLoader = config.module.rules
      .find(rule => Object.keys(rule).includes('oneOf'))
      .oneOf.find(
        rule =>
          rule.loader &&
          rule.loader.includes('babel-loader') &&
          rule.options.customize
      )

    babelLoader.options.plugins.push(plugin)

    return config
  }
}
@ltkn
Copy link

ltkn commented Oct 3, 2018

the issue might be the declaration here 57bb094#diff-168726dbe96b3ce427e7fedce31bb0bcR16

@walmor
Copy link

walmor commented Oct 3, 2018

I was facing the same issue and I did a little further investigation to understand the problem. Hope that can help.

I was using the react-scripts version 2.0.3, that was released two days ago (Oct, 1th). The PR #5170 was merged two days before the release, where they removed the thread-loader. That PR changed the shape of the configuration object, removing the use array from the babel loader rule.

That means that the current version of the customize-cra works well for [email protected].* versions but it doesn't work for the version 2.0.3.

To avoid a breaking change for the users using versions 2.0.0, I came up with a version of the addBabelPlugin that is able to handle both versions.

function addBabelPlugin(plugin, config, env) {
  const babelLoaderFilter = rule =>
    rule.loader && rule.loader.includes('babel') && rule.options && rule.options.plugins;

  // First, try to find the babel loader inside the oneOf array.
  // This is where we can find it when working with [email protected].
  let loaders = config.module.rules.find(rule => Array.isArray(rule.oneOf)).oneOf;

  let babelLoader = loaders.find(babelLoaderFilter);

  // If the loader was not found, try to find it inside of the "use" array, within the rules.
  // This should work when dealing with [email protected].* versions.
  if (!babelLoader) {
    loaders = loaders.reduce((ldrs, rule) => ldrs.concat(rule.use || []), []);
    babelLoader = loaders.find(babelLoaderFilter);
  }

  babelLoader.options.plugins.push(plugin);

  return config;
}

That code is not fully tested. I've only done some simple test against the versions 2.0.3 and 2.0.0-next.fb6e6f70 (the lastest 2.0.0.next.* version). Probably more tests would be necessary before using it as the actual implementation.

@walmor walmor mentioned this issue Oct 3, 2018
@war3k
Copy link

war3k commented Oct 3, 2018

try to use my fork :
https://github.com/war3k/customize-cra

@priezz
Copy link

priezz commented Oct 3, 2018

@war3k Thanks, it works! You should probably send a PR with the changes given.

@war3k
Copy link

war3k commented Oct 3, 2018

sure, I already did that :)

@jgoux jgoux mentioned this issue Oct 3, 2018
@arackaf
Copy link
Owner

arackaf commented Oct 3, 2018

Seems Dan Abramov moved my cheese :P

@walmor's fix is updated for the new 0.1.X api, and released under 0.1.0-beta4.

@arackaf arackaf closed this as completed Oct 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants