@@ -15,12 +15,12 @@ export function replaceResources(
1515 return ( context : ts . TransformationContext ) => {
1616 const typeChecker = getTypeChecker ( ) ;
1717 const resourceImportDeclarations : ts . ImportDeclaration [ ] = [ ] ;
18-
18+ const moduleKind = context . getCompilerOptions ( ) . module ;
1919 const visitNode : ts . Visitor = ( node : ts . Node ) => {
2020 if ( ts . isClassDeclaration ( node ) ) {
2121 const decorators = ts . visitNodes ( node . decorators , node =>
2222 ts . isDecorator ( node )
23- ? visitDecorator ( node , typeChecker , directTemplateLoading , resourceImportDeclarations )
23+ ? visitDecorator ( node , typeChecker , directTemplateLoading , resourceImportDeclarations , moduleKind )
2424 : node ,
2525 ) ;
2626
@@ -68,6 +68,7 @@ function visitDecorator(
6868 typeChecker : ts . TypeChecker ,
6969 directTemplateLoading : boolean ,
7070 resourceImportDeclarations : ts . ImportDeclaration [ ] ,
71+ moduleKind ?: ts . ModuleKind ,
7172) : ts . Decorator {
7273 if ( ! isComponentDecorator ( node , typeChecker ) ) {
7374 return node ;
@@ -90,7 +91,7 @@ function visitDecorator(
9091 // visit all properties
9192 let properties = ts . visitNodes ( objectExpression . properties , node =>
9293 ts . isObjectLiteralElementLike ( node )
93- ? visitComponentMetadata ( node , styleReplacements , directTemplateLoading , resourceImportDeclarations )
94+ ? visitComponentMetadata ( node , styleReplacements , directTemplateLoading , resourceImportDeclarations , moduleKind )
9495 : node ,
9596 ) ;
9697
@@ -117,6 +118,7 @@ function visitComponentMetadata(
117118 styleReplacements : ts . Expression [ ] ,
118119 directTemplateLoading : boolean ,
119120 resourceImportDeclarations : ts . ImportDeclaration [ ] ,
121+ moduleKind ?: ts . ModuleKind ,
120122) : ts . ObjectLiteralElementLike | undefined {
121123 if ( ! ts . isPropertyAssignment ( node ) || ts . isComputedPropertyName ( node . name ) ) {
122124 return node ;
@@ -128,7 +130,12 @@ function visitComponentMetadata(
128130 return undefined ;
129131
130132 case 'templateUrl' :
131- const importName = createResourceImport ( node . initializer , directTemplateLoading ? '!raw-loader!' : '' , resourceImportDeclarations ) ;
133+ const importName = createResourceImport (
134+ node . initializer ,
135+ directTemplateLoading ? '!raw-loader!' : '' ,
136+ resourceImportDeclarations ,
137+ moduleKind ,
138+ ) ;
132139 if ( ! importName ) {
133140 return node ;
134141 }
@@ -154,7 +161,7 @@ function visitComponentMetadata(
154161 return ts . createLiteral ( node . text ) ;
155162 }
156163
157- return createResourceImport ( node , undefined , resourceImportDeclarations ) || node ;
164+ return createResourceImport ( node , undefined , resourceImportDeclarations , moduleKind ) || node ;
158165 } ) ;
159166
160167 // Styles should be placed first
@@ -170,28 +177,6 @@ function visitComponentMetadata(
170177 }
171178}
172179
173- export function createResourceImport (
174- node : ts . Node ,
175- loader : string | undefined ,
176- resourceImportDeclarations : ts . ImportDeclaration [ ] ,
177- ) : ts . Identifier | null {
178- const url = getResourceUrl ( node , loader ) ;
179- if ( ! url ) {
180- return null ;
181- }
182-
183- const importName = ts . createIdentifier ( `__NG_CLI_RESOURCE__${ resourceImportDeclarations . length } ` ) ;
184-
185- resourceImportDeclarations . push ( ts . createImportDeclaration (
186- undefined ,
187- undefined ,
188- ts . createImportClause ( importName , undefined ) ,
189- ts . createLiteral ( url ) ,
190- ) ) ;
191-
192- return importName ;
193- }
194-
195180export function getResourceUrl ( node : ts . Node , loader = '' ) : string | null {
196181 // only analyze strings
197182 if ( ! ts . isStringLiteral ( node ) && ! ts . isNoSubstitutionTemplateLiteral ( node ) ) {
@@ -214,6 +199,41 @@ function isComponentDecorator(node: ts.Node, typeChecker: ts.TypeChecker): node
214199 return false ;
215200}
216201
202+ function createResourceImport (
203+ node : ts . Node ,
204+ loader : string | undefined ,
205+ resourceImportDeclarations : ts . ImportDeclaration [ ] ,
206+ moduleKind = ts . ModuleKind . ES2015 ,
207+ ) : ts . Identifier | ts . Expression | null {
208+ const url = getResourceUrl ( node , loader ) ;
209+ if ( ! url ) {
210+ return null ;
211+ }
212+
213+ const urlLiteral = ts . createLiteral ( url ) ;
214+
215+ if ( moduleKind < ts . ModuleKind . ES2015 ) {
216+ return ts . createPropertyAccess (
217+ ts . createCall (
218+ ts . createIdentifier ( 'require' ) ,
219+ [ ] ,
220+ [ urlLiteral ] ,
221+ ) ,
222+ 'default' ,
223+ ) ;
224+ } else {
225+ const importName = ts . createIdentifier ( `__NG_CLI_RESOURCE__${ resourceImportDeclarations . length } ` ) ;
226+ resourceImportDeclarations . push ( ts . createImportDeclaration (
227+ undefined ,
228+ undefined ,
229+ ts . createImportClause ( importName , undefined ) ,
230+ urlLiteral ,
231+ ) ) ;
232+
233+ return importName ;
234+ }
235+ }
236+
217237interface DecoratorOrigin {
218238 name : string ;
219239 module : string ;
0 commit comments