66 * found in the LICENSE file at https://angular.io/license
77 */
88import { RawSourceMap , SourceMapConsumer , SourceMapGenerator } from 'source-map' ;
9- import * as webpack from 'webpack' ; // tslint:disable-line:no-implicit-dependencies
9+ import * as webpack from 'webpack' ; // tslint:disable-line:no-implicit-dependencies
1010
1111const loaderUtils = require ( 'loader-utils' ) ;
1212
1313import { buildOptimizer } from './build-optimizer' ;
1414
15-
1615interface BuildOptimizerLoaderOptions {
1716 sourceMap : boolean ;
1817}
@@ -21,17 +20,25 @@ export const buildOptimizerLoaderPath = __filename;
2120
2221const alwaysProcess = ( path : string ) =>
2322 // Always process TS files.
24- path . endsWith ( '.ts' ) || path . endsWith ( '.tsx' )
23+ path . endsWith ( '.ts' ) ||
24+ path . endsWith ( '.tsx' ) ||
2525 // Always process factory files.
26- || path . endsWith ( '.ngfactory.js' ) || path . endsWith ( '.ngstyle.js' ) ;
27-
28- export default function buildOptimizerLoader
29- ( this : webpack . loader . LoaderContext , content : string , previousSourceMap : RawSourceMap ) {
26+ path . endsWith ( '.ngfactory.js' ) ||
27+ path . endsWith ( '.ngstyle.js' ) ;
28+
29+ export default function buildOptimizerLoader (
30+ this : webpack . loader . LoaderContext ,
31+ content : string ,
32+ previousSourceMap : RawSourceMap ,
33+ ) {
3034 this . cacheable ( ) ;
35+ const callback = this . async ( ) ;
36+ if ( ! callback ) {
37+ throw new Error ( 'Async loader support is required.' ) ;
38+ }
3139
32- const skipBuildOptimizer = this . _module
33- && this . _module . factoryMeta
34- && this . _module . factoryMeta . skipBuildOptimizer ;
40+ const skipBuildOptimizer =
41+ this . _module && this . _module . factoryMeta && this . _module . factoryMeta . skipBuildOptimizer ;
3542
3643 if ( ! alwaysProcess ( this . resourcePath ) && skipBuildOptimizer ) {
3744 // Skip loading processing this file with Build Optimizer if we determined in
@@ -56,9 +63,8 @@ export default function buildOptimizerLoader
5663 inputFilePath,
5764 outputFilePath,
5865 emitSourceMap : options . sourceMap ,
59- isSideEffectFree : this . _module
60- && this . _module . factoryMeta
61- && this . _module . factoryMeta . sideEffectFree ,
66+ isSideEffectFree :
67+ this . _module && this . _module . factoryMeta && this . _module . factoryMeta . sideEffectFree ,
6268 } ) ;
6369
6470 if ( boOutput . emitSkipped || boOutput . content === null ) {
@@ -89,10 +95,17 @@ export default function buildOptimizerLoader
8995 previousSourceMap . file = inputFilePath ;
9096
9197 // Chain the sourcemaps.
92- const consumer = new SourceMapConsumer ( intermediateSourceMap ) ;
93- const generator = SourceMapGenerator . fromSourceMap ( consumer ) ;
94- generator . applySourceMap ( new SourceMapConsumer ( previousSourceMap ) ) ;
95- newSourceMap = generator . toJSON ( ) ;
98+ SourceMapConsumer . with ( intermediateSourceMap , null , intermediate => {
99+ return SourceMapConsumer . with ( previousSourceMap , null , previous => {
100+ const generator = SourceMapGenerator . fromSourceMap ( intermediate ) ;
101+ generator . applySourceMap ( previous ) ;
102+
103+ return generator . toJSON ( ) ;
104+ } ) ;
105+ // tslint:disable-next-line: no-any
106+ } ) . then ( map => callback ( null , newContent , map as any ) , error => callback ( error ) ) ;
107+
108+ return ;
96109 } else {
97110 // Otherwise just return our generated sourcemap.
98111 newSourceMap = intermediateSourceMap ;
@@ -101,5 +114,5 @@ export default function buildOptimizerLoader
101114
102115 // Webpack typings for previousSourceMap are wrong, they are JSON objects and not strings.
103116 // tslint:disable-next-line:no-any
104- this . callback ( null , newContent , newSourceMap as any ) ;
117+ callback ( null , newContent , newSourceMap as any ) ;
105118}
0 commit comments