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

Commit 933e075

Browse files
feat: custom minify function
1 parent 249eef3 commit 933e075

File tree

12 files changed

+263
-11
lines changed

12 files changed

+263
-11
lines changed

README.md

Lines changed: 52 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}`|`(file, uglifyOptions) => uglify.minify(file, uglifyOptions)`|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|
@@ -189,6 +190,57 @@ Number of concurrent runs.
189190

190191
> ⚠️ **`cheap-source-map` options don't work with this plugin**
191192
193+
### `minify`
194+
195+
**webpack.config.js**
196+
```js
197+
[
198+
new UglifyJsPlugin({
199+
minify(file, uglifyOptions) {
200+
return uglifyCompatibilityPackage.minify(file, uglifyOptions);
201+
}
202+
})
203+
]
204+
```
205+
206+
By default plugin use `uglify-es.minify` function.
207+
208+
Examples:
209+
210+
#### `uglify-js`
211+
212+
```bash
213+
npm i -D uglify-js
214+
```
215+
216+
**webpack.config.js**
217+
```js
218+
[
219+
new UglifyJsPlugin({
220+
minify(file, uglifyOptions) {
221+
return require('uglify-js').minify(file, uglifyOptions);
222+
}
223+
})
224+
]
225+
```
226+
227+
#### `terser`
228+
229+
```bash
230+
npm i -D terser
231+
```
232+
233+
**webpack.config.js**
234+
```js
235+
[
236+
new UglifyJsPlugin({
237+
minify(file, uglifyOptions) {
238+
return require('terser').minify(file, uglifyOptions);
239+
}
240+
})
241+
]
242+
```
243+
192244
### [`uglifyOptions`](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)
193245

194246
|Name|Type|Default|Description|

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
"pre-commit": "^1.2.2",
5959
"standard-version": "^4.3.0",
6060
"webpack": "^3.10.0",
61-
"webpack-defaults": "^1.6.0"
61+
"webpack-defaults": "^1.6.0",
62+
"uglify-js": "^3.4.2",
63+
"terser": "^3.7.6"
6264
},
6365
"peerDependencies": {
6466
"webpack": "^2.0.0 || ^3.0.0 || ^4.0.0"

src/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources';
88
import RequestShortener from 'webpack/lib/RequestShortener';
99
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
1010
import validateOptions from 'schema-utils';
11+
import uglify from 'uglify-es';
1112
import schema from './options.json';
12-
import Uglify from './uglify';
13+
import Runner from './uglify/Runner';
1314
import versions from './uglify/versions';
1415
import utils from './utils';
1516

@@ -20,6 +21,10 @@ class UglifyJsPlugin {
2021
validateOptions(schema, options, 'UglifyJs Plugin');
2122

2223
const {
24+
minify = (file, uglifyOptions) => uglify.minify(
25+
file,
26+
uglifyOptions,
27+
),
2328
uglifyOptions = {},
2429
test = /\.js(\?.*)?$/i,
2530
warningsFilter = () => true,
@@ -42,6 +47,7 @@ class UglifyJsPlugin {
4247
parallel,
4348
include,
4449
exclude,
50+
minify,
4551
uglifyOptions: {
4652
compress: {
4753
inline: 1,
@@ -114,7 +120,7 @@ class UglifyJsPlugin {
114120
};
115121

116122
const optimizeFn = (compilation, chunks, callback) => {
117-
const uglify = new Uglify({
123+
const runner = new Runner({
118124
cache: this.options.cache,
119125
parallel: this.options.parallel,
120126
});
@@ -169,6 +175,7 @@ class UglifyJsPlugin {
169175
commentsFile,
170176
extractComments: this.options.extractComments,
171177
uglifyOptions: this.options.uglifyOptions,
178+
minify: this.options.minify,
172179
};
173180

174181
if (this.options.cache) {
@@ -196,7 +203,7 @@ class UglifyJsPlugin {
196203
}
197204
});
198205

199-
uglify.runTasks(tasks, (tasksError, results) => {
206+
runner.runTasks(tasks, (tasksError, results) => {
200207
if (tasksError) {
201208
compilation.errors.push(tasksError);
202209
return;
@@ -294,7 +301,7 @@ class UglifyJsPlugin {
294301
}
295302
});
296303

297-
uglify.exit();
304+
runner.exit();
298305

299306
callback();
300307
});

src/options.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
{ "type": "integer" }
2020
]
2121
},
22+
"minify": {
23+
"instanceof": "Function"
24+
},
2225
"warningsFilter": {},
2326
"extractComments": {},
2427
"sourceMap": {

src/uglify/index.js renamed to src/uglify/Runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ try {
1212
workerFile = require.resolve('../../dist/uglify/worker');
1313
} catch (e) { } // eslint-disable-line no-empty
1414

15-
export default class {
15+
export default class Runner {
1616
constructor(options = {}) {
1717
const { cache, parallel } = options;
1818
this.cacheDir = cache === true ? findCacheDir({ name: 'uglifyjs-webpack-plugin' }) : cache;

src/uglify/minify.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable
22
arrow-body-style
33
*/
4-
import uglify from 'uglify-es';
5-
64
const buildUglifyOptions = ({
75
ecma,
86
warnings,
@@ -127,9 +125,9 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
127125
};
128126

129127
const minify = (options) => {
130-
const { file, input, inputSourceMap, extractComments } = options;
128+
const { file, input, inputSourceMap, extractComments, minify: minifyFn } = options;
131129
// Copy uglify options
132-
const uglifyOptions = buildUglifyOptions(options.uglifyOptions);
130+
const uglifyOptions = minify ? options.uglifyOptions : buildUglifyOptions(options.uglifyOptions);
133131

134132
// Add source map data
135133
if (inputSourceMap) {
@@ -148,7 +146,7 @@ const minify = (options) => {
148146
);
149147
}
150148

151-
const { error, map, code, warnings } = uglify.minify(
149+
const { error, map, code, warnings } = minifyFn(
152150
{ [file]: input },
153151
uglifyOptions,
154152
);

test/__snapshots__/extract-comments-options.test.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ exports[`when options.extractComments normalizes when options.extractComments is
44

55
exports[`when options.extractComments normalizes when options.extractComments is boolean - "true": test.js 1`] = `"var foo=1;"`;
66

7+
exports[`when options.extractComments normalizes when options.extractComments is boolean - "true": test.js.LICENSE 1`] = `
8+
"/*! Legal Comment */
9+
"
10+
`;
11+
712
exports[`when options.extractComments normalizes when options.extractComments is boolean - "true": test1.js 1`] = `
813
"/*! For license information please see test1.js.LICENSE */
914
var foo=1;"
@@ -69,6 +74,11 @@ exports[`when options.extractComments normalizes when options.extractComments is
6974

7075
exports[`when options.extractComments normalizes when options.extractComments is regex: test.js 1`] = `"var foo=1;"`;
7176

77+
exports[`when options.extractComments normalizes when options.extractComments is regex: test.js.LICENSE 1`] = `
78+
"// foo
79+
"
80+
`;
81+
7282
exports[`when options.extractComments normalizes when options.extractComments is regex: test1.js 1`] = `
7383
"/*! For license information please see test1.js.LICENSE */
7484
var foo=1;"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`when applied with minify option matches snapshot for \`terser\` minifier: errors 1`] = `Array []`;
4+
5+
exports[`when applied with minify option matches snapshot for \`terser\` minifier: main.js 1`] = `"webpackJsonp([0],[function(t,e,s){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:!0});e.default=class{constructor(t,e){this.x=t,this.y=e}static distance(t,e){const s=t.x-e.x,c=t.y-e.y;return Math.hypot(s,c)}}}],[0]);"`;
6+
7+
exports[`when applied with minify option matches snapshot for \`terser\` minifier: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a<e.length;a++)i=e[a],o[i]&&l.push(o[i][0]),o[i]=0;for(f in u)Object.prototype.hasOwnProperty.call(u,f)&&(r[f]=u[f]);for(n&&n(e,u,c);l.length;)l.shift()();if(c)for(a=0;a<c.length;a++)p=t(t.s=c[a]);return p};var e={},o={1:0};function t(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return r[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}t.m=r,t.c=e,t.d=function(r,n,e){t.o(r,n)||Object.defineProperty(r,n,{configurable:!1,enumerable:!0,get:e})},t.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(n,\\"a\\",n),n},t.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},t.p=\\"\\",t.oe=function(r){throw console.error(r),r}}([]);"`;
8+
9+
exports[`when applied with minify option matches snapshot for \`terser\` minifier: warnings 1`] = `Array []`;
10+
11+
exports[`when applied with minify option matches snapshot for \`uglify-es\` minifier: errors 1`] = `Array []`;
12+
13+
exports[`when applied with minify option matches snapshot for \`uglify-es\` minifier: main.js 1`] = `"webpackJsonp([0],[function(t,e,s){\\"use strict\\";Object.defineProperty(e,\\"__esModule\\",{value:!0});e.default=class{constructor(t,e){this.x=t,this.y=e}static distance(t,e){const s=t.x-e.x,c=t.y-e.y;return Math.hypot(s,c)}}}],[0]);"`;
14+
15+
exports[`when applied with minify option matches snapshot for \`uglify-es\` minifier: manifest.js 1`] = `"!function(r){var n=window.webpackJsonp;window.webpackJsonp=function(e,u,c){for(var f,i,p,a=0,l=[];a<e.length;a++)i=e[a],o[i]&&l.push(o[i][0]),o[i]=0;for(f in u)Object.prototype.hasOwnProperty.call(u,f)&&(r[f]=u[f]);for(n&&n(e,u,c);l.length;)l.shift()();if(c)for(a=0;a<c.length;a++)p=t(t.s=c[a]);return p};var e={},o={1:0};function t(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return r[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}t.m=r,t.c=e,t.d=function(r,n,e){t.o(r,n)||Object.defineProperty(r,n,{configurable:!1,enumerable:!0,get:e})},t.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(n,\\"a\\",n),n},t.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},t.p=\\"\\",t.oe=function(r){throw console.error(r),r}}([]);"`;
16+
17+
exports[`when applied with minify option matches snapshot for \`uglify-es\` minifier: warnings 1`] = `Array []`;
18+
19+
exports[`when applied with minify option matches snapshot for \`uglify-js\` minifier: errors 1`] = `Array []`;
20+
21+
exports[`when applied with minify option matches snapshot for \`uglify-js\` minifier: main.js 1`] = `"webpackJsonp([0],[function(e,n){e.exports=function(){var baz=document.getElementById(\\"root\\").innerHTML;document.getElementById(\\"demo\\").innerHTML=\\"Paragraph changed.\\"+baz}}],[0]);"`;
22+
23+
exports[`when applied with minify option matches snapshot for \`uglify-js\` minifier: manifest.js 1`] = `"!function(i){var p=window.webpackJsonp;window.webpackJsonp=function(r,n,e){for(var o,t,u,c=0,f=[];c<r.length;c++)t=r[c],a[t]&&f.push(a[t][0]),a[t]=0;for(o in n)Object.prototype.hasOwnProperty.call(n,o)&&(i[o]=n[o]);for(p&&p(r,n,e);f.length;)f.shift()();if(e)for(c=0;c<e.length;c++)u=l(l.s=e[c]);return u};var e={},a={1:0};function l(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return i[r].call(n.exports,n,n.exports,l),n.l=!0,n.exports}l.m=i,l.c=e,l.d=function(r,n,e){l.o(r,n)||Object.defineProperty(r,n,{configurable:!1,enumerable:!0,get:e})},l.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return l.d(n,\\"a\\",n),n},l.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},l.p=\\"\\",l.oe=function(r){throw console.error(r),r}}([]);"`;
24+
25+
exports[`when applied with minify option matches snapshot for \`uglify-js\` minifier: warnings 1`] = `Array []`;

test/fixtures/minify/terser.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Point {
2+
constructor(x, y) {
3+
this.x = x;
4+
this.y = y;
5+
}
6+
7+
static distance(a, b) {
8+
const dx = a.x - b.x;
9+
const dy = a.y - b.y;
10+
11+
return Math.hypot(dx, dy);
12+
}
13+
}
14+
15+
export default Point;

test/fixtures/minify/uglify-es.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Point {
2+
constructor(x, y) {
3+
this.x = x;
4+
this.y = y;
5+
}
6+
7+
static distance(a, b) {
8+
const dx = a.x - b.x;
9+
const dy = a.y - b.y;
10+
11+
return Math.hypot(dx, dy);
12+
}
13+
}
14+
15+
export default Point;

0 commit comments

Comments
 (0)