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

Commit 63c4c7f

Browse files
committed
fix(deep-linking): convert deep linking to use TS Transform. DeepLinking now works on TypeScript src instead of on transpiled JS code
1 parent ab31be4 commit 63c4c7f

17 files changed

+484
-1538
lines changed

src/aot/aot-compiler.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import AngularCompilerOptions from '@angular/tsc-wrapped/src/options';
99

1010
import { HybridFileSystem } from '../util/hybrid-file-system';
1111
import { getInstance as getHybridFileSystem } from '../util/hybrid-file-system-factory';
12-
import { getInstance } from './compiler-host-factory';
13-
import { NgcCompilerHost } from './compiler-host';
12+
import { getInMemoryCompilerHostInstance } from './compiler-host-factory';
13+
import { InMemoryCompilerHost } from './compiler-host';
1414
import { getFallbackMainContent, replaceBootstrap } from './utils';
1515
import { Logger } from '../logger/logger';
1616
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from '../logger/logger-diagnostics';
@@ -26,7 +26,7 @@ export class AotCompiler {
2626
private tsConfig: ParsedTsConfig;
2727
private angularCompilerOptions: AngularCompilerOptions;
2828
private program: Program;
29-
private compilerHost: NgcCompilerHost;
29+
private compilerHost: InMemoryCompilerHost;
3030
private fileSystem: HybridFileSystem;
3131
private lazyLoadedModuleDictionary: any;
3232

@@ -39,7 +39,7 @@ export class AotCompiler {
3939
});
4040

4141
this.fileSystem = getHybridFileSystem(false);
42-
this.compilerHost = getInstance(this.tsConfig.parsed.options);
42+
this.compilerHost = getInMemoryCompilerHostInstance(this.tsConfig.parsed.options);
4343
this.program = createProgram(this.tsConfig.parsed.fileNames, this.tsConfig.parsed.options, this.compilerHost);
4444
}
4545

@@ -68,16 +68,6 @@ export class AotCompiler {
6868
Logger.debug('[AotCompiler] compile: Creating and validating new TypeScript Program ...');
6969
this.program = errorCheckProgram(this.context, this.tsConfig, this.compilerHost, this.program);
7070
Logger.debug('[AotCompiler] compile: Creating and validating new TypeScript Program ... DONE');
71-
})
72-
.then(() => {
73-
74-
Logger.debug('[AotCompiler] compile: The following files are included in the program: ');
75-
for ( const fileName of this.tsConfig.parsed.fileNames) {
76-
Logger.debug(`[AotCompiler] compile: ${fileName}`);
77-
const cleanedFileName = normalize(resolve(fileName));
78-
const content = readFileSync(cleanedFileName).toString();
79-
this.context.fileCache.set(cleanedFileName, { path: cleanedFileName, content: content});
80-
}
8171
}).then(() => {
8272
Logger.debug('[AotCompiler] compile: Starting to process and modify entry point ...');
8373
const mainFile = this.context.fileCache.get(this.options.entryPoint);
@@ -114,7 +104,7 @@ export class AotCompiler {
114104
}
115105
}
116106

117-
function errorCheckProgram(context: BuildContext, tsConfig: ParsedTsConfig, compilerHost: NgcCompilerHost, cachedProgram: Program) {
107+
function errorCheckProgram(context: BuildContext, tsConfig: ParsedTsConfig, compilerHost: InMemoryCompilerHost, cachedProgram: Program) {
118108
// Create a new Program, based on the old one. This will trigger a resolution of all
119109
// transitive modules, which include files that might just have been generated.
120110
const program = createProgram(tsConfig.parsed.fileNames, tsConfig.parsed.options, compilerHost, cachedProgram);

src/aot/compiler-host-factory.ts

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

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

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

src/aot/compiler-host.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface OnErrorFn {
77
(message: string): void;
88
}
99

10-
export class NgcCompilerHost implements CompilerHost {
10+
export class InMemoryCompilerHost implements CompilerHost {
1111
private sourceFileMap: Map<string, SourceFile>;
1212
private diskCompilerHost: CompilerHost;
1313

@@ -68,7 +68,6 @@ export class NgcCompilerHost implements CompilerHost {
6868
this.sourceFileMap.set(filePath, typescriptSourceFile);
6969
return typescriptSourceFile;
7070
}
71-
// dang, it's not in memory, load it from disk and cache it
7271
const diskSourceFile = this.diskCompilerHost.getSourceFile(filePath, languageVersion, onError);
7372
this.sourceFileMap.set(filePath, diskSourceFile);
7473
return diskSourceFile;

src/build.spec.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import * as Constants from './util/constants';
22
import { BuildContext } from './util/interfaces';
33
import * as helpers from './util/helpers';
44
import * as build from './build';
5+
import * as buildUtils from './build/util';
56

67
import * as bundle from './bundle';
78
import * as copy from './copy';
89
import * as clean from './clean';
10+
import * as deepLinking from './deep-linking';
911
import * as lint from './lint';
1012
import * as minify from './minify';
1113
import * as ngc from './ngc';
@@ -26,8 +28,10 @@ describe('build', () => {
2628
});
2729
});
2830

31+
spyOn(buildUtils, buildUtils.scanSrcTsFiles.name).and.returnValue(Promise.resolve());
2932
spyOn(bundle, bundle.bundle.name).and.returnValue(Promise.resolve());
3033
spyOn(copy, copy.copy.name).and.returnValue(Promise.resolve());
34+
spyOn(deepLinking, deepLinking.deepLinking.name).and.returnValue(Promise.resolve());
3135
spyOn(minify, minify.minifyCss.name).and.returnValue(Promise.resolve());
3236
spyOn(minify, minify.minifyJs.name).and.returnValue(Promise.resolve());
3337
spyOn(lint, lint.lint.name).and.returnValue(Promise.resolve());
@@ -50,19 +54,19 @@ describe('build', () => {
5054
const getBooleanPropertyValueSpy = spyOn(helpers, helpers.getBooleanPropertyValue.name).and.returnValue(true);
5155

5256
return build.build(context).then(() => {
57+
expect(buildUtils.scanSrcTsFiles).toHaveBeenCalled();
5358
expect(helpers.readFileAsync).toHaveBeenCalled();
5459
expect(copy.copy).toHaveBeenCalled();
60+
expect(deepLinking.deepLinking).toHaveBeenCalled();
5561
expect(ngc.ngc).toHaveBeenCalled();
5662
expect(bundle.bundle).toHaveBeenCalled();
5763
expect(minify.minifyJs).toHaveBeenCalled();
5864
expect(sass.sass).toHaveBeenCalled();
5965
expect(minify.minifyCss).toHaveBeenCalled();
6066
expect(lint.lint).toHaveBeenCalled();
61-
expect(getBooleanPropertyValueSpy.calls.first().args[0]).toEqual(Constants.ENV_ENABLE_LINT);
67+
expect(getBooleanPropertyValueSpy.calls.all()[1].args[0]).toEqual(Constants.ENV_ENABLE_LINT);
6268

6369
expect(transpile.transpile).not.toHaveBeenCalled();
64-
}).catch(err => {
65-
expect(true).toEqual(false);
6670
});
6771
});
6872

@@ -78,20 +82,20 @@ describe('build', () => {
7882
const getBooleanPropertyValueSpy = spyOn(helpers, helpers.getBooleanPropertyValue.name).and.returnValue(true);
7983

8084
return build.build(context).then(() => {
85+
expect(buildUtils.scanSrcTsFiles).toHaveBeenCalled();
8186
expect(helpers.readFileAsync).toHaveBeenCalled();
8287
expect(copy.copy).toHaveBeenCalled();
88+
expect(deepLinking.deepLinking).toHaveBeenCalled();
8389
expect(transpile.transpile).toHaveBeenCalled();
8490
expect(bundle.bundle).toHaveBeenCalled();
8591
expect(sass.sass).toHaveBeenCalled();
8692
expect(lint.lint).toHaveBeenCalled();
87-
expect(getBooleanPropertyValueSpy.calls.first().args[0]).toEqual(Constants.ENV_ENABLE_LINT);
93+
expect(getBooleanPropertyValueSpy.calls.all()[1].args[0]).toEqual(Constants.ENV_ENABLE_LINT);
8894
expect(postprocess.postprocess).toHaveBeenCalled();
8995
expect(preprocess.preprocess).toHaveBeenCalled();
9096
expect(ngc.ngc).not.toHaveBeenCalled();
9197
expect(minify.minifyJs).not.toHaveBeenCalled();
9298
expect(minify.minifyCss).not.toHaveBeenCalled();
93-
}).catch(err => {
94-
expect(true).toEqual(false);
9599
});
96100
});
97101

@@ -107,20 +111,19 @@ describe('build', () => {
107111
const getBooleanPropertyValueSpy = spyOn(helpers, helpers.getBooleanPropertyValue.name).and.returnValue(false);
108112

109113
return build.build(context).then(() => {
114+
expect(buildUtils.scanSrcTsFiles).toHaveBeenCalled();
110115
expect(helpers.readFileAsync).toHaveBeenCalled();
111116
expect(copy.copy).toHaveBeenCalled();
112117
expect(transpile.transpile).toHaveBeenCalled();
113118
expect(bundle.bundle).toHaveBeenCalled();
114119
expect(sass.sass).toHaveBeenCalled();
115120
expect(lint.lint).not.toHaveBeenCalled();
116-
expect(getBooleanPropertyValueSpy.calls.first().args[0]).toEqual(Constants.ENV_ENABLE_LINT);
121+
expect(getBooleanPropertyValueSpy.calls.all()[1].args[0]).toEqual(Constants.ENV_ENABLE_LINT);
117122
expect(postprocess.postprocess).toHaveBeenCalled();
118123
expect(preprocess.preprocess).toHaveBeenCalled();
119124
expect(ngc.ngc).not.toHaveBeenCalled();
120125
expect(minify.minifyJs).not.toHaveBeenCalled();
121126
expect(minify.minifyCss).not.toHaveBeenCalled();
122-
}).catch(err => {
123-
expect(true).toEqual(false);
124127
});
125128
});
126129
});
@@ -190,10 +193,11 @@ describe('test project requirements before building', () => {
190193
});
191194
});
192195

193-
it('should succeed if IONIC_TS_CONFIG file contains compilerOptions.sourceMap === true', () => {
196+
it('should succeed if IONIC_TS_CONFIG file contains compilerOptions.sourceMap is true', () => {
194197
process.env[Constants.ENV_APP_ENTRY_POINT] = 'src/app/main.ts';
195198
process.env[Constants.ENV_TS_CONFIG] = 'tsConfig.js';
196199

200+
spyOn(buildUtils, buildUtils.scanSrcTsFiles.name).and.returnValue(Promise.resolve());
197201
spyOn(bundle, bundle.bundle.name).and.returnValue(Promise.resolve());
198202
spyOn(clean, clean.clean.name);
199203
spyOn(copy, copy.copy.name).and.returnValue(Promise.resolve());

src/build.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { join } from 'path';
2+
import { scanSrcTsFiles } from './build/util';
13
import * as Constants from './util/constants';
24
import { BuildContext, BuildState, BuildUpdateMessage, ChangedFile } from './util/interfaces';
35
import { BuildError } from './util/errors';
@@ -6,6 +8,7 @@ import { getBooleanPropertyValue, readFileAsync, setContext } from './util/helpe
68
import { bundle, bundleUpdate } from './bundle';
79
import { clean } from './clean';
810
import { copy } from './copy';
11+
import { deepLinking, deepLinkingUpdate } from './deep-linking';
912
import { lint, lintUpdate } from './lint';
1013
import { Logger } from './logger/logger';
1114
import { minifyCss, minifyJs } from './minify';
@@ -98,9 +101,17 @@ function buildProject(context: BuildContext) {
98101
buildId++;
99102

100103
const copyPromise = copy(context);
101-
const compilePromise = (context.runAot) ? ngc(context) : transpile(context);
102104

103-
return compilePromise
105+
return scanSrcTsFiles(context)
106+
.then(() => {
107+
if (getBooleanPropertyValue(Constants.ENV_PARSE_DEEPLINKS)) {
108+
return deepLinking(context);
109+
}
110+
})
111+
.then(() => {
112+
const compilePromise = (context.runAot) ? ngc(context) : transpile(context);
113+
return compilePromise;
114+
})
104115
.then(() => {
105116
return preprocess(context);
106117
})
@@ -224,6 +235,12 @@ function buildUpdateTasks(changedFiles: ChangedFile[], context: BuildContext) {
224235
};
225236

226237
return loadFiles(changedFiles, context)
238+
.then(() => {
239+
// DEEP LINKING
240+
if (getBooleanPropertyValue(Constants.ENV_PARSE_DEEPLINKS)) {
241+
return deepLinkingUpdate(changedFiles, context);
242+
}
243+
})
227244
.then(() => {
228245
// TEMPLATE
229246
if (context.templateState === BuildState.RequiresUpdate) {

src/build/util.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { join } from 'path';
2+
import { GlobResult, globAll } from '../util/glob-util';
3+
import { readFileAsync } from '../util/helpers';
4+
import { BuildContext } from '../util/interfaces';
5+
6+
export function scanSrcTsFiles(context: BuildContext) {
7+
const tsFileGlob = join(context.srcDir, '**', '*.ts');
8+
return globAll([tsFileGlob]).then((results: GlobResult[]) => {
9+
const promises = results.map(result => {
10+
const promise = readFileAsync(result.absolutePath);
11+
promise.then((fileContent: string) => {
12+
context.fileCache.set(result.absolutePath, { path: result.absolutePath, content: fileContent});
13+
});
14+
return promise;
15+
});
16+
return Promise.all(promises);
17+
});
18+
}

0 commit comments

Comments
 (0)