Change default behavior to act the same as babel-preset-latest (#33)

* Do not throw on empty options, and default to latest preset

* fix lint
This commit is contained in:
Henry Zhu 2016-11-02 11:57:26 -04:00 committed by GitHub
parent aa61aabb82
commit 414acf5fda
15 changed files with 46 additions and 56 deletions

View File

@ -0,0 +1 @@
fixtures

View File

@ -18,6 +18,8 @@ This should be straightforward to do in most cases. There might be cases were pl
#### Support all plugins in Babel that are considered `latest`
> Default behavior without options is the same as `babel-preset-latest`.
[#14](https://github.com/babel/babel-preset-env/issues/14) - It won't include `stage-x` plugins. env will support all plugins in what we consider the latest version of Javascript (by matching what we do in [`babel-preset-latest`](http://babeljs.io/docs/plugins/preset-latest/)).
#### Determine the lowest common denominator of plugins to be included in the preset
@ -55,28 +57,13 @@ Currently: "chrome, edge, firefox, safari, ie, 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).
* `modules` - Enable transformation of ES6 module syntax to another module type (Enabled by default to `"commonjs"`).
* `loose` (boolean) - Enable "loose" transformations for any plugins in this preset that allow them (Defaults to `false`).
* `modules` - Enable transformation of ES6 module syntax to another module type (Defaults 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`.
* `debug` (boolean) - `console.log` out the targets and plugins being used as well as the version specified in `/data/plugins.json`. (Defaults to `false`)
* `whitelist` (Array<string>) - Enable a whitelist of plugins to always include. (Defaults to `[]`)
* Useful if there is a bug in a native implementation, or a combination of a non-supported feature + a supported one doesn't work. (Ex: Node 4 supports native classes but not spread)
```js
{
"presets": [
["env", {
"targets": {
"chrome": 52,
"browsers": "last 2 safari versions"
},
"loose": true,
"modules": false
}]
]
}
```
### Example
```js
@ -85,11 +72,9 @@ export class A {}
```
```js
// default is to run all transforms
// default is to run all transforms (acts as babel-preset-latest)
{
"presets": [
["env", {}]
]
"presets": ["env"]
}
// ...
@ -118,14 +103,15 @@ exports.A = A;
```
```js
// target chrome 52 with webpack 2/rollup
// target chrome 52 with webpack 2/rollup and loose mode
{
"presets": [
["env", {
"targets": {
"chrome": 52
},
"modules": false
"modules": false,
"loose": true
}]
]
}

View File

@ -14,9 +14,8 @@
"lint": "eslint scripts src test",
"fix": "eslint scripts src test --fix",
"ci": "npm run test",
"prepublish": "npm run build",
"changelog": "git log `git describe --tags --abbrev=0`..HEAD --pretty=format:' * %s (%an)' | grep -v 'Merge pull request'",
"test": "mocha ./test --compilers js:babel-register"
"test": "npm run build && mocha ./test --compilers js:babel-register"
},
"dependencies": {
"babel-plugin-check-es2015-constants": "^6.3.13",
@ -85,6 +84,9 @@
},
"rules": {
"max-len": 0
},
"env": {
"mocha": true
}
}
}

View File

@ -26,7 +26,7 @@ export const isPluginRequired = (supportedEnvironments, plugin) => {
if (targetEnvironments.length === 0) { return true; }
const isRequiredForEnvironments = targetEnvironments
.filter(environment => {
.filter((environment) => {
// Feature is not implemented in that environment
if (!plugin[environment]) { return true; }
@ -43,7 +43,7 @@ export const isPluginRequired = (supportedEnvironments, plugin) => {
return isRequiredForEnvironments.length > 0 ? true : false;
};
const isBrowsersQueryValid = browsers => {
const isBrowsersQueryValid = (browsers) => {
return typeof browsers === "string" || Array.isArray(browsers);
};
@ -64,7 +64,7 @@ const mergeBrowsers = (fromQuery, fromTarget) => {
}, fromQuery);
};
const getTargets = targetOpts => {
const getTargets = (targetOpts = {}) => {
const browserOpts = targetOpts.browsers;
if (isBrowsersQueryValid(browserOpts)) {
const queryBrowsers = getLowestVersions(browserslist(browserOpts));
@ -113,23 +113,7 @@ export const validateWhitelistOption = (whitelistOpt = []) => {
return whitelistOpt;
};
export default function buildPreset(context, opts) {
if (!opts.targets) {
throw new Error(
`
babel-preset-env requires a "targets" option:
{
"presets": [
["env", {
"targets": {
"chrome": 50
}
}]
]
}
`);
}
export default function buildPreset(context, opts = {}) {
const loose = validateLooseOption(opts.loose);
const moduleType = validateModulesOption(opts.modules);
const whitelist = validateWhitelistOption(opts.whitelist);
@ -137,7 +121,7 @@ babel-preset-env requires a "targets" option:
const debug = opts.debug;
let transformations = Object.keys(pluginList)
.filter(pluginName => isPluginRequired(targets, pluginList[pluginName]));
.filter((pluginName) => isPluginRequired(targets, pluginList[pluginName]));
if (debug) {
console.log("");
@ -146,7 +130,7 @@ babel-preset-env requires a "targets" option:
console.log("Using plugins:");
console.log("");
console.log(`module: ${moduleType}`);
transformations.forEach(transform => {
transformations.forEach((transform) => {
let envList = pluginList[transform];
let filteredList = Object.keys(targets)
.reduce((a, b) => {
@ -157,7 +141,7 @@ babel-preset-env requires a "targets" option:
});
}
transformations = [...transformations, ...whitelist].map(pluginName => {
transformations = [...transformations, ...whitelist].map((pluginName) => {
return [require(`babel-plugin-${pluginName}`), { loose }];
});

View File

@ -0,0 +1 @@
const a = "1";

View File

@ -0,0 +1,3 @@
"use strict";
var a = "1";

View File

@ -0,0 +1,5 @@
{
"presets": [
["../../../../lib", {}]
]
}

View File

@ -1 +1 @@
import a from 'a';
import a from "a";

View File

@ -1 +1 @@
import a from 'a';
import a from "a";

View File

@ -1,7 +1,6 @@
{
"presets": [
["../../../../lib", {
"targets": {},
"modules": false
}]
]

View File

@ -0,0 +1 @@
const a = "1";

View File

@ -0,0 +1,3 @@
"use strict";
var a = "1";

View File

@ -0,0 +1,5 @@
{
"presets": [
"../../../../lib"
]
}

View File

@ -1 +1 @@
import a from 'a';
import a from "a";

View File

@ -1,7 +1,7 @@
'use strict';
"use strict";
var _a = require('a');
var _a = require("a");
var _a2 = _interopRequireDefault(_a);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }