From b80dfaaa7f88cd569b037bd54fc1712329af13d1 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Thu, 6 Oct 2016 22:33:03 -0400 Subject: [PATCH] update node data, fix version issues (#13) --- data/plugins.json | 30 ++++++++++++++-------- package.json | 2 +- src/index.js | 29 +++++++++++++--------- test/index.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 101 insertions(+), 23 deletions(-) diff --git a/data/plugins.json b/data/plugins.json index f47c5b57..2e0b5935 100644 --- a/data/plugins.json +++ b/data/plugins.json @@ -3,7 +3,8 @@ "chrome": 47, "edge": 13, "firefox": 45, - "safari": 10 + "safari": 10, + "node": 6 }, "transform-es2015-block-scoped-functions": { "chrome": 41, @@ -14,7 +15,8 @@ "transform-es2015-block-scoping": { "chrome": 49, "firefox": 51, - "safari": 10 + "safari": 10, + "node": 6 }, "transform-es2015-classes": { "chrome": 46, @@ -33,19 +35,23 @@ "check-es2015-constants": { "chrome": 49, "firefox": 51, - "safari": 10 + "safari": 10, + "node": 6 }, "transform-es2015-destructuring": { "chrome": 51, - "safari": 10 + "safari": 10, + "node": 6.5 }, "transform-es2015-for-of": { "chrome": 51, - "safari": 10 + "safari": 10, + "node": 6.5 }, "transform-es2015-function-name": { "chrome": 51, - "safari": 10 + "safari": 10, + "node": 6.5 }, "transform-es2015-literals": { "chrome": 44, @@ -63,7 +69,8 @@ "transform-es2015-parameters": { "chrome": 49, "edge": 14, - "safari": 10 + "safari": 10, + "node": 6 }, "transform-es2015-shorthand-properties": { "chrome": 43, @@ -83,7 +90,8 @@ "chrome": 49, "edge": 13, "firefox": 3, - "safari": 10 + "safari": 10, + "node": 6 }, "transform-es2015-template-literals": { "chrome": 41, @@ -103,12 +111,14 @@ "chrome": 50, "edge": 13, "firefox": 46, - "safari": 10 + "safari": 10, + "node": 6 }, "transform-regenerator": { "chrome": 50, "edge": 13, - "safari": 10 + "safari": 10, + "node": 6 }, "transform-exponentiation-operator": { "chrome": 52, diff --git a/package.json b/package.json index bf66c17a..23379efa 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "babel-plugin-transform-flow-strip-types": "^6.8.0", "babel-preset-es2015": "^6.14.0", "babel-register": "^6.14.0", - "compat-table": "github:kangax/compat-table#gh-pages", + "compat-table": "github:hzoo/compat-table#node-fix", "eslint": "^3.3.1", "eslint-config-babel": "^1.0.1", "eslint-plugin-babel": "^3.3.0", diff --git a/src/index.js b/src/index.js index d061ffc3..0bc88c83 100644 --- a/src/index.js +++ b/src/index.js @@ -44,13 +44,20 @@ export const isPluginRequired = (supportedEnvironments, plugin) => { if (targetEnvironments.length === 0) { return true; } const isRequiredForEnvironments = targetEnvironments - .filter(environemt => { + .filter(environment => { // Feature is not implemented in that environment - if (!plugin[environemt]) { return true; } + if (!plugin[environment]) { return true; } - const lowestImplementedVersion = plugin[environemt]; - const lowestTargetedVersion = targetEnvironments[environemt]; - if (lowestTargetedVersion <= lowestImplementedVersion) { return true; } + const lowestImplementedVersion = plugin[environment]; + const lowestTargetedVersion = supportedEnvironments[environment]; + + if (environment === "node" && lowestTargetedVersion % 1 === 0) { + throw new Error("Please use a minor version when specifying `node`: 6.5, 6.7"); + } + + if (lowestTargetedVersion < lowestImplementedVersion) { + return true; + } return false; }); @@ -60,7 +67,7 @@ export const isPluginRequired = (supportedEnvironments, plugin) => { const getTargets = targetOpts => { return targetOpts || {}; -} +}; // TODO: Allow specifying plugins as either shortened or full name // babel-plugin-transform-es2015-classes @@ -71,16 +78,16 @@ export const validateLooseOption = (looseOpt = false) => { } return looseOpt; -} +}; export const validateModulesOption = (modulesOpt = "commonjs") => { if (modulesOpt !== false && Object.keys(MODULE_TRANSFORMATIONS).indexOf(modulesOpt) === -1) { - throw new Error("The 'modules' option must be 'false' to indicate no modules\n" + - "or a module type which be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'"); + throw new Error("The 'modules' option must be 'false' to indicate no modules\n" + + "or a module type which be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'"); } return modulesOpt; -} +}; export default function buildPreset(context, opts) { const loose = validateLooseOption(opts.loose); @@ -90,7 +97,7 @@ export default function buildPreset(context, opts) { const transformations = Object.keys(pluginList) .filter(pluginName => isPluginRequired(targets, pluginList[pluginName])) .map(pluginName => { - return [require(`babel-plugin-${pluginName}`), { loose }] + return [require(`babel-plugin-${pluginName}`), { loose }]; }); const modules = [ diff --git a/test/index.js b/test/index.js index 8c636fdf..9094515b 100644 --- a/test/index.js +++ b/test/index.js @@ -14,13 +14,14 @@ describe("babel-preset-env", () => { const isRequired = babelPresetEnv.isPluginRequired({}, {}); assert(isRequired); }); + it("returns true if plugin feature is not implemented in one or more targets", () => { let targets; const plugin = { edge: false, firefox: 45, chrome: 49, - } + }; targets = { "chrome": Number.MAX_SAFE_INTEGER, @@ -33,6 +34,66 @@ describe("babel-preset-env", () => { }; assert(babelPresetEnv.isPluginRequired(plugin, plugin) === true); }); + + it("returns false if plugin feature is implemented by lower than target", () => { + const plugin = { + chrome: 49, + }; + const targets = { + "chrome": Number.MAX_SAFE_INTEGER, + }; + assert(babelPresetEnv.isPluginRequired(targets, plugin) === false); + }); + + it("returns false if plugin feature is implemented is equal to target", () => { + const plugin = { + chrome: 49, + }; + const targets = { + "chrome": 49, + }; + assert(babelPresetEnv.isPluginRequired(targets, plugin) === false); + }); + + it("returns true if plugin feature is implemented is greater than target", () => { + const plugin = { + chrome: 50, + }; + const targets = { + "chrome": 49, + }; + assert(babelPresetEnv.isPluginRequired(targets, plugin) === true); + }); + + it("doesn't throw when specifiying a decimal for node", () => { + let targets; + const plugin = { + node: 6 + }; + + targets = { + "node": 6.5 + }; + + assert.doesNotThrow(() => { + babelPresetEnv.isPluginRequired(targets, plugin); + }, Error); + }); + + it("throws when specifiying an integer for node", () => { + let targets; + const plugin = { + node: 6 + }; + + targets = { + "node": 6 + }; + + assert.throws(() => { + babelPresetEnv.isPluginRequired(targets, plugin); + }, Error); + }); }); describe("validateLooseOption", () => {