Skip to content

Commit

Permalink
feat: switch to core-js@3 & update all babel presets and plugin deps
Browse files Browse the repository at this point in the history
BREAKING CHANGE: change preset options & add core-js@3
- remove dynamicImport option
  parsing is enabled always
  transform is enabled when modules: false
- make changes to use core-js@3
  • Loading branch information
Mayank1791989 committed Jul 14, 2019
1 parent 2810501 commit 9ffb510
Show file tree
Hide file tree
Showing 9 changed files with 436 additions and 324 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ $ yarn add --dev babel-preset-playlyfe
## Options

* `flow` - bool (default true) Enable flow support.
* `dynamicImport` - boolean (default false) Allow dynamic imports.
* `modules` - 'amd' | 'umd' | 'systemjs' | 'commonjs' | false (default 'commonjs').
* `targets` - same as babel-preset-env [targets option](https://github.com/babel/babel-preset-env#options)
* `useBuiltIns` - (default: true) same as babel-preset-env [useBuiltIns option](https://github.com/babel/babel-preset-env#options)
Expand Down
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,20 @@
"README.md"
],
"dependencies": {
"@babel/plugin-proposal-class-properties": "7.2.3",
"@babel/plugin-proposal-object-rest-spread": "7.2.0",
"@babel/plugin-syntax-dynamic-import": "7.2.0",
"@babel/plugin-transform-runtime": "7.2.0",
"@babel/preset-env": "7.2.3",
"@babel/plugin-proposal-class-properties": "7.5.0",
"@babel/plugin-proposal-object-rest-spread": "7.5.4",
"@babel/plugin-transform-runtime": "7.5.0",
"@babel/preset-env": "7.5.4",
"@babel/preset-flow": "7.0.0",
"@babel/preset-react": "7.0.0",
"@babel/runtime": "7.2.0",
"babel-plugin-dynamic-import-node": "2.2.0",
"@babel/runtime": "7.5.4",
"babel-plugin-react-intl": "3.0.1",
"flow-runtime": "0.17.0",
"lodash": "4.17.11"
"lodash": "4.17.14"
},
"devDependencies": {
"@babel/cli": "7.5.0",
"@babel/core": "7.5.4",
"@babel/polyfill": "7.4.4",
"babel-jest": "24.8.0",
"babel-plugin-flow-runtime": "0.19.0",
"codecov": "3.5.0",
Expand Down
354 changes: 209 additions & 145 deletions src/__tests__/__snapshots__/es-test.js.snap

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/__tests__/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ exports[`preset-options throws when unknown options passed 1`] = `
Expected: {|
flow: boolean;
asyncAwait: boolean;
dynamicImport: false | \\"webpack\\" | \\"node\\";
modules: \\"amd\\" | \\"umd\\" | \\"systemjs\\" | \\"commonjs\\" | false;
targets: {
Expand All @@ -23,7 +22,6 @@ Expected: {|
Actual Value: {
\\"flow\\": true,
\\"asyncAwait\\": false,
\\"dynamicImport\\": false,
\\"modules\\": \\"commonjs\\",
\\"targets\\": {},
\\"useBuiltIns\\": true,
Expand All @@ -38,7 +36,6 @@ Actual Value: {
Actual Type: {
flow: boolean;
asyncAwait: boolean;
dynamicImport: boolean;
modules: string;
targets: {
Expand Down
176 changes: 82 additions & 94 deletions src/__tests__/es-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ test('Transform class properties', () => {
{ modules: false },
);
expect(code).toMatchInlineSnapshot(`
"import _classCallCheck from \\"@babel/runtime/helpers/esm/classCallCheck\\";
"import _classCallCheck from \\"@babel/runtime/helpers/esm/classCallCheck\\";
var Test = function Test() {
_classCallCheck(this, Test);
var Test = function Test() {
_classCallCheck(this, Test);
this.y = 10;
};
this.y = 10;
};
Test.x = 5;"
`);
Test.x = 5;"
`);
});

test('Use commonjs by default for modules', () => {
Expand All @@ -61,19 +61,19 @@ test('Use commonjs by default for modules', () => {
`,
);
expect(code).toMatchInlineSnapshot(`
"\\"use strict\\";
"\\"use strict\\";
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
var _classCallCheck2 = _interopRequireDefault(require(\\"@babel/runtime/helpers/classCallCheck\\"));
var _classCallCheck2 = _interopRequireDefault(require(\\"@babel/runtime/helpers/classCallCheck\\"));
var Test = function Test() {
(0, _classCallCheck2.default)(this, Test);
this.y = 10;
};
var Test = function Test() {
(0, _classCallCheck2[\\"default\\"])(this, Test);
this.y = 10;
};
Test.x = 5;"
`);
Test.x = 5;"
`);
});

testParseCode('Allow Object rest spread', {
Expand All @@ -100,16 +100,16 @@ test('correctly transform object-rest-spread', () => {
const c = { ...a, ...b };
`);
expect(code).toMatchInlineSnapshot(`
"\\"use strict\\";
var a = {
name: 'babel'
};
var b = {
core: 'core'
};
var c = Object.assign({}, a, b);"
`);
"\\"use strict\\";
var a = {
name: 'babel'
};
var b = {
core: 'core'
};
var c = Object.assign({}, a, {}, b);"
`);
});

testParseCode('Allow exports of form export { name } from module', {
Expand Down Expand Up @@ -160,12 +160,12 @@ test('[Bug] flow should strip whole import if it contains only types', () => {
`);

expect(transformed.code).toMatchInlineSnapshot(`
"\\"use strict\\";
"\\"use strict\\";
function test() {
console.log('test');
}"
`);
function test() {
console.log('test');
}"
`);
});

testParseCode('Support import shorthand', {
Expand All @@ -176,79 +176,67 @@ testParseCode('Support import shorthand', {
`,
});

testParseCode('Support dynamic import if enabled', {
opts: [{ dynamicImport: 'webpack' }],
throws: false,
code: `
function someDynamicImport() {
import('xyz').then(() => console.log('dynamic import'));
}
`,
});

describe('Transform dynamic import', () => {
const code = `
function someDynamicImport() {
import('xyz').then(() => console.log('dynamic import'));
}
`;
describe('Dynamic Imports', () => {
testParseCode('Parsing', {
opts: [undefined, { modules: false }],
throws: false,
code: `
function someDynamicImport() {
import('xyz').then(() => console.log('dynamic import'));
}
`,
});

test('webpack', () => {
const transformed = transform(code, {
dynamicImport: 'webpack',
describe('Transform dynamic import', () => {
const code = `
function someDynamicImport() {
import('xyz').then(() => console.log('dynamic import'));
}
`;

test('Skip transformation if modules: false (for webpack)', () => {
const transformed = transform(code, { modules: false });
expect(transformed.code).toMatchInlineSnapshot(`
"function someDynamicImport() {
import('xyz').then(function () {
return console.log('dynamic import');
});
}"
`);
});

expect(transformed.code).toMatchInlineSnapshot(`
"\\"use strict\\";
test('transform if modules enabled', () => {
const transformed = transform(code, { modules: 'commonjs' });

function someDynamicImport() {
import('xyz').then(function () {
return console.log('dynamic import');
});
}"
`);
});
expect(transformed.code).toMatchInlineSnapshot(`
"\\"use strict\\";
test('node', () => {
const transformed = transform(code, {
dynamicImport: 'node',
modules: false,
});
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
expect(transformed.code).toMatchInlineSnapshot(`
"import _interopRequireWildcard from \\"@babel/runtime/helpers/esm/interopRequireWildcard\\";
var _interopRequireWildcard2 = _interopRequireDefault(require(\\"@babel/runtime/helpers/interopRequireWildcard\\"));
function someDynamicImport() {
Promise.resolve().then(function () {
return _interopRequireWildcard(require('xyz'));
}).then(function () {
return console.log('dynamic import');
});
}"
`);
function someDynamicImport() {
Promise.resolve().then(function () {
return (0, _interopRequireWildcard2[\\"default\\"])(require('xyz'));
}).then(function () {
return console.log('dynamic import');
});
}"
`);
});
});
});

testParseCode('By default dont support dynamic import', {
opts: [null],
throws: true,
code: `
function someDynamicImport() {
import('xyz').then(() => console.log('dynamic import'));
}
`,
});

test('Support disable import conversion', () => {
const code = `
import { test } from 'test';
console.log('test');
`;
const transformed = transform(code, { modules: false });
expect(transformed.code).toMatchInlineSnapshot(`
"import { test } from 'test';
console.log('test');"
`);
"import { test } from 'test';
console.log('test');"
`);
});

// supports async await
Expand All @@ -260,12 +248,12 @@ test('default dont transform transform async await', () => {
`);

expect(transformed.code).toMatchInlineSnapshot(`
"\\"use strict\\";
"\\"use strict\\";
async function test() {
var a = await test2();
}"
`);
async function test() {
var a = await test2();
}"
`);
});

test('transform if Async await enabled', () => {
Expand Down Expand Up @@ -304,10 +292,10 @@ testExecCode('async await should work', {
`,
});

test('Correctly replace @babel/polyfill with individual import for target', () => {
test('Correctly replace core-js with individual import for target', () => {
const { code } = transform(
`
import '@babel/polyfill';
import 'core-js/stable';
`,
{ modules: false },
);
Expand Down
7 changes: 5 additions & 2 deletions src/__tests__/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import preset, { type Opts as PresetOpts } from '../index';

type Opts = $Shape<PresetOpts>;

export function transform(code: string, presetOpts: ?Opts) {
return babel.transform(code, {
export function transform(
code: string,
presetOpts: ?Opts,
): { code: string, metadata: { +[string]: mixed } } {
return babel.transformSync(code, {
presets: [[preset, presetOpts]], // eslint-disable-line global-require
// required for transformation
filename: '/test-file.js',
Expand Down
13 changes: 3 additions & 10 deletions src/presets/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ import presetEnv from '@babel/preset-env';
import presetFlow from '@babel/preset-flow';
import transformClassProperties from '@babel/plugin-proposal-class-properties';
import transformObjectRestSpread from '@babel/plugin-proposal-object-rest-spread';
import syntaxDynamicImport from '@babel/plugin-syntax-dynamic-import';
import pluginDynamicImportNode from 'babel-plugin-dynamic-import-node';

export type Opts = {
export type Opts = {|
flow: boolean,
asyncAwait: boolean,
dynamicImport: false | 'webpack' | 'node',
modules: 'amd' | 'umd' | 'systemjs' | 'commonjs' | false,
targets: Object,
useBuiltIns: boolean,
debug: boolean,
};
|};

export const DEFAULT_OPTS: Opts = Object.freeze({
flow: true,
asyncAwait: false,
dynamicImport: false,
modules: 'commonjs',
targets: {},
useBuiltIns: true,
Expand All @@ -38,6 +34,7 @@ export default (context: any, opts: Opts) => {
modules,
debug,
useBuiltIns: useBuiltIns ? 'entry' : false,
corejs: 3,
// only enable generator when asyncAwait enabled
exclude: !asyncAwait // eslint-disable-line no-negated-condition
? ['transform-regenerator', 'transform-async-to-generator']
Expand All @@ -48,10 +45,6 @@ export default (context: any, opts: Opts) => {
],

plugins: [
// dynamic import
opts.dynamicImport ? syntaxDynamicImport : null,
opts.dynamicImport === 'node' ? pluginDynamicImportNode : null,

// add class properties support
// NOTE: keeping loose: true see issue: https://github.com/facebook/create-react-app/issues/4263
[transformClassProperties, { loose: true }],
Expand Down
4 changes: 2 additions & 2 deletions src/presets/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import presetReact from '@babel/preset-react';
import pluginReactIntl from '../plugins/react-intl';

export type Opts = {
export type Opts = {|
reactIntl: boolean,
useBuiltIns: boolean,
development: boolean,
};
|};

export const DEFAULT_OPTS: Opts = Object.freeze({
reactIntl: true,
Expand Down
Loading

0 comments on commit 9ffb510

Please sign in to comment.