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

Closed
Bermudarat opened this issue Jul 11, 2019 · 4 comments
Closed

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

Bermudarat opened this issue Jul 11, 2019 · 4 comments
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Bermudarat
Copy link

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
Copy link
Collaborator

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
Copy link
Member

nicolo-ribaudo 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
Copy link
Member

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

@JLHwung
Copy link
Contributor

JLHwung commented Sep 18, 2019

Close it as it duplicates #10273.

@JLHwung JLHwung closed this as completed Sep 18, 2019
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Dec 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants