hardcode a current node version option (#35)

This commit is contained in:
Henry Zhu 2016-11-02 16:17:42 -04:00 committed by GitHub
parent 9e97b59a2f
commit d40c684723
3 changed files with 47 additions and 4 deletions

View File

@ -55,7 +55,7 @@ Currently: "chrome, edge, firefox, safari, ie, node".
> 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 you want to compile against the current node version, you can specify `"node": true` or `"node": "current"` which would be the same as `node": parseFloat(process.versions.node)`
* `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"`).
@ -64,7 +64,7 @@ Currently: "chrome, edge, firefox, safari, ie, node".
* `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)
### Example
### Examples
```js
// src
@ -141,6 +141,26 @@ export var A = function A() {
};
```
#### Example with `node: true` or `node: "current"`
```js
// process.versions.node -> 6.9.0
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
}
// ...
class A {}
exports.A = A;
```
### Example with `debug: true`
```js
@ -179,7 +199,6 @@ syntax-trailing-function-commas {}
transform-es2015-arrow-functions {}
```
## Caveats
### Using `babel-plugin-transform-object-rest-spread` and targeting node.js 6.5 or higher

View File

@ -64,7 +64,15 @@ const mergeBrowsers = (fromQuery, fromTarget) => {
}, fromQuery);
};
const getTargets = (targetOpts = {}) => {
export const getCurrentNodeVersion = () => {
return parseFloat(process.versions.node);
};
export const getTargets = (targetOpts = {}) => {
if (targetOpts.node === true || targetOpts.node === "current") {
targetOpts.node = getCurrentNodeVersion();
}
const browserOpts = targetOpts.browsers;
if (isBrowsersQueryValid(browserOpts)) {
const queryBrowsers = getLowestVersions(browserslist(browserOpts));

View File

@ -9,6 +9,22 @@ const {
} = babelPresetEnv;
describe("babel-preset-env", () => {
describe("getTargets", () => {
it("should return the current node version with option 'current'", function() {
assert.deepEqual(babelPresetEnv.getTargets({
node: true
}), {
node: parseFloat(process.versions.node)
});
assert.deepEqual(babelPresetEnv.getTargets({
node: "current"
}), {
node: parseFloat(process.versions.node)
});
});
});
describe("isPluginRequired", () => {
it("returns true if no targets are specified", () => {
const isRequired = babelPresetEnv.isPluginRequired({}, {});