Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat multiple minify options #429

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 81 additions & 5 deletions README.md
Expand Up @@ -60,9 +60,9 @@ Using supported `devtool` values enable source map generation.
| :---------------------------------------: | :-----------------------------------------------------------------------------: | :-------------------------------------------------------------: | :--------------------------------------------------------------------------- |
| **[`test`](#test)** | `String\|RegExp\|Array<String\|RegExp>` | `/\.m?js(\?.*)?$/i` | Test to match files against. |
| **[`include`](#include)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to include. |
| **[`exclude`](#)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to exclude. |
| **[`exclude`](#exclude)** | `String\|RegExp\|Array<String\|RegExp>` | `undefined` | Files to exclude. |
| **[`parallel`](#parallel)** | `Boolean\|Number` | `true` | Use multi-process parallel running to improve the build speed. |
| **[`minify`](#minify)** | `Function` | `undefined` | Allows you to override default minify function. |
| **[`minify`](#minify)** | `Function` | `TerserPlugin.terserMinify` | Allows you to override default minify function. |
| **[`terserOptions`](#terseroptions)** | `Object` | [`default`](https://github.com/terser-js/terser#minify-options) | Terser [minify options](https://github.com/terser-js/terser#minify-options). |
| **[`extractComments`](#extractcomments)** | `Boolean\|String\|RegExp\|Function<(node, comment) -> Boolean\|Object>\|Object` | `true` | Whether comments shall be extracted to a separate file. |

Expand Down Expand Up @@ -184,15 +184,17 @@ module.exports = {

### `minify`

Type: `Function`
Default: `undefined`
Type: `Function|Array<Function>`
Default: `TerserPlugin.terserMinify`

Allows you to override default minify function.
By default plugin uses [terser](https://github.com/terser-js/terser) package.
Useful for using and testing unpublished versions or forks.

> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.

#### `Function`

**webpack.config.js**

```js
Expand Down Expand Up @@ -225,13 +227,51 @@ module.exports = {
};
```

#### `Array`

If an array of functions is passed to the `minify` option, the `terserOptions` must also be an array.
The function index in the `minify` array corresponds to the options object with the same index in the `terserOptions` array.

**webpack.config.js**

```js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: [
{}, // Options for the first function (CssMinimizerPlugin.swcMinify)
{}, // Options for the second function
],
minify: [
CssMinimizerPlugin.swcMinify,
async (input, inputSourceMap, minimizerOptions, extractComments) => {
// To do something
return {
code: `const foo = "bar"`,
map: `{"version": "3", ...}`,
warnings: [],
errors: [],
extractComments: [],
};
},
],
}),
],
},
};
```

### `terserOptions`

Type: `Object`
Type: `Object|Array<Object>`
Default: [default](https://github.com/terser-js/terser#minify-options)

Terser minify [options](https://github.com/terser-js/terser#minify-options).

#### `Object`

**webpack.config.js**

```js
Expand Down Expand Up @@ -262,6 +302,42 @@ module.exports = {
};
```

### `Array`

If an array of functions is passed to the `minify` option, the `terserOptions` must also be an array.
The function index in the `minify` array corresponds to the options object with the same index in the `terserOptions` array.

**webpack.config.js**

```js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: [
{}, // Options for the first function (CssMinimizerPlugin.swcMinify)
{}, // Options for the second function
],
minify: [
CssMinimizerPlugin.swcMinify,
async (input, inputSourceMap, minimizerOptions, extractComments) => {
// To do something
return {
code: `const foo = "bar"`,
map: `{"version": "3", ...}`,
warnings: [],
errors: [],
extractComments: [],
};
},
],
}),
],
},
};
```

### `extractComments`

Type: `Boolean|String|RegExp|Function<(node, comment) -> Boolean|Object>|Object`
Expand Down
51 changes: 34 additions & 17 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -56,6 +56,7 @@
},
"dependencies": {
"jest-worker": "^27.0.6",
"merge-source-map": "^1.1.0",
"p-limit": "^3.1.0",
"schema-utils": "^3.1.1",
"serialize-javascript": "^6.0.0",
Expand Down Expand Up @@ -91,7 +92,7 @@
"standard-version": "^9.3.1",
"typescript": "^4.3.5",
"uglify-js": "^3.14.1",
"webpack": "^5.48.0",
"webpack": "^5.51.1",
"worker-loader": "^3.0.8"
},
"keywords": [
Expand Down
26 changes: 17 additions & 9 deletions src/index.js
Expand Up @@ -84,14 +84,18 @@ import { minify as minifyFn } from "./minify";
* @typedef {ExtractCommentsCondition | ExtractCommentsObject} ExtractCommentsOptions
*/

/**
* @typedef {TerserMinifyOptions | UglifyJSMinifyOptions | SwcMinifyOptions | EsbuildMinifyOptions | CustomMinifyOptions} MinimizerOptions
*/

/**
* @typedef {Object} InternalMinifyOptions
* @property {string} name
* @property {string} input
* @property {RawSourceMap | undefined} inputSourceMap
* @property {ExtractCommentsOptions | undefined} extractComments
* @property {MinifyFunction} minify
* @property {TerserMinifyOptions | CustomMinifyOptions} minifyOptions
* @property {MinifyFunction | Array<MinifyFunction>} minimizer
* @property {MinimizerOptions | Array<MinimizerOptions>} minimizerOptions
*/

/**
Expand All @@ -101,7 +105,6 @@ import { minify as minifyFn } from "./minify";
* @property {Array<Error | string>} [errors]
* @property {Array<Error | string>} [warnings]
* @property {Array<string>} [extractedComments]
* @property {Array<string>} [extractedComments]
*/

/**
Expand Down Expand Up @@ -436,18 +439,23 @@ class TerserPlugin {
name,
input,
inputSourceMap,
minify: this.options.minify,
minifyOptions: { ...this.options.terserOptions },
// TODO rename to `minimizer` in the next major release
minimizer: this.options.minify,
minimizerOptions: Array.isArray(this.options.terserOptions)
? this.options.terserOptions.map((item) => {
return { ...item };
})
: { ...this.options.terserOptions },
extractComments: this.options.extractComments,
};

if (typeof options.minifyOptions.module === "undefined") {
if (typeof options.minimizerOptions.module === "undefined") {
if (typeof info.javascriptModule !== "undefined") {
options.minifyOptions.module = info.javascriptModule;
options.minimizerOptions.module = info.javascriptModule;
} else if (/\.mjs(\?.*)?$/i.test(name)) {
options.minifyOptions.module = true;
options.minimizerOptions.module = true;
} else if (/\.cjs(\?.*)?$/i.test(name)) {
options.minifyOptions.module = false;
options.minimizerOptions.module = false;
}
}

Expand Down