@@ -302,6 +302,46 @@ function getExistingSolutionBuilderHost(key: FilePathKey) {
302302 return undefined ;
303303}
304304
305+ // Adding assets in afterCompile is deprecated in webpack 5 so we
306+ // need different behavior for webpack4 and 5
307+ const addAssetHooks = ! ! webpack . version ! . match ( / ^ 4 .* / )
308+ ? ( loader : webpack . loader . LoaderContext , instance : TSInstance ) => {
309+ // add makeAfterCompile with addAssets = true to emit assets and report errors
310+ loader . _compiler . hooks . afterCompile . tapAsync (
311+ 'ts-loader' ,
312+ makeAfterCompile ( instance , true , true , instance . configFilePath )
313+ ) ;
314+ }
315+ : ( loader : webpack . loader . LoaderContext , instance : TSInstance ) => {
316+ // We must be running under webpack 5+
317+
318+ // Add makeAfterCompile with addAssets = false to suppress emitting assets
319+ // during the afterCompile stage. Errors will be still be reported to webpack
320+ loader . _compiler . hooks . afterCompile . tapAsync (
321+ 'ts-loader' ,
322+ makeAfterCompile ( instance , false , true , instance . configFilePath )
323+ ) ;
324+
325+ // Emit the assets at the afterProcessAssets stage
326+ loader . _compilation . hooks . afterProcessAssets . tap (
327+ 'ts-loader' ,
328+ ( _ : any ) => {
329+ makeAfterCompile (
330+ instance ,
331+ true ,
332+ false ,
333+ instance . configFilePath
334+ ) ( loader . _compilation , ( ) => {
335+ return null ;
336+ } ) ;
337+ }
338+ ) ;
339+
340+ // It may be better to add assets at the processAssets stage (https://webpack.js.org/api/compilation-hooks/#processassets)
341+ // This requires Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, which does not exist in webpack4
342+ // Consider changing this when ts-loader is built using webpack5
343+ } ;
344+
305345export function initializeInstance (
306346 loader : webpack . loader . LoaderContext ,
307347 instance : TSInstance
@@ -350,26 +390,7 @@ export function initializeInstance(
350390 instance . transformers = getCustomTransformers ( program ) ;
351391 // Setup watch run for solution building
352392 if ( instance . solutionBuilderHost ) {
353- if ( loader . _compilation . hooks . afterProcessAssets ) {
354- // afterProcessAssets does not exist in webpack4
355- loader . _compilation . hooks . afterProcessAssets . tap (
356- 'ts-loader' ,
357- ( _ : any ) => {
358- makeAfterCompile ( instance , instance . configFilePath ) (
359- loader . _compilation ,
360- ( ) => {
361- return null ;
362- }
363- ) ;
364- }
365- ) ;
366- } else {
367- // adding assets in afterCompile is deprecated in webpack 5
368- loader . _compiler . hooks . afterCompile . tapAsync (
369- 'ts-loader' ,
370- makeAfterCompile ( instance , instance . configFilePath )
371- ) ;
372- }
393+ addAssetHooks ( loader , instance ) ;
373394 loader . _compiler . hooks . watchRun . tapAsync (
374395 'ts-loader' ,
375396 makeWatchRun ( instance , loader )
@@ -416,26 +437,8 @@ export function initializeInstance(
416437 instance . languageService ! . getProgram ( )
417438 ) ;
418439 }
419- if ( loader . _compilation . hooks . afterProcessAssets ) {
420- // afterProcessAssets does not exist in webpack4
421- loader . _compilation . hooks . afterProcessAssets . tap (
422- 'ts-loader' ,
423- ( _ : any ) => {
424- makeAfterCompile ( instance , instance . configFilePath ) (
425- loader . _compilation ,
426- ( ) => {
427- return null ;
428- }
429- ) ;
430- }
431- ) ;
432- } else {
433- // adding assets in afterCompile is deprecated in webpack 5
434- loader . _compiler . hooks . afterCompile . tapAsync (
435- 'ts-loader' ,
436- makeAfterCompile ( instance , instance . configFilePath )
437- ) ;
438- }
440+
441+ addAssetHooks ( loader , instance ) ;
439442
440443 loader . _compiler . hooks . watchRun . tapAsync (
441444 'ts-loader' ,
0 commit comments