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

Babel polyfill documentation for Webpack is wrong resulting in all polyfills being compiled into bundle #7254

Closed
kroko opened this issue Jan 22, 2018 · 4 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@kroko
Copy link

kroko commented Jan 22, 2018

Bug report

Bug in Babel polyfill documentation for webpack

Input Code

Repo

Revelation posted in babel-loader

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

.babelrc

{
  "presets": [
  	[
      "@babel/preset-env",
    	{
        "useBuiltIns": "usage",
        "forceAllTransforms": true,
        "ignoreBrowserslistConfig": false,
        "modules": false,
        "debug": true
      }
    ]
  ]
}

.browserslistrc

[production]
last 1 Chrome version

[development]
last 1 Chrome version

or

[production]
> 0.0001%

[development]
> 0.0001%

webpack.config.js

entry: {
    site: [
      '@babel/polyfill',
      path.join(__dirname, 'src/site.js')
    ],
    // ...
  }

Expected Behavior

When '@babel/polyfill' is included in webpack entry point as per documentation and "useBuiltIns": "usage" is used then then all polyfills are stripped and only ones found by union of usage within the code and specified browserlist are left.

When '@babel/polyfill' is included in webpack entry point as per documentation and "useBuiltIns": "entry" is used then then all polyfills are stripped and only ones found by specified browserslist are left.

Current Behavior

When '@babel/polyfill' is included in webpack entry point as per documentation and "useBuiltIns": "usage" is used then then all polyfills will be included as well as ones found by usage and browserlist combo resulting in monstrous bundle.

When '@babel/polyfill' is included in webpack entry point as per documentation and "useBuiltIns": "entry" is used then then @babel/polyfill is not found, which effectively means that babel cannot strip unneeded, thus all polyfills will be included in the bundle resulting in monstrous bundle.

Possible Solution

Make changes in Polyfill documentation as well as in babel-preset-env documentation

The current statement

With webpack.config.js, add babel-polyfill to your entry array

is correct only in one of three possible useBuiltIns settings (so in 2 cases out of 3 users are building all polyfills into their bundle).

They should state that:

  • If @babel/preset-env has "useBuiltIns": "usage" then babel polyfill should not be included neither in source nor webpack entry (but it has to be installed of course). Browserslist configuration should be present.
  • If @babel/preset-env has "useBuiltIns": "entry" then babel polyfill should be included in source, not webpack entry. Browserslist configuration should be present.
  • If @babel/preset-env does not have "useBuiltIns" key or it is explicitly set to false "useBuiltIns": false then babel polyfill should be included in webpack entry, but it can be included also in source (the latter being unadvised due to hoisting).

Context

By trusting documentation I have deployed unnecessary large bundles although I have targeted new browsers.
I use .browserslistrc file as it is also used by PostCSS autoprefixer (JS and CSS features stay in sync).

Your Environment

software version(s)
Babel 7.0.0-beta.38
node 8.9.4
npm 5.6.0
Operating System macOS 10.13.2
Webpack 3.10.0
Babel loader 8.0.0-beta.0
Babel polyfill 7.0.0-beta.38
Babel preset env 7.0.0-beta.38
@babel-bot
Copy link
Collaborator

Hey @kroko! 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.

@kroko
Copy link
Author

kroko commented Jan 22, 2018

Reference to the original webpack-loader ticket babel/babel-loader#567

@kroko
Copy link
Author

kroko commented Jan 22, 2018

PR babel/website#1520

agilgur5 added a commit to agilgur5/front-end-base that referenced this issue May 29, 2018
- per babel/babel#7254
  - like in the bug report, it was including _all_ polyfills

- per the docs, 'usage' works roughly similarly to 'entry' because
  "a bundler will load the same polyfill only once"

- still need core-js installed to install polyfills for libraries
  that require it
  - i.e. isomorphic-fetch requires a Promise polyfill
    - Babel can't detect that it needs it because Babel isn't parsing
      imported libraries
agilgur5 added a commit to agilgur5/front-end-base that referenced this issue May 29, 2018
- per babel/babel#7254
  - like in the bug report, it was including _all_ polyfills

- per the docs, 'usage' works roughly similarly to 'entry' because
  "a bundler will load the same polyfill only once"

- still need core-js installed to install polyfills for libraries
  that require it
  - i.e. isomorphic-fetch requires a Promise polyfill
    - Babel can't detect that it needs it because Babel isn't parsing
      imported libraries
  - improve comments of the polyfills file
existentialism pushed a commit to babel/website that referenced this issue Oct 4, 2018
@existentialism
Copy link
Member

Doc changes landed in babel/website#1858!

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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

3 participants