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

Commit a0656df

Browse files
feat: support minify function option
1 parent 249eef3 commit a0656df

File tree

11 files changed

+304
-6
lines changed

11 files changed

+304
-6
lines changed

README.md

Lines changed: 87 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 custom `minify` function please read `minify` option section for cache invalidation correctly.
94+
9295
#### `{Boolean}`
9396

9497
**webpack.config.js**
@@ -189,6 +192,90 @@ Number of concurrent runs.
189192

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

194281
|Name|Type|Default|Description|

package-lock.json

Lines changed: 36 additions & 0 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",

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
127127
};
128128

129129
const minify = (options) => {
130-
const { file, input, inputSourceMap, extractComments } = options;
130+
const { file, input, inputSourceMap, extractComments, minify: minifyFn } = options;
131+
132+
if (minifyFn) {
133+
return minifyFn({ [file]: input });
134+
}
135+
131136
// Copy uglify options
132137
const uglifyOptions = buildUglifyOptions(options.uglifyOptions);
133138

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/es5.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function myFunction() {
2+
var baz = document.getElementById("root").innerHTML;
3+
4+
document.getElementById("demo").innerHTML = "Paragraph changed." + baz;
5+
}
6+
7+
module.exports = myFunction;

test/fixtures/minify/es6.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)