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

Commit

Permalink
fix(deep-linking): convert deep linking to use TS Transform. DeepLink…
Browse files Browse the repository at this point in the history
…ing now works on TypeScript src instead of on transpiled JS code
  • Loading branch information
danbucholtz committed Sep 20, 2017
1 parent ab31be4 commit 63c4c7f
Show file tree
Hide file tree
Showing 17 changed files with 484 additions and 1,538 deletions.
20 changes: 5 additions & 15 deletions src/aot/aot-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import AngularCompilerOptions from '@angular/tsc-wrapped/src/options';

import { HybridFileSystem } from '../util/hybrid-file-system';
import { getInstance as getHybridFileSystem } from '../util/hybrid-file-system-factory';
import { getInstance } from './compiler-host-factory';
import { NgcCompilerHost } from './compiler-host';
import { getInMemoryCompilerHostInstance } from './compiler-host-factory';
import { InMemoryCompilerHost } from './compiler-host';
import { getFallbackMainContent, replaceBootstrap } from './utils';
import { Logger } from '../logger/logger';
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from '../logger/logger-diagnostics';
Expand All @@ -26,7 +26,7 @@ export class AotCompiler {
private tsConfig: ParsedTsConfig;
private angularCompilerOptions: AngularCompilerOptions;
private program: Program;
private compilerHost: NgcCompilerHost;
private compilerHost: InMemoryCompilerHost;
private fileSystem: HybridFileSystem;
private lazyLoadedModuleDictionary: any;

Expand All @@ -39,7 +39,7 @@ export class AotCompiler {
});

this.fileSystem = getHybridFileSystem(false);
this.compilerHost = getInstance(this.tsConfig.parsed.options);
this.compilerHost = getInMemoryCompilerHostInstance(this.tsConfig.parsed.options);
this.program = createProgram(this.tsConfig.parsed.fileNames, this.tsConfig.parsed.options, this.compilerHost);
}

Expand Down Expand Up @@ -68,16 +68,6 @@ export class AotCompiler {
Logger.debug('[AotCompiler] compile: Creating and validating new TypeScript Program ...');
this.program = errorCheckProgram(this.context, this.tsConfig, this.compilerHost, this.program);
Logger.debug('[AotCompiler] compile: Creating and validating new TypeScript Program ... DONE');
})
.then(() => {

Logger.debug('[AotCompiler] compile: The following files are included in the program: ');
for ( const fileName of this.tsConfig.parsed.fileNames) {
Logger.debug(`[AotCompiler] compile: ${fileName}`);
const cleanedFileName = normalize(resolve(fileName));
const content = readFileSync(cleanedFileName).toString();
this.context.fileCache.set(cleanedFileName, { path: cleanedFileName, content: content});
}
}).then(() => {
Logger.debug('[AotCompiler] compile: Starting to process and modify entry point ...');
const mainFile = this.context.fileCache.get(this.options.entryPoint);
Expand Down Expand Up @@ -114,7 +104,7 @@ export class AotCompiler {
}
}

function errorCheckProgram(context: BuildContext, tsConfig: ParsedTsConfig, compilerHost: NgcCompilerHost, cachedProgram: Program) {
function errorCheckProgram(context: BuildContext, tsConfig: ParsedTsConfig, compilerHost: InMemoryCompilerHost, cachedProgram: Program) {
// Create a new Program, based on the old one. This will trigger a resolution of all
// transitive modules, which include files that might just have been generated.
const program = createProgram(tsConfig.parsed.fileNames, tsConfig.parsed.options, compilerHost, cachedProgram);
Expand Down
8 changes: 4 additions & 4 deletions src/aot/compiler-host-factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CompilerOptions } from 'typescript';
import { NgcCompilerHost } from './compiler-host';
import { InMemoryCompilerHost } from './compiler-host';
import { getInstance as getFileSystemInstance } from '../util/hybrid-file-system-factory';

let instance: NgcCompilerHost = null;
let instance: InMemoryCompilerHost = null;

export function getInstance(options: CompilerOptions) {
export function getInMemoryCompilerHostInstance(options: CompilerOptions) {
if (!instance) {
instance = new NgcCompilerHost(options, getFileSystemInstance(false));
instance = new InMemoryCompilerHost(options, getFileSystemInstance(false));
}
return instance;
}
3 changes: 1 addition & 2 deletions src/aot/compiler-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface OnErrorFn {
(message: string): void;
}

export class NgcCompilerHost implements CompilerHost {
export class InMemoryCompilerHost implements CompilerHost {
private sourceFileMap: Map<string, SourceFile>;
private diskCompilerHost: CompilerHost;

Expand Down Expand Up @@ -68,7 +68,6 @@ export class NgcCompilerHost implements CompilerHost {
this.sourceFileMap.set(filePath, typescriptSourceFile);
return typescriptSourceFile;
}
// dang, it's not in memory, load it from disk and cache it
const diskSourceFile = this.diskCompilerHost.getSourceFile(filePath, languageVersion, onError);
this.sourceFileMap.set(filePath, diskSourceFile);
return diskSourceFile;
Expand Down
24 changes: 14 additions & 10 deletions src/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as Constants from './util/constants';
import { BuildContext } from './util/interfaces';
import * as helpers from './util/helpers';
import * as build from './build';
import * as buildUtils from './build/util';

import * as bundle from './bundle';
import * as copy from './copy';
import * as clean from './clean';
import * as deepLinking from './deep-linking';
import * as lint from './lint';
import * as minify from './minify';
import * as ngc from './ngc';
Expand All @@ -26,8 +28,10 @@ describe('build', () => {
});
});

spyOn(buildUtils, buildUtils.scanSrcTsFiles.name).and.returnValue(Promise.resolve());
spyOn(bundle, bundle.bundle.name).and.returnValue(Promise.resolve());
spyOn(copy, copy.copy.name).and.returnValue(Promise.resolve());
spyOn(deepLinking, deepLinking.deepLinking.name).and.returnValue(Promise.resolve());
spyOn(minify, minify.minifyCss.name).and.returnValue(Promise.resolve());
spyOn(minify, minify.minifyJs.name).and.returnValue(Promise.resolve());
spyOn(lint, lint.lint.name).and.returnValue(Promise.resolve());
Expand All @@ -50,19 +54,19 @@ describe('build', () => {
const getBooleanPropertyValueSpy = spyOn(helpers, helpers.getBooleanPropertyValue.name).and.returnValue(true);

return build.build(context).then(() => {
expect(buildUtils.scanSrcTsFiles).toHaveBeenCalled();
expect(helpers.readFileAsync).toHaveBeenCalled();
expect(copy.copy).toHaveBeenCalled();
expect(deepLinking.deepLinking).toHaveBeenCalled();
expect(ngc.ngc).toHaveBeenCalled();
expect(bundle.bundle).toHaveBeenCalled();
expect(minify.minifyJs).toHaveBeenCalled();
expect(sass.sass).toHaveBeenCalled();
expect(minify.minifyCss).toHaveBeenCalled();
expect(lint.lint).toHaveBeenCalled();
expect(getBooleanPropertyValueSpy.calls.first().args[0]).toEqual(Constants.ENV_ENABLE_LINT);
expect(getBooleanPropertyValueSpy.calls.all()[1].args[0]).toEqual(Constants.ENV_ENABLE_LINT);

expect(transpile.transpile).not.toHaveBeenCalled();
}).catch(err => {
expect(true).toEqual(false);
});
});

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

return build.build(context).then(() => {
expect(buildUtils.scanSrcTsFiles).toHaveBeenCalled();
expect(helpers.readFileAsync).toHaveBeenCalled();
expect(copy.copy).toHaveBeenCalled();
expect(deepLinking.deepLinking).toHaveBeenCalled();
expect(transpile.transpile).toHaveBeenCalled();
expect(bundle.bundle).toHaveBeenCalled();
expect(sass.sass).toHaveBeenCalled();
expect(lint.lint).toHaveBeenCalled();
expect(getBooleanPropertyValueSpy.calls.first().args[0]).toEqual(Constants.ENV_ENABLE_LINT);
expect(getBooleanPropertyValueSpy.calls.all()[1].args[0]).toEqual(Constants.ENV_ENABLE_LINT);
expect(postprocess.postprocess).toHaveBeenCalled();
expect(preprocess.preprocess).toHaveBeenCalled();
expect(ngc.ngc).not.toHaveBeenCalled();
expect(minify.minifyJs).not.toHaveBeenCalled();
expect(minify.minifyCss).not.toHaveBeenCalled();
}).catch(err => {
expect(true).toEqual(false);
});
});

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

return build.build(context).then(() => {
expect(buildUtils.scanSrcTsFiles).toHaveBeenCalled();
expect(helpers.readFileAsync).toHaveBeenCalled();
expect(copy.copy).toHaveBeenCalled();
expect(transpile.transpile).toHaveBeenCalled();
expect(bundle.bundle).toHaveBeenCalled();
expect(sass.sass).toHaveBeenCalled();
expect(lint.lint).not.toHaveBeenCalled();
expect(getBooleanPropertyValueSpy.calls.first().args[0]).toEqual(Constants.ENV_ENABLE_LINT);
expect(getBooleanPropertyValueSpy.calls.all()[1].args[0]).toEqual(Constants.ENV_ENABLE_LINT);
expect(postprocess.postprocess).toHaveBeenCalled();
expect(preprocess.preprocess).toHaveBeenCalled();
expect(ngc.ngc).not.toHaveBeenCalled();
expect(minify.minifyJs).not.toHaveBeenCalled();
expect(minify.minifyCss).not.toHaveBeenCalled();
}).catch(err => {
expect(true).toEqual(false);
});
});
});
Expand Down Expand Up @@ -190,10 +193,11 @@ describe('test project requirements before building', () => {
});
});

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

spyOn(buildUtils, buildUtils.scanSrcTsFiles.name).and.returnValue(Promise.resolve());
spyOn(bundle, bundle.bundle.name).and.returnValue(Promise.resolve());
spyOn(clean, clean.clean.name);
spyOn(copy, copy.copy.name).and.returnValue(Promise.resolve());
Expand Down
21 changes: 19 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { join } from 'path';
import { scanSrcTsFiles } from './build/util';
import * as Constants from './util/constants';
import { BuildContext, BuildState, BuildUpdateMessage, ChangedFile } from './util/interfaces';
import { BuildError } from './util/errors';
Expand All @@ -6,6 +8,7 @@ import { getBooleanPropertyValue, readFileAsync, setContext } from './util/helpe
import { bundle, bundleUpdate } from './bundle';
import { clean } from './clean';
import { copy } from './copy';
import { deepLinking, deepLinkingUpdate } from './deep-linking';
import { lint, lintUpdate } from './lint';
import { Logger } from './logger/logger';
import { minifyCss, minifyJs } from './minify';
Expand Down Expand Up @@ -98,9 +101,17 @@ function buildProject(context: BuildContext) {
buildId++;

const copyPromise = copy(context);
const compilePromise = (context.runAot) ? ngc(context) : transpile(context);

return compilePromise
return scanSrcTsFiles(context)
.then(() => {
if (getBooleanPropertyValue(Constants.ENV_PARSE_DEEPLINKS)) {
return deepLinking(context);
}
})
.then(() => {
const compilePromise = (context.runAot) ? ngc(context) : transpile(context);
return compilePromise;
})
.then(() => {
return preprocess(context);
})
Expand Down Expand Up @@ -224,6 +235,12 @@ function buildUpdateTasks(changedFiles: ChangedFile[], context: BuildContext) {
};

return loadFiles(changedFiles, context)
.then(() => {
// DEEP LINKING
if (getBooleanPropertyValue(Constants.ENV_PARSE_DEEPLINKS)) {
return deepLinkingUpdate(changedFiles, context);
}
})
.then(() => {
// TEMPLATE
if (context.templateState === BuildState.RequiresUpdate) {
Expand Down
18 changes: 18 additions & 0 deletions src/build/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { join } from 'path';
import { GlobResult, globAll } from '../util/glob-util';
import { readFileAsync } from '../util/helpers';
import { BuildContext } from '../util/interfaces';

export function scanSrcTsFiles(context: BuildContext) {
const tsFileGlob = join(context.srcDir, '**', '*.ts');
return globAll([tsFileGlob]).then((results: GlobResult[]) => {
const promises = results.map(result => {
const promise = readFileAsync(result.absolutePath);
promise.then((fileContent: string) => {
context.fileCache.set(result.absolutePath, { path: result.absolutePath, content: fileContent});
});
return promise;
});
return Promise.all(promises);
});
}
Loading

0 comments on commit 63c4c7f

Please sign in to comment.