Skip to content

Commit a1191dd

Browse files
fix(config): let a module library type enable output.module
A `module` or `modern-module` library type is another spelling of `output.module` rather than something that can disagree with it, so it turns module output on instead of being rejected next to `output.module: false`. That drops the configuration error and leaves `lib/WebpackOptionsApply.js` untouched.
1 parent c1e94fa commit a1191dd

3 files changed

Lines changed: 30 additions & 40 deletions

File tree

‎lib/WebpackOptionsApply.js‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ const { NODE_MODULES_REGEXP } = require("./util/identifier");
8282
* WebpackOptionsNormalizedWithDefaults as WebpackOptions
8383
* } from "./config/defaults"
8484
*/
85-
/** @import { LibraryType } from "../declarations/WebpackOptions" */
8685
/** @import { WebpackOptionsInterception } from "./config/normalization" */
8786
/** @import Compiler from "./Compiler" */
8887
/** @import { InputFileSystem, IntermediateFileSystem } from "./util/fs" */
@@ -387,18 +386,6 @@ class WebpackOptionsApply extends OptionsApply {
387386
/** @type {NonNullable<WebpackOptions["output"]["enabledLibraryTypes"]>} */
388387
(options.output.enabledLibraryTypes);
389388

390-
if (!options.output.module) {
391-
// The defaults turn `output.module` on for these types, so reaching here
392-
// means it was turned back off by hand.
393-
for (const type of ["module", "modern-module"]) {
394-
if (enabledLibraryTypes.includes(/** @type {LibraryType} */ (type))) {
395-
throw new Error(
396-
`library type "${type}" is only allowed when 'output.module' is enabled`
397-
);
398-
}
399-
}
400-
}
401-
402389
if (enabledLibraryTypes.length > 0) {
403390
let once = true;
404391
for (const type of enabledLibraryTypes) {

‎lib/config/defaults.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,16 +1681,20 @@ const applyOutputDefaults = (
16811681
return found;
16821682
};
16831683

1684+
// A library type that is an ECMAScript module is another spelling of
1685+
// `output.module`, so it turns it on rather than disagreeing with it.
1686+
if (hasModuleLibrary()) {
1687+
output.module = true;
1688+
}
1689+
16841690
// the universal target (web + node, neither specific), deno and bun only work
1685-
// as ESM, unless `module: false` says the target cannot read one — and so does
1686-
// a library type whose output is an ECMAScript module
1691+
// as ESM, unless `module: false` says the target cannot read one
16871692
F(output, "module", () =>
16881693
Boolean(
1689-
hasModuleLibrary() ||
1690-
(tp &&
1691-
((tp.node === null && tp.web === null && tp.module !== false) ||
1692-
tp.deno === true ||
1693-
tp.bun === true))
1694+
tp &&
1695+
((tp.node === null && tp.web === null && tp.module !== false) ||
1696+
tp.deno === true ||
1697+
tp.bun === true)
16941698
)
16951699
);
16961700

‎test/Defaults.unittest.js‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6198,26 +6198,25 @@ describe("module library types", () => {
61986198
expect(compiler.options.output.iife).toBe(false);
61996199
});
62006200

6201-
it("should reject a module library when module output is disabled", () => {
6202-
const webpack = require("..");
6201+
it("should enable module output when it was disabled by hand", () => {
6202+
const config = getDefaultConfig({
6203+
mode: "none",
6204+
entry: "./index.js",
6205+
output: { module: false, library: { type: "module" } }
6206+
});
62036207

6204-
expect(() =>
6205-
webpack({
6206-
mode: "none",
6207-
entry: "./index.js",
6208-
output: { module: false, library: { type: "module" } }
6209-
})
6210-
).toThrow(
6211-
"library type \"module\" is only allowed when 'output.module' is enabled"
6212-
);
6213-
expect(() =>
6214-
webpack({
6215-
mode: "none",
6216-
entry: "./index.js",
6217-
output: { module: false, library: { type: "modern-module" } }
6218-
})
6219-
).toThrow(
6220-
"library type \"modern-module\" is only allowed when 'output.module' is enabled"
6221-
);
6208+
expect(config.output.module).toBe(true);
6209+
expect(config.output.iife).toBe(false);
6210+
});
6211+
6212+
it("should enable module output for a modern-module library", () => {
6213+
const config = getDefaultConfig({
6214+
mode: "none",
6215+
entry: "./index.js",
6216+
output: { module: false, library: { type: "modern-module" } }
6217+
});
6218+
6219+
expect(config.output.module).toBe(true);
6220+
expect(config.output.iife).toBe(false);
62226221
});
62236222
});

0 commit comments

Comments
 (0)