Skip to content

Commit 4e0ea09

Browse files
committed
fix: don't throw when both minimizerOptions and terserOptions are set
Prefer `minimizerOptions` silently when both are provided instead of throwing. This keeps existing configurations that pass the deprecated `terserOptions` working unchanged, even if a tooling layer also injects the new key.
1 parent 5c31d86 commit 4e0ea09

5 files changed

Lines changed: 17 additions & 23 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ reused for every minimizer.
413413
> **Note**
414414
>
415415
> `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.
416+
> backwards compatibility — passing either is equivalent. If both are set,
417+
> `minimizerOptions` wins. Prefer `minimizerOptions` in new code.
418418
419419
**webpack.config.js**
420420

src/index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,15 @@ class TerserPlugin {
192192
exclude,
193193
} = options || {};
194194

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-
195+
// `terserOptions` is a deprecated alias of `minimizerOptions`; prefer the
196+
// new name when both are provided.
204197
const resolvedMinimizerOptions =
205198
/** @type {MinimizerOptions<T>} */
206-
(minimizerOptions || terserOptions || {});
199+
(
200+
typeof minimizerOptions !== "undefined"
201+
? minimizerOptions
202+
: terserOptions || {}
203+
);
207204

208205
/**
209206
* @private

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,3 @@ exports[`validation validate 19`] = `
233233
* options.minimizerOptions should be an array:
234234
[object {}, ...] (should not have fewer than 1 item)"
235235
`;
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)."`;

test/minimizerOptions-option.test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ describe("minimizerOptions option", () => {
4646
);
4747
});
4848

49-
it("should throw when both `minimizerOptions` and `terserOptions` are provided", () => {
50-
expect(
51-
() =>
52-
new TerserPlugin({
53-
minimizerOptions: { mangle: false },
54-
terserOptions: { mangle: false },
55-
}),
56-
).toThrow(/can't be used together/);
49+
it("should prefer `minimizerOptions` when both `minimizerOptions` and `terserOptions` are provided", () => {
50+
const plugin = new TerserPlugin({
51+
minimizerOptions: { mangle: false },
52+
terserOptions: { mangle: true, compress: false },
53+
});
54+
55+
expect(plugin.options.minimizer.options).toEqual({ mangle: false });
5756
});
5857

5958
it("should default to an empty object when neither option is provided", () => {

test/validate-options.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ describe("validation", () => {
333333
minimizerOptions: { ecma: 5 },
334334
terserOptions: { ecma: 5 },
335335
});
336-
}).toThrowErrorMatchingSnapshot();
336+
}).not.toThrow();
337337
});
338338
});
339339
/* eslint-enable no-new */

0 commit comments

Comments
 (0)