@@ -12,15 +12,23 @@ import {
12
12
purgeDeepLinkDecoratorTSTransform }
13
13
from './deep-linking/util' ;
14
14
15
+ import {
16
+ convertDeepLinkConfigEntriesToString ,
17
+ getUpdatedAppNgModuleContentWithDeepLinkConfig ,
18
+ filterTypescriptFilesForDeepLinks ,
19
+ isDeepLinkingFile ,
20
+ purgeDeepLinkDecorator
21
+ } from './deep-linking/util' ;
22
+
15
23
import { Logger } from './logger/logger' ;
16
24
import { printDiagnostics , clearDiagnostics , DiagnosticsType } from './logger/logger-diagnostics' ;
17
25
import { runTypeScriptDiagnostics } from './logger/logger-typescript' ;
18
26
import { inlineTemplate } from './template' ;
19
27
import * as Constants from './util/constants' ;
20
28
import { BuildError } from './util/errors' ;
21
29
import { FileCache } from './util/file-cache' ;
22
- import { changeExtension , getBooleanPropertyValue } from './util/helpers' ;
23
- import { BuildContext , BuildState , ChangedFile } from './util/interfaces' ;
30
+ import { changeExtension , getBooleanPropertyValue , getParsedDeepLinkConfig , getStringPropertyValue } from './util/helpers' ;
31
+ import { BuildContext , BuildState , ChangedFile , File } from './util/interfaces' ;
24
32
25
33
export function transpile ( context : BuildContext ) {
26
34
@@ -109,24 +117,36 @@ export function transpileWorker(context: BuildContext, workerConfig: TranspileWo
109
117
110
118
// let's start a new tsFiles object to cache all the transpiled files in
111
119
const host = getInMemoryCompilerHostInstance ( tsConfig . options ) ;
112
- // ts.createCompilerHost(tsConfig.options);
113
120
114
- const program = ts . createProgram ( tsFileNames , tsConfig . options , host , cachedProgram ) ;
121
+ if ( workerConfig . useTransforms && getBooleanPropertyValue ( Constants . ENV_PARSE_DEEPLINKS ) ) {
122
+ // beforeArray.push(purgeDeepLinkDecoratorTSTransform());
123
+ // beforeArray.push(getInjectDeepLinkConfigTypescriptTransform());
115
124
116
- const beforeArray : ts . TransformerFactory < ts . SourceFile > [ ] = [ ] ;
125
+ // temporarily copy the files to a new location
126
+ copyOriginalSourceFiles ( context . fileCache ) ;
117
127
118
- if ( workerConfig . useTransforms && getBooleanPropertyValue ( Constants . ENV_PARSE_DEEPLINKS ) ) {
119
- beforeArray . push ( purgeDeepLinkDecoratorTSTransform ( ) ) ;
120
- beforeArray . push ( getInjectDeepLinkConfigTypescriptTransform ( ) ) ;
128
+ // okay, purge the deep link files NOT using a transform
129
+ const deepLinkFiles = filterTypescriptFilesForDeepLinks ( context . fileCache ) ;
130
+
131
+ deepLinkFiles . forEach ( file => {
132
+ file . content = purgeDeepLinkDecorator ( file . content ) ;
133
+ } ) ;
134
+
135
+ const file = context . fileCache . get ( getStringPropertyValue ( Constants . ENV_APP_NG_MODULE_PATH ) ) ;
136
+ const deepLinkString = convertDeepLinkConfigEntriesToString ( getParsedDeepLinkConfig ( ) ) ;
137
+ file . content = getUpdatedAppNgModuleContentWithDeepLinkConfig ( file . path , file . content , deepLinkString ) ;
121
138
}
122
139
140
+ const program = ts . createProgram ( tsFileNames , tsConfig . options , host , cachedProgram ) ;
141
+
142
+ resetSourceFiles ( context . fileCache ) ;
143
+
144
+ const beforeArray : ts . TransformerFactory < ts . SourceFile > [ ] = [ ] ;
145
+
123
146
program . emit ( undefined , ( path : string , data : string , writeByteOrderMark : boolean , onError : Function , sourceFiles : ts . SourceFile [ ] ) => {
124
147
if ( workerConfig . writeInMemory ) {
125
- writeSourceFiles ( context . fileCache , sourceFiles ) ;
126
148
writeTranspiledFilesCallback ( context . fileCache , path , data , workerConfig . inlineTemplate ) ;
127
149
}
128
- } , null , false , {
129
- before : beforeArray
130
150
} ) ;
131
151
132
152
// cache the typescript program for later use
@@ -181,26 +201,19 @@ function transpileUpdateWorker(event: string, filePath: string, context: BuildCo
181
201
182
202
const beforeArray : ts . TransformerFactory < ts . SourceFile > [ ] = [ ] ;
183
203
184
- if ( workerConfig . useTransforms && getBooleanPropertyValue ( Constants . ENV_PARSE_DEEPLINKS ) ) {
185
- beforeArray . push ( purgeDeepLinkDecoratorTSTransform ( ) ) ;
186
- beforeArray . push ( getInjectDeepLinkConfigTypescriptTransform ( ) ) ;
187
- }
188
-
189
204
const transpileOptions : ts . TranspileOptions = {
190
205
compilerOptions : cachedTsConfig . options ,
191
206
fileName : filePath ,
192
207
reportDiagnostics : true ,
193
- transformers : {
194
- before : beforeArray
195
- }
196
208
} ;
197
209
198
210
// let's manually transpile just this one ts file
199
211
// since it is an update, it's in memory already
200
212
const sourceText = context . fileCache . get ( filePath ) . content ;
213
+ const textToTranspile = workerConfig . useTransforms && getBooleanPropertyValue ( Constants . ENV_PARSE_DEEPLINKS ) ? transformSource ( filePath , sourceText ) : sourceText ;
201
214
202
215
// transpile this one module
203
- const transpileOutput = ts . transpileModule ( sourceText , transpileOptions ) ;
216
+ const transpileOutput = ts . transpileModule ( textToTranspile , transpileOptions ) ;
204
217
205
218
const diagnostics = runTypeScriptDiagnostics ( context , transpileOutput . diagnostics ) ;
206
219
@@ -294,13 +307,6 @@ function cleanFileNames(context: BuildContext, fileNames: string[]) {
294
307
return fileNames ;
295
308
}
296
309
297
- function writeSourceFiles ( fileCache : FileCache , sourceFiles : ts . SourceFile [ ] ) {
298
- for ( const sourceFile of sourceFiles ) {
299
- const fileName = path . normalize ( path . resolve ( sourceFile . fileName ) ) ;
300
- fileCache . set ( fileName , { path : fileName , content : sourceFile . text } ) ;
301
- }
302
- }
303
-
304
310
function writeTranspiledFilesCallback ( fileCache : FileCache , sourcePath : string , data : string , shouldInlineTemplate : boolean ) {
305
311
sourcePath = path . normalize ( path . resolve ( sourcePath ) ) ;
306
312
@@ -390,6 +396,42 @@ export function transpileTsString(context: BuildContext, filePath: string, strin
390
396
return ts . transpileModule ( stringToTranspile , transpileOptions ) ;
391
397
}
392
398
399
+ export function transformSource ( filePath : string , input : string ) {
400
+ if ( isDeepLinkingFile ( filePath ) ) {
401
+ input = purgeDeepLinkDecorator ( input ) ;
402
+ } else if ( filePath === getStringPropertyValue ( Constants . ENV_APP_NG_MODULE_PATH ) ) {
403
+ const deepLinkString = convertDeepLinkConfigEntriesToString ( getParsedDeepLinkConfig ( ) ) ;
404
+ input = getUpdatedAppNgModuleContentWithDeepLinkConfig ( filePath , input , deepLinkString ) ;
405
+ }
406
+ return input ;
407
+ }
408
+
409
+ export function copyOriginalSourceFiles ( fileCache : FileCache ) {
410
+ const deepLinkFiles = filterTypescriptFilesForDeepLinks ( fileCache ) ;
411
+ const appNgModule = fileCache . get ( getStringPropertyValue ( Constants . ENV_APP_NG_MODULE_PATH ) ) ;
412
+ deepLinkFiles . push ( appNgModule ) ;
413
+ deepLinkFiles . forEach ( deepLinkFile => {
414
+ fileCache . set ( deepLinkFile . path + inMemoryFileCopySuffix , {
415
+ path : deepLinkFile . path + inMemoryFileCopySuffix ,
416
+ content : deepLinkFile . content
417
+ } ) ;
418
+ } ) ;
419
+ }
420
+
421
+ export function resetSourceFiles ( fileCache : FileCache ) {
422
+ fileCache . getAll ( ) . forEach ( file => {
423
+ if ( path . extname ( file . path ) === `.ts${ inMemoryFileCopySuffix } ` ) {
424
+ const originalExtension = changeExtension ( file . path , '.ts' ) ;
425
+ fileCache . set ( originalExtension , {
426
+ path : originalExtension ,
427
+ content : file . content
428
+ } ) ;
429
+ fileCache . getRawStore ( ) . delete ( file . path ) ;
430
+ }
431
+ } ) ;
432
+ }
433
+
434
+ export const inMemoryFileCopySuffix = 'original' ;
393
435
394
436
let cachedProgram : ts . Program = null ;
395
437
let cachedTsConfig : TsConfig = null ;
0 commit comments