also transform 'core-js'

This commit is contained in:
Henry Zhu 2016-12-06 14:31:16 -05:00
parent 5d32ca6bb3
commit 8e2aa82991
8 changed files with 45 additions and 5 deletions

View File

@ -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

View File

@ -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();

View File

@ -0,0 +1 @@
import "core-js";

View File

@ -0,0 +1,2 @@
import "core-js/modules/es7.string.pad-start";
import "core-js/modules/es7.string.pad-end";

View File

@ -0,0 +1,11 @@
{
"presets": [
["../../../../lib", {
"targets": {
"chrome": 55
},
"modules": false,
"useBuiltIns": true
}]
]
}

View File

@ -0,0 +1,2 @@
import "not-core-js";
import "not-babel-polyfill";

View File

@ -0,0 +1,2 @@
import "not-core-js";
import "not-babel-polyfill";

View File

@ -0,0 +1,11 @@
{
"presets": [
["../../../../lib", {
"targets": {
"chrome": 55
},
"modules": false,
"useBuiltIns": true
}]
]
}