Skip to content

Commit

Permalink
Update to core-js@3 (#7646)
Browse files Browse the repository at this point in the history
### `@babel/runtime`
- Added `@babel/runtime-corejs3` package and `corejs: 3` options to `@babel/plugin-transform-runtime`.
- Added support of instance methods, fixes #8928.
- Added flag `proposals` (in `corejs: { version: 3, proposals: true }` format) for support all proposals polyfills from `core-js`.
- Used separate directories in runtime for `core-js` entry points with proposals and without.
- Used `get-iterator-method` helper for getting iterators, fixes #2500.
- As a cheap bonus, added support of IE8- (except some cases of `regenerator`).

### `@babel/polyfill`
- Should be deprecated in favor of separate usage required features from `core-js` and `regenerator-runtime` with an informative message.

### `@babel/preset-env`
- Uses for built-ins data from [`core-js-compat`](https://github.com/zloirock/core-js/tree/master/packages/core-js-compat) instead of `compat-table` since information from `compat-table` [is not enough](https://github.com/zloirock/core-js/tree/master/packages/core-js-compat).
- `useBuilIns` now requires direct setting of `corejs` version option, without it will be used `2` by default and shown deprecation warning.
- Added support of minor `core-js` versions for simplify updating in the future.
- For preventing some order-related problems, polyfills in the both `core-js@3` plugins added on `post` stage in the order of `core-js-compat` data.
- Divided plugins and polyfills parts of `preset-env`, instead of 2 internal plugins for adding polyfills, we have 6: usage and entry versions of plugins for `core-js@2`, ### Current state:
`core-js@3`, `regenerator-runtime`.
- Added support `samsung` target (for Samsung Internet) since `core-js-compat` and `compat-table` now contains mapping for this, fixes #6602.

#### `useBuilIns: entry` with `corejs: 3`
- No longer transforms `@babel/polyfill`.
- Transforms **all possible** `core-js` entry points to import of related modules (based on data from [`core-js-compat`](https://unpkg.com/core-js-compat@3.0.0-beta.15/entries.json)).
- Since of this, we no longer need `shippedProposals` / `proposals` flags with `useBuilIns: entry`.
- Removes `regenerator-runtime/runtime` import where it's not required.

#### `useBuilIns: usage` with `corejs: 3`
- In addition to `shippedProposals`, added flag `proposals`  (in `corejs: { version: 3, proposals: true }` format) for polyfill all proposals from `core-js`.
- Fixed list of dependencies in built-in definitions.
- Improved the way of determination method / built-in name and source of this method.
- Adds import of required polyfills on `MemberExpression`, `ObjectPattern`, `in` operator.
- Adds import of required polyfills on access to global object properties.
- Adds import of all required common iterators on all syntax features which use iterators protocol (`for-of`, destructuring, spread, `yield` delegation, etc.).
- Adds import of promises on syntax features which use promises (async functions/generators, dynamic import, etc.), fixes #9250, #7402, etc.

### `core-js@2` stuff
I didn't want to tough `core-js@2`-related stuff, however
- Fixed some serious errors in definitions which breaks `Object.getOwnPropertySymbols`, `Symbol.toStringTag` logic, `Promise#finally`, `Array#forEach`, etc.
- `Array#flatMap` and trim methods moved to stable features as a part of ES2019 and loaded by deprecated `@babel/polyfill` and `@babel/preset-env` with `corejs: 2` option.
  • Loading branch information
zloirock authored and nicolo-ribaudo committed Mar 19, 2019
1 parent 7df0d16 commit 3303b07
Show file tree
Hide file tree
Showing 889 changed files with 18,538 additions and 1,577 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package.json
lerna.json
packages/babel-runtime
!packages/babel-plugin-transform-runtime/scripts
!packages/babel-runtime-corejs2/core-js.js
packages/babel-runtime-corejs2
packages/babel-runtime-corejs3
packages/*/node_modules
packages/*/lib
packages/*/dist
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ package-lock.json
/packages/babel-runtime-corejs2/core-js/**/*.js
!/packages/babel-runtime-corejs2/core-js/map.js

/packages/babel-runtime-corejs3/helpers/*.js
/packages/babel-runtime-corejs3/helpers/esm/*.js
/packages/babel-runtime-corejs3/core-js/**/*.js
/packages/babel-runtime-corejs3/core-js-stable/**/*.js

/packages/babel-register/test/.babel
/packages/babel-cli/test/tmp
/packages/babel-node/test/tmp
Expand Down
31 changes: 28 additions & 3 deletions lib/third-party-libs.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,31 @@ declare module "lodash/merge" {
}

declare module "semver" {
declare class SemVer {
build: Array<string>;
loose: ?boolean;
major: number;
minor: number;
patch: number;
prerelease: Array<string | number>;
raw: string;
version: string;

constructor(version: string | SemVer): SemVer;
}

declare module.exports: {
valid(v: string): boolean;
SemVer: SemVer;

coerce(version: string | SemVer): SemVer | null;
gt(v1: string, v2: string): boolean;
intersects(r1: string, r2: string): boolean;
lt(v1: string, v2: string): boolean;
major(v: string): number;
minor(v: string): number;
patch(v: string): number;
satisfies(v1: string, r1: string): boolean;

intersects(r1: string, r2: string): boolean;
valid(v: string): boolean;
}
}

Expand Down Expand Up @@ -180,3 +195,13 @@ declare module "js-levenshtein" {
(string, string): number,
};
}

declare module "core-js-compat/data" {
declare type Target = "node" | "chrome" | "opera" | "edge" | "firefox" | "safari" | "ie" | "ios" | "android" | "electron" | "samsung";

declare module.exports: {
[key: string]: {
[target: Target]: string;
}
}
}
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ helpers.initializerDefineProperty = helper("7.0.0-beta.0")`
helpers.applyDecoratedDescriptor = helper("7.0.0-beta.0")`
export default function _applyDecoratedDescriptor(target, property, decorators, descriptor, context){
var desc = {};
Object['ke' + 'ys'](descriptor).forEach(function(key){
Object.keys(descriptor).forEach(function(key){
desc[key] = descriptor[key];
});
desc.enumerable = !!desc.enumerable;
Expand All @@ -1019,7 +1019,7 @@ helpers.applyDecoratedDescriptor = helper("7.0.0-beta.0")`
if (desc.initializer === void 0){
// This is a hack to avoid this being processed by 'transform-runtime'.
// See issue #9.
Object['define' + 'Property'](target, property, desc);
Object.defineProperty(target, property, desc);
desc = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }

function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'proposal-class-properties is enabled and set to use loose mode. ' + 'To use proposal-class-properties in spec mode with decorators, wait for ' + 'the next major version of decorators in stage 2.'); }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _class, _class2;

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }

let A = (_class2 = class A {
foo() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var _class2;

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }

let _class = (_class2 = class {
bar() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"targets": {
"node": 8
},
"useBuiltIns": "usage"
"useBuiltIns": "usage",
"corejs": 3
}
]
],
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-plugin-transform-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"@babel/helper-plugin-test-runner": "^7.0.0",
"@babel/helpers": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.3.4",
"@babel/plugin-transform-member-expression-literals": "^7.2.0",
"@babel/plugin-transform-property-literals": "^7.2.0",
"@babel/preset-env": "^7.3.4",
"@babel/runtime": "^7.3.4",
"@babel/template": "^7.0.0",
Expand Down
84 changes: 62 additions & 22 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,81 @@ const template = require("@babel/template");
const t = require("@babel/types");

const transformRuntime = require("../");
const getCorejs2Definitions = require("../lib/definitions").default;
const corejs2Definitions = getCorejs2Definitions();
const transformMemberExpressionLiterals = require("@babel/plugin-transform-member-expression-literals");
const transformPropertyLiterals = require("@babel/plugin-transform-property-literals");

writeHelpers("@babel/runtime");
const corejs2Definitions = require("../lib/runtime-corejs2-definitions").default();
const corejs3Definitions = require("../lib/runtime-corejs3-definitions").default();

writeCoreJS2("@babel/runtime-corejs2");
writeHelpers("@babel/runtime");
writeHelpers("@babel/runtime-corejs2", { corejs: 2 });

function writeCoreJS2(runtimeName) {
const pkgDirname = getRuntimeRoot(runtimeName);

const paths = [
writeHelpers("@babel/runtime-corejs3", {
corejs: { version: 3, proposals: true },
});

writeCoreJS({
corejs: 2,
proposals: true,
definitions: corejs2Definitions,
paths: [
"is-iterable",
"get-iterator",

// This was previously in definitions, but was removed to work around
// zloirock/core-js#262. We need to keep it in @babel/runtime-corejs2 to
// avoid a breaking change there.
"symbol/async-iterator",
];

Object.keys(corejs2Definitions.builtins).forEach(key => {
const path = corejs2Definitions.builtins[key];
paths.push(path);
],
corejsRoot: "core-js/library/fn",
});
writeCoreJS({
corejs: 3,
proposals: false,
definitions: corejs3Definitions,
paths: [],
corejsRoot: "core-js-pure/stable",
});
writeCoreJS({
corejs: 3,
proposals: true,
definitions: corejs3Definitions,
paths: ["is-iterable", "get-iterator", "get-iterator-method"],
corejsRoot: "core-js-pure/features",
});

function writeCoreJS({
corejs,
proposals,
definitions: { BuiltIns, StaticProperties, InstanceProperties },
paths,
corejsRoot,
}) {
const pkgDirname = getRuntimeRoot(`@babel/runtime-corejs${corejs}`);

Object.keys(BuiltIns).forEach(name => {
const { stable, path } = BuiltIns[name];
if (stable || proposals) paths.push(path);
});

Object.keys(corejs2Definitions.methods).forEach(key => {
const props = corejs2Definitions.methods[key];
Object.keys(props).forEach(key2 => {
paths.push(props[key2]);
Object.keys(StaticProperties).forEach(builtin => {
const props = StaticProperties[builtin];
Object.keys(props).forEach(name => {
const { stable, path } = props[name];
if (stable || proposals) paths.push(path);
});
});

paths.forEach(function(corePath) {
if (InstanceProperties) {
Object.keys(InstanceProperties).forEach(name => {
const { stable, path } = InstanceProperties[name];
if (stable || proposals) paths.push(`instance/${path}`);
});
}

const runtimeRoot = proposals ? "core-js" : "core-js-stable";
paths.forEach(function(corejsPath) {
outputFile(
path.join(pkgDirname, "core-js", `${corePath}.js`),
`module.exports = require("core-js/library/fn/${corePath}");`
path.join(pkgDirname, runtimeRoot, `${corejsPath}.js`),
`module.exports = require("${corejsRoot}/${corejsPath}");`
);
});
}
Expand Down Expand Up @@ -118,6 +156,8 @@ function buildHelper(
presets: [[require("@babel/preset-env"), { modules: false }]],
plugins: [
[transformRuntime, { corejs, useESModules: esm }],
[transformMemberExpressionLiterals],
[transformPropertyLiterals],
buildRuntimeRewritePlugin(
runtimeName,
path.relative(path.dirname(helperFilename), pkgDirname),
Expand Down
Loading

0 comments on commit 3303b07

Please sign in to comment.