add loose/useBuiltIns option to stage presets, use it, opt babylon build (#6733)

* add loose, useBuiltIns options to presets + use loose class properties

* whitelist helpers for babylon

* use transform-for-of-as-array in babylon
This commit is contained in:
Henry Zhu 2017-11-03 14:22:06 -04:00 committed by GitHub
parent 9cf017b2ca
commit 1196ec1e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 217 additions and 44 deletions

View File

@ -31,27 +31,34 @@ function istanbulHacks() {
} }
let envOpts = { let envOpts = {
loose: true loose: true,
exclude: ["transform-typeof-symbol"],
}; };
module.exports = { const config = {
comments: false, comments: false,
presets: [ presets: [
["@babel/env", envOpts], ["@babel/env", envOpts],
"@babel/stage-0",
"@babel/flow" "@babel/flow"
], ],
env: { plugins: [
cov: { ["@babel/proposal-class-properties", { loose: true }],
auxiliaryCommentBefore: "istanbul ignore next", "@babel/proposal-export-namespace",
plugins: [istanbulHacks] "@babel/proposal-numeric-separator",
} ["@babel/proposal-object-rest-spread", { useBuiltIns: true}],
} ]
}; };
if (process.env.BABEL_ENV === "cov") {
config.auxiliaryCommentBefore = "istanbul ignore next";
config.plugins.push(istanbulHacks);
}
if (process.env.BABEL_ENV === "development") { if (process.env.BABEL_ENV === "development") {
envOpts.targets = { envOpts.targets = {
node: "current" node: "current"
}; };
envOpts.debug = true; envOpts.debug = true;
} }
module.exports = config;

View File

@ -29,12 +29,28 @@ npm install --save-dev @babel/preset-react
**.babelrc** **.babelrc**
Without options:
```json ```json
{ {
"presets": ["@babel/react"] "presets": ["@babel/react"]
} }
``` ```
With options:
```json
{
"presets": [
["@babel/react", {
"pragma": "dom", // default pragma is React.createElement
"pragmaFrag": "DomFrag", // default is React.Fragment
"throwIfNamespace": false // defaults to true
}]
]
}
```
### Via CLI ### Via CLI
```sh ```sh
@ -51,6 +67,24 @@ require("@babel/core").transform("code", {
## Options ## Options
### `pragma`
`string`, defaults to `React.createElement`.
Replace the function used when compiling JSX expressions.
### `pragmaFrag`
`string`, defaults to `React.Fragment`.
Replace the component used when compiling JSX fragments.
### `useBuiltIns`
`boolean`, defaults to `false`.
Will use the native built-in instead of trying to polyfill behavior for any plugins that require one.
### `development` ### `development`
`boolean`, defaults to `false`. `boolean`, defaults to `false`.
@ -59,13 +93,23 @@ Toggles plugins that aid in development, such as [`@babel/plugin-transform-react
This is useful when combined with either a `babelrc.js` or [env option in a .babelrc](https://babeljs.io/docs/usage/babelrc/#env-option) configuration: This is useful when combined with either a `babelrc.js` or [env option in a .babelrc](https://babeljs.io/docs/usage/babelrc/#env-option) configuration:
### `throwIfNamespace`
`boolean`, defaults to `true`.
Toggles whether or not to throw an error if a XML namespaced tag name is used. For example:
<f:image />
Though the JSX spec allows this, it is disabled by default since React's JSX does not currently have support for it.
#### babelrc.js #### babelrc.js
```js ```js
module.exports = { module.exports = {
presets: [ presets: [
["@babel/react", { ["@babel/react", {
development: process.env.BABEL_ENV === "development" development: process.env.BABEL_ENV === "development",
}], }],
], ],
} }

View File

@ -5,15 +5,25 @@ import transformReactJSXSource from "@babel/plugin-transform-react-jsx-source";
import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self"; import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self";
export default function(context, opts = {}) { export default function(context, opts = {}) {
const development = opts.development || false; const pragma = opts.pragma || "React.createElement";
const pragmaFrag = opts.pragmaFrag || "React.Fragment";
const throwIfNamespace =
opts.throwIfNamespace === undefined ? true : !!opts.throwIfNamespace;
const development = !!opts.development;
const useBuiltIns = !!opts.useBuiltIns;
if (typeof development !== "boolean") { if (typeof development !== "boolean") {
throw new Error("Preset react 'development' option must be a boolean."); throw new Error(
"@babel/preset-react 'development' option must be a boolean.",
);
} }
return { return {
plugins: [ plugins: [
[
transformReactJSX, transformReactJSX,
{ pragma, pragmaFrag, throwIfNamespace, useBuiltIns },
],
transformSyntaxJSX, transformSyntaxJSX,
transformReactDisplayName, transformReactDisplayName,

View File

@ -7,14 +7,4 @@ describe("react preset", () => {
react(null); react(null);
}).not.to.throw(); }).not.to.throw();
}); });
describe("options", () => {
describe("development", () => {
it("throws on non-boolean value", () => {
expect(function() {
react(null, { development: 1 });
}).to.throw(/must be a boolean/);
});
});
});
}); });

View File

@ -33,3 +33,17 @@ require("@babel/core").transform("code", {
presets: ["@babel/stage-0"] presets: ["@babel/stage-0"]
}); });
``` ```
## Options
### `loose`
`boolean`, defaults to `false`.
Enable "loose" transformations for any plugins in this preset that allow them.
### `useBuiltIns`
`boolean`, defaults to `false`.
Will use the native built-in instead of trying to polyfill behavior for any plugins that require one.

View File

@ -3,9 +3,26 @@ import presetStage1 from "@babel/preset-stage-1";
import transformDoExpressions from "@babel/plugin-proposal-do-expressions"; import transformDoExpressions from "@babel/plugin-proposal-do-expressions";
import transformFunctionBind from "@babel/plugin-proposal-function-bind"; import transformFunctionBind from "@babel/plugin-proposal-function-bind";
export default function() { export default function(context, opts = {}) {
let loose = false;
let useBuiltIns = false;
if (opts !== undefined) {
if (opts.loose !== undefined) loose = opts.loose;
if (opts.useBuiltIns !== undefined) useBuiltIns = opts.useBuiltIns;
}
if (typeof loose !== "boolean") {
throw new Error("@babel/preset-stage-0 'loose' option must be a boolean.");
}
if (typeof useBuiltIns !== "boolean") {
throw new Error(
"@babel/preset-stage-0 'useBuiltIns' option must be a boolean.",
);
}
return { return {
presets: [presetStage1], presets: [[presetStage1, { loose, useBuiltIns }]],
plugins: [transformDoExpressions, transformFunctionBind], plugins: [transformDoExpressions, transformFunctionBind],
}; };
} }

View File

@ -44,6 +44,20 @@ require("@babel/core").transform("code", {
}); });
``` ```
## Options
### `loose`
`boolean`, defaults to `false`.
Enable "loose" transformations for any plugins in this preset that allow them.
### `useBuiltIns`
`boolean`, defaults to `false`.
Will use the native built-in instead of trying to polyfill behavior for any plugins that require one.
## References ## References
- Chapter "[The TC39 process for ECMAScript features](http://exploringjs.com/es2016-es2017/ch_tc39-process.html)" in "Exploring ES2016 and ES2017" by Axel Rauschmayer - Chapter "[The TC39 process for ECMAScript features](http://exploringjs.com/es2016-es2017/ch_tc39-process.html)" in "Exploring ES2016 and ES2017" by Axel Rauschmayer

View File

@ -6,15 +6,32 @@ import transformOptionalChaining from "@babel/plugin-proposal-optional-chaining"
import transformPipelineOperator from "@babel/plugin-proposal-pipeline-operator"; import transformPipelineOperator from "@babel/plugin-proposal-pipeline-operator";
import transformNullishCoalescingOperator from "@babel/plugin-proposal-nullish-coalescing-operator"; import transformNullishCoalescingOperator from "@babel/plugin-proposal-nullish-coalescing-operator";
export default function() { export default function(context, opts = {}) {
let loose = false;
let useBuiltIns = false;
if (opts !== undefined) {
if (opts.loose !== undefined) loose = opts.loose;
if (opts.useBuiltIns !== undefined) useBuiltIns = opts.useBuiltIns;
}
if (typeof loose !== "boolean") {
throw new Error("@babel/preset-stage-1 'loose' option must be a boolean.");
}
if (typeof useBuiltIns !== "boolean") {
throw new Error(
"@babel/preset-stage-1 'useBuiltIns' option must be a boolean.",
);
}
return { return {
presets: [presetStage2], presets: [[presetStage2, { loose, useBuiltIns }]],
plugins: [ plugins: [
transformDecorators, transformDecorators,
transformExportDefault, transformExportDefault,
transformOptionalChaining, [transformOptionalChaining, { loose }],
transformPipelineOperator, transformPipelineOperator,
transformNullishCoalescingOperator, [transformNullishCoalescingOperator, { loose }],
], ],
}; };
} }

View File

@ -12,8 +12,6 @@ The gist of Stage 2 is:
> >
> **Whats next?** Only incremental changes are expected from now on. > **Whats next?** Only incremental changes are expected from now on.
## Install ## Install
```sh ```sh
@ -45,6 +43,21 @@ require("@babel/core").transform("code", {
presets: ["@babel/stage-2"] presets: ["@babel/stage-2"]
}); });
``` ```
## Options
### `loose`
`boolean`, defaults to `false`.
Enable "loose" transformations for any plugins in this preset that allow them.
### `useBuiltIns`
`boolean`, defaults to `false`.
Will use the native built-in instead of trying to polyfill behavior for any plugins that require one.
## References ## References
- Chapter "[The TC39 process for ECMAScript features](http://exploringjs.com/es2016-es2017/ch_tc39-process.html)" in "Exploring ES2016 and ES2017" by Axel Rauschmayer - Chapter "[The TC39 process for ECMAScript features](http://exploringjs.com/es2016-es2017/ch_tc39-process.html)" in "Exploring ES2016 and ES2017" by Axel Rauschmayer

View File

@ -5,9 +5,26 @@ import transformExportNamespace from "@babel/plugin-proposal-export-namespace";
import transformNumericSeparator from "@babel/plugin-proposal-numeric-separator"; import transformNumericSeparator from "@babel/plugin-proposal-numeric-separator";
import transformThrowExpressions from "@babel/plugin-proposal-throw-expressions"; import transformThrowExpressions from "@babel/plugin-proposal-throw-expressions";
export default function() { export default function(context, opts = {}) {
let loose = false;
let useBuiltIns = false;
if (opts !== undefined) {
if (opts.loose !== undefined) loose = opts.loose;
if (opts.useBuiltIns !== undefined) useBuiltIns = opts.useBuiltIns;
}
if (typeof loose !== "boolean") {
throw new Error("@babel/preset-stage-2 'loose' option must be a boolean.");
}
if (typeof useBuiltIns !== "boolean") {
throw new Error(
"@babel/preset-stage-2 'useBuiltIns' option must be a boolean.",
);
}
return { return {
presets: [presetStage3], presets: [[presetStage3, { loose, useBuiltIns }]],
plugins: [ plugins: [
transformFunctionSent, transformFunctionSent,
transformExportNamespace, transformExportNamespace,

View File

@ -12,8 +12,6 @@ The gist of Stage 3 is:
> >
> **Whats next?** Henceforth, changes should only be made in response to critical issues raised by the implementations and their use. > **Whats next?** Henceforth, changes should only be made in response to critical issues raised by the implementations and their use.
## Install ## Install
```sh ```sh
@ -46,6 +44,20 @@ require("@babel/core").transform("code", {
}); });
``` ```
## Options
### `loose`
`boolean`, defaults to `false`.
Enable "loose" transformations for any plugins in this preset that allow them.
### `useBuiltIns`
`boolean`, defaults to `false`.
Will use the native built-in instead of trying to polyfill behavior for any plugins that require one.
## References ## References
- Chapter "[The TC39 process for ECMAScript features](http://exploringjs.com/es2016-es2017/ch_tc39-process.html)" in "Exploring ES2016 and ES2017" by Axel Rauschmayer - Chapter "[The TC39 process for ECMAScript features](http://exploringjs.com/es2016-es2017/ch_tc39-process.html)" in "Exploring ES2016 and ES2017" by Axel Rauschmayer

View File

@ -5,13 +5,30 @@ import transformObjectRestSpread from "@babel/plugin-proposal-object-rest-spread
import transformOptionalCatchBinding from "@babel/plugin-proposal-optional-catch-binding"; import transformOptionalCatchBinding from "@babel/plugin-proposal-optional-catch-binding";
import transformUnicodePropertyRegex from "@babel/plugin-proposal-unicode-property-regex"; import transformUnicodePropertyRegex from "@babel/plugin-proposal-unicode-property-regex";
export default function() { export default function(context, opts = {}) {
let loose = false;
let useBuiltIns = false;
if (opts !== undefined) {
if (opts.loose !== undefined) loose = opts.loose;
if (opts.useBuiltIns !== undefined) useBuiltIns = opts.useBuiltIns;
}
if (typeof loose !== "boolean") {
throw new Error("@babel/preset-stage-3 'loose' option must be a boolean.");
}
if (typeof useBuiltIns !== "boolean") {
throw new Error(
"@babel/preset-stage-3 'useBuiltIns' option must be a boolean.",
);
}
return { return {
plugins: [ plugins: [
syntaxDynamicImport, syntaxDynamicImport,
transformAsyncGeneratorFunctions, transformAsyncGeneratorFunctions,
transformClassProperties, [transformClassProperties, { loose }],
transformObjectRestSpread, [transformObjectRestSpread, { useBuiltIns }],
transformOptionalCatchBinding, transformOptionalCatchBinding,
transformUnicodePropertyRegex, transformUnicodePropertyRegex,
], ],

View File

@ -24,6 +24,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/helper-fixtures": "7.0.0-beta.5", "@babel/helper-fixtures": "7.0.0-beta.5",
"babel-plugin-transform-for-of-as-array": "1.0.4",
"rollup": "^0.50.0", "rollup": "^0.50.0",
"rollup-plugin-babel": "^4.0.0-beta.0", "rollup-plugin-babel": "^4.0.0-beta.0",
"rollup-plugin-node-resolve": "^3.0.0", "rollup-plugin-node-resolve": "^3.0.0",

View File

@ -9,6 +9,7 @@ export default {
}, },
plugins: [ plugins: [
babel({ babel({
externalHelpersWhitelist: ["inheritsLoose"],
babelrc: false, babelrc: false,
presets: [ presets: [
[ [
@ -23,6 +24,7 @@ export default {
], ],
"@babel/flow", "@babel/flow",
], ],
plugins: ["transform-for-of-as-array"],
}), }),
nodeResolve(), nodeResolve(),
], ],

View File

@ -56,12 +56,10 @@ export default class LValParser extends NodeUtils {
case "ObjectExpression": case "ObjectExpression":
node.type = "ObjectPattern"; node.type = "ObjectPattern";
for (const [index, prop] of node.properties.entries()) { for (let index = 0; index < node.properties.length; index++) {
this.toAssignableObjectExpressionProp( const prop = node.properties[index];
prop, const isLast = index === node.properties.length - 1;
isBinding, this.toAssignableObjectExpressionProp(prop, isBinding, isLast);
index === node.properties.length - 1,
);
} }
break; break;