Skip to content

Commit e2988be

Browse files
fix(config): default output.module to true for module library types (#22071)
* fix(config): default output.module to true for module library types A `module` or `modern-module` library type only exists as an ECMAScript module, but since the `experiments.outputModule` removal nothing turned `output.module` on for it — so `output.iife` stayed true and the emitted `export` statement landed inside an IIFE, which no engine or minifier parses. `output.module` now defaults to true wherever such a library type is declared: `output.library`, `output.libraryTarget`, an entry's `library`, and `output.enabledLibraryTypes` as `ModuleFederationPlugin` leaves it. Disabling `output.module` by hand alongside one is rejected with the error the experiment check used to raise. * test(library): cover the module library types the harness reads The test runner reads the config as written, so its ESM dispatch has to mirror every place the default reads a module library type from — `output.enabledLibraryTypes` was missing. The entry-level case now covers `modern-module` next to `module`, and the changeset moves back out of the pending `experiments.outputModule` entry into one of its own. * 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 e5ad5fb commit e2988be

8 files changed

Lines changed: 216 additions & 15 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Enable `output.module` for `module` and `modern-module` library types.

‎lib/config/defaults.js‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ const A = (obj, prop, factory) => {
303303
}
304304
};
305305

306+
/**
307+
* Whether the library type emits an ECMAScript module, which only `output.module`
308+
* output can carry — every other type is written into a script.
309+
* @param {LibraryType | undefined} type library type
310+
* @returns {boolean} true, when the type is only emitted as an ECMAScript module
311+
*/
312+
const isModuleLibraryType = (type) =>
313+
type === "module" || type === "modern-module";
314+
306315
/**
307316
* Apply webpack options base defaults.
308317
* @param {WebpackOptionsNormalized} options options to be modified
@@ -419,9 +428,7 @@ const applyWebpackOptionsDefaults = (options, compilerIndex) => {
419428
// is read by another bundler, where an eval wrapper hides `import.meta` from it.
420429
const noDevSourceMap = /** @type {LibraryType[]} */ (
421430
options.output.enabledLibraryTypes
422-
).some(
423-
(type) => futureDefaults || type === "module" || type === "modern-module"
424-
);
431+
).some((type) => futureDefaults || isModuleLibraryType(type));
425432

426433
F(
427434
options,
@@ -1641,6 +1648,45 @@ const applyOutputDefaults = (
16411648
}
16421649
});
16431650

1651+
/**
1652+
* Processes the provided fn.
1653+
* @param {(entryDescription: EntryDescription) => void} fn iterator
1654+
* @returns {void}
1655+
*/
1656+
const forEachEntry = (fn) => {
1657+
for (const name of Object.keys(entry)) {
1658+
fn(/** @type {{ [k: string]: EntryDescription }} */ (entry)[name]);
1659+
}
1660+
};
1661+
1662+
/**
1663+
* Whether a library type is asked for that can only be emitted as an
1664+
* ECMAScript module, wherever it was declared.
1665+
* @returns {boolean} true, when an ESM-only library type is configured
1666+
*/
1667+
const hasModuleLibrary = () => {
1668+
if (output.library && isModuleLibraryType(output.library.type)) return true;
1669+
// A plugin declaring its own library (`ModuleFederationPlugin`) is applied
1670+
// before the defaults and has pushed its type here already.
1671+
if (
1672+
output.enabledLibraryTypes &&
1673+
output.enabledLibraryTypes.some(isModuleLibraryType)
1674+
) {
1675+
return true;
1676+
}
1677+
let found = false;
1678+
forEachEntry((desc) => {
1679+
if (desc.library && isModuleLibraryType(desc.library.type)) found = true;
1680+
});
1681+
return found;
1682+
};
1683+
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+
16441690
// the universal target (web + node, neither specific), deno and bun only work
16451691
// as ESM, unless `module: false` says the target cannot read one
16461692
F(output, "module", () =>
@@ -2123,16 +2169,6 @@ const applyOutputDefaults = (
21232169
D(trustedTypes, "onPolicyCreationFailure", "stop");
21242170
}
21252171

2126-
/**
2127-
* Processes the provided fn.
2128-
* @param {(entryDescription: EntryDescription) => void} fn iterator
2129-
* @returns {void}
2130-
*/
2131-
const forEachEntry = (fn) => {
2132-
for (const name of Object.keys(entry)) {
2133-
fn(/** @type {{ [k: string]: EntryDescription }} */ (entry)[name]);
2134-
}
2135-
};
21362172
A(output, "enabledLibraryTypes", () => {
21372173
/** @type {LibraryType[]} */
21382174
const enabledLibraryTypes = [];

‎test/Defaults.unittest.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6177,3 +6177,46 @@ describe("optimization.minimize", () => {
61776177
});
61786178
});
61796179
});
6180+
6181+
describe("module library types", () => {
6182+
it("should enable module output for a library declared by a plugin", () => {
6183+
const webpack = require("..");
6184+
6185+
const compiler = webpack({
6186+
mode: "none",
6187+
entry: "./index.js",
6188+
plugins: [
6189+
new webpack.container.ModuleFederationPlugin({
6190+
name: "container",
6191+
library: { type: "module" },
6192+
exposes: { "./a": "./index.js" }
6193+
})
6194+
]
6195+
});
6196+
6197+
expect(compiler.options.output.module).toBe(true);
6198+
expect(compiler.options.output.iife).toBe(false);
6199+
});
6200+
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+
});
6207+
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);
6221+
});
6222+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const answer = 42;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { answer } from "./dep";
2+
3+
export default answer;
4+
export const name = "module-library";
5+
6+
it("should run as an ECMAScript module without an explicit 'output.module'", () => {
7+
expect(answer).toBe(42);
8+
expect(name).toBe("module-library");
9+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
const BUNDLES = [
4+
["module.mjs"],
5+
["modern-module.mjs"],
6+
["entry-module.mjs", "entry-modern-module.mjs"]
7+
];
8+
9+
module.exports = {
10+
findBundle(i) {
11+
return BUNDLES[i].map((name) => `./${name}`);
12+
}
13+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict";
2+
3+
/** @import { Compilation, Compiler, Configuration } from "../../../../" */
4+
5+
/**
6+
* A library type that is an ECMAScript module turns `output.module` on, which in
7+
* turn drops the IIFE an `export` statement cannot live in.
8+
* @this {Compiler}
9+
* @returns {void}
10+
*/
11+
function expectModuleOutput() {
12+
/**
13+
* @param {Compilation} compilation compilation
14+
*/
15+
const handler = (compilation) => {
16+
expect(compilation.outputOptions.module).toBe(true);
17+
expect(compilation.outputOptions.iife).toBe(false);
18+
};
19+
this.hooks.compilation.tap("testcase", handler);
20+
}
21+
22+
/**
23+
* @param {string} name emitted bundle base name
24+
* @param {"module" | "modern-module"} type library type declared on `output`
25+
* @returns {Configuration} configuration
26+
*/
27+
const outputLibrary = (name, type) => ({
28+
mode: "development",
29+
entry: "./index.js",
30+
output: {
31+
filename: `${name}.mjs`,
32+
library: { type }
33+
},
34+
plugins: [expectModuleOutput]
35+
});
36+
37+
/** @type {Configuration[]} */
38+
module.exports = [
39+
outputLibrary("module", "module"),
40+
outputLibrary("modern-module", "modern-module"),
41+
{
42+
mode: "development",
43+
entry: {
44+
"entry-module": {
45+
import: "./index.js",
46+
library: { type: "module" }
47+
},
48+
"entry-modern-module": {
49+
import: "./index.js",
50+
library: { type: "modern-module" }
51+
}
52+
},
53+
output: { filename: "[name].mjs" },
54+
plugins: [expectModuleOutput]
55+
}
56+
];

‎test/harness/runner/index.js‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ const rewriteImportMeta = require("./rewriteImportMeta");
2222

2323
const [major] = getNodeVersion();
2424

25+
/**
26+
* Mirrors the library types `lib/config/defaults.js` turns `output.module` on for.
27+
* @param {string | undefined} type library type
28+
* @returns {boolean} true, when the type is only emitted as an ECMAScript module
29+
*/
30+
const isModuleLibraryType = (type) =>
31+
type === "module" || type === "modern-module";
32+
2533
/**
2634
* @typedef {object} TestMeta
2735
* @property {string} category
@@ -111,6 +119,37 @@ class TestRunner {
111119
this._moduleRunners = this.createModuleRunners();
112120
}
113121

122+
/**
123+
* Whether the bundle is an ECMAScript module. The runner reads the config as
124+
* written, so it has to read the library types `output.module` defaults from.
125+
* @param {EXPECTED_ANY} webpackOptions webpack options
126+
* @returns {boolean} whether the output is an ECMAScript module
127+
*/
128+
static isModuleOutput(webpackOptions) {
129+
const output = webpackOptions.output || {};
130+
if (output.module) return true;
131+
if (isModuleLibraryType(output.libraryTarget)) return true;
132+
if (output.library && isModuleLibraryType(output.library.type)) return true;
133+
if (
134+
output.enabledLibraryTypes &&
135+
output.enabledLibraryTypes.some(isModuleLibraryType)
136+
) {
137+
return true;
138+
}
139+
const { entry } = webpackOptions;
140+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
141+
return false;
142+
}
143+
return Object.keys(entry).some((name) => {
144+
const description = entry[name];
145+
return Boolean(
146+
description &&
147+
description.library &&
148+
isModuleLibraryType(description.library.type)
149+
);
150+
});
151+
}
152+
114153
/**
115154
* Whether the target is universal, i.e. its merged platform is neither
116155
* node- nor web-specific (the `"universal"` preset, or arrays like
@@ -480,8 +519,7 @@ class TestRunner {
480519
}
481520
if (
482521
modulePath.endsWith(".mjs") &&
483-
this.webpackOptions.output &&
484-
this.webpackOptions.output.module
522+
TestRunner.isModuleOutput(this.webpackOptions)
485523
) {
486524
return this._moduleRunners.esm(moduleInfo, context);
487525
}

0 commit comments

Comments
 (0)