Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Commit

Permalink
update node data, fix version issues (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo authored Oct 7, 2016
1 parent c235b49 commit b80dfaa
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 23 deletions.
30 changes: 20 additions & 10 deletions data/plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"chrome": 47,
"edge": 13,
"firefox": 45,
"safari": 10
"safari": 10,
"node": 6

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 1, 2016

Member

Aren't arrow functions supported in Node 4?

This comment has been minimized.

Copy link
@hzoo

hzoo Nov 1, 2016

Author Member

Yeah I guess this is what I mean by #16 - it's just what we get from the compat-table data

This comment has been minimized.

Copy link
@hzoo

hzoo Nov 1, 2016

Author Member

Was wondering about that
screen shot 2016-11-01 at 3 25 28 pm

works in most cases so need to account for that somehow

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 1, 2016

Member

Ya it looks like it really only fails one test, correct precedence, since the others are non-strict-mode (which babel doesn't compile to) or related to the new.target feature which isn't in v4.

The correct precedence bug looks like an edge case of

0 || () => 2 // should throw SyntaxError: Unexpected token )

This comment has been minimized.

Copy link
@hzoo

hzoo Nov 1, 2016

Author Member

Oh good point, so we should just be ok with strict mode

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 1, 2016

Member

It looks like babel will throw an error without the es2015 preset for

0 || () => 2

anyway (based on repl without es2015 present selected) which means that it's basically 100% for Node 4.

This comment has been minimized.

Copy link
@hzoo

hzoo Nov 1, 2016

Author Member

yeah in reality it should check against what babel is able to do although would be more work

This comment has been minimized.

Copy link
@jdalton

jdalton Nov 1, 2016

Member

I wonder if you could use the es compat table as a base-line then apply overrides for cases like this.

This comment has been minimized.

Copy link
@hzoo

hzoo Nov 1, 2016

Author Member

We totally can because it's just a generated file so if compat-table changes it's not affected until we rerun the script + publish

This comment has been minimized.

Copy link
@hzoo

hzoo Nov 1, 2016

Author Member

(You should join our #development room on slack)

},
"transform-es2015-block-scoped-functions": {
"chrome": 41,
Expand All @@ -14,7 +15,8 @@
"transform-es2015-block-scoping": {
"chrome": 49,
"firefox": 51,
"safari": 10
"safari": 10,
"node": 6
},
"transform-es2015-classes": {
"chrome": 46,
Expand All @@ -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,
Expand All @@ -63,7 +69,8 @@
"transform-es2015-parameters": {
"chrome": 49,
"edge": 14,
"safari": 10
"safari": 10,
"node": 6
},
"transform-es2015-shorthand-properties": {
"chrome": 43,
Expand All @@ -83,7 +90,8 @@
"chrome": 49,
"edge": 13,
"firefox": 3,
"safari": 10
"safari": 10,
"node": 6
},
"transform-es2015-template-literals": {
"chrome": 41,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 18 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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 = [
Expand Down
63 changes: 62 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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", () => {
Expand Down

0 comments on commit b80dfaa

Please sign in to comment.