Skip to content

Commit 3da2713

Browse files
authored
Fix processing of transform dependencies (#41)
* Fix processing of transform dependencies * Remove unnecessary default property from webpack require * Adjust comments
1 parent b79021f commit 3da2713

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/index.cts

+29-17
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
137137
);
138138

139139
// Add dependencies for modules with top level await. This can be done
140-
// during parsing to avoid rebuilding.
140+
// during parsing to avoid manual processing.
141141
const logger = compilation.getLogger(PLUGIN_NAME);
142142
for (const key of ["javascript/auto", "javascript/esm"]) {
143143
normalModuleFactory.hooks.parser
@@ -161,8 +161,8 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
161161
// Async modules are flagged internally after all modules are built,
162162
// and thus their dependencies have already been processed. This means
163163
// that for each async module, excluding those already handled while
164-
// parsing, we need to add the dependencies and then invalidate and
165-
// reprocess all of its dependencies.
164+
// parsing, we need to add the dependencies and then process them
165+
// manually.
166166
compilation.hooks.finishModules.tapPromise(
167167
PLUGIN_NAME,
168168
async (modules) => {
@@ -174,15 +174,29 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
174174
!this.#parsedTLAModules.has(m)
175175
) {
176176
identifiers.push(m.readableIdentifier(shortener));
177-
processes.push(
178-
new Promise((resolve, reject) => {
179-
this.#addDependenciesToModule(m);
180-
compilation.processDependenciesQueue.invalidate(m);
181-
compilation.processModuleDependencies(m, (err, result) =>
182-
err ? reject(err) : resolve(result),
183-
);
184-
}),
185-
);
177+
// Steps to process are taken from _processModuleDependencies:
178+
// https://github.com/webpack/webpack/blob/7b4775cebe372f9396d8f3b61ef1347cb633956e/lib/Compilation.js#L1473
179+
// After eliminating manual async fluff and focusing on only the
180+
// new dependencies, this boils down to modifying the module
181+
// graph and then calling the method to build the new modules.
182+
const startIndex = m.dependencies.length;
183+
this.#addDependenciesToModule(m);
184+
const transformDependencies = m.dependencies.slice(startIndex);
185+
for (const [i, dep] of transformDependencies.entries()) {
186+
compilation.moduleGraph.setParents(dep, m, m, startIndex + i);
187+
processes.push(
188+
new Promise((resolve, reject) => {
189+
compilation.handleModuleCreation(
190+
{
191+
factory: normalModuleFactory,
192+
dependencies: [dep],
193+
originModule: m,
194+
},
195+
(err, result) => (err ? reject(err) : resolve(result)),
196+
);
197+
}),
198+
);
199+
}
186200
}
187201
}
188202
logger.debug(
@@ -196,13 +210,11 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
196210
);
197211
};
198212

199-
#addDependenciesToModule = (module: Module) => {
213+
#addDependenciesToModule(module: Module) {
200214
for (const [request, identifier] of this.#dependencies.entries()) {
201-
module.addDependency(
202-
new TransformAsyncDependency(request, identifier, ["default"]),
203-
);
215+
module.addDependency(new TransformAsyncDependency(request, identifier));
204216
}
205-
};
217+
}
206218

207219
#applyTransformHooks = (compiler: Compiler) => {
208220
const { SourceMapSource } = compiler.webpack.sources;

0 commit comments

Comments
 (0)