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({

test/TestCases.test.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("TestCases", () => {
4343
name: "normal",
4444
}, {
4545
name: "production",
46-
mode: "production"
46+
mode: "production",
4747
}, {
4848
name: "development",
4949
mode: "development",
@@ -90,29 +90,16 @@ describe("TestCases", () => {
9090
}, {
9191
name: "devtool-cheap-source-map",
9292
devtool: "cheap-source-map"
93-
}, {
94-
name: "minimized",
95-
mode: "production",
96-
minimize: true,
97-
plugins: [
98-
new webpack.optimize.UglifyJsPlugin({
99-
sourceMap: false
100-
})
101-
]
10293
}, {
10394
name: "minimized-source-map",
10495
mode: "production",
10596
devtool: "eval-cheap-module-source-map",
106-
minimize: true,
107-
plugins: [
108-
new webpack.optimize.UglifyJsPlugin()
109-
]
97+
minimize: true
11098
}, {
11199
name: "minimized-hashed-modules",
112100
mode: "production",
113101
minimize: true,
114102
plugins: [
115-
new webpack.optimize.UglifyJsPlugin(),
116103
new webpack.HashedModuleIdsPlugin()
117104
]
118105
}, {
@@ -122,7 +109,6 @@ describe("TestCases", () => {
122109
minimize: true,
123110
plugins: [
124111
new webpack.HotModuleReplacementPlugin(),
125-
new webpack.optimize.UglifyJsPlugin(),
126112
new webpack.NamedModulesPlugin(),
127113
new webpack.NamedChunksPlugin()
128114
]

test/binCases/plugins/uglifyjsplugin-empty-args/test.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
--output-filename [name].js
44
--output-chunk-filename [id].chunk.js
55
--target async-node
6-
--plugin webpack/lib/optimize/UglifyJsPlugin
6+
--plugin uglifyjs-webpack-plugin

test/browsertest/middlewareTest.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ app.configure(function() {
3737
chunkFilename: "[chunkhash].chunk.js"
3838
},
3939
plugins: [
40-
new webpack.optimize.UglifyJsPlugin(),
4140
new webpack.HotModuleReplacementPlugin()
4241
]
4342
}), {

test/cases/parsing/harmony-tdz/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import value, { exception } from "./module";
33
it("should have a TDZ for exported const values", function() {
44
(typeof exception).should.be.eql("object");
55
exception.should.be.instanceof(Error);
6-
exception.message.should.be.eql("value is not defined");
6+
exception.message.should.match(/ is not defined$/);
77
value.should.be.eql("value");
88
});

test/configCases/code-generation/use-strict/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
__filename: false
55
},
66
optimization: {
7-
concatenateModules: false
7+
concatenateModules: false,
8+
minimize: false
89
}
910
};

test/configCases/externals/externals-in-commons-chunk/webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module.exports = {
1818
__dirname: false,
1919
__filename: false
2020
},
21+
optimization: {
22+
minimize: false
23+
},
2124
plugins: [
2225
new webpack.optimize.CommonsChunkPlugin({
2326
name: "common"

test/configCases/extract-text/issue-14/webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ module.exports = {
1212
}
1313
]
1414
},
15+
optimization: {
16+
minimize: false
17+
},
1518
plugins: [
1619
new ETP("style.css")
1720
]

test/configCases/filename-template/module-filename-template/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
output: {
34
devtoolLineToLine: true,
45
devtoolModuleFilenameTemplate: function(info) {
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = {};
1+
module.exports = {
2+
optimization: {
3+
minimize: false
4+
}
5+
};
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = {};
1+
module.exports = {
2+
optimization: {
3+
minimize: false
4+
}
5+
};

test/configCases/plugins/source-map-dev-tool-plugin/webpack.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var webpack = require("../../../../");
2+
var UglifyJsPlugin = require("uglifyjs-webpack-plugin");
23
module.exports = {
34
node: {
45
__dirname: false,
@@ -11,6 +12,13 @@ module.exports = {
1112
output: {
1213
filename: "[name].js"
1314
},
15+
optimization: {
16+
minimizer: [
17+
new UglifyJsPlugin({
18+
sourceMap: true
19+
})
20+
]
21+
},
1422
plugins: [
1523
new webpack.SourceMapDevToolPlugin({
1624
filename: "sourcemaps/[file].map",
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const webpack = require("../../../../");
2-
1+
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
32
module.exports = {
43
node: {
54
__dirname: false,
@@ -15,28 +14,31 @@ module.exports = {
1514
output: {
1615
filename: "[name].js"
1716
},
18-
plugins: [
19-
new webpack.optimize.UglifyJsPlugin({
20-
exclude: [
21-
"vendors.js",
22-
"extract.js"
23-
]
24-
}),
25-
new webpack.optimize.UglifyJsPlugin({
26-
extractComments: true,
27-
include: [
28-
"extract.js"
29-
]
30-
}),
31-
new webpack.optimize.UglifyJsPlugin({
32-
uglifyOptions: {
33-
compress: {
34-
passes: 2
35-
}
36-
},
37-
include: [
38-
"compress.js"
39-
]
40-
})
41-
]
17+
optimization: {
18+
minimize: true,
19+
minimizer: [
20+
new UglifyJsPlugin({
21+
exclude: [
22+
"vendors.js",
23+
"extract.js"
24+
]
25+
}),
26+
new UglifyJsPlugin({
27+
extractComments: true,
28+
include: [
29+
"extract.js"
30+
]
31+
}),
32+
new UglifyJsPlugin({
33+
uglifyOptions: {
34+
compress: {
35+
passes: 2
36+
}
37+
},
38+
include: [
39+
"compress.js"
40+
]
41+
})
42+
]
43+
}
4244
};

test/configCases/source-map/exclude-chunks-source-map/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var webpack = require("../../../../");
22
module.exports = {
3+
mode: "development",
4+
devtool: false,
35
node: {
46
__dirname: false,
57
__filename: false

test/configCases/source-map/line-to-line/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
output: {
34
devtoolLineToLine: true
45
},

test/configCases/source-map/module-names/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
output: {
34
chunkFilename: "[name].js",
45
devtoolModuleFilenameTemplate: "module",

test/configCases/source-map/namespace-source-path.library/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
output: {
34
library: "mylibrary"
45
},

test/configCases/source-map/namespace-source-path/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
output: {
34
devtoolNamespace: "mynamespace"
45
},

test/configCases/source-map/nosources/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
node: {
34
__dirname: false,
45
__filename: false

test/configCases/source-map/relative-source-map-path/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
output: {
34
chunkFilename: "js/chunks/c.js"
45
},

0 commit comments

Comments
 (0)