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

Commit

Permalink
feat: support minify function option
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jul 9, 2018
1 parent 249eef3 commit 876119d
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 37 deletions.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
|**`cacheKeys`**|`{Function(defaultCacheKeys, file) -> {Object}}`|`defaultCacheKeys => defaultCacheKeys`|Allows you to override default cache keys|
|**`parallel`**|`{Boolean\|Number}`|`false`|Use multi-process parallel running to improve the build speed|
|**`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**|
|**`minify`**|`{Function}`|`undefined`|Allows you to override default minify function|
|**`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)|
|**`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`)|
|**`warningsFilter`**|`{Function(source) -> {Boolean}}`|`() => true`|Allow to filter uglify warnings|
Expand Down Expand Up @@ -89,6 +90,8 @@ module.exports = {

### `cache`

If you use your own `minify` function please read the `minify` section for cache invalidation correctly.

#### `{Boolean}`

**webpack.config.js**
Expand Down Expand Up @@ -178,6 +181,8 @@ Number of concurrent runs.
### `sourceMap`

If you use your own `minify` function please read the `minify` section for handling source maps correctly.

**webpack.config.js**
```js
[
Expand All @@ -189,6 +194,102 @@ Number of concurrent runs.

> ⚠️ **`cheap-source-map` options don't work with this plugin**
### `minify`

**webpack.config.js**
```js
[
new UglifyJsPlugin({
minify(file, sourceMap) {
const extractedComments = [];

// Custom logic for extract comments

const { error, map, code, warnings } = minify(
file,
{ /* Your options for minification */ },
);

return { error, map, code, warnings, extractedComments };
}
})
]
```

By default plugin uses `uglify-es` package.

Examples:

#### `uglify-js`

```bash
npm i -D uglify-js
```

**webpack.config.js**
```js
[
new UglifyJsPlugin({
// Uncomment lines below for cache invalidation correctly
// cache: true,
// cacheKeys(defaultCacheKeys) {
// return Object.assign(
// {},
// defaultCacheKeys,
// { 'uglify-js': require('uglify-js/package.json').version },
// );
// },
minify(file, sourceMap) {
// https://github.com/mishoo/UglifyJS2#minify-options
const uglifyJsOptions = { /* your `uglify-js` package options */ };

if (sourceMap) {
uglifyJsOptions.sourceMap = {
content: sourceMap,
};
}

return require('uglify-js').minify(file, uglifyJsOptions);
}
})
]
```

#### `terser`

```bash
npm i -D terser
```

**webpack.config.js**
```js
[
new UglifyJsPlugin({
// Uncomment lines below for cache invalidation correctly
// cache: true,
// cacheKeys(defaultCacheKeys) {
// return Object.assign(
// {},
// defaultCacheKeys,
// { terser: require('terser/package.json').version },
// );
// },
minify(file, sourceMap) {
// https://github.com/fabiosantoscode/terser#minify-options
const terserOptions = { /* your `terser` package options */ };

if (sourceMap) {
terserOption.sourceMap = {
content: sourceMap,
};
}

return require('terser').minify(file, terserOptions);
}
})
]
```

### [`uglifyOptions`](https://github.com/mishoo/UglifyJS2/tree/harmony#minify-options)

|Name|Type|Default|Description|
Expand Down
109 changes: 78 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"nsp": "^3.1.0",
"pre-commit": "^1.2.2",
"standard-version": "^4.3.0",
"terser": "^3.7.6",
"uglify-js": "^3.4.3",
"webpack": "^3.10.0",
"webpack-defaults": "^1.6.0"
},
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import RequestShortener from 'webpack/lib/RequestShortener';
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
import validateOptions from 'schema-utils';
import schema from './options.json';
import Uglify from './uglify';
import Runner from './uglify/Runner';
import versions from './uglify/versions';
import utils from './utils';

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

const {
minify,
uglifyOptions = {},
test = /\.js(\?.*)?$/i,
warningsFilter = () => true,
Expand All @@ -42,6 +43,7 @@ class UglifyJsPlugin {
parallel,
include,
exclude,
minify,
uglifyOptions: {
compress: {
inline: 1,
Expand Down Expand Up @@ -114,7 +116,7 @@ class UglifyJsPlugin {
};

const optimizeFn = (compilation, chunks, callback) => {
const uglify = new Uglify({
const runner = new Runner({
cache: this.options.cache,
parallel: this.options.parallel,
});
Expand Down Expand Up @@ -169,6 +171,7 @@ class UglifyJsPlugin {
commentsFile,
extractComments: this.options.extractComments,
uglifyOptions: this.options.uglifyOptions,
minify: this.options.minify,
};

if (this.options.cache) {
Expand Down Expand Up @@ -196,7 +199,7 @@ class UglifyJsPlugin {
}
});

uglify.runTasks(tasks, (tasksError, results) => {
runner.runTasks(tasks, (tasksError, results) => {
if (tasksError) {
compilation.errors.push(tasksError);
return;
Expand Down Expand Up @@ -294,7 +297,7 @@ class UglifyJsPlugin {
}
});

uglify.exit();
runner.exit();

callback();
});
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"sourceMap": {
"type": "boolean"
},
"minify": {
"instanceof": "Function"
},
"uglifyOptions": {
"additionalProperties": true,
"type": "object",
Expand Down
Loading

0 comments on commit 876119d

Please sign in to comment.