Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit 0174605

Browse files
feat: support minify function option (#325)
1 parent 249eef3 commit 0174605

File tree

11 files changed

+628
-37
lines changed

11 files changed

+628
-37
lines changed

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = {
5050
|**`cacheKeys`**|`{Function(defaultCacheKeys, file) -> {Object}}`|`defaultCacheKeys => defaultCacheKeys`|Allows you to override default cache keys|
5151
|**`parallel`**|`{Boolean\|Number}`|`false`|Use multi-process parallel running to improve the build speed|
5252
|**`sourceMap`**|`{Boolean}`|`false`|Use source maps to map error message locations to modules (This slows down the compilation) ⚠️ **`cheap-source-map` options don't work with this plugin**|
53+
|**`minify`**|`{Function}`|`undefined`|Allows you to override default minify function|
5354
|**`uglifyOptions`**|`{Object}`|[`{...defaults}`](https://github.com/webpack-contrib/uglifyjs-webpack-plugin/tree/master#uglifyoptions)|`uglify` [Options](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)|
5455
|**`extractComments`**|`{Boolean\|RegExp\|Function<(node, comment) -> {Boolean\|Object}>}`|`false`|Whether comments shall be extracted to a separate file, (see [details](https://github.com/webpack/webpack/commit/71933e979e51c533b432658d5e37917f9e71595a) (`webpack >= 2.3.0`)|
5556
|**`warningsFilter`**|`{Function(source) -> {Boolean}}`|`() => true`|Allow to filter uglify warnings|
@@ -89,6 +90,8 @@ module.exports = {
8990

9091
### `cache`
9192

93+
If you use your own `minify` function please read the `minify` section for cache invalidation correctly.
94+
9295
#### `{Boolean}`
9396

9497
**webpack.config.js**
@@ -178,6 +181,8 @@ Number of concurrent runs.
178181
179182
### `sourceMap`
180183

184+
If you use your own `minify` function please read the `minify` section for handling source maps correctly.
185+
181186
**webpack.config.js**
182187
```js
183188
[
@@ -189,6 +194,102 @@ Number of concurrent runs.
189194

190195
> ⚠️ **`cheap-source-map` options don't work with this plugin**
191196
197+
### `minify`
198+
199+
**webpack.config.js**
200+
```js
201+
[
202+
new UglifyJsPlugin({
203+
minify(file, sourceMap) {
204+
const extractedComments = [];
205+
206+
// Custom logic for extract comments
207+
208+
const { error, map, code, warnings } = minify(
209+
file,
210+
{ /* Your options for minification */ },
211+
);
212+
213+
return { error, map, code, warnings, extractedComments };
214+
}
215+
})
216+
]
217+
```
218+
219+
By default plugin uses `uglify-es` package.
220+
221+
Examples:
222+
223+
#### `uglify-js`
224+
225+
```bash
226+
npm i -D uglify-js
227+
```
228+
229+
**webpack.config.js**
230+
```js
231+
[
232+
new UglifyJsPlugin({
233+
// Uncomment lines below for cache invalidation correctly
234+
// cache: true,
235+
// cacheKeys(defaultCacheKeys) {
236+
// return Object.assign(
237+
// {},
238+
// defaultCacheKeys,
239+
// { 'uglify-js': require('uglify-js/package.json').version },
240+
// );
241+
// },
242+
minify(file, sourceMap) {
243+
// https://github.com/mishoo/UglifyJS2#minify-options
244+
const uglifyJsOptions = { /* your `uglify-js` package options */ };
245+
246+
if (sourceMap) {
247+
uglifyJsOptions.sourceMap = {
248+
content: sourceMap,
249+
};
250+
}
251+
252+
return require('uglify-js').minify(file, uglifyJsOptions);
253+
}
254+
})
255+
]
256+
```
257+
258+
#### `terser`
259+
260+
```bash
261+
npm i -D terser
262+
```
263+
264+
**webpack.config.js**
265+
```js
266+
[
267+
new UglifyJsPlugin({
268+
// Uncomment lines below for cache invalidation correctly
269+
// cache: true,
270+
// cacheKeys(defaultCacheKeys) {
271+
// return Object.assign(
272+
// {},
273+
// defaultCacheKeys,
274+
// { terser: require('terser/package.json').version },
275+
// );
276+
// },
277+
minify(file, sourceMap) {
278+
// https://github.com/fabiosantoscode/terser#minify-options
279+
const terserOptions = { /* your `terser` package options */ };
280+
281+
if (sourceMap) {
282+
terserOption.sourceMap = {
283+
content: sourceMap,
284+
};
285+
}
286+
287+
return require('terser').minify(file, terserOptions);
288+
}
289+
})
290+
]
291+
```
292+
192293
### [`uglifyOptions`](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)
193294

194295
|Name|Type|Default|Description|

package-lock.json

Lines changed: 78 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
"nsp": "^3.1.0",
5858
"pre-commit": "^1.2.2",
5959
"standard-version": "^4.3.0",
60+
"terser": "^3.7.6",
61+
"uglify-js": "^3.4.3",
6062
"webpack": "^3.10.0",
6163
"webpack-defaults": "^1.6.0"
6264
},

src/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import RequestShortener from 'webpack/lib/RequestShortener';
99
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
1010
import validateOptions from 'schema-utils';
1111
import schema from './options.json';
12-
import Uglify from './uglify';
12+
import Runner from './uglify/Runner';
1313
import versions from './uglify/versions';
1414
import utils from './utils';
1515

@@ -20,6 +20,7 @@ class UglifyJsPlugin {
2020
validateOptions(schema, options, 'UglifyJs Plugin');
2121

2222
const {
23+
minify,
2324
uglifyOptions = {},
2425
test = /\.js(\?.*)?$/i,
2526
warningsFilter = () => true,
@@ -42,6 +43,7 @@ class UglifyJsPlugin {
4243
parallel,
4344
include,
4445
exclude,
46+
minify,
4547
uglifyOptions: {
4648
compress: {
4749
inline: 1,
@@ -114,7 +116,7 @@ class UglifyJsPlugin {
114116
};
115117

116118
const optimizeFn = (compilation, chunks, callback) => {
117-
const uglify = new Uglify({
119+
const runner = new Runner({
118120
cache: this.options.cache,
119121
parallel: this.options.parallel,
120122
});
@@ -169,6 +171,7 @@ class UglifyJsPlugin {
169171
commentsFile,
170172
extractComments: this.options.extractComments,
171173
uglifyOptions: this.options.uglifyOptions,
174+
minify: this.options.minify,
172175
};
173176

174177
if (this.options.cache) {
@@ -196,7 +199,7 @@ class UglifyJsPlugin {
196199
}
197200
});
198201

199-
uglify.runTasks(tasks, (tasksError, results) => {
202+
runner.runTasks(tasks, (tasksError, results) => {
200203
if (tasksError) {
201204
compilation.errors.push(tasksError);
202205
return;
@@ -294,7 +297,7 @@ class UglifyJsPlugin {
294297
}
295298
});
296299

297-
uglify.exit();
300+
runner.exit();
298301

299302
callback();
300303
});

src/options.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"sourceMap": {
2525
"type": "boolean"
2626
},
27+
"minify": {
28+
"instanceof": "Function"
29+
},
2730
"uglifyOptions": {
2831
"additionalProperties": true,
2932
"type": "object",

0 commit comments

Comments
 (0)