Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 47ef069

Browse files
committed
refactor(transpile): update transpile to use our in-memory compiler host and transform TS before giving it to the program. Do not use stateless ts transforms since 2.3 has issues with them
1 parent 7f3c096 commit 47ef069

File tree

2 files changed

+95
-27
lines changed

2 files changed

+95
-27
lines changed

src/transpile.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as transpile from './transpile';
2+
3+
import { FileCache } from './util/file-cache';
4+
import { BuildContext } from './util/interfaces';
5+
6+
describe('transpile', () => {
7+
describe('resetSourceFiles', () => {
8+
it('should remove any files with temporary suffix, and reset content to the original, non-modified value', () => {
9+
const context = {
10+
fileCache: new FileCache()
11+
};
12+
13+
const aboutFilePath = 'about.ts';
14+
const aboutFile = { path: aboutFilePath, content: 'modifiedContent'};
15+
const originalAboutFilePath = aboutFilePath + transpile.inMemoryFileCopySuffix;
16+
const originalAboutFile = { path: originalAboutFilePath, content: 'originalContent'};
17+
context.fileCache.set(aboutFilePath, aboutFile);
18+
context.fileCache.set(originalAboutFilePath, originalAboutFile);
19+
20+
transpile.resetSourceFiles(context.fileCache);
21+
22+
expect(context.fileCache.get(originalAboutFilePath)).toBeFalsy();
23+
expect(context.fileCache.get(aboutFilePath).content).toEqual(originalAboutFile.content);
24+
});
25+
});
26+
});

src/transpile.ts

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@ import {
1212
purgeDeepLinkDecoratorTSTransform }
1313
from './deep-linking/util';
1414

15+
import {
16+
convertDeepLinkConfigEntriesToString,
17+
getUpdatedAppNgModuleContentWithDeepLinkConfig,
18+
filterTypescriptFilesForDeepLinks,
19+
isDeepLinkingFile,
20+
purgeDeepLinkDecorator
21+
} from './deep-linking/util';
22+
1523
import { Logger } from './logger/logger';
1624
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from './logger/logger-diagnostics';
1725
import { runTypeScriptDiagnostics } from './logger/logger-typescript';
1826
import { inlineTemplate } from './template';
1927
import * as Constants from './util/constants';
2028
import { BuildError } from './util/errors';
2129
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';
2432

2533
export function transpile(context: BuildContext) {
2634

@@ -109,24 +117,36 @@ export function transpileWorker(context: BuildContext, workerConfig: TranspileWo
109117

110118
// let's start a new tsFiles object to cache all the transpiled files in
111119
const host = getInMemoryCompilerHostInstance(tsConfig.options);
112-
// ts.createCompilerHost(tsConfig.options);
113120

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());
115124

116-
const beforeArray: ts.TransformerFactory<ts.SourceFile>[] = [];
125+
// temporarily copy the files to a new location
126+
copyOriginalSourceFiles(context.fileCache);
117127

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);
121138
}
122139

140+
const program = ts.createProgram(tsFileNames, tsConfig.options, host, cachedProgram);
141+
142+
resetSourceFiles(context.fileCache);
143+
144+
const beforeArray: ts.TransformerFactory<ts.SourceFile>[] = [];
145+
123146
program.emit(undefined, (path: string, data: string, writeByteOrderMark: boolean, onError: Function, sourceFiles: ts.SourceFile[]) => {
124147
if (workerConfig.writeInMemory) {
125-
writeSourceFiles(context.fileCache, sourceFiles);
126148
writeTranspiledFilesCallback(context.fileCache, path, data, workerConfig.inlineTemplate);
127149
}
128-
}, null, false, {
129-
before: beforeArray
130150
});
131151

132152
// cache the typescript program for later use
@@ -181,26 +201,19 @@ function transpileUpdateWorker(event: string, filePath: string, context: BuildCo
181201

182202
const beforeArray: ts.TransformerFactory<ts.SourceFile>[] = [];
183203

184-
if (workerConfig.useTransforms && getBooleanPropertyValue(Constants.ENV_PARSE_DEEPLINKS)) {
185-
beforeArray.push(purgeDeepLinkDecoratorTSTransform());
186-
beforeArray.push(getInjectDeepLinkConfigTypescriptTransform());
187-
}
188-
189204
const transpileOptions: ts.TranspileOptions = {
190205
compilerOptions: cachedTsConfig.options,
191206
fileName: filePath,
192207
reportDiagnostics: true,
193-
transformers: {
194-
before: beforeArray
195-
}
196208
};
197209

198210
// let's manually transpile just this one ts file
199211
// since it is an update, it's in memory already
200212
const sourceText = context.fileCache.get(filePath).content;
213+
const textToTranspile = workerConfig.useTransforms && getBooleanPropertyValue(Constants.ENV_PARSE_DEEPLINKS) ? transformSource(filePath, sourceText) : sourceText;
201214

202215
// transpile this one module
203-
const transpileOutput = ts.transpileModule(sourceText, transpileOptions);
216+
const transpileOutput = ts.transpileModule(textToTranspile, transpileOptions);
204217

205218
const diagnostics = runTypeScriptDiagnostics(context, transpileOutput.diagnostics);
206219

@@ -294,13 +307,6 @@ function cleanFileNames(context: BuildContext, fileNames: string[]) {
294307
return fileNames;
295308
}
296309

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-
304310
function writeTranspiledFilesCallback(fileCache: FileCache, sourcePath: string, data: string, shouldInlineTemplate: boolean) {
305311
sourcePath = path.normalize(path.resolve(sourcePath));
306312

@@ -390,6 +396,42 @@ export function transpileTsString(context: BuildContext, filePath: string, strin
390396
return ts.transpileModule(stringToTranspile, transpileOptions);
391397
}
392398

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';
393435

394436
let cachedProgram: ts.Program = null;
395437
let cachedTsConfig: TsConfig = null;

0 commit comments

Comments
 (0)