Adds browsers property to use browserslist's queries (#19)
* Use browserslist to parse browsers from query. * Update README. * Use int values. * Allow `isPluginRequired` use browserslist queries. * Fix conflicts during different versions merging. * Add tests for browserslist queries. * Early return for getTargets. * Update README: Describe `browsers` option. * fix doc [skip ci] * Move to dependencies [skip ci] * Remove unused const. * Use doublequotes for strings.
This commit is contained in:
parent
4a3893a49e
commit
03f6cae25f
@ -42,21 +42,26 @@ The data for this is currently at: [/data/plugins.json](/data/plugins.json) and
|
|||||||
|
|
||||||
Currently: "chrome, edge, firefox, safari, node"
|
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`.
|
||||||
|
|
||||||
> If your config is a js file, also do `"node": parseFloat(process.versions.node)`
|
> If your config is a js file, also do `"node": parseFloat(process.versions.node)`
|
||||||
|
|
||||||
* `loose` (boolean) - Enable "loose" transformations for any plugins in this preset that allow them (Disabled by default).
|
* `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"`).
|
* `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"]`
|
* 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`
|
* `debug` (boolean) - `console.log` out the targets and plugins being used as well as the version specified in `/data/plugins.json`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
"presets": [
|
"presets": [
|
||||||
["env", {
|
["env", {
|
||||||
"targets": {
|
"targets": {
|
||||||
"chrome": 52
|
"chrome": 52,
|
||||||
|
"browsers": "last 2 safari versions"
|
||||||
},
|
},
|
||||||
"loose": true,
|
"loose": true,
|
||||||
"modules": false
|
"modules": false
|
||||||
@ -123,6 +128,24 @@ exports.A = A;
|
|||||||
export class 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`
|
### Example with `debug: true`
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -44,7 +44,8 @@
|
|||||||
"babel-plugin-transform-es2015-typeof-symbol": "^6.6.0",
|
"babel-plugin-transform-es2015-typeof-symbol": "^6.6.0",
|
||||||
"babel-plugin-transform-es2015-unicode-regex": "^6.3.13",
|
"babel-plugin-transform-es2015-unicode-regex": "^6.3.13",
|
||||||
"babel-plugin-transform-exponentiation-operator": "^6.8.0",
|
"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": {
|
"devDependencies": {
|
||||||
"babel-cli": "^6.11.4",
|
"babel-cli": "^6.11.4",
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
// "es5-property-mutators",
|
// "es5-property-mutators",
|
||||||
|
|
||||||
import pluginList from "../data/plugins.json";
|
import pluginList from "../data/plugins.json";
|
||||||
|
import browserslist from "browserslist";
|
||||||
|
|
||||||
export const plugins = [
|
export const plugins = [
|
||||||
"es3-member-expression-literals",
|
"es3-member-expression-literals",
|
||||||
@ -39,6 +40,10 @@ export const MODULE_TRANSFORMATIONS = {
|
|||||||
* @return {Boolean} Whether or not the transformation is required
|
* @return {Boolean} Whether or not the transformation is required
|
||||||
*/
|
*/
|
||||||
export const isPluginRequired = (supportedEnvironments, plugin) => {
|
export const isPluginRequired = (supportedEnvironments, plugin) => {
|
||||||
|
if (supportedEnvironments.browsers) {
|
||||||
|
supportedEnvironments = getTargets(supportedEnvironments);
|
||||||
|
}
|
||||||
|
|
||||||
const targetEnvironments = Object.keys(supportedEnvironments);
|
const targetEnvironments = Object.keys(supportedEnvironments);
|
||||||
|
|
||||||
if (targetEnvironments.length === 0) { return true; }
|
if (targetEnvironments.length === 0) { return true; }
|
||||||
@ -61,8 +66,34 @@ export const isPluginRequired = (supportedEnvironments, plugin) => {
|
|||||||
return isRequiredForEnvironments.length > 0 ? true : false;
|
return isRequiredForEnvironments.length > 0 ? true : false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isBrowsersQueryValid = browsers => {
|
||||||
|
return typeof browsers === "string" || Array.isArray(browsers);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getLowestVersions = (browsers) => {
|
||||||
|
return browsers.reduce((all, browser) => {
|
||||||
|
const [browserName, browserVersion] = browser.split(" ");
|
||||||
|
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 getTargets = targetOpts => {
|
||||||
return targetOpts || {};
|
const browserOpts = targetOpts.browsers;
|
||||||
|
if (isBrowsersQueryValid(browserOpts)) {
|
||||||
|
const queryBrowsers = getLowestVersions(browserslist(browserOpts));
|
||||||
|
return mergeBrowsers(queryBrowsers, targetOpts);
|
||||||
|
}
|
||||||
|
return targetOpts;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: Allow specifying plugins as either shortened or full name
|
// TODO: Allow specifying plugins as either shortened or full name
|
||||||
|
|||||||
@ -65,6 +65,38 @@ describe("babel-preset-env", () => {
|
|||||||
assert(babelPresetEnv.isPluginRequired(targets, plugin) === true);
|
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", () => {
|
it("doesn't throw when specifiying a decimal for node", () => {
|
||||||
let targets;
|
let targets;
|
||||||
const plugin = {
|
const plugin = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user