also transform 'core-js'
This commit is contained in:
parent
5d32ca6bb3
commit
8e2aa82991
@ -126,6 +126,9 @@ Defaults to `false`.
|
||||
|
||||
A way to apply `babel-preset-env` for polyfills (via "babel-polyfill").
|
||||
|
||||
> NOTE: This does not currently polyfill experimental/stage-x built-ins like the regular "babel-polyfill" does.
|
||||
> This will only work with npm >= 3 (which should be used with Babel 6 anyway)
|
||||
|
||||
```
|
||||
npm install babel-polyfill --save
|
||||
```
|
||||
@ -151,6 +154,12 @@ import "core-js/modules/es6.symbol.species";
|
||||
import "core-js/modules/es6.array.from";
|
||||
```
|
||||
|
||||
> This will also work for "core-js" directly (`import "core-js";`)
|
||||
|
||||
```
|
||||
npm install core-js --save
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Examples
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
const polyfillSource = "babel-polyfill";
|
||||
function isPolyfillSource(value) {
|
||||
return value === "babel-polyfill" || value === "core-js";
|
||||
}
|
||||
|
||||
export default function ({ types: t }) {
|
||||
function createImportDeclaration(polyfill) {
|
||||
@ -18,20 +20,20 @@ export default function ({ types: t }) {
|
||||
);
|
||||
}
|
||||
|
||||
function isRequire(path, source) {
|
||||
function isRequire(path) {
|
||||
return t.isExpressionStatement(path.node) &&
|
||||
t.isCallExpression(path.node.expression) &&
|
||||
t.isIdentifier(path.node.expression.callee) &&
|
||||
path.node.expression.callee.name === "require" &&
|
||||
path.node.expression.arguments.length === 1 &&
|
||||
t.isStringLiteral(path.node.expression.arguments[0]) &&
|
||||
path.node.expression.arguments[0].value === source;
|
||||
isPolyfillSource(path.node.expression.arguments[0].value);
|
||||
}
|
||||
|
||||
const isPolyfillImport = {
|
||||
ImportDeclaration(path, state) {
|
||||
if (path.node.specifiers.length === 0 &&
|
||||
path.node.source.value === polyfillSource) {
|
||||
isPolyfillSource(path.node.source.value)) {
|
||||
this.numPolyfillImports++;
|
||||
if (this.numPolyfillImports > 1) {
|
||||
path.remove();
|
||||
@ -53,7 +55,7 @@ to the "transform-polyfill-require" plugin
|
||||
`);
|
||||
}
|
||||
path.get("body").forEach((bodyPath) => {
|
||||
if (isRequire(bodyPath, polyfillSource)) {
|
||||
if (isRequire(bodyPath)) {
|
||||
this.numPolyfillImports++;
|
||||
if (this.numPolyfillImports > 1) {
|
||||
path.remove();
|
||||
|
||||
1
experimental/babel-preset-env/test/fixtures/preset-options/core-js/actual.js
vendored
Normal file
1
experimental/babel-preset-env/test/fixtures/preset-options/core-js/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "core-js";
|
||||
2
experimental/babel-preset-env/test/fixtures/preset-options/core-js/expected.js
vendored
Normal file
2
experimental/babel-preset-env/test/fixtures/preset-options/core-js/expected.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import "core-js/modules/es7.string.pad-start";
|
||||
import "core-js/modules/es7.string.pad-end";
|
||||
11
experimental/babel-preset-env/test/fixtures/preset-options/core-js/options.json
vendored
Normal file
11
experimental/babel-preset-env/test/fixtures/preset-options/core-js/options.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"presets": [
|
||||
["../../../../lib", {
|
||||
"targets": {
|
||||
"chrome": 55
|
||||
},
|
||||
"modules": false,
|
||||
"useBuiltIns": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
2
experimental/babel-preset-env/test/fixtures/preset-options/no-transform/actual.js
vendored
Normal file
2
experimental/babel-preset-env/test/fixtures/preset-options/no-transform/actual.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import "not-core-js";
|
||||
import "not-babel-polyfill";
|
||||
2
experimental/babel-preset-env/test/fixtures/preset-options/no-transform/expected.js
vendored
Normal file
2
experimental/babel-preset-env/test/fixtures/preset-options/no-transform/expected.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import "not-core-js";
|
||||
import "not-babel-polyfill";
|
||||
11
experimental/babel-preset-env/test/fixtures/preset-options/no-transform/options.json
vendored
Normal file
11
experimental/babel-preset-env/test/fixtures/preset-options/no-transform/options.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"presets": [
|
||||
["../../../../lib", {
|
||||
"targets": {
|
||||
"chrome": 55
|
||||
},
|
||||
"modules": false,
|
||||
"useBuiltIns": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user