Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove stage presets #69

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions __tests__/__snapshots__/packageData.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ Object {
}
`;

exports[`replaces stage presets 1`] = `
Object {
"@babel/core": "7.0.0-beta.39",
"@babel/plugin-proposal-class-properties": "7.0.0-beta.39",
"@babel/plugin-proposal-decorators": "7.0.0-beta.39",
"@babel/plugin-proposal-export-namespace-from": "7.0.0-beta.39",
"@babel/plugin-proposal-function-sent": "7.0.0-beta.39",
"@babel/plugin-proposal-json-strings": "7.0.0-beta.39",
"@babel/plugin-proposal-numeric-separator": "7.0.0-beta.39",
"@babel/plugin-proposal-throw-expressions": "7.0.0-beta.39",
"@babel/plugin-syntax-dynamic-import": "7.0.0-beta.39",
"@babel/plugin-syntax-import-meta": "7.0.0-beta.39",
}
`;

exports[`scripts 1`] = `
Object {
"name": "mocha-scripts-test",
Expand Down Expand Up @@ -164,6 +179,8 @@ Object {
"@babel/plugin-proposal-export-namespace-from": "7.0.0-beta.39",
"@babel/plugin-proposal-function-bind": "7.0.0-beta.39",
"@babel/plugin-proposal-function-sent": "7.0.0-beta.39",
"@babel/plugin-proposal-json-strings": "7.0.0-beta.39",
"@babel/plugin-proposal-logical-assignment-operators": "7.0.0-beta.39",
"@babel/plugin-proposal-nullish-coalescing-operator": "7.0.0-beta.39",
"@babel/plugin-proposal-numeric-separator": "7.0.0-beta.39",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.39",
Expand Down Expand Up @@ -245,10 +262,6 @@ Object {
"@babel/preset-env-standalone": "7.0.0-beta.39",
"@babel/preset-flow": "7.0.0-beta.39",
"@babel/preset-react": "7.0.0-beta.39",
"@babel/preset-stage-0": "7.0.0-beta.39",
"@babel/preset-stage-1": "7.0.0-beta.39",
"@babel/preset-stage-2": "7.0.0-beta.39",
"@babel/preset-stage-3": "7.0.0-beta.39",
"@babel/preset-typescript": "7.0.0-beta.39",
"@babel/register": "7.0.0-beta.39",
"@babel/runtime": "7.0.0-beta.39",
Expand Down
77 changes: 33 additions & 44 deletions __tests__/__snapshots__/upgradeConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,6 @@ Object {
},
],
],
"presets": Array [
Array [
"@babel/preset-stage-0",
Object {
"decoratorsLegacy": true,
"pipelineProposal": "minimal",
},
],
Array [
"@babel/preset-stage-1",
Object {
"decoratorsLegacy": true,
"pipelineProposal": "minimal",
},
],
Array [
"@babel/preset-stage-2",
Object {
"decoratorsLegacy": true,
},
],
],
}
`;

Expand All @@ -66,28 +44,6 @@ Object {
},
],
],
"presets": Array [
Array [
"@babel/preset-stage-0",
Object {
"decoratorsLegacy": true,
"pipelineProposal": "minimal",
},
],
Array [
"@babel/preset-stage-1",
Object {
"decoratorsLegacy": true,
"pipelineProposal": "minimal",
},
],
Array [
"@babel/preset-stage-2",
Object {
"decoratorsLegacy": true,
},
],
],
}
`;

Expand Down Expand Up @@ -244,3 +200,36 @@ Object {
],
}
`;

exports[`replaces stage presets 1`] = `
Object {
"plugins": Array [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-json-strings",
Array [
"@babel/plugin-proposal-decorators",
Object {
"legacy": true,
},
],
"@babel/plugin-proposal-function-sent",
"@babel/plugin-proposal-export-namespace-from",
"@babel/plugin-proposal-numeric-separator",
"@babel/plugin-proposal-throw-expressions",
"@babel/plugin-proposal-export-default-from",
"@babel/plugin-proposal-logical-assignment-operators",
"@babel/plugin-proposal-optional-chaining",
Array [
"@babel/plugin-proposal-pipeline-operator",
Object {
"proposal": "minimal",
},
],
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-do-expressions",
],
"presets": Array [],
}
`;
6 changes: 6 additions & 0 deletions __tests__/packageData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ test('jest-cli babel-core bridge', async () => {
test('webpack v1 compatibility', async () => {
expect(await updatePackageJSON(webpackV1Fixture)).toMatchSnapshot();
});

test('replaces stage presets', () => {
expect(upgradeDeps({
"@babel/preset-stage-2": "7.0.0-alpha.0"
}, VERSION)).toMatchSnapshot();
});
16 changes: 8 additions & 8 deletions __tests__/upgradeConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ test("adds legacy option to decorators", () => {
"transform-decorators",
"@babel/plugin-syntax-decorators"
],
"presets": [
["@babel/preset-stage-0", { pipelineProposal: "minimal" }],
["babel-preset-stage-1", { pipelineProposal: "minimal" }],
"stage-2"
]
};

expect(upgradeConfig(config)).toMatchSnapshot();
Expand All @@ -105,10 +100,15 @@ test("adds proposal option to pipeline", () => {
"transform-pipeline-operator",
"@babel/plugin-syntax-pipeline-operator"
],
};

expect(upgradeConfig(config)).toMatchSnapshot();
});

test("replaces stage presets", () => {
const config = {
"presets": [
["@babel/preset-stage-0", { decoratorsLegacy: true }],
["babel-preset-stage-1", { decoratorsLegacy: true }],
["stage-2", { decoratorsLegacy: true }]
"stage-1"
]
};

Expand Down
30 changes: 30 additions & 0 deletions src/packageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,39 @@ const packages = Object.assign(

const latestPackages = new Set(Object.values(packages));

const stagePresets = Object.create(null);
stagePresets[3] = [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-json-strings",
];
stagePresets[2] = [
...stagePresets[3],
["@babel/plugin-proposal-decorators", { "legacy": true }],
"@babel/plugin-proposal-function-sent",
"@babel/plugin-proposal-export-namespace-from",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI This got moved to a needs consensus but not merged yet: tc39/ecma262#1174

"@babel/plugin-proposal-numeric-separator",
"@babel/plugin-proposal-throw-expressions",
];
stagePresets[1] = [
...stagePresets[2],
"@babel/plugin-proposal-export-default-from",
"@babel/plugin-proposal-logical-assignment-operators",
"@babel/plugin-proposal-optional-chaining",
["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }],
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-do-expressions",
];
stagePresets[0] = [
...stagePresets[1],
"@babel/plugin-proposal-function-bind",
];

module.exports = {
packages,
presets,
plugins,
latestPackages,
stagePresets,
};
14 changes: 12 additions & 2 deletions src/upgradeConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { presets: oldPresets, plugins: oldPlugins } = require('./packageData');
const { presets: oldPresets, plugins: oldPlugins, stagePresets } = require('./packageData');
const upgradeOptions = require('./upgradeOptions');

function changeName(originalName, kind) {
Expand All @@ -23,6 +23,7 @@ function changeName(originalName, kind) {
// TODO: fix all of this
function changePresets(config, options = {}) {
let presets = config.presets;
const newPlugins = [];

if (!Array.isArray(presets) && typeof presets === 'string') {
presets = config.presets = config.presets.split(',').map((preset) => preset.trim());
Expand All @@ -39,9 +40,14 @@ function changePresets(config, options = {}) {
const isArray = Array.isArray(preset);

const name = changeName(isArray ? preset[0] : preset, 'preset');
if (name === null) {
if (name === null || name.startsWith('@babel/preset-stage-')) {
presets.splice(i, 1);
i--;

if (name !== null) {
const stage = name.slice(-1);
newPlugins.push(stagePresets[stage]);
}
} else {
if (isArray) preset[0] = name;
else preset = name;
Expand All @@ -53,6 +59,10 @@ function changePresets(config, options = {}) {
if (options.hasFlow && !presets.includes('@babel/preset-flow')) {
presets.push('@babel/preset-flow');
}

if (newPlugins.length > 0) {
config.plugins = (config.plugins || []).concat(...newPlugins);
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/upgradeDeps.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const semver = require('semver');
const { packages: oldPackages, latestPackages } = require('./packageData');
const { packages: oldPackages, latestPackages, stagePresets } = require('./packageData');

const otherPackages = {
'babel-loader': '^8.0.0-beta.0',
Expand Down Expand Up @@ -81,5 +81,15 @@ module.exports = function upgradeDeps(dependencies, version, options = {}) {
dependencies['babel-core'] = '^7.0.0-bridge.0';
}

for (let stage = 0; stage <= 3; stage++) {
if (dependencies[`@babel/preset-stage-${stage}`]) {
delete dependencies[`@babel/preset-stage-${stage}`];
for (const plugin of stagePresets[stage]) {
const name = typeof plugin === "string" ? plugin : plugin[0];
dependencies[name] = version;
}
}
}

return dependencies;
}
4 changes: 0 additions & 4 deletions src/upgradeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ const updaters = {

"@babel/plugin-proposal-pipeline-operator": define({ proposal: "minimal" }),
"@babel/plugin-syntax-pipeline-operator": define({ proposal: "minimal" }),

"@babel/preset-stage-0": define({ decoratorsLegacy: true, pipelineProposal: "minimal" }),
"@babel/preset-stage-1": define({ decoratorsLegacy: true, pipelineProposal: "minimal" }),
"@babel/preset-stage-2": define({ decoratorsLegacy: true }),
};

module.exports = function updateOptions(entry) {
Expand Down