1
1
import * as path from 'path' ;
2
2
import * as ts from 'typescript' ;
3
- import { AotPlugin } from './plugin' ;
4
- import { TypeScriptFileRefactor } from './refactor' ;
5
- import { LoaderContext , ModuleReason } from './webpack' ;
3
+ import { AotPlugin } from './plugin' ;
4
+ import { TypeScriptFileRefactor } from './refactor' ;
5
+ import { LoaderContext , ModuleReason } from './webpack' ;
6
6
7
7
interface Platform {
8
8
name : string ;
@@ -11,9 +11,10 @@ interface Platform {
11
11
12
12
const loaderUtils = require ( 'loader-utils' ) ;
13
13
const NormalModule = require ( 'webpack/lib/NormalModule' ) ;
14
+ const fs = require ( 'fs' ) ;
14
15
15
16
// This is a map of changes which need to be made
16
- const changeMap : { [ key : string ] : Platform } = {
17
+ const changeMap : { [ key : string ] : Platform } = {
17
18
platformBrowserDynamic : {
18
19
name : 'platformBrowser' ,
19
20
importLocation : '@angular/platform-browser'
@@ -77,8 +78,8 @@ function _angularImportsFromNode(node: ts.ImportDeclaration, _sourceFile: ts.Sou
77
78
78
79
79
80
function _ctorParameterFromTypeReference ( paramNode : ts . ParameterDeclaration ,
80
- angularImports : string [ ] ,
81
- refactor : TypeScriptFileRefactor ) {
81
+ angularImports : string [ ] ,
82
+ refactor : TypeScriptFileRefactor ) {
82
83
let typeName = 'undefined' ;
83
84
84
85
if ( paramNode . type ) {
@@ -134,8 +135,8 @@ function _ctorParameterFromTypeReference(paramNode: ts.ParameterDeclaration,
134
135
135
136
136
137
function _addCtorParameters ( classNode : ts . ClassDeclaration ,
137
- angularImports : string [ ] ,
138
- refactor : TypeScriptFileRefactor ) {
138
+ angularImports : string [ ] ,
139
+ refactor : TypeScriptFileRefactor ) {
139
140
// For every classes with constructors, output the ctorParameters function which contains a list
140
141
// of injectable types.
141
142
const ctor = (
@@ -212,7 +213,7 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
212
213
. map ( call => call . expression as ts . PropertyAccessExpression )
213
214
. filter ( access => {
214
215
return access . name . kind == ts . SyntaxKind . Identifier
215
- && access . name . text == 'bootstrapModule' ;
216
+ && access . name . text == 'bootstrapModule' ;
216
217
} ) ;
217
218
218
219
const calls : ts . CallExpression [ ] = bootstraps
@@ -273,13 +274,13 @@ function _removeModuleId(refactor: TypeScriptFileRefactor) {
273
274
. filter ( ( node : ts . ObjectLiteralExpression ) => {
274
275
return node . properties . some ( prop => {
275
276
return prop . kind == ts . SyntaxKind . PropertyAssignment
276
- && _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ;
277
+ && _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ;
277
278
} ) ;
278
279
} )
279
280
. forEach ( ( node : ts . ObjectLiteralExpression ) => {
280
281
const moduleIdProp = node . properties . filter ( ( prop : ts . ObjectLiteralElement , _idx : number ) => {
281
282
return prop . kind == ts . SyntaxKind . PropertyAssignment
282
- && _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ;
283
+ && _getContentOfKeyLiteral ( sourceFile , prop . name ) == 'moduleId' ;
283
284
} ) [ 0 ] ;
284
285
// Get the trailing comma.
285
286
const moduleIdCommaProp = moduleIdProp . parent
@@ -299,6 +300,19 @@ function _getResourceRequest(element: ts.Expression, sourceFile: ts.SourceFile)
299
300
}
300
301
}
301
302
303
+ function _getPrecompiledResult ( fileName : string ) {
304
+ const basePath = fileName . substr ( 0 , fileName . lastIndexOf ( '\\' ) + 1 ) ,
305
+ packageJson = require ( basePath + "package.json" ) ,
306
+ precompiledFileName = basePath + packageJson . es2015 ,
307
+ output = fs . readFileSync ( precompiledFileName , 'utf8' ) ,
308
+ sourceMap = fs . readFileSync ( precompiledFileName + '.map' , 'utf8' ) ;
309
+
310
+ return {
311
+ outputText : output ,
312
+ sourceMap : sourceMap
313
+ } ;
314
+ }
315
+
302
316
function _replaceResources ( refactor : TypeScriptFileRefactor ) : void {
303
317
const sourceFile = refactor . sourceFile ;
304
318
@@ -331,7 +345,7 @@ function _getResourceNodes(refactor: TypeScriptFileRefactor) {
331
345
332
346
// Find all object literals.
333
347
return refactor . findAstNodes ( sourceFile , ts . SyntaxKind . ObjectLiteralExpression , true )
334
- // Get all their property assignments.
348
+ // Get all their property assignments.
335
349
. map ( node => refactor . findAstNodes ( node , ts . SyntaxKind . PropertyAssignment ) )
336
350
// Flatten into a single array (from an array of array<property assignments>).
337
351
. reduce ( ( prev , curr ) => curr ? prev . concat ( curr ) : prev , [ ] )
@@ -398,6 +412,7 @@ function _diagnoseDeps(reasons: ModuleReason[], plugin: AotPlugin, checked: Set<
398
412
export function ngcLoader ( this : LoaderContext & { _compilation : any } ) {
399
413
const cb = this . async ( ) ;
400
414
const sourceFileName : string = this . resourcePath ;
415
+ const regex = / \. d \. t s / ig;
401
416
402
417
const plugin = this . _compilation . _ngToolsWebpackPluginInstance as AotPlugin ;
403
418
// We must verify that AotPlugin is an instance of the right class.
@@ -442,33 +457,38 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }) {
442
457
sourceRoot : plugin . basePath
443
458
} ) ;
444
459
445
- const result = refactor . transpile ( compilerOptions ) ;
460
+ const result = ( regex . test ( sourceFileName ) ) ? _getPrecompiledResult ( sourceFileName ) : refactor . transpile ( compilerOptions ) ;
446
461
cb ( null , result . outputText , result . sourceMap ) ;
447
462
} )
448
463
. catch ( err => cb ( err ) ) ;
449
464
} else {
450
- const options = loaderUtils . getOptions ( this ) || { } ;
451
- const tsConfigPath = options . tsConfigPath ;
452
- const tsConfig = ts . readConfigFile ( tsConfigPath , ts . sys . readFile ) ;
453
-
454
- if ( tsConfig . error ) {
455
- throw tsConfig . error ;
456
- }
465
+ if ( regex . test ( sourceFileName ) ) {
466
+ const result = _getPrecompiledResult ( sourceFileName ) ;
467
+ cb ( null , result . outputText , result . sourceMap ) ;
468
+ } else {
469
+ const options = loaderUtils . getOptions ( this ) || { } ;
470
+ const tsConfigPath = options . tsConfigPath ;
471
+ const tsConfig = ts . readConfigFile ( tsConfigPath , ts . sys . readFile ) ;
472
+
473
+ if ( tsConfig . error ) {
474
+ throw tsConfig . error ;
475
+ }
457
476
458
- const compilerOptions : ts . CompilerOptions = tsConfig . config . compilerOptions ;
459
- for ( const key of Object . keys ( options ) ) {
460
- if ( key == 'tsConfigPath' ) {
461
- continue ;
477
+ const compilerOptions : ts . CompilerOptions = tsConfig . config . compilerOptions ;
478
+ for ( const key of Object . keys ( options ) ) {
479
+ if ( key == 'tsConfigPath' ) {
480
+ continue ;
481
+ }
482
+ compilerOptions [ key ] = options [ key ] ;
462
483
}
463
- compilerOptions [ key ] = options [ key ] ;
484
+ const compilerHost = ts . createCompilerHost ( compilerOptions ) ;
485
+ const refactor = new TypeScriptFileRefactor ( sourceFileName , compilerHost ) ;
486
+ _replaceResources ( refactor ) ;
487
+
488
+ const result = refactor . transpile ( compilerOptions ) ;
489
+ // Webpack is going to take care of this.
490
+ result . outputText = result . outputText . replace ( / ^ \/ \/ # s o u r c e M a p p i n g U R L = [ ^ \r \n ] * / gm, '' ) ;
491
+ cb ( null , result . outputText , result . sourceMap ) ;
464
492
}
465
- const compilerHost = ts . createCompilerHost ( compilerOptions ) ;
466
- const refactor = new TypeScriptFileRefactor ( sourceFileName , compilerHost ) ;
467
- _replaceResources ( refactor ) ;
468
-
469
- const result = refactor . transpile ( compilerOptions ) ;
470
- // Webpack is going to take care of this.
471
- result . outputText = result . outputText . replace ( / ^ \/ \/ # s o u r c e M a p p i n g U R L = [ ^ \r \n ] * / gm, '' ) ;
472
- cb ( null , result . outputText , result . sourceMap ) ;
473
493
}
474
494
}
0 commit comments