Support spec option (#98)
This commit is contained in:
parent
4648bb6022
commit
75db91940e
@ -132,6 +132,12 @@ To prevent these errors - specify the uglify option, which will enable all plugi
|
||||
|
||||
> NOTE: Uglify has a work-in-progress "Harmony" branch to address the lack of ES6 support, but it is not yet stable. You can follow its progress in [UglifyJS2 issue #448](https://github.com/mishoo/UglifyJS2/issues/448). If you require an alternative minifier which _does_ support ES6 syntax, we recommend using [Babili](https://github.com/babel/babili).
|
||||
|
||||
### `spec`
|
||||
|
||||
`boolean`, defaults to `false`.
|
||||
|
||||
Enable more spec compliant, but potentially slower, transformations for any plugins in this preset that support them.
|
||||
|
||||
### `loose`
|
||||
|
||||
`boolean`, defaults to `false`.
|
||||
|
||||
@ -165,13 +165,12 @@ function getPlatformSpecificDefaultFor(targets) {
|
||||
|
||||
export default function buildPreset(context, opts = {}) {
|
||||
const validatedOptions = normalizeOptions(opts);
|
||||
const { debug, loose, moduleType, useBuiltIns } = validatedOptions;
|
||||
const { debug, loose, moduleType, spec, useBuiltIns } = validatedOptions;
|
||||
|
||||
const targets = getTargets(validatedOptions.targets);
|
||||
const include = transformIncludesAndExcludes(validatedOptions.include);
|
||||
const exclude = transformIncludesAndExcludes(validatedOptions.exclude);
|
||||
|
||||
|
||||
const filterPlugins = filterItem.bind(null, targets, exclude.plugins, pluginList);
|
||||
const transformations = Object.keys(pluginList)
|
||||
.filter(filterPlugins)
|
||||
@ -210,11 +209,13 @@ export default function buildPreset(context, opts = {}) {
|
||||
const modulePlugin = moduleType !== false && moduleTransformations[moduleType];
|
||||
const plugins = [];
|
||||
|
||||
// NOTE: not giving spec here yet to avoid compatibility issues when
|
||||
// babel-plugin-transform-es2015-modules-commonjs gets its spec mode
|
||||
modulePlugin &&
|
||||
plugins.push([require(`babel-plugin-${modulePlugin}`), { loose }]);
|
||||
|
||||
plugins.push(...transformations.map((pluginName) =>
|
||||
[require(`babel-plugin-${pluginName}`), { loose }]
|
||||
[require(`babel-plugin-${pluginName}`), { spec, loose }]
|
||||
));
|
||||
|
||||
useBuiltIns &&
|
||||
|
||||
@ -53,15 +53,21 @@ export const checkDuplicateIncludeExcludes = (include = [], exclude = []) => {
|
||||
);
|
||||
};
|
||||
|
||||
export const validateLooseOption = (looseOpt = false) => {
|
||||
invariant(
|
||||
typeof looseOpt === "boolean",
|
||||
"Invalid Option: The 'loose' option must be a boolean."
|
||||
);
|
||||
export const validateBoolOption = (name, value, defaultValue) => {
|
||||
if (typeof value === "undefined") {
|
||||
value = defaultValue;
|
||||
}
|
||||
|
||||
return looseOpt;
|
||||
if (typeof value !== "boolean") {
|
||||
throw new Error(`Preset env: '${name}' option must be a boolean.`);
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
export const validateLooseOption = (looseOpt) => validateBoolOption("loose", looseOpt, false);
|
||||
export const validateSpecOption = (specOpt) => validateBoolOption("spec", specOpt, false);
|
||||
|
||||
export const validateModulesOption = (modulesOpt = "commonjs") => {
|
||||
invariant(
|
||||
modulesOpt === false || Object.keys(moduleTransformations).indexOf(modulesOpt) > -1,
|
||||
@ -104,6 +110,7 @@ export default function normalizeOptions(opts) {
|
||||
include: validateIncludesAndExcludes(opts.include, "include"),
|
||||
loose: validateLooseOption(opts.loose),
|
||||
moduleType: validateModulesOption(opts.modules),
|
||||
spec: validateSpecOption(opts.spec),
|
||||
targets: opts.targets,
|
||||
useBuiltIns: opts.useBuiltIns
|
||||
};
|
||||
|
||||
2
experimental/babel-preset-env/test/fixtures/preset-options/spec/actual.js
vendored
Normal file
2
experimental/babel-preset-env/test/fixtures/preset-options/spec/actual.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
const bar = "bar";
|
||||
const x = () => `foo${bar}`;
|
||||
10
experimental/babel-preset-env/test/fixtures/preset-options/spec/expected.js
vendored
Normal file
10
experimental/babel-preset-env/test/fixtures/preset-options/spec/expected.js
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
function _newArrowCheck(innerThis, boundThis) { if (innerThis !== boundThis) { throw new TypeError("Cannot instantiate an arrow function"); } }
|
||||
|
||||
var bar = "bar";
|
||||
var x = function () {
|
||||
_newArrowCheck(undefined, undefined);
|
||||
|
||||
return "foo" + bar;
|
||||
}.bind(undefined);
|
||||
7
experimental/babel-preset-env/test/fixtures/preset-options/spec/options.json
vendored
Normal file
7
experimental/babel-preset-env/test/fixtures/preset-options/spec/options.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"presets": [
|
||||
["../../../../lib", {
|
||||
"spec": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
@ -5,10 +5,11 @@ const assert = require("assert");
|
||||
|
||||
const {
|
||||
checkDuplicateIncludeExcludes,
|
||||
normalizePluginNames,
|
||||
validateIncludesAndExcludes,
|
||||
validateLooseOption,
|
||||
validateModulesOption,
|
||||
normalizePluginNames
|
||||
validateSpecOption,
|
||||
} = normalizeOptions;
|
||||
|
||||
describe("normalize-options", () => {
|
||||
@ -60,6 +61,20 @@ describe("normalize-options", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateSpecOption", () => {
|
||||
it("`undefined` option returns false", () => {
|
||||
assert(validateSpecOption() === false);
|
||||
});
|
||||
|
||||
it("`false` option returns false", () => {
|
||||
assert(validateSpecOption(false) === false);
|
||||
});
|
||||
|
||||
it("`true` option returns true", () => {
|
||||
assert(validateSpecOption(true) === true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkDuplicateIncludeExcludes", function() {
|
||||
it("should throw if duplicate names in both", function() {
|
||||
assert.throws(() => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user