Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack es2015 tree shaking with React

I would like to use tree shaking feature seems we don't need to install babel-preset-es2015-webpack. We still can use babel-preset-es2015 and set the modules flag to false for es2015 preset. I changed my webpack configuration as shown below which results in "Unexpected token import" error on import line in my react components.

  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel',
        query: {
          presets: [['es2015', {modules: false}], 'react']
        }
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader!sass-loader")
      }
    ]
  }

I also tried to set the presets as

['es2015', 'react', {modules: false}]

Then I got different error on console:

 Module build failed: ReferenceError: [BABEL] C:\FE-Proj-Templates\webpack\main.js: Using removed Babel 5 option: foreign.modules - Use the corresponding module transform plugin in the `plugins` option. Check out http://babeljs.io/docs/plugins/#modules 

How can I set the preset es2015 with modules flag false and also use React preset?

like image 787
Negin Basiri Avatar asked Aug 22 '16 02:08

Negin Basiri


People also ask

Does webpack automatically tree shake?

In Node. js, for example, you can conditionally run require with a variable to load a random script. Webpack can't possibly know all of your imports and exports at build time, so it will attempt to tree shake a handful of constructs and bail as soon as things get too dynamic.

Does create react app do tree shaking?

If you're using modern tooling, such as webpack, create-react-app and similars, you already have tree shaking set up.


1 Answers

It's updated and works

presets: [['es2015', {modules: false}], 'react']
like image 78
Negin Basiri Avatar answered Oct 23 '22 05:10

Negin Basiri