From 73d242a26a60d9e1460ce0ac994c26f18b8c57b3 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 7 Oct 2016 15:50:55 +0300 Subject: [PATCH 01/12] Use browserslist to parse browsers from query. --- package.json | 1 + src/index.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3f4a4888..aae2ca84 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "babel-plugin-transform-flow-strip-types": "^6.8.0", "babel-preset-es2015": "^6.14.0", "babel-register": "^6.14.0", + "browserslist": "^1.4.0", "compat-table": "github:hzoo/compat-table#node-fix", "eslint": "^3.3.1", "eslint-config-babel": "^1.0.1", diff --git a/src/index.js b/src/index.js index 3a5eaa4f..a4c2094a 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ // "es5-property-mutators", import pluginList from "../data/plugins.json"; +import browserslist from "browserslist"; export const plugins = [ "es3-member-expression-literals", @@ -62,7 +63,16 @@ export const isPluginRequired = (supportedEnvironments, plugin) => { }; const getTargets = targetOpts => { - return targetOpts || {}; + const mergedOpts = targetOpts || {}; + const browserOpts = targetOpts['browsers']; + if (typeof browserOpts === 'string' || Array.isArray(browserOpts)) { + delete mergedOpts.browsers; + browserslist(browserOpts).forEach(browser => { + const [browserName, browserVer] = browser.split(' '); + if (!mergedOpts[browserName]) mergedOpts[browserName] = browserVer; + }); + } + return mergedOpts; }; // TODO: Allow specifying plugins as either shortened or full name From 5c659f988a71cc79a4a0d5179aedcb3017ecf307 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 7 Oct 2016 16:03:39 +0300 Subject: [PATCH 02/12] Update README. --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 45b6e337..3c1484ff 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,24 @@ exports.A = A; export class A {} ``` +```js +// using browserslist +{ + "presets": [ + ["env", { + "targets": { + "chrome": 52, + "browsers": ["last 2 versions", "safari 7"] + } + }] + ] +} + +// ... + +export class A {} +``` + ### Example with `debug: true` ```js From 4f80969d5f10c96cb0bbd8b943f404f27acaace0 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 7 Oct 2016 17:53:59 +0300 Subject: [PATCH 03/12] Use int values. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index a4c2094a..7fa417e3 100644 --- a/src/index.js +++ b/src/index.js @@ -69,7 +69,7 @@ const getTargets = targetOpts => { delete mergedOpts.browsers; browserslist(browserOpts).forEach(browser => { const [browserName, browserVer] = browser.split(' '); - if (!mergedOpts[browserName]) mergedOpts[browserName] = browserVer; + if (!mergedOpts[browserName]) mergedOpts[browserName] = parseInt(browserVer); }); } return mergedOpts; From dd908e060d1e18b886b756e7ea0dadc22a384595 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Fri, 7 Oct 2016 17:53:59 +0300 Subject: [PATCH 04/12] Allow `isPluginRequired` use browserslist queries. --- src/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7fa417e3..7590772d 100644 --- a/src/index.js +++ b/src/index.js @@ -40,6 +40,10 @@ export const MODULE_TRANSFORMATIONS = { * @return {Boolean} Whether or not the transformation is required */ export const isPluginRequired = (supportedEnvironments, plugin) => { + if (supportedEnvironments.browsers) { + supportedEnvironments = getTargets(supportedEnvironments); + } + const targetEnvironments = Object.keys(supportedEnvironments); if (targetEnvironments.length === 0) { return true; } @@ -62,10 +66,14 @@ export const isPluginRequired = (supportedEnvironments, plugin) => { return isRequiredForEnvironments.length > 0 ? true : false; }; +const isBrowsersQueryValid = browsers => { + return typeof browsers === 'string' || Array.isArray(browsers); +}; + const getTargets = targetOpts => { const mergedOpts = targetOpts || {}; const browserOpts = targetOpts['browsers']; - if (typeof browserOpts === 'string' || Array.isArray(browserOpts)) { + if (isBrowsersQueryValid(browserOpts)) { delete mergedOpts.browsers; browserslist(browserOpts).forEach(browser => { const [browserName, browserVer] = browser.split(' '); From b21e564c7f7bcb7009a6753caa17ca01fff7e847 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Sat, 8 Oct 2016 02:49:27 +0300 Subject: [PATCH 05/12] Fix conflicts during different versions merging. --- src/index.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 7590772d..820a867d 100644 --- a/src/index.js +++ b/src/index.js @@ -70,15 +70,32 @@ const isBrowsersQueryValid = browsers => { return typeof browsers === 'string' || Array.isArray(browsers); }; +const getLowestVersions = (browsers) => { + return browsers.reduce((all, browser) => { + const [browserName, browserVersion] = browser.split(' '); + const previousBrowserVersion = all[browserName]; + all[browserName] = parseInt(browserVersion); + return all; + }, {}); +}; + +const mergeBrowsers = (fromQuery, fromTarget) => { + return Object.keys(fromTarget).reduce((queryObj, targKey) => { + if (targKey !== 'browsers') { + queryObj[targKey] = fromTarget[targKey]; + } + return queryObj; + }, fromQuery); +}; + const getTargets = targetOpts => { - const mergedOpts = targetOpts || {}; - const browserOpts = targetOpts['browsers']; + let mergedOpts; + const browserOpts = targetOpts.browsers; if (isBrowsersQueryValid(browserOpts)) { - delete mergedOpts.browsers; - browserslist(browserOpts).forEach(browser => { - const [browserName, browserVer] = browser.split(' '); - if (!mergedOpts[browserName]) mergedOpts[browserName] = parseInt(browserVer); - }); + const queryBrowsers = getLowestVersions(browserslist(browserOpts)); + mergedOpts = mergeBrowsers(queryBrowsers, targetOpts); + } else { + mergedOpts = targetOpts; } return mergedOpts; }; From 1152f623f16bd60e5302cee0bae47e2889d987b8 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Sat, 8 Oct 2016 03:12:53 +0300 Subject: [PATCH 06/12] Add tests for browserslist queries. --- test/index.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/index.js b/test/index.js index 0a4c038c..a29c2187 100644 --- a/test/index.js +++ b/test/index.js @@ -65,6 +65,38 @@ describe("babel-preset-env", () => { assert(babelPresetEnv.isPluginRequired(targets, plugin) === true); }); + it("returns false if plugin feature is implemented by lower than target defined in browsers query", () => { + const plugin = { + chrome: 49, + }; + const targets = { + "browsers": "chrome > 50" + }; + assert(babelPresetEnv.isPluginRequired(targets, plugin) === false); + }); + + it("returns true if plugin feature is implemented is greater than target defined in browsers query", () => { + const plugin = { + chrome: 52, + }; + const targets = { + "browsers": "chrome > 50" + }; + assert(babelPresetEnv.isPluginRequired(targets, plugin) === true); + }); + + it("returns true if target's root items overrides versions defined in browsers query", () => { + const plugin = { + chrome: 45, + }; + const targets = { + browsers: "last 2 Chrome versions", + chrome: 44 + }; + + assert(babelPresetEnv.isPluginRequired(targets, plugin) === true); + }); + it("doesn't throw when specifiying a decimal for node", () => { let targets; const plugin = { From 4a6fbef69f07aabab3220b4a461b0582d0f1635d Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Sat, 8 Oct 2016 23:30:39 +0300 Subject: [PATCH 07/12] Early return for getTargets. --- src/index.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 820a867d..243c2e6b 100644 --- a/src/index.js +++ b/src/index.js @@ -89,15 +89,12 @@ const mergeBrowsers = (fromQuery, fromTarget) => { }; const getTargets = targetOpts => { - let mergedOpts; const browserOpts = targetOpts.browsers; if (isBrowsersQueryValid(browserOpts)) { const queryBrowsers = getLowestVersions(browserslist(browserOpts)); - mergedOpts = mergeBrowsers(queryBrowsers, targetOpts); - } else { - mergedOpts = targetOpts; + return mergeBrowsers(queryBrowsers, targetOpts); } - return mergedOpts; + return targetOpts; }; // TODO: Allow specifying plugins as either shortened or full name From 0bff66762ef2fc96c8767f87be92fd573e228186 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Sat, 8 Oct 2016 23:58:06 +0300 Subject: [PATCH 08/12] Update README: Describe `browsers` option. --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3c1484ff..ef997f50 100644 --- a/README.md +++ b/README.md @@ -20,19 +20,23 @@ The data for this is currently at: [/data/plugins.json](/data/plugins.json) and Currently: "chrome, edge, firefox, safari, node" -> Some node features are > `6.5` +> Some node features are > `6.5`. + +*browsers* (array/string) - an query to select browsers (ex: last 2 versions, > 5%). +Note, browsers' results are overridden by explicit items from `targets`. * `loose` (boolean) - Enable "loose" transformations for any plugins in this preset that allow them (Disabled by default). * `modules` - Enable transformation of ES6 module syntax to another module type (Enabled by default to `"commonjs"`). - * Can be `false` to not transform modules, or one of `["amd", "umd", "systemjs", "commonjs"]` -* `debug` (boolean) - `console.log` out the targets and plugins being used as well as the version specified in `/data/plugins.json` + * Can be `false` to not transform modules, or one of `["amd", "umd", "systemjs", "commonjs"]`. +* `debug` (boolean) - `console.log` out the targets and plugins being used as well as the version specified in `/data/plugins.json`. ```js { "presets": [ ["env", { "targets": { - "chrome": 52 + "chrome": 52, + "browsers": "last 2 safari versions" }, "loose": true, "modules": false From c22ba0e59d98e1e96b91070a8e80554806bba0e8 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Wed, 12 Oct 2016 18:16:12 -0400 Subject: [PATCH 09/12] fix doc [skip ci] --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef997f50..225f7901 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,9 @@ Currently: "chrome, edge, firefox, safari, node" > Some node features are > `6.5`. -*browsers* (array/string) - an query to select browsers (ex: last 2 versions, > 5%). -Note, browsers' results are overridden by explicit items from `targets`. +* `browsers` (array/string) - an query to select browsers (ex: last 2 versions, > 5%). + +> Note, browsers' results are overridden by explicit items from `targets`. * `loose` (boolean) - Enable "loose" transformations for any plugins in this preset that allow them (Disabled by default). * `modules` - Enable transformation of ES6 module syntax to another module type (Enabled by default to `"commonjs"`). From fc0934614ff55281797113df007e88fcf54438cf Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Wed, 12 Oct 2016 18:17:09 -0400 Subject: [PATCH 10/12] Move to dependencies [skip ci] --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index aae2ca84..de3bd292 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "babel-plugin-transform-es2015-typeof-symbol": "^6.6.0", "babel-plugin-transform-es2015-unicode-regex": "^6.3.13", "babel-plugin-transform-exponentiation-operator": "^6.8.0", - "babel-plugin-transform-regenerator": "^6.6.0" + "babel-plugin-transform-regenerator": "^6.6.0", + "browserslist": "^1.4.0" }, "devDependencies": { "babel-cli": "^6.11.4", @@ -52,7 +53,6 @@ "babel-plugin-transform-flow-strip-types": "^6.8.0", "babel-preset-es2015": "^6.14.0", "babel-register": "^6.14.0", - "browserslist": "^1.4.0", "compat-table": "github:hzoo/compat-table#node-fix", "eslint": "^3.3.1", "eslint-config-babel": "^1.0.1", From 56f2209742b5a4ca97f2bccbbafdc1ed2320e0c8 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Thu, 13 Oct 2016 04:02:03 +0300 Subject: [PATCH 11/12] Remove unused const. --- src/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.js b/src/index.js index 243c2e6b..6f41e270 100644 --- a/src/index.js +++ b/src/index.js @@ -73,7 +73,6 @@ const isBrowsersQueryValid = browsers => { const getLowestVersions = (browsers) => { return browsers.reduce((all, browser) => { const [browserName, browserVersion] = browser.split(' '); - const previousBrowserVersion = all[browserName]; all[browserName] = parseInt(browserVersion); return all; }, {}); From 99a63ef026c457ffe46f4fe0103c8cd9bbab2a16 Mon Sep 17 00:00:00 2001 From: Artem Yavorsky Date: Thu, 13 Oct 2016 04:04:42 +0300 Subject: [PATCH 12/12] Use doublequotes for strings. --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 6f41e270..4d624645 100644 --- a/src/index.js +++ b/src/index.js @@ -67,12 +67,12 @@ export const isPluginRequired = (supportedEnvironments, plugin) => { }; const isBrowsersQueryValid = browsers => { - return typeof browsers === 'string' || Array.isArray(browsers); + return typeof browsers === "string" || Array.isArray(browsers); }; const getLowestVersions = (browsers) => { return browsers.reduce((all, browser) => { - const [browserName, browserVersion] = browser.split(' '); + const [browserName, browserVersion] = browser.split(" "); all[browserName] = parseInt(browserVersion); return all; }, {}); @@ -80,7 +80,7 @@ const getLowestVersions = (browsers) => { const mergeBrowsers = (fromQuery, fromTarget) => { return Object.keys(fromTarget).reduce((queryObj, targKey) => { - if (targKey !== 'browsers') { + if (targKey !== "browsers") { queryObj[targKey] = fromTarget[targKey]; } return queryObj;