Skip to content

Commit c64fd70

Browse files
authored
Merge pull request webpack#6123 from webpack/feature/minimize-production
Switch on minimizing in production mode by default
2 parents d6e69dc + 2148f55 commit c64fd70

File tree

33 files changed

+161
-117
lines changed

33 files changed

+161
-117
lines changed

lib/OptionsDefaulter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const getProperty = (obj, name) => {
88
name = name.split(".");
99
for(let i = 0; i < name.length - 1; i++) {
1010
obj = obj[name[i]];
11-
if(typeof obj !== "object" || !obj) return;
11+
if(typeof obj !== "object" || !obj || Array.isArray(obj)) return;
1212
}
1313
return obj[name.pop()];
1414
};
@@ -17,6 +17,7 @@ const setProperty = (obj, name, value) => {
1717
name = name.split(".");
1818
for(let i = 0; i < name.length - 1; i++) {
1919
if(typeof obj[name[i]] !== "object" && typeof obj[name[i]] !== "undefined") return;
20+
if(Array.isArray(obj[name[i]])) return;
2021
if(!obj[name[i]]) obj[name[i]] = {};
2122
obj = obj[name[i]];
2223
}

lib/WebpackOptionsApply.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ class WebpackOptionsApply extends OptionsApply {
314314
"process.env.NODE_ENV": JSON.stringify(options.optimization.nodeEnv)
315315
}));
316316
}
317+
if(options.optimization.minimize) {
318+
for(const minimizer of options.optimization.minimizer) {
319+
compiler.apply(minimizer);
320+
}
321+
}
317322

318323
if(options.performance) {
319324
compiler.apply(new SizeLimitsPlugin(options.performance));

lib/WebpackOptionsDefaulter.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
138138
this.set("optimization.namedModules", "make", options => options.mode === "development");
139139
this.set("optimization.namedChunks", "make", options => options.mode === "development");
140140
this.set("optimization.portableRecords", "make", options => !!(options.recordsInputPath || options.recordsOutputPath || options.recordsPath));
141+
this.set("optimization.minimize", "make", options => options.mode === "production");
142+
this.set("optimization.minimizer", "make", options => [{
143+
apply: compiler => {
144+
// Lazy load the uglifyjs plugin
145+
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
146+
new UglifyJsPlugin({
147+
sourceMap: options.devtool && /source-?map/.test(options.devtool)
148+
}).apply(compiler);
149+
}
150+
}]);
141151
this.set("optimization.nodeEnv", "make", options => options.mode);
142152

143153
this.set("resolve", "call", value => Object.assign({}, value));

lib/optimize/UglifyJsPlugin.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/webpack.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ exportPlugins(exports.optimize = {}, {
112112
"MinChunkSizePlugin": () => require("./optimize/MinChunkSizePlugin"),
113113
"ModuleConcatenationPlugin": () => require("./optimize/ModuleConcatenationPlugin"),
114114
"SideEffectsFlagPlugin": () => require("./optimize/SideEffectsFlagPlugin"),
115-
"OccurrenceOrderPlugin": () => require("./optimize/OccurrenceOrderPlugin"),
116-
"UglifyJsPlugin": () => require("./optimize/UglifyJsPlugin")
115+
"OccurrenceOrderPlugin": () => require("./optimize/OccurrenceOrderPlugin")
117116
});
118117
exportPlugins(exports.web = {}, {
119118
"JsonpTemplatePlugin": () => require("./web/JsonpTemplatePlugin"),

schemas/WebpackOptions.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,25 @@
12791279
"description": "Generate records with relative paths to be able to move the context folder",
12801280
"type": "boolean"
12811281
},
1282+
"minimize": {
1283+
"description": "Enable minimizing the output. Uses optimization.minimizer.",
1284+
"type": "boolean"
1285+
},
1286+
"minimizer": {
1287+
"description": "Minimizer(s) to use for minimizing the output",
1288+
"type": "array",
1289+
"items": {
1290+
"description": "Plugin of type object or instanceof Function",
1291+
"anyOf": [
1292+
{
1293+
"$ref": "#/definitions/common.pluginObject"
1294+
},
1295+
{
1296+
"$ref": "#/definitions/common.pluginFunction"
1297+
}
1298+
]
1299+
}
1300+
},
12821301
"nodeEnv": {
12831302
"description": "Set process.env.NODE_ENV to a specific value",
12841303
"anyOf": [

test/Compiler.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ describe("Compiler", () => {
1818
options.context = path.join(__dirname, "fixtures");
1919
if(noOutputPath) options.output.path = "/";
2020
options.output.pathinfo = true;
21+
options.optimization = {
22+
minimize: false
23+
};
2124
const logs = {
2225
mkdirp: [],
2326
writeFile: [],

test/Integration.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ describe("Integration", function() {
4949
amd: {
5050
fromOptions: true
5151
},
52+
optimization: {
53+
minimize: false
54+
},
5255
resolve: {
5356
// cannot resolve should outside the outermost node_modules
5457
// so it is injected here

test/NodeTemplatePlugin.test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ describe("NodeTemplatePlugin", () => {
2020
library: "abc",
2121
libraryTarget: "commonjs",
2222
},
23-
entry: "./entry",
24-
plugins: [
25-
new webpack.optimize.UglifyJsPlugin()
26-
]
23+
entry: "./entry"
2724
}, (err, stats) => {
2825
if(err) return err;
2926
stats.hasErrors().should.be.not.ok();
@@ -61,8 +58,7 @@ describe("NodeTemplatePlugin", () => {
6158
plugins: [
6259
new webpack.optimize.LimitChunkCountPlugin({
6360
maxChunks: 1
64-
}),
65-
new webpack.optimize.UglifyJsPlugin()
61+
})
6662
]
6763
}, (err, stats) => {
6864
if(err) return err;

test/StatsTestCases.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ describe("StatsTestCases", () => {
3434
if(!options.output) options.output = options.output || {};
3535
if(!options.output.path) options.output.path = path.join(outputBase, testName);
3636
if(!options.plugins) options.plugins = [];
37+
if(!options.optimization) options.optimization = {};
38+
if(options.optimization.minimize === undefined) options.optimization.minimize = false;
3739
// To support deprecated loaders
3840
// TODO remove in webpack 5
3941
options.plugins.push(new webpack.LoaderOptionsPlugin({

0 commit comments

Comments
 (0)