Skip to content

Commit 0027179

Browse files
fix(externals): keep a core module external as the code spells it when node: is supported (#22593)
* fix(externals): keep a core module external as the code spells it when `node:` is supported #22565 also prefixed a bare core module external with `node:` whenever `output.environment.nodePrefixForCoreModules` was on. An unversioned `node` target turns that option on, so bundles run on Node 12 required `node:worker_threads` and failed. A bare external now stays bare again; only a universal bundle gains the prefix. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * fix(css): read a block owner's at-keyword through `_atKeywordOf` when ordering declarations Ported from #22586: main throws minifying an at-rule block of several declarations, which fails this branch's lint, unit and CSS checks. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 135a87b commit 0027179

10 files changed

Lines changed: 24 additions & 26 deletions

File tree

‎.changeset/030-node-prefix-external-module-id.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Name a core module external by its key whichever spelling the code uses, and write it with `node:` when `output.environment.nodePrefixForCoreModules` is on.
5+
Name a core module external by its key whichever spelling the code uses.

‎lib/externals/ExternalModule.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ const NODE_RESOLVING_EXTERNAL_TYPES = new Set([
152152
]);
153153

154154
/**
155-
* Normalizes a node.js core module specifier for the target: adds `node:` where
156-
* `output.environment.nodePrefixForCoreModules` is on or the bundle is universal,
157-
* strips it where that is off, and keeps the author's notation when it is unset.
155+
* Normalizes a node.js core module specifier for the target. Strips `node:`
156+
* when the target can't resolve a prefixed request, adds `node:` for a universal
157+
* bundle (which may run on deno/bun where the prefix is required), and otherwise
158+
* keeps the author's original notation.
158159
* @param {string | string[]} request the external request
159160
* @param {ExternalsType} externalType the resolved external type
160161
* @param {RuntimeTemplate} runtimeTemplate the runtime template
@@ -175,17 +176,14 @@ const normalizeNodeCoreModuleRequest = (
175176
const withName = (next) =>
176177
Array.isArray(request) ? [next, ...request.slice(1)] : next;
177178

178-
const prefix =
179-
runtimeTemplate.outputOptions.environment.nodePrefixForCoreModules;
180-
if (prefix === false) {
179+
if (
180+
runtimeTemplate.outputOptions.environment.nodePrefixForCoreModules === false
181+
) {
181182
if (!name.startsWith(NODE_PREFIX)) return request;
182183
const stripped = name.slice(NODE_PREFIX.length);
183184
return coreModules.has(stripped) ? withName(stripped) : request;
184185
}
185-
if (
186-
(prefix || runtimeTemplate.isUniversalTarget()) &&
187-
!name.startsWith(NODE_PREFIX)
188-
) {
186+
if (runtimeTemplate.isUniversalTarget() && !name.startsWith(NODE_PREFIX)) {
189187
return coreModules.has(name) ? withName(NODE_PREFIX + name) : request;
190188
}
191189
return request;

‎test/configCases/chunks-order/cjs/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports.modules = {
1515
1616
1717
;// external \\"fs\\"
18-
const external_fs_namespaceObject = require(\\"node:fs\\");
18+
const external_fs_namespaceObject = require(\\"fs\\");
1919
var external_fs_default = /*#__PURE__*/__webpack_require__.n(external_fs_namespaceObject);
2020
// EXTERNAL MODULE: ./dependency.js
2121
var dependency = __webpack_require__(784);

‎test/configCases/chunks-order/cjs/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports.modules = {
1515
1616
1717
;// external \\"fs\\"
18-
const external_fs_namespaceObject = require(\\"node:fs\\");
18+
const external_fs_namespaceObject = require(\\"fs\\");
1919
var external_fs_default = /*#__PURE__*/__webpack_require__.n(external_fs_namespaceObject);
2020
// EXTERNAL MODULE: ./dependency.js
2121
var dependency = __webpack_require__(784);

‎test/configCases/concatenate-modules/commonjs-wrapped-external-new-expression/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-wrapped-external-new-
5454
5555
// MODULE: external \\"events\\"
5656
var external_events_namespaceFn = /*#__PURE__*/__webpack_require__.cw((module) => {
57-
module.exports = require(\\"node:events\\");
57+
module.exports = require(\\"events\\");
5858
});
5959
6060
// MODULE: ./mid.cjs
@@ -161,7 +161,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-wrapped-external-new-
161161
162162
// MODULE: external \\"events\\"
163163
var external_events_namespaceFn = /*#__PURE__*/__webpack_require__.cw((module) => {
164-
module.exports = require(\\"node:events\\");
164+
module.exports = require(\\"events\\");
165165
});
166166
167167
// MODULE: ./mid.cjs
@@ -268,7 +268,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-wrapped-external-new-
268268
269269
// MODULE: external \\"events\\"
270270
var external_events_namespaceFn = /*#__PURE__*/__webpack_require__.cw((module) => {
271-
module.exports = require(\\"node:events\\");
271+
module.exports = require(\\"events\\");
272272
});
273273
274274
// MODULE: ./mid.cjs
@@ -375,7 +375,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-wrapped-external-new-
375375
376376
// MODULE: external \\"events\\"
377377
var external_events_namespaceFn = /*#__PURE__*/__webpack_require__.cw((module) => {
378-
module.exports = require(\\"node:events\\");
378+
module.exports = require(\\"events\\");
379379
});
380380
381381
// MODULE: ./mid.cjs

‎test/configCases/concatenate-modules/commonjs-wrapped-external-new-expression/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports[`ConfigTestCases concatenate-modules commonjs-wrapped-external-new-expre
5454
5555
// MODULE: external \\"events\\"
5656
var external_events_namespaceFn = /*#__PURE__*/__webpack_require__.cw((module) => {
57-
module.exports = require(\\"node:events\\");
57+
module.exports = require(\\"events\\");
5858
});
5959
6060
// MODULE: ./mid.cjs

‎test/configCases/externals/node-prefix-user-externals/index.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ if (__STATS_I__ === 0) {
3030
expect(source).toContain(ext(prefixed("path")));
3131
});
3232
} else {
33-
it("should add `node:` to a bare external value when supported", () => {
34-
expect(source).toContain(ext(prefixed("fs")));
35-
expect(source).toContain(ext(prefixed("path")));
36-
expect(source).not.toContain(ext("fs"));
37-
expect(source).not.toContain(ext("path"));
33+
it("should keep a bare external value bare when supported", () => {
34+
expect(source).toContain(ext("fs"));
35+
expect(source).toContain(ext("path"));
36+
expect(source).not.toContain(ext(prefixed("fs")));
37+
expect(source).not.toContain(ext(prefixed("path")));
3838
});
3939
}

‎test/configCases/externals/node-prefix-user-externals/webpack.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = [
2929
}
3030
},
3131
{
32-
name: "add",
32+
name: "keep-bare",
3333
target: "web",
3434
externals: { fs: "commonjs fs", path: "commonjs path" },
3535
output: {

‎test/configCases/node/output-module-external/import.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ it("should load node builtins via dynamic import", async () => {
162162
for (const [request, imported] of Object.entries(builtinImports)) {
163163
expect(imported).toBeDefined();
164164
expect(content).toMatch(
165-
new RegExp(`import\\(\\s*["']node:${escapeRegExp(request)}["']\\s*\\)`)
165+
new RegExp(`import\\(\\s*["']${escapeRegExp(request)}["']\\s*\\)`)
166166
);
167167
}
168168

‎test/configCases/node/prefix-in-runtime/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ it(`should have/have not 'node:' prefix ${__filename}`, () => {
44
const content = fs.readFileSync(__filename, "utf-8");
55

66
if (/bundle7\.js$/.test(__filename)) {
7-
expect(content).toContain("require(\"node:fs\");");
7+
expect(content).toContain("require(\"fs\");");
88
} else if (/(bundle1\.mjs|bundle3\.mjs|bundle6\.mjs)$/.test(__filename)) {
99
expect(content).toContain("from \"url\"");
1010
} else {

0 commit comments

Comments
 (0)