Skip to content

Commit 9bfbbf1

Browse files
fix(config): let a script library type keep script output
A library type read out of a script — every type but `module` and `modern-module` — states that the output is not an ECMAScript module, so it holds the `futureDefaults` default back rather than being overridden into an ESM bundle that assigns its name to a module-scoped variable and exports nothing. An explicit `output.module` still wins either way.
1 parent b6f219a commit 9bfbbf1

6 files changed

Lines changed: 190 additions & 96 deletions

File tree

‎lib/config/defaults.js‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,33 +1660,41 @@ const applyOutputDefaults = (
16601660
};
16611661

16621662
/**
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
1663+
* Every library type the configuration states, wherever it was declared.
1664+
* @returns {LibraryType[]} the declared library types
16661665
*/
1667-
const hasModuleLibrary = () => {
1668-
if (output.library && isModuleLibraryType(output.library.type)) return true;
1666+
const declaredLibraryTypes = () => {
1667+
/** @type {LibraryType[]} */
1668+
const types = [];
1669+
if (output.library && output.library.type) types.push(output.library.type);
16691670
// 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;
1671+
// before the defaults and has pushed its type here already. `"..."` is the
1672+
// extend placeholder `A` resolves later, never a type of its own.
1673+
if (output.enabledLibraryTypes) {
1674+
for (const type of output.enabledLibraryTypes) {
1675+
if (type !== "...") types.push(type);
1676+
}
16761677
}
1677-
let found = false;
16781678
forEachEntry((desc) => {
1679-
if (desc.library && isModuleLibraryType(desc.library.type)) found = true;
1679+
if (desc.library && desc.library.type) types.push(desc.library.type);
16801680
});
1681-
return found;
1681+
return types;
16821682
};
16831683

1684+
const libraryTypes = declaredLibraryTypes();
1685+
16841686
// A library type that is an ECMAScript module is another spelling of
16851687
// `output.module`, so it turns it on rather than disagreeing with it.
1686-
if (hasModuleLibrary()) {
1688+
if (libraryTypes.some(isModuleLibraryType)) {
16871689
output.module = true;
16881690
}
16891691

1692+
// Every other library type is read out of a script, which is a statement that
1693+
// the output is not an ECMAScript module — so it holds the default below back.
1694+
const hasScriptLibrary = libraryTypes.some(
1695+
(type) => !isModuleLibraryType(type)
1696+
);
1697+
16901698
// the universal target (web + node, neither specific), deno and bun only work
16911699
// as ESM, unless `module: false` says the target cannot read one
16921700
F(output, "module", () =>
@@ -1697,7 +1705,7 @@ const applyOutputDefaults = (
16971705
tp.bun === true ||
16981706
// webpack@6 emits ESM wherever the target reads one — a browserslist
16991707
// query (or `esX`/`nodeX`) without ESM support keeps script output
1700-
(futureDefaults && tp.module !== false))
1708+
(futureDefaults && tp.module !== false && !hasScriptLibrary))
17011709
)
17021710
);
17031711

‎test/Defaults.unittest.js‎

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,14 +1976,6 @@ describe("snapshots", () => {
19761976
- "progress": false,
19771977
+ "progress": "auto",
19781978
@@ ... @@
1979-
- "dynamicImport": undefined,
1980-
- "dynamicImportInWorker": undefined,
1981-
+ "dynamicImport": true,
1982-
+ "dynamicImportInWorker": true,
1983-
@@ ... @@
1984-
- "module": undefined,
1985-
+ "module": true,
1986-
@@ ... @@
19871979
- "mode": "none",
19881980
+ "mode": "development",
19891981
@@ ... @@
@@ -1993,7 +1985,7 @@ describe("snapshots", () => {
19931985
+ Object {
19941986
+ "resourceQuery": /(\\?|&)raw(&|$)/,
19951987
+ "type": "asset/source",
1996-
@@ ... @@
1988+
+ },
19971989
+ Object {
19981990
+ "resourceQuery": /(\\?|&)url(&|$)/,
19991991
+ "type": "asset/resource",
@@ -2006,9 +1998,7 @@ describe("snapshots", () => {
20061998
+ "resourceQuery": /(\\?|&)inline(&|$)/,
20071999
+ "type": "asset/inline",
20082000
+ },
2009-
@@ ... @@
2010-
+ },
2011-
+ ],
2001+
+ ],
20122002
@@ ... @@
20132003
- "localIdentHashFunction": "md4",
20142004
+ "localIdentHashFunction": "xxhash64",
@@ -2033,9 +2023,6 @@ describe("snapshots", () => {
20332023
@@ ... @@
20342024
+ "exportsPresence": "error",
20352025
@@ ... @@
2036-
- "importMeta": true,
2037-
+ "importMeta": "preserve-unknown",
2038-
@@ ... @@
20392026
- "strictModeViolations": "warn",
20402027
+ "strictModeViolations": "error",
20412028
@@ ... @@
@@ -2064,57 +2051,29 @@ describe("snapshots", () => {
20642051
+ "minRemainingSize": 0,
20652052
@@ ... @@
20662053
- "charset": true,
2067-
- "chunkFilename": "[name].js",
2068-
- "chunkFormat": "array-push",
20692054
+ "charset": false,
2070-
+ "chunkFilename": "[name].mjs",
2071-
+ "chunkFormat": "module",
20722055
@@ ... @@
2073-
- "chunkLoading": "jsonp",
20742056
- "chunkLoadingGlobal": "webpackChunkwebpack",
2075-
+ "chunkLoading": "import",
20762057
+ "chunkLoadingGlobal": "webpackChunkmyLib",
20772058
@@ ... @@
20782059
- "devtoolNamespace": "webpack",
20792060
+ "devtoolNamespace": "myLib",
20802061
@@ ... @@
2081-
- "jsonp",
2082-
- "import-scripts",
2083-
+ "import",
20842062
+ ],
20852063
+ "enabledLibraryTypes": Array [
20862064
+ "var",
20872065
@@ ... @@
20882066
- "enabledLibraryTypes": Array [],
20892067
@@ ... @@
2090-
- "dynamicImport": undefined,
2091-
- "dynamicImportInWorker": undefined,
2092-
+ "dynamicImport": true,
2093-
+ "dynamicImportInWorker": true,
2094-
@@ ... @@
2095-
- "module": undefined,
2096-
+ "module": true,
2097-
@@ ... @@
2098-
- "filename": "[name].js",
2099-
+ "filename": "[name].mjs",
2100-
@@ ... @@
21012068
- "hashDigestLength": 20,
21022069
- "hashFunction": "md4",
21032070
+ "hashDigestLength": 16,
21042071
+ "hashFunction": "xxhash64",
21052072
@@ ... @@
2106-
- "hotUpdateChunkFilename": "[id].[fullhash].hot-update.js",
21072073
- "hotUpdateGlobal": "webpackHotUpdatewebpack",
2108-
- "hotUpdateMainFilename": "[runtime].[fullhash].hot-update.json",
2109-
+ "hotUpdateChunkFilename": "[id].[fullhash].hot-update.mjs",
21102074
+ "hotUpdateGlobal": "webpackHotUpdatemyLib",
2111-
+ "hotUpdateMainFilename": "[runtime].[fullhash].hot-update.json.mjs",
2112-
@@ ... @@
2113-
- "iife": true,
2114-
+ "iife": false,
21152075
@@ ... @@
21162076
- "library": undefined,
2117-
- "module": false,
21182077
+ "library": Object {
21192078
+ "amdContainer": undefined,
21202079
+ "auxiliaryComment": undefined,
@@ -2124,31 +2083,16 @@ describe("snapshots", () => {
21242083
+ "umdAmdContainer": undefined,
21252084
+ "umdNamedDefine": undefined,
21262085
+ },
2127-
+ "module": true,
21282086
@@ ... @@
21292087
- "pathinfo": false,
21302088
+ "pathinfo": true,
21312089
@@ ... @@
2132-
- "resourceHints": undefined,
2133-
- "scriptType": false,
2134-
+ "resourceHints": Object {
2135-
+ "dedupe": false,
2136-
+ "initial": true,
2137-
+ "modulePreloadPolyfill": false,
2138-
+ },
2139-
+ "scriptType": "module",
2140-
@@ ... @@
21412090
- "strictModuleResolution": false,
21422091
+ "strictModuleResolution": true,
21432092
@@ ... @@
21442093
- "uniqueName": "webpack",
21452094
+ "uniqueName": "myLib",
21462095
@@ ... @@
2147-
- "workerChunkFilename": "[name].js",
2148-
- "workerChunkLoading": "import-scripts",
2149-
+ "workerChunkFilename": "[name].mjs",
2150-
+ "workerChunkLoading": "import",
2151-
@@ ... @@
21522096
- "production",
21532097
+ "development",
21542098
@@ ... @@
@@ -6544,6 +6488,74 @@ describe("futureDefaults module output", () => {
65446488
).toBe(true);
65456489
});
65466490

6491+
it("should keep script output for a declared script library type", () => {
6492+
for (const library of [
6493+
{ type: /** @type {"var"} */ ("var"), name: "myLib" },
6494+
{ type: /** @type {"umd"} */ ("umd"), name: "myLib" },
6495+
{ type: /** @type {"commonjs2"} */ ("commonjs2") }
6496+
]) {
6497+
const config = getDefaultConfig({
6498+
mode: "none",
6499+
output: { library },
6500+
experiments: { futureDefaults: true }
6501+
});
6502+
6503+
expect(config.output.module).toBe(false);
6504+
expect(config.output.iife).toBe(true);
6505+
}
6506+
});
6507+
6508+
it("should keep script output for a library type declared by an entry", () => {
6509+
const config = getDefaultConfig({
6510+
mode: "none",
6511+
entry: {
6512+
main: { import: "./index.js", library: { type: "var", name: "myLib" } }
6513+
},
6514+
experiments: { futureDefaults: true }
6515+
});
6516+
6517+
expect(config.output.module).toBe(false);
6518+
});
6519+
6520+
it("should keep script output for a library type declared by a plugin", () => {
6521+
const webpack = require("..");
6522+
6523+
const compiler = webpack({
6524+
mode: "none",
6525+
entry: "./index.js",
6526+
experiments: { futureDefaults: true },
6527+
plugins: [
6528+
new webpack.container.ModuleFederationPlugin({
6529+
name: "container",
6530+
library: { type: "var", name: "container" },
6531+
exposes: { "./a": "./index.js" }
6532+
})
6533+
]
6534+
});
6535+
6536+
expect(compiler.options.output.module).toBe(false);
6537+
});
6538+
6539+
it("should still emit an ECMAScript module for a module library type", () => {
6540+
expect(
6541+
getDefaultConfig({
6542+
mode: "none",
6543+
output: { library: { type: "module" } },
6544+
experiments: { futureDefaults: true }
6545+
}).output.module
6546+
).toBe(true);
6547+
});
6548+
6549+
it("should let an explicit output.module win over a script library type", () => {
6550+
expect(
6551+
getDefaultConfig({
6552+
mode: "none",
6553+
output: { module: true, library: { type: "var", name: "myLib" } },
6554+
experiments: { futureDefaults: true }
6555+
}).output.module
6556+
).toBe(true);
6557+
});
6558+
65476559
it("should keep script output without futureDefaults", () => {
65486560
expect(
65496561
getDefaultConfig({ mode: "none", target: "browserslist: chrome 100" })
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const answer = 42;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { answer } from "./dep";
2+
3+
export default answer;
4+
5+
it("should stay a script under futureDefaults", () => {
6+
expect(answer).toBe(42);
7+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
3+
/** @import { Compilation, Compiler, Configuration } from "../../../../" */
4+
5+
/**
6+
* A library type read out of a script says the output is not an ECMAScript
7+
* module, so it holds the `futureDefaults` module default back.
8+
* @this {Compiler}
9+
* @returns {void}
10+
*/
11+
function expectScriptOutput() {
12+
/**
13+
* @param {Compilation} compilation compilation
14+
*/
15+
const handler = (compilation) => {
16+
expect(compilation.outputOptions.module).toBe(false);
17+
expect(compilation.outputOptions.iife).toBe(true);
18+
};
19+
this.hooks.compilation.tap("testcase", handler);
20+
}
21+
22+
/**
23+
* @param {import("../../../../").LibraryType} type library type declared on `output`
24+
* @returns {Configuration} configuration
25+
*/
26+
const outputLibrary = (type) => ({
27+
mode: "development",
28+
entry: "./index.js",
29+
experiments: { futureDefaults: true },
30+
output: { library: { type, name: "myLib" } },
31+
plugins: [expectScriptOutput]
32+
});
33+
34+
/** @type {Configuration[]} */
35+
module.exports = [
36+
outputLibrary("var"),
37+
outputLibrary("umd"),
38+
{
39+
mode: "development",
40+
entry: {
41+
main: {
42+
import: "./index.js",
43+
library: { type: "var", name: "myLib" }
44+
}
45+
},
46+
experiments: { futureDefaults: true },
47+
plugins: [expectScriptOutput]
48+
}
49+
];

0 commit comments

Comments
 (0)