@@ -10,6 +10,20 @@ import ts from 'typescript';
1010
1111import { STYLES , STYLE_URLS , TEMPLATE_URL , TEMPLATE , REQUIRE , COMPONENT } from '../constants' ;
1212
13+ const isAfterVersion = ( targetMajor : number , targetMinor : number ) : boolean => {
14+ const [ major , minor ] = ts . versionMajorMinor . split ( '.' ) . map ( ( part ) => parseInt ( part ) ) ;
15+
16+ if ( major < targetMajor ) {
17+ return false ;
18+ } else if ( major > targetMajor ) {
19+ return true ;
20+ } else {
21+ return minor >= targetMinor ;
22+ }
23+ } ;
24+
25+ const IS_TS_48 = isAfterVersion ( 4 , 8 ) ;
26+
1327const shouldTransform = ( fileName : string ) => ! fileName . endsWith ( '.ngfactory.ts' ) && ! fileName . endsWith ( '.ngstyle.ts' ) ;
1428/**
1529 * Source https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/transformers/replace_resources.ts
@@ -52,21 +66,7 @@ export function replaceResources({ program }: TsCompilerInstance): ts.Transforme
5266
5367 const visitNode : ts . Visitor = ( node : ts . Node ) => {
5468 if ( ts . isClassDeclaration ( node ) ) {
55- const decorators = ts . visitNodes ( node . decorators , ( node ) =>
56- ts . isDecorator ( node )
57- ? visitDecorator ( nodeFactory , node , typeChecker , resourceImportDeclarations , moduleKind )
58- : node ,
59- ) ;
60-
61- return nodeFactory . updateClassDeclaration (
62- node ,
63- decorators ,
64- node . modifiers ,
65- node . name ,
66- node . typeParameters ,
67- node . heritageClauses ,
68- node . members ,
69- ) ;
69+ return visitClassDeclaration ( nodeFactory , typeChecker , node , resourceImportDeclarations , moduleKind ) ;
7070 }
7171
7272 return ts . visitEachChild ( node , visitNode , context ) ;
@@ -94,6 +94,59 @@ export function replaceResources({ program }: TsCompilerInstance): ts.Transforme
9494 } ;
9595}
9696
97+ function visitClassDeclaration (
98+ nodeFactory : ts . NodeFactory ,
99+ typeChecker : ts . TypeChecker ,
100+ node : ts . ClassDeclaration ,
101+ resourceImportDeclarations : ts . ImportDeclaration [ ] ,
102+ moduleKind : ts . ModuleKind | undefined ,
103+ ) : ts . ClassDeclaration {
104+ let decorators : ts . Decorator [ ] | undefined ;
105+ let modifiers : ts . Modifier [ ] | undefined ;
106+
107+ if ( IS_TS_48 ) {
108+ node . modifiers ?. forEach ( ( modifier ) => {
109+ if ( ts . isDecorator ( modifier ) ) {
110+ decorators ??= [ ] ;
111+ decorators . push ( modifier ) ;
112+ } else {
113+ modifiers = modifiers ??= [ ] ;
114+ modifiers . push ( modifier ) ;
115+ }
116+ } ) ;
117+ } else {
118+ decorators = node . decorators as unknown as ts . Decorator [ ] ;
119+ modifiers = node . modifiers as unknown as ts . Modifier [ ] ;
120+ }
121+
122+ if ( ! decorators || ! decorators . length ) {
123+ return node ;
124+ }
125+
126+ decorators = decorators . map ( ( current ) =>
127+ visitDecorator ( nodeFactory , current , typeChecker , resourceImportDeclarations , moduleKind ) ,
128+ ) ;
129+
130+ return IS_TS_48
131+ ? nodeFactory . updateClassDeclaration (
132+ node ,
133+ [ ...decorators , ...( modifiers ?? [ ] ) ] ,
134+ node . name ,
135+ node . typeParameters ,
136+ node . heritageClauses ,
137+ node . members ,
138+ )
139+ : nodeFactory . updateClassDeclaration (
140+ node ,
141+ decorators ,
142+ modifiers ,
143+ node . name ,
144+ node . typeParameters ,
145+ node . heritageClauses ,
146+ node . members ,
147+ ) ;
148+ }
149+
97150function visitDecorator (
98151 nodeFactory : ts . NodeFactory ,
99152 node : ts . Decorator ,
0 commit comments