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

Commit 266a871

Browse files
intodevelopmentliamdebeasi
authored andcommitted
fix(): always serve latest file changes (#1521)
When running `ionic serve` it often happens that the transpiled files become out of date. After making a change to one of the TypeScript source files, ionic serve runs, but when looking at the built JavaScript files, the new code is not present. I found that the app-scripts build process kept two file caches. One in the app-script context and one in the so called InMemoryCompilerHost. The first was updated when files changed, but the latter wasn't. The InMemoryCompilerHost is in my opinion superfluous, because it got injected a HybridFileSystem, which nicely uses the app-scripts context fileCache. My solution is to rename the InMemoryCompilerHost to FileSystemCompilerHost and let the injected HybridFileSystem handle the caching. This solution not only fixes the out-of-date-files issue, but also redecuses memory usage of app-scripts.
1 parent 5d7ca38 commit 266a871

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

src/aot/aot-compiler.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717

1818
import { HybridFileSystem } from '../util/hybrid-file-system';
1919
import { getInstance as getHybridFileSystem } from '../util/hybrid-file-system-factory';
20-
import { getInMemoryCompilerHostInstance } from './compiler-host-factory';
21-
import { InMemoryCompilerHost } from './compiler-host';
20+
import { getFileSystemCompilerHostInstance } from './compiler-host-factory';
21+
import { FileSystemCompilerHost } from './compiler-host';
2222
import { getFallbackMainContent, replaceBootstrapImpl } from './utils';
2323
import { Logger } from '../logger/logger';
2424
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from '../logger/logger-diagnostics';
@@ -40,7 +40,7 @@ export async function runAot(context: BuildContext, options: AotOptions) {
4040
const aggregateCompilerOption = Object.assign(tsConfig.options, angularCompilerOptions);
4141

4242
const fileSystem = getHybridFileSystem(false);
43-
const compilerHost = getInMemoryCompilerHostInstance(tsConfig.options);
43+
const compilerHost = getFileSystemCompilerHostInstance(tsConfig.options);
4444
// todo, consider refactoring at some point
4545
const tsProgram = createProgram(tsConfig.fileNames, tsConfig.options, compilerHost);
4646

@@ -77,7 +77,7 @@ export async function runAot(context: BuildContext, options: AotOptions) {
7777
}
7878
}
7979

80-
function errorCheckProgram(context: BuildContext, tsConfig: TsConfig, compilerHost: InMemoryCompilerHost, cachedProgram: Program) {
80+
function errorCheckProgram(context: BuildContext, tsConfig: TsConfig, compilerHost: FileSystemCompilerHost, cachedProgram: Program) {
8181
// Create a new Program, based on the old one. This will trigger a resolution of all
8282
// transitive modules, which include files that might just have been generated.
8383
const program = createProgram(tsConfig.fileNames, tsConfig.options, compilerHost, cachedProgram);

src/aot/compiler-host-factory.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { CompilerOptions } from 'typescript';
2-
import { InMemoryCompilerHost } from './compiler-host';
2+
import { FileSystemCompilerHost } from './compiler-host';
33
import { getInstance as getFileSystemInstance } from '../util/hybrid-file-system-factory';
44

5-
let instance: InMemoryCompilerHost = null;
5+
let instance: FileSystemCompilerHost = null;
66

7-
export function getInMemoryCompilerHostInstance(options: CompilerOptions) {
7+
export function getFileSystemCompilerHostInstance(options: CompilerOptions) {
88
if (!instance) {
9-
instance = new InMemoryCompilerHost(options, getFileSystemInstance(false));
9+
instance = new FileSystemCompilerHost(options, getFileSystemInstance(false));
1010
}
1111
return instance;
1212
}

src/aot/compiler-host.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ export interface OnErrorFn {
88
(message: string): void;
99
}
1010

11-
export class InMemoryCompilerHost implements CompilerHost {
12-
private sourceFileMap: Map<string, SourceFile>;
11+
export class FileSystemCompilerHost implements CompilerHost {
1312
private diskCompilerHost: CompilerHost;
1413

1514
constructor(private options: CompilerOptions, private fileSystem: VirtualFileSystem, private setParentNodes = true) {
1615
this.diskCompilerHost = createCompilerHost(this.options, this.setParentNodes);
17-
this.sourceFileMap = new Map<string, SourceFile>();
1816
}
1917

2018
fileExists(filePath: string): boolean {
@@ -64,19 +62,13 @@ export class InMemoryCompilerHost implements CompilerHost {
6462

6563
getSourceFile(filePath: string, languageVersion: ScriptTarget, onError?: OnErrorFn) {
6664
filePath = normalize(filePath);
67-
const existingSourceFile = this.sourceFileMap.get(filePath);
68-
if (existingSourceFile) {
69-
return existingSourceFile;
70-
}
7165
// we haven't created a source file for this yet, so try to use what's in memory
7266
const fileContentFromMemory = this.fileSystem.getFileContent(filePath);
7367
if (fileContentFromMemory) {
7468
const typescriptSourceFile = getTypescriptSourceFile(filePath, fileContentFromMemory, languageVersion, this.setParentNodes);
75-
this.sourceFileMap.set(filePath, typescriptSourceFile);
7669
return typescriptSourceFile;
7770
}
7871
const diskSourceFile = this.diskCompilerHost.getSourceFile(filePath, languageVersion, onError);
79-
this.sourceFileMap.set(filePath, diskSourceFile);
8072
return diskSourceFile;
8173
}
8274

src/transpile.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from 'path';
55

66
import * as ts from 'typescript';
77

8-
import { getInMemoryCompilerHostInstance } from './aot/compiler-host-factory';
8+
import { getFileSystemCompilerHostInstance } from './aot/compiler-host-factory';
99
import { buildJsSourceMaps } from './bundle';
1010
import {
1111
getInjectDeepLinkConfigTypescriptTransform,
@@ -117,7 +117,7 @@ export function transpileWorker(context: BuildContext, workerConfig: TranspileWo
117117
tsConfig.options.declaration = undefined;
118118

119119
// let's start a new tsFiles object to cache all the transpiled files in
120-
const host = getInMemoryCompilerHostInstance(tsConfig.options);
120+
const host = getFileSystemCompilerHostInstance(tsConfig.options);
121121

122122
if (workerConfig.useTransforms && getBooleanPropertyValue(Constants.ENV_PARSE_DEEPLINKS)) {
123123
// beforeArray.push(purgeDeepLinkDecoratorTSTransform());

0 commit comments

Comments
 (0)