@@ -137,7 +137,7 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
137
137
) ;
138
138
139
139
// 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 .
141
141
const logger = compilation . getLogger ( PLUGIN_NAME ) ;
142
142
for ( const key of [ "javascript/auto" , "javascript/esm" ] ) {
143
143
normalModuleFactory . hooks . parser
@@ -161,8 +161,8 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
161
161
// Async modules are flagged internally after all modules are built,
162
162
// and thus their dependencies have already been processed. This means
163
163
// 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 .
166
166
compilation . hooks . finishModules . tapPromise (
167
167
PLUGIN_NAME ,
168
168
async ( modules ) => {
@@ -174,15 +174,29 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
174
174
! this . #parsedTLAModules. has ( m )
175
175
) {
176
176
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
+ }
186
200
}
187
201
}
188
202
logger . debug (
@@ -196,13 +210,11 @@ export class TransformAsyncModulesPlugin implements WebpackPluginInstance {
196
210
) ;
197
211
} ;
198
212
199
- #addDependenciesToModule = ( module : Module ) => {
213
+ #addDependenciesToModule( module : Module ) {
200
214
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 ) ) ;
204
216
}
205
- } ;
217
+ }
206
218
207
219
#applyTransformHooks = ( compiler : Compiler ) => {
208
220
const { SourceMapSource } = compiler . webpack . sources ;
0 commit comments