Skip to content

Commit c01042d

Browse files
authored
refactor: render star reexports of externals from code generation data (#22190)
1 parent 5d23e24 commit c01042d

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

‎lib/library/ModuleLibraryPlugin.js‎

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const ConcatenatedModule = require("../optimize/ConcatenatedModule");
1616
const { InlinedUsedName } = require("../optimize/InlineExports");
1717
const RuntimeGlobals = require("../runtime/RuntimeGlobals");
1818
const Template = require("../template/Template");
19-
const { toJsStringLiteral } = require("../util/identifier");
2019
const {
2120
RESERVED_IDENTIFIER,
2221
SAFE_IDENTIFIER,
@@ -34,6 +33,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
3433
* } from "../../declarations/WebpackOptions"
3534
*/
3635
/** @import Chunk from "../Chunk" */
36+
/** @import CodeGenerationResults from "../CodeGenerationResults" */
3737
/** @import Compilation from "../Compilation" */
3838
/** @import Compiler from "../Compiler" */
3939
/** @import ModuleGraph from "../graph/ModuleGraph" */
@@ -44,7 +44,6 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
4444
* ModuleRenderContext
4545
* } from "../javascript/JavascriptModulesPlugin"
4646
*/
47-
/** @import { ImportAttributes } from "../javascript/JavascriptParser" */
4847
/** @import { RuntimeSpec } from "../util/runtime" */
4948
/**
5049
* @import {
@@ -105,18 +104,6 @@ const noNativeExports = (module, moduleGraph) => {
105104
const toExportName = (name) =>
106105
SAFE_IDENTIFIER.test(name) ? name : JSON.stringify(name);
107106

108-
/**
109-
* Renders the import attributes of a reexport in the syntax the author wrote,
110-
* since a legacy `assert` clause and a `with` clause are not interchangeable.
111-
* @param {ImportAttributes | undefined} attributes the import attributes
112-
* @returns {string} the attributes clause, empty when there are none
113-
*/
114-
const toAttributesClause = (attributes) => {
115-
if (!attributes) return "";
116-
const { _isLegacyAssert: isLegacyAssert, ...values } = attributes;
117-
return ` ${isLegacyAssert ? "assert" : "with"} ${JSON.stringify(values)}`;
118-
};
119-
120107
/**
121108
* Whether the export name can also be used as the declared binding name.
122109
* @param {string} name export name
@@ -403,6 +390,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
403390
* @param {Module} module module
404391
* @param {ModuleGraph} moduleGraph moduleGraph
405392
* @param {RuntimeSpec} runtime chunk runtime
393+
* @param {CodeGenerationResults} codeGenerationResults the code generation results
406394
* @param {[string, string][]} exports exports
407395
* @param {Set<string>} alreadyRenderedExports already rendered exports
408396
* @returns {ConcatSource} source with null provided exports
@@ -412,6 +400,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
412400
module,
413401
moduleGraph,
414402
runtime,
403+
codeGenerationResults,
415404
exports,
416405
alreadyRenderedExports
417406
) {
@@ -450,20 +439,14 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
450439
) {
451440
// Handle export * from 'external'
452441
if (importedModule instanceof ExternalModule) {
453-
const request = importedModule.getResolvedRequest();
454-
// A property path is reported by `_checkStarReexportedExternals`,
455-
// and an absent one by the external's own build
456-
const specifier =
457-
typeof request === "string"
458-
? request
459-
: Array.isArray(request) && request.length === 1
460-
? request[0]
461-
: undefined;
462-
if (specifier !== undefined) {
463-
externalReexports.add(
464-
`export * from ${toJsStringLiteral(specifier)}${toAttributesClause(dep.attributes)};\n`
465-
);
466-
}
442+
const from = codeGenerationResults.has(importedModule, runtime)
443+
? codeGenerationResults.getData(
444+
importedModule,
445+
runtime,
446+
"externalReexport"
447+
)
448+
: undefined;
449+
if (from) externalReexports.add(`export * ${from};\n`);
467450
} else if (!visitedReexportModules.has(importedModule)) {
468451
visitedReexportModules.add(importedModule);
469452
resolveDynamicStarReexport(importedModule, true);
@@ -718,6 +701,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
718701
module,
719702
moduleGraph,
720703
chunk.runtime,
704+
codeGenerationResults,
721705
exports,
722706
alreadyRenderedExports
723707
);

0 commit comments

Comments
 (0)