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

docs: clarify modules option in preset-env #2301

Merged
merged 4 commits into from
Jul 30, 2020

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jul 27, 2020

{ modules: false } has been long used as a fast switch to "preserve ES module" so bundlers can leverage from the module syntax. However the defaults { modules: "auto" } should be preferred because it reads caller support data and reduces cognitive loads -- Idea like "I want to disable module transforms so Webpack can do tree shaking" is not straightforward, it is the defaults that should work out of that for you.

Since Babel 7.11, preset-env will assume { modules: false } as to "preserve ES module" and the compiled code will be served without bundling. By doing so preset-env can determine whether it should transpile export * as ns based on the targets. This assumption only works when it is not passed to bundlers, where bundlers may not support latest module syntax, (e.g. Webpack 5.0.0-beta.21 supports this recently, thus export * as ns will break on Webpack 4.

Although export * as ns will break on Webpack 4, since it is not included in preset-env before, users should have supplied @babel/plugin-proposal-export-namesapce-from so they are good when upgrading to Babel 7.11. However users just begin to use export * as ns in preset-env 7.11 with

{
  presets: ["@babel/preset-env", {
    modules: false,
    targets: { browsers: "latest Chrome 2 versions" }
  }]
}

may surprisedly find that preset-env does not transpile export * as ns and errors are thrown from Webpack.

@netlify
Copy link

netlify bot commented Jul 27, 2020

Deploy preview for babel ready!

Built without sensitive environment variables with commit 17d14a5

https://deploy-preview-2301--babel.netlify.app

docs/preset-env.md Outdated Show resolved Hide resolved
docs/preset-env.md Outdated Show resolved Hide resolved
docs/options.md Outdated
Comment on lines 49 to 52
supportsStaticESM: boolean;
supportsDynamicImport: boolean;
supportsTopLevelAwait: boolean;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
supportsStaticESM: boolean;
supportsDynamicImport: boolean;
supportsTopLevelAwait: boolean;
}
supportsStaticESM?: boolean;
supportsDynamicImport?: boolean;
supportsTopLevelAwait?: boolean;
}

These should all be optional

@JLHwung JLHwung merged commit 8dcb45f into babel:master Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants