Skip to content

Commit a2eee00

Browse files
committed
Make ServiceWorker.minify option auto-detect usage of optimization.minimize (webpack 4+)
Fixes #379
1 parent 0f73617 commit a2eee00

File tree

24 files changed

+180
-37
lines changed

24 files changed

+180
-37
lines changed

lib/misc/sw-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function () {};
1212
module.exports.pitch = function pitch(remainingRequest, precedingRequest, data) {
1313
var _this = this;
1414

15-
this.cacheable && this.cacheable();;
15+
this.cacheable && this.cacheable();
1616

1717
var callback = this.async();
1818
var templatePath = path.join(__dirname, 'sw-template.js');

lib/service-worker.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,36 @@ var ServiceWorker = (function () {
8888
childCompiler.context = compiler.context;
8989
new _webpackLibSingleEntryPlugin2['default'](compiler.context, entry, name).apply(childCompiler);
9090

91+
var optimization = compiler.options.optimization || {};
92+
93+
var uglifyOptions = {
94+
compress: {
95+
warnings: false,
96+
dead_code: true,
97+
drop_console: true,
98+
unused: true
99+
},
100+
101+
output: {
102+
comments: false
103+
}
104+
};
105+
91106
if (this.minify === true) {
92107
if (!_miscGetUglifyPlugin2['default']) {
93-
throw new Error('OfflinePlugin: uglifyjs-webpack-plugin is required to preform minification');
108+
throw new Error('OfflinePlugin: uglifyjs-webpack-plugin is required to preform a minification');
94109
}
95110

96111
var options = {
97112
test: new RegExp(name),
98-
uglifyOptions: {
99-
compress: {
100-
warnings: false,
101-
dead_code: true,
102-
drop_console: true,
103-
unused: true
104-
},
105-
106-
output: {
107-
comments: false
108-
}
109-
}
113+
uglifyOptions: uglifyOptions
110114
};
111115

112116
new _miscGetUglifyPlugin2['default'](options).apply(childCompiler);
113-
} else if (this.minify !== false && _miscGetUglifyPlugin2['default']) {
117+
} else if ((this.minify !== false || optimization.minimize) && _miscGetUglifyPlugin2['default']) {
114118
// Do not perform auto-minification if UglifyJsPlugin isn't installed
115119

116-
(compiler.options.plugins || []).some(function (plugin) {
120+
var added = (optimization.minimize && optimization.minimizer || []).concat(compiler.options.plugins || []).some(function (plugin) {
117121
if (plugin instanceof _miscGetUglifyPlugin2['default']) {
118122
var options = (0, _deepExtend2['default'])({}, plugin.options);
119123

@@ -123,6 +127,15 @@ var ServiceWorker = (function () {
123127
return true;
124128
}
125129
});
130+
131+
if (!added && optimization.minimize) {
132+
var options = {
133+
test: new RegExp(name),
134+
uglifyOptions: uglifyOptions
135+
};
136+
137+
new _miscGetUglifyPlugin2['default'](options).apply(childCompiler);
138+
}
126139
}
127140

128141
// Needed for HMR. offline-plugin doesn't support it,
@@ -163,9 +176,9 @@ var ServiceWorker = (function () {
163176
if (typeof this.minify === 'boolean') {
164177
minify = this.minify;
165178
} else {
166-
minify = !!_miscGetUglifyPlugin2['default'] && !!(compiler.options.plugins || []).some(function (plugin) {
179+
minify = !!_miscGetUglifyPlugin2['default'] && (!!(compiler.options.optimization && compiler.options.optimization.minimize) || !!(compiler.options.plugins || []).some(function (plugin) {
167180
return plugin instanceof _miscGetUglifyPlugin2['default'];
168-
});
181+
}));
169182
}
170183

171184
var source = this.getDataTemplate(plugin.caches, plugin, minify);

src/service-worker.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,39 @@ export default class ServiceWorker {
6666
childCompiler.context = compiler.context;
6767
new SingleEntryPlugin(compiler.context, entry, name).apply(childCompiler);
6868

69+
const optimization = compiler.options.optimization || {};
70+
71+
const uglifyOptions = {
72+
compress: {
73+
warnings: false,
74+
dead_code: true,
75+
drop_console: true,
76+
unused: true
77+
},
78+
79+
output: {
80+
comments: false
81+
}
82+
};
83+
6984
if (this.minify === true) {
7085
if (!UglifyJsPlugin) {
71-
throw new Error('OfflinePlugin: uglifyjs-webpack-plugin is required to preform minification')
86+
throw new Error('OfflinePlugin: uglifyjs-webpack-plugin is required to preform a minification')
7287
}
7388

7489
const options = {
7590
test: new RegExp(name),
76-
uglifyOptions: {
77-
compress: {
78-
warnings: false,
79-
dead_code: true,
80-
drop_console: true,
81-
unused: true
82-
},
83-
84-
output: {
85-
comments: false
86-
}
87-
}
91+
uglifyOptions: uglifyOptions
8892
};
8993

9094
new UglifyJsPlugin(options).apply(childCompiler);
91-
} else if (this.minify !== false && UglifyJsPlugin) {
95+
} else if (
96+
(this.minify !== false || optimization.minimize) && UglifyJsPlugin
97+
) {
9298
// Do not perform auto-minification if UglifyJsPlugin isn't installed
9399

94-
(compiler.options.plugins || []).some((plugin) => {
100+
const added = ((optimization.minimize && optimization.minimizer) || [])
101+
.concat(compiler.options.plugins || []).some((plugin) => {
95102
if (plugin instanceof UglifyJsPlugin) {
96103
const options = deepExtend({}, plugin.options);
97104

@@ -101,6 +108,15 @@ export default class ServiceWorker {
101108
return true;
102109
}
103110
});
111+
112+
if (!added && optimization.minimize) {
113+
const options = {
114+
test: new RegExp(name),
115+
uglifyOptions: uglifyOptions
116+
};
117+
118+
new UglifyJsPlugin(options).apply(childCompiler);
119+
}
104120
}
105121

106122
// Needed for HMR. offline-plugin doesn't support it,
@@ -140,10 +156,16 @@ export default class ServiceWorker {
140156
if (typeof this.minify === 'boolean') {
141157
minify = this.minify;
142158
} else {
143-
minify = !!UglifyJsPlugin &&
144-
!!(compiler.options.plugins || []).some((plugin) => {
145-
return plugin instanceof UglifyJsPlugin;
146-
});
159+
minify = !!UglifyJsPlugin && (
160+
!!(
161+
compiler.options.optimization &&
162+
compiler.options.optimization.minimize
163+
) || !!(
164+
(compiler.options.plugins || []).some((plugin) => {
165+
return plugin instanceof UglifyJsPlugin;
166+
})
167+
)
168+
);
147169
}
148170

149171
let source = this.getDataTemplate(plugin.caches, plugin, minify);

tests/legacy/compare.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ module.exports = function(testDir) {
3535
var expectedFolder = path.join(testDir, '__expected', 'webpack' + webpackVersion.split('.')[0]);
3636
var outputFolder = path.join(testDir, '__output');
3737

38+
try {
39+
fs.accessSync(expectedFolder);
40+
} catch (e) {
41+
return Promise.resolve();
42+
}
43+
3844
var res = dircompare.compareSync(
3945
expectedFolder,
4046
outputFolder,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CACHE MANIFEST
2+
#ver:da39a3ee5e6b4b0d3255bfef95601890afd80709
3+
4+
CACHE:
5+
../external.js
6+
7+
NETWORK:
8+
*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!doctype html>
2+
<html manifest="manifest.appcache"></html>

tests/legacy/fixtures/sw-minify-auto/__expected/webpack2/main.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/legacy/fixtures/sw-minify-auto/__expected/webpack2/sw.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CACHE MANIFEST
2+
#ver:da39a3ee5e6b4b0d3255bfef95601890afd80709
3+
4+
CACHE:
5+
../external.js
6+
7+
NETWORK:
8+
*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!doctype html>
2+
<html manifest="manifest.appcache"></html>

0 commit comments

Comments
 (0)