Skip to content

Commit e0d04f2

Browse files
fix(config): default output.module to true when using module library type
When output.library.type (or an entry library.type) is set to 'module' or 'modern-module', output.module must default to true so that webpack does not emit export statements wrapped inside an IIFE. Also throws a descriptive configuration error if output.module is explicitly set to false with a module library type. Fixes #22061.
1 parent 3a69d29 commit e0d04f2

9 files changed

Lines changed: 171 additions & 18 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+
Default `output.module` to `true` when using `library.type: "module"` or `"modern-module"`, and throw if `output.module` is explicitly disabled.

‎lib/WebpackOptionsApply.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,19 @@ class WebpackOptionsApply extends OptionsApply {
502502
sideEffectFree: options.experiments.futureDefaults
503503
}).apply(compiler);
504504

505+
if (!options.output.module) {
506+
if (options.output.enabledLibraryTypes.includes("module")) {
507+
throw new Error(
508+
"library type \"module\" is only allowed when 'output.module' is enabled"
509+
);
510+
}
511+
if (options.output.enabledLibraryTypes.includes("modern-module")) {
512+
throw new Error(
513+
"library type \"modern-module\" is only allowed when 'output.module' is enabled"
514+
);
515+
}
516+
}
517+
505518
if (options.output.module) {
506519
// Analyzable output is ESM output, so this is every build that can reserve a name.
507520
const RuntimeTemplate = require("./RuntimeTemplate");

‎lib/config/defaults.js‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,14 +1640,47 @@ const applyOutputDefaults = (
16401640
}
16411641
});
16421642

1643-
// the universal target (web + node, neither specific), deno and bun only work
1644-
// as ESM, unless `module: false` says the target cannot read one
1643+
/**
1644+
* Processes the provided fn.
1645+
* @param {(entryDescription: EntryDescription) => void} fn iterator
1646+
* @returns {void}
1647+
*/
1648+
const forEachEntry = (fn) => {
1649+
for (const name of Object.keys(entry)) {
1650+
fn(/** @type {{ [k: string]: EntryDescription }} */ (entry)[name]);
1651+
}
1652+
};
1653+
1654+
const hasModuleLibrary = () => {
1655+
if (
1656+
output.library &&
1657+
(output.library.type === "module" ||
1658+
output.library.type === "modern-module")
1659+
) {
1660+
return true;
1661+
}
1662+
let hasModule = false;
1663+
forEachEntry((desc) => {
1664+
if (
1665+
desc.library &&
1666+
(desc.library.type === "module" ||
1667+
desc.library.type === "modern-module")
1668+
) {
1669+
hasModule = true;
1670+
}
1671+
});
1672+
return hasModule;
1673+
};
1674+
1675+
// the universal target (web + node, neither specific), deno, bun and module
1676+
// libraries only work as ESM, unless `module: false` says the target cannot read one
16451677
F(output, "module", () =>
16461678
Boolean(
1647-
tp &&
1648-
((tp.node === null && tp.web === null && tp.module !== false) ||
1649-
tp.deno === true ||
1650-
tp.bun === true)
1679+
hasModuleLibrary() ||
1680+
(tp &&
1681+
((tp.node === null && tp.web === null && tp.module !== false) ||
1682+
tp.deno === true ||
1683+
tp.bun === true))
16511684
)
16521685
);
16531686

@@ -2122,16 +2155,6 @@ const applyOutputDefaults = (
21222155
D(trustedTypes, "onPolicyCreationFailure", "stop");
21232156
}
21242157

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

‎test/Defaults.unittest.js‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6175,4 +6175,83 @@ describe("optimization.minimize", () => {
61756175
html: false
61766176
});
61776177
});
6178+
6179+
describe("library module output defaults", () => {
6180+
it("should default output.module to true when library.type is module", () => {
6181+
const config = getDefaultConfig({
6182+
mode: "none",
6183+
output: {
6184+
library: {
6185+
type: "module"
6186+
}
6187+
}
6188+
});
6189+
expect(config.output.module).toBe(true);
6190+
});
6191+
6192+
it("should default output.module to true when library.type is modern-module", () => {
6193+
const config = getDefaultConfig({
6194+
mode: "none",
6195+
output: {
6196+
library: {
6197+
type: "modern-module"
6198+
}
6199+
}
6200+
});
6201+
expect(config.output.module).toBe(true);
6202+
});
6203+
6204+
it("should default output.module to true when entry library.type is module", () => {
6205+
const config = getDefaultConfig({
6206+
mode: "none",
6207+
entry: {
6208+
main: {
6209+
import: "./src",
6210+
library: {
6211+
type: "module"
6212+
}
6213+
}
6214+
}
6215+
});
6216+
expect(config.output.module).toBe(true);
6217+
});
6218+
6219+
it("should throw when library.type is module and output.module is false", () => {
6220+
const webpack = require("..");
6221+
6222+
expect(() => {
6223+
webpack({
6224+
mode: "none",
6225+
entry: "./src",
6226+
output: {
6227+
module: false,
6228+
library: {
6229+
type: "module"
6230+
}
6231+
}
6232+
});
6233+
}).toThrow(
6234+
"library type \"module\" is only allowed when 'output.module' is enabled"
6235+
);
6236+
});
6237+
6238+
it("should throw when library.type is modern-module and output.module is false", () => {
6239+
const webpack = require("..");
6240+
6241+
expect(() => {
6242+
webpack({
6243+
mode: "none",
6244+
entry: "./src",
6245+
output: {
6246+
module: false,
6247+
library: {
6248+
type: "modern-module"
6249+
}
6250+
}
6251+
});
6252+
}).toThrow(
6253+
"library type \"modern-module\" is only allowed when 'output.module' is enabled"
6254+
);
6255+
});
6256+
});
61786257
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import value from "./dep";
2+
3+
export default value;
4+
export const name = "module-lib";
5+
6+
it("should run as an ES module when library.type is module without explicit output.module", () => {
7+
expect(value).toBe(42);
8+
expect(name).toBe("module-lib");
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
module.exports = {
4+
findBundle() {
5+
return ["main.mjs"];
6+
}
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
output: {
7+
filename: "[name].mjs",
8+
library: {
9+
type: "module"
10+
}
11+
}
12+
};

‎test/harness/runner/index.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,12 @@ class TestRunner {
480480
}
481481
if (
482482
modulePath.endsWith(".mjs") &&
483-
this.webpackOptions.output &&
484-
this.webpackOptions.output.module
483+
((this.webpackOptions.output &&
484+
(this.webpackOptions.output.module ||
485+
(this.webpackOptions.output.library &&
486+
(this.webpackOptions.output.library.type === "module" ||
487+
this.webpackOptions.output.library.type === "modern-module")))) ||
488+
TestRunner.isUniversalTarget(this.webpackOptions))
485489
) {
486490
return this._moduleRunners.esm(moduleInfo, context);
487491
}

0 commit comments

Comments
 (0)