Skip to content

Commit 5c31d86

Browse files
committed
feat: rename terserOptions to minimizerOptions
Introduce `minimizerOptions` as the canonical name for the options that are forwarded to the active minimizer (Terser by default, or whatever function is supplied via `minify`). The previous `terserOptions` key keeps working as a deprecated alias; passing both at the same time now throws an explicit error. Schema, JSDoc-derived TypeScript declarations, README examples and tests have been updated to favour the new name while still validating the old one.
1 parent e62c2f8 commit 5c31d86

8 files changed

Lines changed: 241 additions & 35 deletions

README.md

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Using supported `devtool` values enable source map generation.
7474
- **[`exclude`](#exclude)**
7575
- **[`parallel`](#parallel)**
7676
- **[`minify`](#minify)**
77-
- **[`terserOptions`](#terseroptions)**
77+
- **[`minimizerOptions`](#minimizeroptions)**
7878
- **[`extractComments`](#extractcomments)**
7979

8080
### `test`
@@ -287,7 +287,7 @@ Allows you to override the default minify function.
287287
By default plugin uses [terser](https://github.com/terser/terser) package.
288288
Useful for using and testing unpublished versions or forks.
289289

290-
An array of functions can also be provided to chain multiple minimizers — the output of each minimizer is fed as input to the next. When an array is used, the [`terserOptions`](#terseroptions) option may also be an array (index-paired with `minify`) or a single object that is reused for every minimizer.
290+
An array of functions can also be provided to chain multiple minimizers — the output of each minimizer is fed as input to the next. When an array is used, the [`minimizerOptions`](#minimizeroptions) option may also be an array (index-paired with `minify`) or a single object that is reused for every minimizer.
291291

292292
> **Warning**
293293
>
@@ -300,7 +300,7 @@ An array of functions can also be provided to chain multiple minimizers — the
300300
```js
301301
// Can be async
302302
const minify = (input, sourceMap, minimizerOptions, extractsComments) => {
303-
// The `minimizerOptions` option contains option from the `terserOptions` option
303+
// The `minimizerOptions` argument contains options from the `minimizerOptions` plugin option
304304
// You can use `minimizerOptions.myCustomOption`
305305

306306
// Custom logic for extract comments
@@ -333,7 +333,7 @@ module.exports = {
333333
minimize: true,
334334
minimizer: [
335335
new TerserPlugin({
336-
terserOptions: {
336+
minimizerOptions: {
337337
myCustomOption: true,
338338
},
339339
minify,
@@ -346,7 +346,7 @@ module.exports = {
346346
#### `array`
347347

348348
If an array of functions is passed to the `minify` option, the output of each
349-
minimizer is fed as input to the next one. The `terserOptions` option can be
349+
minimizer is fed as input to the next one. The `minimizerOptions` option can be
350350
either an array of option objects (index-paired with `minify`) or a single
351351
object that will be shared by all minimizers. Warnings, errors and extracted
352352
comments from all minimizers are merged together.
@@ -360,8 +360,8 @@ module.exports = {
360360
minimizer: [
361361
new TerserPlugin({
362362
minify: [TerserPlugin.terserMinify, TerserPlugin.swcMinify],
363-
// `terserOptions` can be an array of options, one per `minify` entry
364-
terserOptions: [
363+
// `minimizerOptions` can be an array of options, one per `minify` entry
364+
minimizerOptions: [
365365
// Options for `TerserPlugin.terserMinify`
366366
{ mangle: false },
367367
// Options for `TerserPlugin.swcMinify`
@@ -373,12 +373,12 @@ module.exports = {
373373
};
374374
```
375375

376-
### `terserOptions`
376+
### `minimizerOptions`
377377

378378
Type:
379379

380380
```ts
381-
interface terserOptions {
381+
interface minimizerOptions {
382382
compress?: boolean | CompressOptions;
383383
ecma?: ECMA;
384384
enclose?: boolean | string;
@@ -397,18 +397,25 @@ interface terserOptions {
397397
toplevel?: boolean;
398398
}
399399

400-
type options = terserOptions | terserOptions[];
400+
type options = minimizerOptions | minimizerOptions[];
401401
```
402402

403403
Default: [default](https://github.com/terser/terser#minify-options)
404404

405-
Terser [options](https://github.com/terser/terser#minify-options).
405+
Options for the active minimizer. With the default Terser minify, see Terser's
406+
[minify options](https://github.com/terser/terser#minify-options).
406407

407-
When the [`minify`](#minify) option is an array of minimizers, `terserOptions`
408+
When the [`minify`](#minify) option is an array of minimizers, `minimizerOptions`
408409
can also be an array. Each element is passed to the minimizer at the same
409410
index in the `minify` array. If a single object is provided instead, it is
410411
reused for every minimizer.
411412

413+
> **Note**
414+
>
415+
> `terserOptions` is kept as a deprecated alias of `minimizerOptions` for
416+
> backwards compatibility — passing either is equivalent. Passing both at the
417+
> same time throws an error. Prefer `minimizerOptions` in new code.
418+
412419
**webpack.config.js**
413420

414421
```js
@@ -417,7 +424,7 @@ module.exports = {
417424
minimize: true,
418425
minimizer: [
419426
new TerserPlugin({
420-
terserOptions: {
427+
minimizerOptions: {
421428
ecma: undefined,
422429
parse: {},
423430
compress: {},
@@ -492,7 +499,7 @@ By default, extract only comments using `/^\**!|@preserve|@license|@cc_on/i` Reg
492499

493500
If the original file is named `foo.js`, then the comments will be stored to `foo.js.LICENSE.txt`.
494501

495-
The `terserOptions.format.comments` option specifies whether the comment will be preserved - i.e., it is possible to preserve some comments (e.g. annotations) while extracting others, or even preserve comments that have already been extracted.
502+
The `minimizerOptions.format.comments` option specifies whether the comment will be preserved - i.e., it is possible to preserve some comments (e.g. annotations) while extracting others, or even preserve comments that have already been extracted.
496503

497504
#### `boolean`
498505

@@ -741,7 +748,7 @@ module.exports = {
741748
minimize: true,
742749
minimizer: [
743750
new TerserPlugin({
744-
terserOptions: {
751+
minimizerOptions: {
745752
format: {
746753
comments: /@license/i,
747754
},
@@ -765,7 +772,7 @@ module.exports = {
765772
minimize: true,
766773
minimizer: [
767774
new TerserPlugin({
768-
terserOptions: {
775+
minimizerOptions: {
769776
format: {
770777
comments: false,
771778
},
@@ -790,9 +797,9 @@ module.exports = {
790797
minimizer: [
791798
new TerserPlugin({
792799
minify: TerserPlugin.uglifyJsMinify,
793-
// `terserOptions` options will be passed to `uglify-js`
800+
// `minimizerOptions` will be passed to `uglify-js`
794801
// Link to options - https://github.com/mishoo/UglifyJS#minify-options
795-
terserOptions: {},
802+
minimizerOptions: {},
796803
}),
797804
],
798805
},
@@ -818,9 +825,9 @@ module.exports = {
818825
minimizer: [
819826
new TerserPlugin({
820827
minify: TerserPlugin.swcMinify,
821-
// `terserOptions` options will be passed to `swc` (`@swc/core`)
828+
// `minimizerOptions` will be passed to `swc` (`@swc/core`)
822829
// Link to options - https://swc.rs/docs/config-js-minify
823-
terserOptions: {},
830+
minimizerOptions: {},
824831
}),
825832
],
826833
},
@@ -844,16 +851,16 @@ module.exports = {
844851
minimizer: [
845852
new TerserPlugin({
846853
minify: TerserPlugin.esbuildMinify,
847-
// `terserOptions` options will be passed to `esbuild`
854+
// `minimizerOptions` will be passed to `esbuild`
848855
// Link to options - https://esbuild.github.io/api/#minify
849856
// Note: the `minify` options is true by default (and override other `minify*` options), so if you want to disable the `minifyIdentifiers` option (or other `minify*` options) please use:
850-
// terserOptions: {
857+
// minimizerOptions: {
851858
// minify: false,
852859
// minifyWhitespace: true,
853860
// minifyIdentifiers: false,
854861
// minifySyntax: true,
855862
// },
856-
terserOptions: {},
863+
minimizerOptions: {},
857864
}),
858865
],
859866
},
@@ -878,7 +885,7 @@ module.exports = {
878885
test: /\.json$/,
879886
minify: TerserPlugin.jsonMinify,
880887
// We are supporting `space` and `replacer` options, you can set them below
881-
terserOptions: {},
888+
minimizerOptions: {},
882889
}),
883890
],
884891
},
@@ -927,7 +934,7 @@ module.exports = {
927934
minimize: true,
928935
minimizer: [
929936
new TerserPlugin({
930-
terserOptions: {
937+
minimizerOptions: {
931938
compress: true,
932939
},
933940
}),
@@ -950,27 +957,27 @@ module.exports = {
950957
minimizer: [
951958
new TerserPlugin<SwcOptions>({
952959
minify: TerserPlugin.swcMinify,
953-
terserOptions: {
960+
minimizerOptions: {
954961
// `swc` options
955962
},
956963
}),
957964
new TerserPlugin<UglifyJSOptions>({
958965
minify: TerserPlugin.uglifyJsMinify,
959-
terserOptions: {
966+
minimizerOptions: {
960967
// `uglif-js` options
961968
},
962969
}),
963970
new TerserPlugin<EsbuildOptions>({
964971
minify: TerserPlugin.esbuildMinify,
965-
terserOptions: {
972+
minimizerOptions: {
966973
// `esbuild` options
967974
},
968975
}),
969976

970977
// Alternative usage:
971978
new TerserPlugin<TerserOptions>({
972979
minify: TerserPlugin.terserMinify,
973-
terserOptions: {
980+
minimizerOptions: {
974981
// `terser` options
975982
},
976983
}),

src/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ const {
153153

154154
/**
155155
* @template T
156-
* @typedef {T extends import("terser").MinifyOptions ? { minify?: MinimizerImplementation<T> | undefined, terserOptions?: MinimizerOptions<T> | undefined } : { minify: MinimizerImplementation<T>, terserOptions?: MinimizerOptions<T> | undefined }} DefinedDefaultMinimizerAndOptions
156+
* @typedef {T extends import("terser").MinifyOptions ? { minify?: MinimizerImplementation<T> | undefined, minimizerOptions?: MinimizerOptions<T> | undefined, terserOptions?: MinimizerOptions<T> | undefined } : { minify: MinimizerImplementation<T>, minimizerOptions?: MinimizerOptions<T> | undefined, terserOptions?: MinimizerOptions<T> | undefined }} DefinedDefaultMinimizerAndOptions
157157
*/
158158

159159
/**
@@ -183,14 +183,28 @@ class TerserPlugin {
183183
minify = /** @type {MinimizerImplementation<T>} */ (
184184
/** @type {unknown} */ (terserMinify)
185185
),
186-
terserOptions = /** @type {MinimizerOptions<T>} */ ({}),
186+
minimizerOptions,
187+
terserOptions,
187188
test = /\.[cm]?js(\?.*)?$/i,
188189
extractComments = true,
189190
parallel = true,
190191
include,
191192
exclude,
192193
} = options || {};
193194

195+
if (
196+
typeof minimizerOptions !== "undefined" &&
197+
typeof terserOptions !== "undefined"
198+
) {
199+
throw new Error(
200+
"The `minimizerOptions` and `terserOptions` options can't be used together. Please use only `minimizerOptions` (`terserOptions` is a deprecated alias).",
201+
);
202+
}
203+
204+
const resolvedMinimizerOptions =
205+
/** @type {MinimizerOptions<T>} */
206+
(minimizerOptions || terserOptions || {});
207+
194208
/**
195209
* @private
196210
* @type {InternalPluginOptions<T>}
@@ -203,7 +217,7 @@ class TerserPlugin {
203217
exclude,
204218
minimizer: {
205219
implementation: minify,
206-
options: terserOptions,
220+
options: resolvedMinimizerOptions,
207221
},
208222
};
209223
}

src/options.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,26 @@
6464
}
6565
]
6666
},
67-
"terserOptions": {
67+
"minimizerOptions": {
6868
"description": "Options for `terser` (by default) or custom `minify` function.",
69+
"link": "https://github.com/webpack/terser-webpack-plugin#minimizeroptions",
70+
"anyOf": [
71+
{
72+
"additionalProperties": true,
73+
"type": "object"
74+
},
75+
{
76+
"type": "array",
77+
"minItems": 1,
78+
"items": {
79+
"additionalProperties": true,
80+
"type": "object"
81+
}
82+
}
83+
]
84+
},
85+
"terserOptions": {
86+
"description": "Deprecated alias for `minimizerOptions`. Options for `terser` (by default) or custom `minify` function.",
6987
"link": "https://github.com/webpack/terser-webpack-plugin#terseroptions",
7088
"anyOf": [
7189
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`minimizerOptions option should accept \`minimizerOptions\` and apply them like \`terserOptions\`: assets 1`] = `
4+
Object {
5+
"main.js": "!function() {
6+
var __webpack_modules__ = {
7+
326: function(module) {
8+
var abc, Person = {
9+
firstName: null,
10+
lastName: null
11+
}, Employee = Object.create(Person, {
12+
id: {
13+
value: null,
14+
enumerable: !0,
15+
configurable: !0,
16+
writable: !0
17+
}
18+
}), Manager = Object.create(Employee, {
19+
department: {
20+
value: null,
21+
enumerable: !0,
22+
configurable: !0,
23+
writable: !0
24+
}
25+
});
26+
module.exports = {
27+
Person: Person,
28+
Employee: Employee,
29+
Manager: Manager
30+
}, abc = {
31+
data() {
32+
return {
33+
a: 2
34+
};
35+
}
36+
}, console.log(abc);
37+
}
38+
}, __webpack_module_cache__ = {};
39+
(function __webpack_require__(moduleId) {
40+
var cachedModule = __webpack_module_cache__[moduleId];
41+
if (void 0 !== cachedModule) return cachedModule.exports;
42+
var module = __webpack_module_cache__[moduleId] = {
43+
exports: {}
44+
};
45+
return __webpack_modules__[moduleId](module, module.exports, __webpack_require__),
46+
module.exports;
47+
})(326);
48+
}();",
49+
}
50+
`;
51+
52+
exports[`minimizerOptions option should accept \`minimizerOptions\` and apply them like \`terserOptions\`: errors 1`] = `Array []`;
53+
54+
exports[`minimizerOptions option should accept \`minimizerOptions\` and apply them like \`terserOptions\`: warnings 1`] = `Array []`;

test/__snapshots__/validate-options.test.js.snap

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ exports[`validation validate 12`] = `
144144
"Invalid options object. Terser Plugin has been initialized using an options object that does not match the API schema.
145145
- options.terserOptions should be one of these:
146146
object {} | [object {}, ...] (should not have fewer than 1 item)
147-
-> Options for \`terser\` (by default) or custom \`minify\` function.
147+
-> Deprecated alias for \`minimizerOptions\`. Options for \`terser\` (by default) or custom \`minify\` function.
148148
-> Read more at https://github.com/webpack/terser-webpack-plugin#terseroptions
149149
Details:
150150
* options.terserOptions should be an object:
@@ -213,5 +213,25 @@ exports[`validation validate 16`] = `
213213
exports[`validation validate 17`] = `
214214
"Invalid options object. Terser Plugin has been initialized using an options object that does not match the API schema.
215215
- options has an unknown property 'unknown'. These properties are valid:
216-
object { test?, include?, exclude?, terserOptions?, extractComments?, parallel?, minify? }"
216+
object { test?, include?, exclude?, minimizerOptions?, terserOptions?, extractComments?, parallel?, minify? }"
217+
`;
218+
219+
exports[`validation validate 18`] = `
220+
"Invalid options object. Terser Plugin has been initialized using an options object that does not match the API schema.
221+
- options.minimizerOptions should be a non-empty array."
217222
`;
223+
224+
exports[`validation validate 19`] = `
225+
"Invalid options object. Terser Plugin has been initialized using an options object that does not match the API schema.
226+
- options.minimizerOptions should be one of these:
227+
object {} | [object {}, ...] (should not have fewer than 1 item)
228+
-> Options for \`terser\` (by default) or custom \`minify\` function.
229+
-> Read more at https://github.com/webpack/terser-webpack-plugin#minimizeroptions
230+
Details:
231+
* options.minimizerOptions should be an object:
232+
object {}
233+
* options.minimizerOptions should be an array:
234+
[object {}, ...] (should not have fewer than 1 item)"
235+
`;
236+
237+
exports[`validation validate 20`] = `"The \`minimizerOptions\` and \`terserOptions\` options can't be used together. Please use only \`minimizerOptions\` (\`terserOptions\` is a deprecated alias)."`;

0 commit comments

Comments
 (0)