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

Commit a8011a4

Browse files
Add useSyntax option and deprecate Uglify target
1 parent 76272c8 commit a8011a4

File tree

17 files changed

+222
-96
lines changed

17 files changed

+222
-96
lines changed

README.md

Lines changed: 10 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
### `loose`
134124

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

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

242+
### `useSyntax`
243+
244+
`boolean`, defaults to `true`.
245+
246+
NOTE: `targets.uglify` is deprecated and will be removed in the next major in favor of this.
247+
248+
Runs the transforms needed for the targeted environment(s). Set this to `false` if you want to force running _all_ transforms which is useful if the output will be run through UglifyJS, or an environment that only supports ES5.
249+
250+
> 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).
251+
252252
---
253253

254254
## Examples

src/index.js

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,12 @@ let hasBeenLogged = false;
5656

5757
const logPlugin = (plugin, targets, list) => {
5858
const envList = list[plugin] || {};
59-
const filteredList = Object.keys(targets).reduce(
60-
(a, b) => {
61-
if (!envList[b] || semver.lt(targets[b], semverify(envList[b]))) {
62-
a[b] = prettifyVersion(targets[b]);
63-
}
64-
return a;
65-
},
66-
{},
67-
);
59+
const filteredList = Object.keys(targets).reduce((a, b) => {
60+
if (!envList[b] || semver.lt(targets[b], semverify(envList[b]))) {
61+
a[b] = prettifyVersion(targets[b]);
62+
}
63+
return a;
64+
}, {});
6865
const logStr = ` ${plugin} ${JSON.stringify(filteredList)}`;
6966
console.log(logStr);
7067
};
@@ -121,18 +118,41 @@ const filterItems = (list, includes, excludes, targets, defaultItems) => {
121118
};
122119

123120
export default function buildPreset(context, opts = {}) {
124-
const validatedOptions = normalizeOptions(opts);
125-
const { debug, loose, moduleType, useBuiltIns } = validatedOptions;
121+
const {
122+
debug,
123+
exclude: optionsExclude,
124+
include: optionsInclude,
125+
loose,
126+
moduleType,
127+
targets: optionsTargets,
128+
useBuiltIns,
129+
useSyntax,
130+
} = normalizeOptions(opts);
131+
132+
// TODO: remove this in next major
133+
let hasUglifyTarget = false;
134+
135+
if (optionsTargets && optionsTargets.uglify) {
136+
hasUglifyTarget = true;
137+
delete optionsTargets.uglify;
138+
139+
console.log("");
140+
console.log("The uglify target has been deprecated. Set the top level");
141+
console.log("option `useSyntax: false` instead.");
142+
console.log("");
143+
}
144+
145+
const targets = getTargets(optionsTargets);
146+
const include = transformIncludesAndExcludes(optionsInclude);
147+
const exclude = transformIncludesAndExcludes(optionsExclude);
126148

127-
const targets = getTargets(validatedOptions.targets);
128-
const include = transformIncludesAndExcludes(validatedOptions.include);
129-
const exclude = transformIncludesAndExcludes(validatedOptions.exclude);
149+
const transformTargets = !useSyntax || hasUglifyTarget ? {} : targets;
130150

131151
const transformations = filterItems(
132152
pluginList,
133153
include.plugins,
134154
exclude.plugins,
135-
targets,
155+
transformTargets,
136156
);
137157

138158
let polyfills;
@@ -156,10 +176,26 @@ export default function buildPreset(context, opts = {}) {
156176
console.log("\nUsing targets:");
157177
console.log(JSON.stringify(prettifyTargets(targets), null, 2));
158178
console.log(`\nModules transform: ${moduleType}`);
159-
console.log("\nUsing plugins:");
160-
transformations.forEach(transform => {
161-
logPlugin(transform, targets, pluginList);
162-
});
179+
console.log("");
180+
console.log("Plugins");
181+
console.log("=========");
182+
console.log("");
183+
184+
if (!transformations.size) {
185+
console.log("Based on your targets none were added.");
186+
} else {
187+
if (!useSyntax) {
188+
console.log("Added all plugins (useSyntax: false):");
189+
} else if (hasUglifyTarget) {
190+
console.log("Added all plugins (target: uglify):");
191+
} else {
192+
console.log("Added the following plugins based on your targets:");
193+
}
194+
195+
transformations.forEach(transform => {
196+
logPlugin(transform, transformTargets, pluginList);
197+
});
198+
}
163199
}
164200

165201
const plugins = [];
@@ -172,7 +208,8 @@ export default function buildPreset(context, opts = {}) {
172208
}
173209

174210
transformations.forEach(pluginName =>
175-
plugins.push([require(`babel-plugin-${pluginName}`), { loose }]));
211+
plugins.push([require(`babel-plugin-${pluginName}`), { loose }]),
212+
);
176213

177214
const regenerator = transformations.has("transform-regenerator");
178215

src/normalize-options.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,24 @@ export const checkDuplicateIncludeExcludes = (include = [], exclude = []) => {
4343
);
4444
};
4545

46-
// TODO: Allow specifying plugins as either shortened or full name
47-
// babel-plugin-transform-es2015-classes
48-
// transform-es2015-classes
49-
export const validateLooseOption = (looseOpt = false) => {
50-
invariant(
51-
typeof looseOpt === "boolean",
52-
"Invalid Option: The 'loose' option must be a boolean.",
53-
);
46+
export const validateBoolOption = (name, value, defaultValue) => {
47+
if (typeof value === "undefined") {
48+
value = defaultValue;
49+
}
5450

55-
return looseOpt;
51+
if (typeof value !== "boolean") {
52+
throw new Error(`Preset env: '${name}' option must be a boolean.`);
53+
}
54+
55+
return value;
5656
};
5757

58+
export const validateLooseOption = looseOpt =>
59+
validateBoolOption("loose", looseOpt, false);
60+
61+
export const validateUseSyntaxOption = useSyntax =>
62+
validateBoolOption("useSyntax", useSyntax, true);
63+
5864
export const validateModulesOption = (modulesOpt = "commonjs") => {
5965
invariant(
6066
modulesOpt === false ||
@@ -97,5 +103,6 @@ export default function normalizeOptions(opts) {
97103
moduleType: validateModulesOption(opts.modules),
98104
targets: opts.targets,
99105
useBuiltIns: validateUseBuiltInsOption(opts.useBuiltIns),
106+
useSyntax: validateUseSyntaxOption(opts.useSyntax),
100107
};
101108
}

src/targets-parser.js

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

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

33-
if (!normalizedBrowserName) {
34-
return all;
35-
}
32+
if (!normalizedBrowserName) {
33+
return all;
34+
}
3635

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

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

48-
return all;
49-
},
50-
{},
51-
);
47+
return all;
48+
}, {});
5249
};
5350

5451
const outputDecimalWarning = (decimalTargets: Array<Object>): void => {
@@ -59,7 +56,8 @@ const outputDecimalWarning = (decimalTargets: Array<Object>): void => {
5956
console.log("Warning, the following targets are using a decimal version:");
6057
console.log("");
6158
decimalTargets.forEach(({ target, value }) =>
62-
console.log(` ${target}: ${value}`));
59+
console.log(` ${target}: ${value}`),
60+
);
6361
console.log("");
6462
console.log(
6563
"We recommend using a string for minor/patch versions to avoid numbers like 6.10",
@@ -80,6 +78,7 @@ const targetParserMap = {
8078
return [target, parsed];
8179
},
8280

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

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1+
The uglify target has been deprecated. Set the top level
2+
option `useSyntax: false` 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

11-
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}
13+
Plugins
14+
=========
15+
16+
Added all plugins (target: uglify):
17+
check-es2015-constants {}
18+
transform-es2015-arrow-functions {}
19+
transform-es2015-block-scoped-functions {}
20+
transform-es2015-block-scoping {}
21+
transform-es2015-classes {}
22+
transform-es2015-computed-properties {}
23+
transform-es2015-destructuring {}
24+
transform-es2015-duplicate-keys {}
25+
transform-es2015-for-of {}
26+
transform-es2015-function-name {}
27+
transform-es2015-literals {}
28+
transform-es2015-object-super {}
29+
transform-es2015-parameters {}
30+
transform-es2015-shorthand-properties {}
31+
transform-es2015-spread {}
32+
transform-es2015-sticky-regex {}
33+
transform-es2015-template-literals {}
34+
transform-es2015-typeof-symbol {}
35+
transform-es2015-unicode-regex {}
36+
transform-regenerator {}
37+
transform-exponentiation-operator {}
38+
transform-async-to-generator {}
39+
syntax-trailing-function-commas {}
3540

3641
Polyfills
3742
=========

test/debug-fixtures/builtins/stdout.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ Using targets:
99

1010
Modules transform: commonjs
1111

12-
Using plugins:
12+
Plugins
13+
=========
14+
15+
Added the following plugins based on your targets:
1316
check-es2015-constants {"ie":"10"}
1417
transform-es2015-arrow-functions {"ie":"10"}
1518
transform-es2015-block-scoped-functions {"ie":"10"}

test/debug-fixtures/electron/stdout.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Using targets:
1414

1515
Modules transform: commonjs
1616

17-
Using plugins:
17+
Plugins
18+
=========
19+
20+
Added the following plugins based on your targets:
1821
check-es2015-constants {"electron":"0.36"}
1922
transform-es2015-block-scoping {"electron":"0.36"}
2023
transform-es2015-destructuring {"electron":"0.36"}

test/debug-fixtures/plugins-only/stdout.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ Using targets:
1515

1616
Modules transform: commonjs
1717

18-
Using plugins:
18+
Plugins
19+
=========
20+
21+
Added the following plugins based on your targets:
1922
transform-es2015-destructuring {"firefox":"52"}
2023
transform-es2015-for-of {"firefox":"52"}
2124
transform-es2015-function-name {"firefox":"52"}

test/debug-fixtures/specific-targets/stdout.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ Using targets:
1212

1313
Modules transform: commonjs
1414

15-
Using plugins:
15+
Plugins
16+
=========
17+
18+
Added the following plugins based on your targets:
1619
check-es2015-constants {"edge":"13","firefox":"49","ie":"10","ios":"9","safari":"7"}
1720
transform-es2015-arrow-functions {"ie":"10","ios":"9","safari":"7"}
1821
transform-es2015-block-scoped-functions {"ie":"10","ios":"9","safari":"7"}
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+
"useSyntax": false
11+
}]
12+
]
13+
}

0 commit comments

Comments
 (0)