33// https://github.com/angular/angular-cli/blob/d202480a1707be6575b2c8cf0383cfe6db44413c/packages/schematics/angular/utility/ng-ast-utils.ts
44// https://github.com/NativeScript/nativescript-schematics/blob/438b9e3ef613389980bfa9d071e28ca1f32ab04f/src/ast-utils.ts
55
6- import { dirname , basename , extname , join } from ' path' ;
7- import * as ts from ' typescript' ;
6+ import { dirname , basename , extname , join } from " path" ;
7+ import * as ts from " typescript" ;
88import {
99 StandardTransform ,
1010 TransformOperation ,
@@ -13,9 +13,9 @@ import {
1313 ReplaceNodeOperation ,
1414 makeTransform
1515} from "@ngtools/webpack/src/transformers" ;
16- import { workaroundResolve } from ' @ngtools/webpack/src/compiler_host' ;
17- import { AngularCompilerPlugin } from ' @ngtools/webpack' ;
18- import { findNode , getSourceNodes , getObjectPropertyMatches } from "../utils/ast-utils" ;
16+ import { workaroundResolve } from " @ngtools/webpack/src/compiler_host" ;
17+ import { AngularCompilerPlugin } from " @ngtools/webpack" ;
18+ import { findNode , getObjectPropertyMatches , getDecoratorMetadata } from "../utils/ast-utils" ;
1919
2020export function nsReplaceLazyLoader ( getNgCompiler : ( ) => AngularCompilerPlugin ) : ts . TransformerFactory < ts . SourceFile > {
2121 const getTypeChecker = ( ) => getNgCompiler ( ) . typeChecker ;
@@ -24,9 +24,7 @@ export function nsReplaceLazyLoader(getNgCompiler: () => AngularCompilerPlugin):
2424 let ops : TransformOperation [ ] = [ ] ;
2525 const ngCompiler = getNgCompiler ( ) ;
2626
27- const entryModule = ngCompiler . entryModule
28- ? { path : workaroundResolve ( ngCompiler . entryModule . path ) , className : getNgCompiler ( ) . entryModule . className }
29- : ngCompiler . entryModule ;
27+ const entryModule = getResolvedEntryModule ( ngCompiler , getNgCompiler ) ;
3028 const sourceFilePath = join ( dirname ( sourceFile . fileName ) , basename ( sourceFile . fileName , extname ( sourceFile . fileName ) ) ) ;
3129 if ( ! entryModule || sourceFilePath !== entryModule . path ) {
3230 return ops ;
@@ -44,13 +42,19 @@ export function nsReplaceLazyLoader(getNgCompiler: () => AngularCompilerPlugin):
4442 return makeTransform ( standardTransform , getTypeChecker ) ;
4543}
4644
45+ function getResolvedEntryModule ( ngCompiler : AngularCompilerPlugin , getNgCompiler : ( ) => AngularCompilerPlugin ) {
46+ return ngCompiler . entryModule
47+ ? { path : workaroundResolve ( ngCompiler . entryModule . path ) , className : getNgCompiler ( ) . entryModule . className }
48+ : ngCompiler . entryModule ;
49+ }
50+
4751export function addArrayPropertyValueToNgModule (
4852 sourceFile : ts . SourceFile ,
4953 targetPropertyName : string ,
5054 newPropertyValueMatch : string ,
5155 newPropertyValue : string
5256) : TransformOperation [ ] {
53- const ngModuleConfigNodesInFile = getDecoratorMetadata ( sourceFile , ' NgModule' , ' @angular/core' ) ;
57+ const ngModuleConfigNodesInFile = getDecoratorMetadata ( sourceFile , " NgModule" , " @angular/core" ) ;
5458 let ngModuleConfigNode : any = ngModuleConfigNodesInFile && ngModuleConfigNodesInFile [ 0 ] ;
5559 if ( ! ngModuleConfigNode ) {
5660 return null ;
@@ -63,7 +67,7 @@ export function addArrayPropertyValueToNgModule(
6367 }
6468
6569 const ngLazyLoaderNode = ts . createIdentifier ( NgLazyLoaderCode ) ;
66- if ( ngModuleConfigNode . kind == ts . SyntaxKind . Identifier ) {
70+ if ( ngModuleConfigNode . kind = == ts . SyntaxKind . Identifier ) {
6771 // cases like @NgModule (myCoolConfig)
6872 const configObjectDeclarationNodes = collectDeepNodes < ts . Node > ( sourceFile , ts . SyntaxKind . VariableStatement ) . filter ( imp => {
6973 return findNode ( imp , ts . SyntaxKind . Identifier , ngModuleConfigNode . getText ( ) ) ;
@@ -79,7 +83,7 @@ export function addArrayPropertyValueToNgModule(
7983 new AddNodeOperation ( sourceFile , lastImport , undefined , ngLazyLoaderNode ) ,
8084 new AddNodeOperation ( sourceFile , configObjectDeclaration || lastImport , undefined , configObjectSetupNode )
8185 ] ;
82- } else if ( ngModuleConfigNode . kind == ts . SyntaxKind . ObjectLiteralExpression ) {
86+ } else if ( ngModuleConfigNode . kind = == ts . SyntaxKind . ObjectLiteralExpression ) {
8387 // cases like @NgModule ({ bootstrap: ... })
8488 const ngModuleConfigObjectNode = ngModuleConfigNode as ts . ObjectLiteralExpression ;
8589 const matchingProperties : ts . ObjectLiteralElement [ ] = getObjectPropertyMatches ( ngModuleConfigObjectNode , sourceFile , targetPropertyName ) ;
@@ -88,8 +92,8 @@ export function addArrayPropertyValueToNgModule(
8892 return null ;
8993 }
9094
91- if ( matchingProperties . length == 0 ) {
92- if ( ngModuleConfigObjectNode . properties . length == 0 ) {
95+ if ( matchingProperties . length = == 0 ) {
96+ if ( ngModuleConfigObjectNode . properties . length = == 0 ) {
9397 // empty object @NgModule ({ })
9498 return null ;
9599 }
@@ -100,7 +104,8 @@ export function addArrayPropertyValueToNgModule(
100104
101105 return [
102106 new AddNodeOperation ( sourceFile , lastConfigObjPropertyNode , undefined , newTargetPropertyNode ) ,
103- new AddNodeOperation ( sourceFile , lastImport , undefined , ngLazyLoaderNode ) ] ;
107+ new AddNodeOperation ( sourceFile , lastImport , undefined , ngLazyLoaderNode )
108+ ] ;
104109
105110 }
106111
@@ -125,110 +130,19 @@ export function addArrayPropertyValueToNgModule(
125130 const lastPropertyValueNode = targetPropertyValues [ targetPropertyValues . length - 1 ] ;
126131 const newPropertyValueNode = ts . createIdentifier ( `${ newPropertyValue } ` ) ;
127132
128- return [ new AddNodeOperation ( sourceFile , lastPropertyValueNode , undefined , newPropertyValueNode ) ,
129- new AddNodeOperation ( sourceFile , lastImport , undefined , ngLazyLoaderNode ) ] ;
133+ return [
134+ new AddNodeOperation ( sourceFile , lastPropertyValueNode , undefined , newPropertyValueNode ) ,
135+ new AddNodeOperation ( sourceFile , lastImport , undefined , ngLazyLoaderNode )
136+ ] ;
130137 } else {
131138 // empty array @NgModule ({ targetProperty: [ ] })
132139 const newTargetPropertyValuesNode = ts . createIdentifier ( `[${ newPropertyValue } ]` ) ;
133140
134- return [ new ReplaceNodeOperation ( sourceFile , targetPropertyValuesNode , newTargetPropertyValuesNode ) ,
135- new AddNodeOperation ( sourceFile , lastImport , undefined , ngLazyLoaderNode ) ] ;
136- }
137- }
138- }
139-
140- function getDecoratorMetadata ( source : ts . SourceFile , identifier : string ,
141- module : string ) : ts . Node [ ] {
142- const angularImports : { [ name : string ] : string }
143- = collectDeepNodes ( source , ts . SyntaxKind . ImportDeclaration )
144- . map ( ( node : ts . ImportDeclaration ) => _angularImportsFromNode ( node , source ) )
145- . reduce ( ( acc : { [ name : string ] : string } , current : { [ name : string ] : string } ) => {
146- for ( const key of Object . keys ( current ) ) {
147- acc [ key ] = current [ key ] ;
148- }
149-
150- return acc ;
151- } , { } ) ;
152-
153- return getSourceNodes ( source )
154- . filter ( node => {
155- return node . kind == ts . SyntaxKind . Decorator
156- && ( node as ts . Decorator ) . expression . kind == ts . SyntaxKind . CallExpression ;
157- } )
158- . map ( node => ( node as ts . Decorator ) . expression as ts . CallExpression )
159- . filter ( expr => {
160- if ( expr . expression . kind == ts . SyntaxKind . Identifier ) {
161- const id = expr . expression as ts . Identifier ;
162-
163- return id . getFullText ( source ) == identifier
164- && angularImports [ id . getFullText ( source ) ] === module ;
165- } else if ( expr . expression . kind == ts . SyntaxKind . PropertyAccessExpression ) {
166- // This covers foo.NgModule when importing * as foo.
167- const paExpr = expr . expression as ts . PropertyAccessExpression ;
168- // If the left expression is not an identifier, just give up at that point.
169- if ( paExpr . expression . kind !== ts . SyntaxKind . Identifier ) {
170- return false ;
171- }
172-
173- const id = paExpr . name . text ;
174- const moduleId = ( paExpr . expression as ts . Identifier ) . getText ( source ) ;
175-
176- return id === identifier && ( angularImports [ moduleId + '.' ] === module ) ;
177- }
178-
179- return false ;
180- } )
181- . filter ( expr => expr . arguments [ 0 ]
182- && ( expr . arguments [ 0 ] . kind == ts . SyntaxKind . ObjectLiteralExpression ||
183- expr . arguments [ 0 ] . kind == ts . SyntaxKind . Identifier ) )
184- . map ( expr => expr . arguments [ 0 ] as ts . Node ) ;
185- }
186-
187- function _angularImportsFromNode ( node : ts . ImportDeclaration ,
188- _sourceFile : ts . SourceFile ) : { [ name : string ] : string } {
189- const ms = node . moduleSpecifier ;
190- let modulePath : string ;
191- switch ( ms . kind ) {
192- case ts . SyntaxKind . StringLiteral :
193- modulePath = ( ms as ts . StringLiteral ) . text ;
194- break ;
195- default :
196- return { } ;
197- }
198-
199- if ( ! modulePath . startsWith ( '@angular/' ) ) {
200- return { } ;
201- }
202-
203- if ( node . importClause ) {
204- if ( node . importClause . name ) {
205- // This is of the form `import Name from 'path'`. Ignore.
206- return { } ;
207- } else if ( node . importClause . namedBindings ) {
208- const nb = node . importClause . namedBindings ;
209- if ( nb . kind == ts . SyntaxKind . NamespaceImport ) {
210- // This is of the form `import * as name from 'path'`. Return `name.`.
211- return {
212- [ ( nb as ts . NamespaceImport ) . name . text + '.' ] : modulePath ,
213- } ;
214- } else {
215- // This is of the form `import {a,b,c} from 'path'`
216- const namedImports = nb as ts . NamedImports ;
217-
218- return namedImports . elements
219- . map ( ( is : ts . ImportSpecifier ) => is . propertyName ? is . propertyName . text : is . name . text )
220- . reduce ( ( acc : { [ name : string ] : string } , curr : string ) => {
221- acc [ curr ] = modulePath ;
222-
223- return acc ;
224- } , { } ) ;
225- }
141+ return [
142+ new ReplaceNodeOperation ( sourceFile , targetPropertyValuesNode , newTargetPropertyValuesNode ) ,
143+ new AddNodeOperation ( sourceFile , lastImport , undefined , ngLazyLoaderNode )
144+ ] ;
226145 }
227-
228- return { } ;
229- } else {
230- // This is of the form `import 'path';`. Nothing to do.
231- return { } ;
232146 }
233147}
234148
@@ -298,4 +212,4 @@ var NSLazyModulesLoader_Generated = /** @class */ (function () {
298212 ], NSLazyModulesLoader_Generated);
299213 return NSLazyModulesLoader_Generated;
300214}());
301- ` ;
215+ ` ;
0 commit comments