Skip to content

Commit be915c3

Browse files
fix: use configured external targets in ESM star reexports (#22079)
* fix: use configured external targets in ESM star reexports * fix(esm): reject a star reexport of an external with a property path An external request array is a module specifier plus a property path, so `export * from` the first element reexports the module instead of the property the external names. Report it from `finishEntryModule`, which runs on every build, rather than from rendering, whose asset the persistent cache restores. * chore(html): fold the duplicate Chunk type import `webpack/no-duplicate-import-tag` from eslint-config-webpack 4.12.0 fails `lint` on the two separate `@import`s of `../Chunk`. --------- Co-authored-by: Alexander Akait <[email protected]>
1 parent 0f7ee78 commit be915c3

15 files changed

Lines changed: 236 additions & 3 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+
Use configured external requests in ESM star reexports; reject property paths.

‎lib/ExternalModule.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,17 @@ class ExternalModule extends Module {
12391239
return this._resolveExternalType(this.externalType) !== "module";
12401240
}
12411241

1242+
/**
1243+
* The request this external resolves to for its own type, with the record form
1244+
* narrowed to its entry. An array is a module specifier plus a property path.
1245+
* @returns {string | string[] | undefined} the resolved request
1246+
*/
1247+
getResolvedRequest() {
1248+
return /** @type {string | string[] | undefined} */ (
1249+
this._getRequestAndExternalType().request
1250+
);
1251+
}
1252+
12421253
/**
12431254
* Get request and external type.
12441255
* @private
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Alexander Akait @alexander-akait
4+
*/
5+
6+
"use strict";
7+
8+
const WebpackError = require("./WebpackError");
9+
10+
/** @import ExternalModule from "../ExternalModule" */
11+
/** @import Module from "../Module" */
12+
13+
/**
14+
* Error raised when a `module` library reexports everything from an external
15+
* whose request names a property path instead of a module.
16+
*/
17+
class ExternalStarReexportError extends WebpackError {
18+
/**
19+
* Captures the external and the library entry reexporting from it.
20+
* @param {ExternalModule} externalModule the reexported external
21+
* @param {Module} module the library entry module
22+
*/
23+
constructor(externalModule, module) {
24+
const request = /** @type {string[]} */ (
25+
externalModule.getResolvedRequest()
26+
);
27+
super(
28+
`Can't 'export * from ${JSON.stringify(
29+
externalModule.userRequest
30+
)}': the external is configured as ${JSON.stringify(
31+
request
32+
)}, which is a property of ${JSON.stringify(
33+
request[0]
34+
)} and has no module specifier to reexport. Reexport the names explicitly or configure the external without a property path.`
35+
);
36+
37+
/** @type {string} */
38+
this.name = "ExternalStarReexportError";
39+
/** @type {Module} */
40+
this.module = module;
41+
}
42+
}
43+
44+
module.exports = ExternalStarReexportError;

‎lib/html/HtmlModulesPlugin.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ const getCssModulesPlugin = memoize(() => require("../css/CssModulesPlugin"));
7373
/** @typedef {import("../../declarations/WebpackOptions").OutputHtmlOptions["favicon"]} FaviconOption */
7474
/** @typedef {import("../../declarations/WebpackOptions").OutputHtmlOptions["manifest"]} ManifestOption */
7575
/** @typedef {Exclude<HtmlFaviconIcon, string>} FaviconIcon */
76-
/** @import Chunk from "../Chunk" */
76+
/** @import Chunk, { ChunkFilenameTemplate } from "../Chunk" */
7777
/** @import ChunkGraph from "../ChunkGraph" */
7878
/** @import Compiler from "../Compiler" */
7979
/** @import Module from "../Module" */
80-
/** @import { ChunkFilenameTemplate } from "../Chunk" */
8180
/** @typedef {import("../Compilation").EntryData} EntryData */
8281
/** @import { HtmlModuleBuildInfo } from "./HtmlModule" */
8382
/** @import HtmlParser from "./HtmlParser" */

‎lib/library/ModuleLibraryPlugin.js‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Template = require("../Template");
1414
const HarmonyExportExpressionDependency = require("../dependencies/HarmonyExportExpressionDependency");
1515
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
1616
const HarmonyExportSpecifierDependency = require("../dependencies/HarmonyExportSpecifierDependency");
17+
const ExternalStarReexportError = require("../errors/ExternalStarReexportError");
1718
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
1819
const ConcatenatedModule = require("../optimize/ConcatenatedModule");
1920
const { InlinedUsedName } = require("../optimize/InlineExports");
@@ -368,6 +369,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
368369
}
369370
}
370371
}
372+
this._checkStarReexportedExternals(module, compilation);
371373
moduleGraph.addExtraReason(module, "used as library export");
372374
let entries = libraryEntryModules.get(compilation);
373375
if (entries === undefined) {
@@ -377,6 +379,61 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
377379
entries.add(module);
378380
}
379381

382+
/**
383+
* Reports every external the entry star-reexports whose request names a property
384+
* path, which `export *` has no way to reference.
385+
* @param {Module} module the library entry module
386+
* @param {Compilation} compilation the compilation
387+
* @returns {void}
388+
*/
389+
_checkStarReexportedExternals(module, compilation) {
390+
const { moduleGraph } = compilation;
391+
/** @type {Set<Module>} */
392+
const visitedReexportModules = new Set([module]);
393+
/** @type {Module[]} */
394+
const queue = [module];
395+
/** @type {Set<ExternalModule>} */
396+
const unsupportedExternals = new Set();
397+
398+
for (const current of queue) {
399+
for (const connection of moduleGraph.getOutgoingConnections(current)) {
400+
const dep = connection.dependency;
401+
402+
// Only handle star-reexport statement
403+
if (
404+
!(dep instanceof HarmonyExportImportedSpecifierDependency) ||
405+
dep.name !== null
406+
) {
407+
continue;
408+
}
409+
const importedModule = connection.resolvedModule;
410+
411+
// Only a module providing unknown exports keeps its reexport rendered
412+
if (
413+
moduleGraph.getExportsInfo(importedModule)._otherExportsInfo
414+
.provided !== null
415+
) {
416+
continue;
417+
}
418+
if (importedModule instanceof ExternalModule) {
419+
const request = importedModule.getResolvedRequest();
420+
if (Array.isArray(request) && request.length > 1) {
421+
unsupportedExternals.add(importedModule);
422+
}
423+
} else if (!visitedReexportModules.has(importedModule)) {
424+
visitedReexportModules.add(importedModule);
425+
queue.push(importedModule);
426+
}
427+
}
428+
}
429+
430+
for (const externalModule of unsupportedExternals) {
431+
compilation.errors.push(
432+
new ExternalStarReexportError(externalModule, module)
433+
);
434+
}
435+
}
436+
380437
/**
381438
* Returns preprocess as needed by overriding.
382439
* @param {LibraryOptions} library normalized library option
@@ -449,7 +506,14 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
449506
) {
450507
// Handle export * from 'external'
451508
if (importedModule instanceof ExternalModule) {
452-
moduleRequests.add(importedModule.userRequest);
509+
const request = importedModule.getResolvedRequest();
510+
// A property path is reported by `_checkStarReexportedExternals`,
511+
// and an absent one by the external's own build
512+
if (typeof request === "string") {
513+
moduleRequests.add(request);
514+
} else if (Array.isArray(request) && request.length === 1) {
515+
moduleRequests.add(request[0]);
516+
}
453517
} else if (!visitedReexportModules.has(importedModule)) {
454518
visitedReexportModules.add(importedModule);
455519
resolveDynamicStarReexport(importedModule, true);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[
5+
/Can't 'export \* from "property-path"'/,
6+
/configured as \["nested-module","inner"\]/
7+
]
8+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as library from "./library.mjs";
2+
3+
it("should reexport the external without a property path", () => {
4+
expect(library.value).toBe(1);
5+
expect(Object.keys(library)).toEqual(["value"]);
6+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "property-path";
2+
export * from "plain-alias";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
module.exports = {
4+
/**
5+
* @returns {string} emitted test bundle
6+
*/
7+
findBundle() {
8+
return "main.mjs";
9+
},
10+
modules: {
11+
"actual-module": { value: 1 },
12+
"nested-module": { inner: { innerValue: 2 } }
13+
}
14+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "development",
6+
entry: {
7+
main: "./index.js",
8+
library: { import: "./library.js", library: { type: "module" } }
9+
},
10+
output: { module: true, filename: "[name].mjs" },
11+
externalsType: "module",
12+
externals: {
13+
"./library.mjs": "./library.mjs",
14+
"plain-alias": "actual-module",
15+
"property-path": ["nested-module", "inner"]
16+
},
17+
optimization: { minimize: false }
18+
};

0 commit comments

Comments
 (0)