Parcel build includes all Core-js polyfills, or none at all
See original GitHub issueš bug report
When using .babelrc and specifying both targets
and useBuiltIns: 'usage'
I was expecting parcel to only include the core-js polyfills that were necessary to work with the targeted browsers. Instead all polyfills are including (making build size huge).
Alternatively, if import 'core-js/stable'
is removed, then no polyfills are included.
š Configuration (.babelrc, package.json, cli command)
package.json
{
"name": "core-js-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"core-js": "^3.3.6",
"parcel-bundler": "^1.12.4"
}
}
.babelrc
{
"presets": [
[
"@babel/preset-env",
{
"targets": "Chrome >75, IE 11",
"useBuiltIns": "usage",
"corejs": 3
}
]
]
}
index.js
import 'core-js/stable';
const str = 'Hello';
const arr = Array.from(str);
console.log(`The string contains an H: ${arr.includes('H')}`);
index-alternative.js
const str = 'Hello';
const arr = Array.from(str);
console.log(`The string contains an H: ${arr.includes('H')}`);
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test of Core JS</title>
</head>
<body>
<div id="app">Loading</div>
<script src="index.js"></script>
<script src="index-alternative.js"></script>
</body>
</html>
š¤ Expected Behavior
Iām not exactly sure. I was expecting one of the index.js files to compile to a version where only the required polyfills was included, to polyfill the Array.prototype.From and Array.includes methods for IE 11:
- require(ācore-js/modules/es.array.fromā);
- require(ācore-js/modules/es.array.includesā);
- require(ācore-js/modules/es.string.includesā);
- require(ācore-js/modules/es.string.iteratorā);
šÆ Current Behavior
When running parcel build index.html
it outputs both js files:
filename | size | time | notes |
---|---|---|---|
dist/core-js-test.e032cec8.js | 218.88 KB | 297ms | includes the entirety of core-js |
dist/index-alternative.5ae4bc85.js | 1.22 KB | 8ms | includes no polyfills at all |
Itās not clear from the documentation whether itās required to import core-js (I think itās not?) but if core-js isnāt imported then the polyfills arenāt included in the built script.
š Your Environment
Software | Version(s) |
---|---|
Parcel | 1.12.4 |
Node | 8.6.0 |
npm/Yarn | 5.4.2 |
Operating System | Mac OS Sierra 10.12.6 |
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
If you really wanna do all that work to save a small amount of data over the wire sure.
Iād just include it all
A workaround is described in #3216 by modifying the babel config for an environment youāre building for. This works with Parcel 1.
Example:
.babelrc
:Parcel 1 will use this configuration and babel will add the needed polyfill imports for you. The
plugins: []
section with at least one plugin is needed to make this work.The import in
index.js
:is only needed when you use
"useBuiltIns": "entry"
(see the entry section in the babel docs) which is a different cup of thea, I canāt get this to work with Parcel 1).Now if you run:
Babel will then add the following imports (and parcel the bundled modules):