Skip to content

Commit

Permalink
Merge env-standalone to babel-standalone (#10821)
Browse files Browse the repository at this point in the history
* feat: add babel-preset-env to babel-standalone

* chore: copy preset-env-standalone test

* polish: tune warning message when babel-preset-env-standalone is loaded
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Jan 11, 2020
1 parent a131033 commit 405c1aa
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/babel-standalone/package.json
Expand Up @@ -18,6 +18,7 @@
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-proposal-decorators": "^7.7.4",
"@babel/plugin-proposal-do-expressions": "^7.7.4",
"@babel/plugin-proposal-dynamic-import": "^7.7.4",
"@babel/plugin-proposal-export-default-from": "^7.7.4",
"@babel/plugin-proposal-export-namespace-from": "^7.7.4",
"@babel/plugin-proposal-function-bind": "^7.7.4",
Expand Down Expand Up @@ -48,6 +49,7 @@
"@babel/plugin-syntax-object-rest-spread": "^7.7.4",
"@babel/plugin-syntax-optional-catch-binding": "^7.7.4",
"@babel/plugin-syntax-pipeline-operator": "^7.7.7",
"@babel/plugin-syntax-top-level-await": "^7.7.4",
"@babel/plugin-syntax-typescript": "^7.7.4",
"@babel/plugin-transform-arrow-functions": "^7.7.4",
"@babel/plugin-transform-async-to-generator": "^7.7.4",
Expand All @@ -71,6 +73,7 @@
"@babel/plugin-transform-modules-commonjs": "^7.7.5",
"@babel/plugin-transform-modules-systemjs": "^7.7.4",
"@babel/plugin-transform-modules-umd": "^7.7.4",
"@babel/plugin-transform-named-capturing-groups-regex": "^7.7.4",
"@babel/plugin-transform-new-target": "^7.7.4",
"@babel/plugin-transform-object-assign": "^7.7.4",
"@babel/plugin-transform-object-set-prototype-of-to-assign": "^7.7.4",
Expand All @@ -97,6 +100,7 @@
"@babel/plugin-transform-typeof-symbol": "^7.7.4",
"@babel/plugin-transform-typescript": "^7.7.4",
"@babel/plugin-transform-unicode-regex": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@babel/preset-flow": "^7.7.4",
"@babel/preset-react": "^7.7.4",
"@babel/preset-typescript": "^7.7.7"
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-standalone/scripts/pluginConfig.json
Expand Up @@ -15,11 +15,13 @@
"syntax-object-rest-spread",
"syntax-optional-catch-binding",
"syntax-pipeline-operator",
"syntax-top-level-await",
"syntax-typescript",
"proposal-async-generator-functions",
"proposal-class-properties",
"proposal-decorators",
"proposal-do-expressions",
"proposal-dynamic-import",
"proposal-export-default-from",
"proposal-export-namespace-from",
"proposal-function-bind",
Expand Down Expand Up @@ -57,6 +59,7 @@
"transform-modules-commonjs",
"transform-modules-systemjs",
"transform-modules-umd",
"transform-named-capturing-groups-regex",
"transform-new-target",
"transform-object-assign",
"transform-object-super",
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-standalone/src/generated/plugins.js
Expand Up @@ -19,11 +19,13 @@ import syntaxJsx from "@babel/plugin-syntax-jsx";
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator";
import syntaxTopLevelAwait from "@babel/plugin-syntax-top-level-await";
import syntaxTypescript from "@babel/plugin-syntax-typescript";
import proposalAsyncGeneratorFunctions from "@babel/plugin-proposal-async-generator-functions";
import proposalClassProperties from "@babel/plugin-proposal-class-properties";
import proposalDecorators from "@babel/plugin-proposal-decorators";
import proposalDoExpressions from "@babel/plugin-proposal-do-expressions";
import proposalDynamicImport from "@babel/plugin-proposal-dynamic-import";
import proposalExportDefaultFrom from "@babel/plugin-proposal-export-default-from";
import proposalExportNamespaceFrom from "@babel/plugin-proposal-export-namespace-from";
import proposalFunctionBind from "@babel/plugin-proposal-function-bind";
Expand Down Expand Up @@ -61,6 +63,7 @@ import transformModulesAmd from "@babel/plugin-transform-modules-amd";
import transformModulesCommonjs from "@babel/plugin-transform-modules-commonjs";
import transformModulesSystemjs from "@babel/plugin-transform-modules-systemjs";
import transformModulesUmd from "@babel/plugin-transform-modules-umd";
import transformNamedCapturingGroupsRegex from "@babel/plugin-transform-named-capturing-groups-regex";
import transformNewTarget from "@babel/plugin-transform-new-target";
import transformObjectAssign from "@babel/plugin-transform-object-assign";
import transformObjectSuper from "@babel/plugin-transform-object-super";
Expand Down Expand Up @@ -105,11 +108,13 @@ export {
syntaxObjectRestSpread,
syntaxOptionalCatchBinding,
syntaxPipelineOperator,
syntaxTopLevelAwait,
syntaxTypescript,
proposalAsyncGeneratorFunctions,
proposalClassProperties,
proposalDecorators,
proposalDoExpressions,
proposalDynamicImport,
proposalExportDefaultFrom,
proposalExportNamespaceFrom,
proposalFunctionBind,
Expand Down Expand Up @@ -147,6 +152,7 @@ export {
transformModulesCommonjs,
transformModulesSystemjs,
transformModulesUmd,
transformNamedCapturingGroupsRegex,
transformNewTarget,
transformObjectAssign,
transformObjectSuper,
Expand Down Expand Up @@ -192,11 +198,13 @@ export const all = {
"syntax-object-rest-spread": syntaxObjectRestSpread,
"syntax-optional-catch-binding": syntaxOptionalCatchBinding,
"syntax-pipeline-operator": syntaxPipelineOperator,
"syntax-top-level-await": syntaxTopLevelAwait,
"syntax-typescript": syntaxTypescript,
"proposal-async-generator-functions": proposalAsyncGeneratorFunctions,
"proposal-class-properties": proposalClassProperties,
"proposal-decorators": proposalDecorators,
"proposal-do-expressions": proposalDoExpressions,
"proposal-dynamic-import": proposalDynamicImport,
"proposal-export-default-from": proposalExportDefaultFrom,
"proposal-export-namespace-from": proposalExportNamespaceFrom,
"proposal-function-bind": proposalFunctionBind,
Expand Down Expand Up @@ -234,6 +242,7 @@ export const all = {
"transform-modules-commonjs": transformModulesCommonjs,
"transform-modules-systemjs": transformModulesSystemjs,
"transform-modules-umd": transformModulesUmd,
"transform-named-capturing-groups-regex": transformNamedCapturingGroupsRegex,
"transform-new-target": transformNewTarget,
"transform-object-assign": transformObjectAssign,
"transform-object-super": transformObjectSuper,
Expand Down
16 changes: 12 additions & 4 deletions packages/babel-standalone/src/index.js
Expand Up @@ -21,8 +21,9 @@ import presetStage0 from "./preset-stage-0";
import presetStage1 from "./preset-stage-1";
import presetStage2 from "./preset-stage-2";
import presetStage3 from "./preset-stage-3";
import presetReact from "@babel/preset-react";
import presetEnv from "@babel/preset-env";
import presetFlow from "@babel/preset-flow";
import presetReact from "@babel/preset-react";
import presetTypescript from "@babel/preset-typescript";

import { runScripts } from "./transformScriptTags";
Expand Down Expand Up @@ -136,9 +137,15 @@ export function registerPlugins(newPlugins: {
*/
export function registerPreset(name: string, preset: Object | Function): void {
if (Object.prototype.hasOwnProperty.call(availablePresets, name)) {
console.warn(
`A preset named "${name}" is already registered, it will be overridden`,
);
if (name === "env") {
console.warn(
"@babel/preset-env is now included in @babel/standalone, please remove @babel/preset-env-standalone",
);
} else {
console.warn(
`A preset named "${name}" is already registered, it will be overridden`,
);
}
}
availablePresets[name] = preset;
}
Expand All @@ -163,6 +170,7 @@ registerPlugins(all);
// Want to get rid of this whitelist of presets?
// Wait! Please read https://github.com/babel/babel/pull/6177 first.
registerPresets({
env: presetEnv,
es2015: preset2015,
es2016: () => {
return {
Expand Down
55 changes: 55 additions & 0 deletions packages/babel-standalone/test/babel.js
Expand Up @@ -110,6 +110,61 @@
).toThrow(/Invalid plugin specified in Babel options: "lolfail"/);
});

describe("env preset", () => {
it("works w/o targets", () => {
const output = Babel.transform("const a = 1;", {
sourceType: "script",
presets: ["env"],
}).code;
expect(output).toBe("var a = 1;");
});

it("doesn't transpile `const` with chrome 60", () => {
const output = Babel.transform("const a = 1;", {
sourceType: "script",
presets: [
[
"env",
{
targets: {
chrome: 60,
},
},
],
],
}).code;
expect(output).toBe("const a = 1;");
});

it("transpiles `const` with chrome 60 and preset-es2015", () => {
const output = Babel.transform("const a = 1;", {
sourceType: "script",
presets: [
[
"env",
{
targets: {
chrome: 60,
},
},
],
"es2015",
],
}).code;
expect(output).toBe("var a = 1;");
});

it("uses transform-new-targets plugin", () => {
const output = Babel.transform("function Foo() {new.target}", {
sourceType: "script",
presets: ["env"],
}).code;
expect(output).toBe(
"function Foo() {\n this instanceof Foo ? this.constructor : void 0;\n}",
);
});
});

describe("custom plugins and presets", () => {
const lolizer = () => ({
visitor: {
Expand Down

0 comments on commit 405c1aa

Please sign in to comment.