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

Commit c6fd48d

Browse files
Add forceAllTranforms option and deprecate Uglify target
1 parent 38f13fb commit c6fd48d

File tree

12 files changed

+206
-93
lines changed

12 files changed

+206
-93
lines changed

README.md

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,6 @@ A query to select browsers (ex: last 2 versions, > 5%) using [browserslist](http
120120

121121
Note, browsers' results are overridden by explicit items from `targets`.
122122

123-
### `targets.uglify`
124-
125-
`number | true`
126-
127-
UglifyJS does not currently support any ES6 syntax, so if you are using Uglify to minify your code, targeting later browsers may cause Uglify to throw syntax errors.
128-
129-
To prevent these errors - specify the uglify option, which will enable all plugins and, as a result, fully compile your code to ES5. However, the `useBuiltIns` option will still work as before, and only include the polyfills that your target(s) need.
130-
131-
> NOTE: Uglify has a work-in-progress "Harmony" branch to address the lack of ES6 support, but it is not yet stable. You can follow its progress in [UglifyJS2 issue #448](https://github.com/mishoo/UglifyJS2/issues/448). If you require an alternative minifier which _does_ support ES6 syntax, we recommend using [Babili](https://github.com/babel/babili).
132-
133123
### `spec`
134124

135125
`boolean`, defaults to `false`.
@@ -259,6 +249,47 @@ import "babel-polyfill/core-js/modules/es7.string.pad-end";
259249

260250
Don't add polyfills automatically per file, or transform `import "babel-polyfill"` to individual polyfills.
261251

252+
### `forceAllTransforms`
253+
254+
`boolean`, defaults to `false`.
255+
256+
<p><details>
257+
<summary><b>Example</b></summary>
258+
259+
With Babel 7's .babelrc.js support, you can force all transforms to be run if env is set to `production`.
260+
261+
```js
262+
module.exports = {
263+
presets: [
264+
["env", {
265+
targets: {
266+
chrome: 59,
267+
edge: 13,
268+
firefox: 50,
269+
},
270+
// for uglifyjs...
271+
forceAllTransforms: process.env === "production"
272+
}],
273+
],
274+
};
275+
```
276+
</details></p>
277+
278+
279+
> NOTE: `targets.uglify` is deprecated and will be removed in the next major in
280+
favor of this.
281+
282+
By default, this preset will run all the transforms needed for the targeted
283+
environment(s). Enable this option if you want to force running _all_
284+
transforms, which is useful if the output will be run through UglifyJS or an
285+
environment that only supports ES5.
286+
287+
> NOTE: Uglify has a work-in-progress "Harmony" branch to address the lack of
288+
ES6 support, but it is not yet stable. You can follow its progress in
289+
[UglifyJS2 issue #448](https://github.com/mishoo/UglifyJS2/issues/448). If you
290+
require an alternative minifier which _does_ support ES6 syntax, we recommend
291+
using [Babili](https://github.com/babel/babili).
292+
262293
---
263294

264295
## Examples

src/index.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,42 @@ const filterItems = (list, includes, excludes, targets, defaultItems) => {
107107
};
108108

109109
export default function buildPreset(context, opts = {}) {
110-
const validatedOptions = normalizeOptions(opts);
111-
const { debug, loose, moduleType, spec, useBuiltIns } = validatedOptions;
110+
const {
111+
debug,
112+
exclude: optionsExclude,
113+
forceAllTransforms,
114+
include: optionsInclude,
115+
loose,
116+
moduleType,
117+
spec,
118+
targets: optionsTargets,
119+
useBuiltIns,
120+
} = normalizeOptions(opts);
121+
122+
// TODO: remove this in next major
123+
let hasUglifyTarget = false;
124+
125+
if (optionsTargets && optionsTargets.uglify) {
126+
hasUglifyTarget = true;
127+
delete optionsTargets.uglify;
112128

113-
const targets = getTargets(validatedOptions.targets);
114-
const include = transformIncludesAndExcludes(validatedOptions.include);
115-
const exclude = transformIncludesAndExcludes(validatedOptions.exclude);
129+
console.log("");
130+
console.log("The uglify target has been deprecated. Set the top level");
131+
console.log("option `forceAllTransforms: true` instead.");
132+
console.log("");
133+
}
134+
135+
const targets = getTargets(optionsTargets);
136+
const include = transformIncludesAndExcludes(optionsInclude);
137+
const exclude = transformIncludesAndExcludes(optionsExclude);
138+
139+
const transformTargets = forceAllTransforms || hasUglifyTarget ? {} : targets;
116140

117141
const transformations = filterItems(
118142
pluginList,
119143
include.plugins,
120144
exclude.plugins,
121-
targets,
145+
transformTargets,
122146
);
123147

124148
let polyfills;

src/normalize-options.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ export const validateBoolOption = (name, value, defaultValue) => {
5757

5858
export const validateLooseOption = looseOpt =>
5959
validateBoolOption("loose", looseOpt, false);
60+
6061
export const validateSpecOption = specOpt =>
6162
validateBoolOption("spec", specOpt, false);
6263

64+
export const validateForceAllTransformsOption = forceAllTransforms =>
65+
validateBoolOption("forceAllTransforms", forceAllTransforms, false);
66+
6367
export const validateModulesOption = (modulesOpt = "commonjs") => {
6468
invariant(
6569
modulesOpt === false ||
@@ -97,6 +101,9 @@ export default function normalizeOptions(opts) {
97101
return {
98102
debug: opts.debug,
99103
exclude: validateIncludesAndExcludes(opts.exclude, "exclude"),
104+
forceAllTransforms: validateForceAllTransformsOption(
105+
opts.forceAllTransforms,
106+
),
100107
include: validateIncludesAndExcludes(opts.include, "include"),
101108
loose: validateLooseOption(opts.loose),
102109
moduleType: validateModulesOption(opts.modules),

src/targets-parser.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,27 @@ const semverMin = (first: ?string, second: string): string => {
2626
};
2727

2828
const getLowestVersions = browsers => {
29-
return browsers.reduce(
30-
(all, browser) => {
31-
const [browserName, browserVersion] = browser.split(" ");
32-
const normalizedBrowserName = browserNameMap[browserName];
29+
return browsers.reduce((all, browser) => {
30+
const [browserName, browserVersion] = browser.split(" ");
31+
const normalizedBrowserName = browserNameMap[browserName];
3332

34-
if (!normalizedBrowserName) {
35-
return all;
36-
}
33+
if (!normalizedBrowserName) {
34+
return all;
35+
}
3736

38-
try {
39-
// Browser version can return as "10.0-10.2"
40-
const splitVersion = browserVersion.split("-")[0];
41-
const parsedBrowserVersion = semverify(splitVersion);
37+
try {
38+
// Browser version can return as "10.0-10.2"
39+
const splitVersion = browserVersion.split("-")[0];
40+
const parsedBrowserVersion = semverify(splitVersion);
4241

43-
all[normalizedBrowserName] = semverMin(
44-
all[normalizedBrowserName],
45-
parsedBrowserVersion,
46-
);
47-
} catch (e) {}
42+
all[normalizedBrowserName] = semverMin(
43+
all[normalizedBrowserName],
44+
parsedBrowserVersion,
45+
);
46+
} catch (e) {}
4847

49-
return all;
50-
},
51-
{},
52-
);
48+
return all;
49+
}, {});
5350
};
5451

5552
const outputDecimalWarning = (decimalTargets: Array<Object>): void => {
@@ -60,7 +57,8 @@ const outputDecimalWarning = (decimalTargets: Array<Object>): void => {
6057
console.log("Warning, the following targets are using a decimal version:");
6158
console.log("");
6259
decimalTargets.forEach(({ target, value }) =>
63-
console.log(` ${target}: ${value}`));
60+
console.log(` ${target}: ${value}`),
61+
);
6462
console.log("");
6563
console.log(
6664
"We recommend using a string for minor/patch versions to avoid numbers like 6.10",
@@ -81,6 +79,7 @@ const targetParserMap = {
8179
return [target, parsed];
8280
},
8381

82+
// TODO: Remove in next version.
8483
// Only valid value for Uglify is `true`
8584
uglify: (target, value) => [target, value === true],
8685
};

test/debug-fixtures/builtins-uglify/stdout.txt

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1+
The uglify target has been deprecated. Set the top level
2+
option `forceAllTransforms: true` instead.
3+
14
babel-preset-env: `DEBUG` option
25

36
Using targets:
47
{
5-
"chrome": "55",
6-
"uglify": true
8+
"chrome": "55"
79
}
810

911
Modules transform: false
1012

1113
Using plugins:
12-
check-es2015-constants {"uglify":true}
13-
transform-es2015-arrow-functions {"uglify":true}
14-
transform-es2015-block-scoped-functions {"uglify":true}
15-
transform-es2015-block-scoping {"uglify":true}
16-
transform-es2015-classes {"uglify":true}
17-
transform-es2015-computed-properties {"uglify":true}
18-
transform-es2015-destructuring {"uglify":true}
19-
transform-es2015-duplicate-keys {"uglify":true}
20-
transform-es2015-for-of {"uglify":true}
21-
transform-es2015-function-name {"uglify":true}
22-
transform-es2015-literals {"uglify":true}
23-
transform-es2015-object-super {"uglify":true}
24-
transform-es2015-parameters {"uglify":true}
25-
transform-es2015-shorthand-properties {"uglify":true}
26-
transform-es2015-spread {"uglify":true}
27-
transform-es2015-sticky-regex {"uglify":true}
28-
transform-es2015-template-literals {"uglify":true}
29-
transform-es2015-typeof-symbol {"uglify":true}
30-
transform-es2015-unicode-regex {"uglify":true}
31-
transform-regenerator {"uglify":true}
32-
transform-exponentiation-operator {"uglify":true}
33-
transform-async-to-generator {"uglify":true}
34-
syntax-trailing-function-commas {"chrome":"55","uglify":true}
14+
check-es2015-constants {}
15+
transform-es2015-arrow-functions {}
16+
transform-es2015-block-scoped-functions {}
17+
transform-es2015-block-scoping {}
18+
transform-es2015-classes {}
19+
transform-es2015-computed-properties {}
20+
transform-es2015-destructuring {}
21+
transform-es2015-duplicate-keys {}
22+
transform-es2015-for-of {}
23+
transform-es2015-function-name {}
24+
transform-es2015-literals {}
25+
transform-es2015-object-super {}
26+
transform-es2015-parameters {}
27+
transform-es2015-shorthand-properties {}
28+
transform-es2015-spread {}
29+
transform-es2015-sticky-regex {}
30+
transform-es2015-template-literals {}
31+
transform-es2015-typeof-symbol {}
32+
transform-es2015-unicode-regex {}
33+
transform-regenerator {}
34+
transform-exponentiation-operator {}
35+
transform-async-to-generator {}
36+
syntax-trailing-function-commas {"chrome":"55"}
3537

3638
Polyfills
3739
=========
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
["../../lib", {
4+
"debug": true,
5+
"modules": false,
6+
"targets": {
7+
"chrome": 55
8+
},
9+
"useBuiltIns": "entry",
10+
"forceAllTransforms": true
11+
}]
12+
]
13+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
babel-preset-env: `DEBUG` option
2+
3+
Using targets:
4+
{
5+
"chrome": "55"
6+
}
7+
8+
Modules transform: false
9+
10+
Using plugins:
11+
check-es2015-constants {}
12+
transform-es2015-arrow-functions {}
13+
transform-es2015-block-scoped-functions {}
14+
transform-es2015-block-scoping {}
15+
transform-es2015-classes {}
16+
transform-es2015-computed-properties {}
17+
transform-es2015-destructuring {}
18+
transform-es2015-duplicate-keys {}
19+
transform-es2015-for-of {}
20+
transform-es2015-function-name {}
21+
transform-es2015-literals {}
22+
transform-es2015-object-super {}
23+
transform-es2015-parameters {}
24+
transform-es2015-shorthand-properties {}
25+
transform-es2015-spread {}
26+
transform-es2015-sticky-regex {}
27+
transform-es2015-template-literals {}
28+
transform-es2015-typeof-symbol {}
29+
transform-es2015-unicode-regex {}
30+
transform-regenerator {}
31+
transform-exponentiation-operator {}
32+
transform-async-to-generator {}
33+
syntax-trailing-function-commas {"chrome":"55"}
34+
35+
Polyfills
36+
=========
37+
38+
Replaced `babel-polyfill` with the following polyfills:
39+
es7.string.pad-start {"chrome":"55"}
40+
es7.string.pad-end {"chrome":"55"}
41+
web.timers {"chrome":"55"}
42+
web.immediate {"chrome":"55"}
43+
web.dom.iterable {"chrome":"55"}
44+
src/in.js -> lib/in.js

test/fixtures/preset-options/exclude-built-ins/expected.js

Whitespace-only changes.

0 commit comments

Comments
 (0)