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

how babel-loader deal with ES6 import? #138

Closed
Fishfaceeast opened this issue Nov 5, 2015 · 5 comments
Closed

how babel-loader deal with ES6 import? #138

Fishfaceeast opened this issue Nov 5, 2015 · 5 comments

Comments

@Fishfaceeast
Copy link

Does babel-loader just leave the import tasks to webpack? Any difference between babel-loader in webpack and the babel tool alone in node CLI?

@smalllong
Copy link

I met the same question. When I use dynamic import in babel-loader, error happened: "unexpected token" at the import().

@smalllong
Copy link

@gimmi
Copy link

gimmi commented Dec 11, 2017

Ok, the default seems to be commonjs so babel will transform to ES6 import and let webpack deal with commonjs modules.

Isn't this sub-optimal for Webpack 2 tree shaking feature?

See also: https://medium.com/@johnstew/webpack-tree-shaking-20914b7a9ca5

@michael-ciniawsky
Copy link
Contributor

michael-ciniawsky commented Dec 11, 2017

@gimmi With CJS Modules there isn't any 'tree-shaking' :). You need to explicitly disable module transpilation via babel when using webpack, since webpack supports ESM and import() 'natively' and needs ESM && import() for scope hoisting and 'tree-shaking'.

babelrc

{
  "presets": [
    [
      "env", {
        "targets": {
           // specify supported browsers (ES2015+ Features to transpile)
          "browsers": { "chrome": 60 } 
         },
        // no module transpilation to CJS (!important)
        "modules": false
      }
    ],
    // if you use React (JSX)
    [ "react" ]
  ],
  "plugins": [
    // Syntax support only so babel doesn't complain, no transpilation
    "syntax-dynamic-import"
  ],
  "env": {
    "test": {
      // Only for testing components (NODE_ENV = 'test') in NodeJS, which needs CJS to work
      "plugins": [ "transform-es2015-modules-commonjs" ]
    }
  }
}

@DrewML
Copy link
Member

DrewML commented Jan 8, 2018

Closing since this has been answered. #477, #521, and #515 can be used to discuss making this experience less confusing moving forward.

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

5 participants