Fix backward-compatibility of babel-preset-es2015 - fixes T7536

This commit is contained in:
Logan Smyth 2016-08-04 20:51:47 -07:00
parent 39865ce746
commit 66317b10db
3 changed files with 39 additions and 38 deletions

View File

@ -273,6 +273,11 @@ export default class OptionManager {
(presetLoc || "a preset") + " which does not accept options.");
}
// For compatibility with babel-core < 6.13.x, allow presets to export an object with a
// a 'buildPreset' function that will return the preset itself, while still exporting a
// simple object (rather than a function), for supporting old Babel versions.
if (typeof val === "object" && val.buildPreset) val = val.buildPreset;
if (typeof val === "function") val = val(context, options);
if (typeof val !== "object") {

View File

@ -1,4 +1,27 @@
module.exports = function(context, opts) {
/**
* This file is a bit of a mess. If you're looking at it as a reference for how to write a preset,
* I'd recommend looking only at `function preset(){}` and ignoring the rest, unless your new preset
* really needs to work on babel-core < 6.13.x, which is unlikely.
*/
/**
* This preset was originally an object, before function-based configurable presets were introduced.
* For backward-compatibility with anything that may have been loading this preset and expecting
* it to be a simple Babel config object, we maintain the old config here.
*/
module.exports = preset({});
// For backward compatibility with babel-core < v6.13.x, we use the 'buildPreset' property
// of the preset object for the preset creation function.
Object.defineProperty(module.exports, "buildPreset", {
configurable: true,
writable: true,
enumerable: false,
value: preset,
});
function preset(context, opts) {
const moduleTypes = ["commonjs", "amd", "umd", "systemjs"];
let loose = false;
let modules = "commonjs";
@ -43,33 +66,5 @@ module.exports = function(context, opts) {
// filter out falsy values
].filter(Boolean)
};
};
}
/**
* This preset was originally an object, before function-based configurable presets were introduced.
* For backward-compatibility with anything that may have been loading this preset and expecting
* it to be a simple Babel config object, we maintain the old config here.
*/
module.exports.plugins = [
require("babel-plugin-transform-es2015-template-literals"),
require("babel-plugin-transform-es2015-literals"),
require("babel-plugin-transform-es2015-function-name"),
require("babel-plugin-transform-es2015-arrow-functions"),
require("babel-plugin-transform-es2015-block-scoped-functions"),
require("babel-plugin-transform-es2015-classes"),
require("babel-plugin-transform-es2015-object-super"),
require("babel-plugin-transform-es2015-shorthand-properties"),
require("babel-plugin-transform-es2015-duplicate-keys"),
require("babel-plugin-transform-es2015-computed-properties"),
require("babel-plugin-transform-es2015-for-of"),
require("babel-plugin-transform-es2015-sticky-regex"),
require("babel-plugin-transform-es2015-unicode-regex"),
require("babel-plugin-check-es2015-constants"),
require("babel-plugin-transform-es2015-spread"),
require("babel-plugin-transform-es2015-parameters"),
require("babel-plugin-transform-es2015-destructuring"),
require("babel-plugin-transform-es2015-block-scoping"),
require("babel-plugin-transform-es2015-typeof-symbol"),
require("babel-plugin-transform-es2015-modules-commonjs"),
[require("babel-plugin-transform-regenerator"), { async: false, asyncGenerators: false }],
];

View File

@ -4,7 +4,8 @@ var expect = require("chai").expect;
suite("es2015 preset", function () {
test("exposes a function", function () {
expect(typeof es2015).to.equal("function");
// Changing this will break compatibility with babel-core < 6.13.x.
expect(typeof es2015).to.equal("object");
});
test("exposes a separate list of plugins", function () {
@ -13,7 +14,7 @@ suite("es2015 preset", function () {
test("doesn't throw with no options passed", function () {
expect(function () {
es2015(null);
es2015.buildPreset(null);
}).not.to.throw();
})
@ -21,7 +22,7 @@ suite("es2015 preset", function () {
suite("loose", function () {
test("throws on non-boolean value", function () {
expect(function () {
es2015(null, { loose: 1});
es2015.buildPreset(null, { loose: 1});
}).to.throw(/must be a boolean/);
});
});
@ -29,25 +30,25 @@ suite("es2015 preset", function () {
test("modules", function () {
test("doesn't throw when passing one false", function () {
expect(function () {
es2015(null, { loose: false });
es2015.buildPreset(null, { loose: false });
}).not.to.throw();
});
test("doesn't throw when passing one of: 'commonjs', 'amd', 'umd', 'systemjs", function () {
expect(function () {
es2015(null, { loose: "commonjs" });
es2015.buildPreset(null, { loose: "commonjs" });
}).not.to.throw();
expect(function () {
es2015(null, { loose: "amd" });
es2015.buildPreset(null, { loose: "amd" });
}).not.to.throw();
expect(function () {
es2015(null, { loose: "umd" });
es2015.buildPreset(null, { loose: "umd" });
}).not.to.throw();
expect(function () {
es2015(null, { loose: "systemjs" });
es2015.buildPreset(null, { loose: "systemjs" });
}).not.to.throw();
});
});