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

code split fail with preset-env modules:commonjs v7.5.4 #10202

Open
Bermudarat opened this issue Jul 11, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@Bermudarat
Copy link

commented Jul 11, 2019

Bug Report

Current Behavior
code split fail with preset-env modules:commonjs v7.5.4

Expected behavior/code
when modules: 'common' and plugin @babel/plugin-syntax-dynamic-import''; babel will tranfrom import(...) to require(...), then will cause code split fail

Babel Configuration (.babelrc, package.json, cli command)

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                modules: 'commonjs'
            }
        ],
        '@babel/preset-react'
    ],
    plugins: [
        '@babel/plugin-syntax-dynamic-import'
    ]
};

Environment

  • Babel version(s): v7.5.4
  • Node/npm version: v11.6.0/6.5.0-next.0
  • OS: [e.g. OSX 10.13.4, Windows 10] OSX
  • Monorepo: [e.g. yes/no/Lerna]no
  • How you are using Babel: [e.g. cli, register, loader] cli
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Jul 11, 2019

Hey @Bermudarat! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Jul 11, 2019

@babel/preset-env goal is explicitly to transpile all the standard syntax features which aren't supported by your target environment.
If you don't specify any target (like in your config), it defaults to "every engine". For this reason, it needs to transpile it to "something" and since you specified that you want your modules to be transformed to CommonJS, it will transform it to a require call.
That said, we have a bug: for example chrome 75 supports import(), but even when it is the only target we currently transpile it.

The reason you saw the change in it's behavior is because @babel/preset-env supports "the latest JavaScript" (quoting the docs), i.e. "standard JavaScript", and import() just become standard.

You will be soon able to disable it using the preset's exclude option (#10194 (comment)). In the meanwhile, you can work it around using modules: false and including @babel/plugin-transform-modules-commonjs in your plugins.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Jul 11, 2019

Out of curiosity, if you are using a bundler which supports code splitting, why do you need to transpile modules to commonjs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.