6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { 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
10
10
11
11
const loaderUtils = require ( 'loader-utils' ) ;
12
12
13
13
import { buildOptimizer } from './build-optimizer' ;
14
14
15
-
16
15
interface BuildOptimizerLoaderOptions {
17
16
sourceMap : boolean ;
18
17
}
@@ -21,17 +20,25 @@ export const buildOptimizerLoaderPath = __filename;
21
20
22
21
const alwaysProcess = ( path : string ) =>
23
22
// Always process TS files.
24
- path . endsWith ( '.ts' ) || path . endsWith ( '.tsx' )
23
+ path . endsWith ( '.ts' ) ||
24
+ path . endsWith ( '.tsx' ) ||
25
25
// 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
+ ) {
30
34
this . cacheable ( ) ;
35
+ const callback = this . async ( ) ;
36
+ if ( ! callback ) {
37
+ throw new Error ( 'Async loader support is required.' ) ;
38
+ }
31
39
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 ;
35
42
36
43
if ( ! alwaysProcess ( this . resourcePath ) && skipBuildOptimizer ) {
37
44
// Skip loading processing this file with Build Optimizer if we determined in
@@ -56,9 +63,8 @@ export default function buildOptimizerLoader
56
63
inputFilePath,
57
64
outputFilePath,
58
65
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 ,
62
68
} ) ;
63
69
64
70
if ( boOutput . emitSkipped || boOutput . content === null ) {
@@ -89,10 +95,17 @@ export default function buildOptimizerLoader
89
95
previousSourceMap . file = inputFilePath ;
90
96
91
97
// 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 ;
96
109
} else {
97
110
// Otherwise just return our generated sourcemap.
98
111
newSourceMap = intermediateSourceMap ;
@@ -101,5 +114,5 @@ export default function buildOptimizerLoader
101
114
102
115
// Webpack typings for previousSourceMap are wrong, they are JSON objects and not strings.
103
116
// tslint:disable-next-line:no-any
104
- this . callback ( null , newContent , newSourceMap as any ) ;
117
+ callback ( null , newContent , newSourceMap as any ) ;
105
118
}
0 commit comments