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): works when there isn't a valid deep link config
Browse files Browse the repository at this point in the history
  • Loading branch information
danbucholtz committed Jan 13, 2017
1 parent 05b981d commit 62f05fc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/aot/aot-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ export class AotCompiler {
modifiedFileContent = getFallbackMainContent();
}


Logger.debug(`[AotCompiler] compile: Modified File Content: ${modifiedFileContent}`);
this.context.fileCache.set(this.options.entryPoint, { path: this.options.entryPoint, content: modifiedFileContent});
Logger.debug('[AotCompiler] compile: Starting to process and modify entry point ... DONE');
})
.then(() => {
Logger.debug('[AotCompiler] compile: Removing decorators from program files ...');
Logger.debug('[AotCompiler] compile: Transpiling files ...');
transpileFiles(this.context, this.tsConfig, this.fileSystem);
Logger.debug('[AotCompiler] compile: Removing decorators from program files ... DONE');
Logger.debug('[AotCompiler] compile: Transpiling files ... DONE');
}).then(() => {
return {
lazyLoadedModuleDictionary: this.lazyLoadedModuleDictionary
Expand Down Expand Up @@ -154,6 +153,7 @@ function errorCheckProgram(context: BuildContext, tsConfig: ParsedTsConfig, comp
function transpileFiles(context: BuildContext, tsConfig: ParsedTsConfig, fileSystem: HybridFileSystem) {
const tsFiles = context.fileCache.getAll().filter(file => extname(file.path) === '.ts' && file.path.indexOf('.d.ts') === -1);
for (const tsFile of tsFiles) {
Logger.debug(`[AotCompiler] transpileFiles: Transpiling file ${tsFile.path} ...`);
const transpileOutput = transpileFileContent(tsFile.path, tsFile.content, tsConfig.parsed.options);
const diagnostics = runTypeScriptDiagnostics(context, transpileOutput.diagnostics);
if (diagnostics.length) {
Expand All @@ -167,9 +167,10 @@ function transpileFiles(context: BuildContext, tsConfig: ParsedTsConfig, fileSys
fileSystem.addVirtualFile(jsFilePath + '.map', transpileOutput.sourceMapText);

// write files to disk here if debug is enabled
if (isDebugMode() || true) {
if (isDebugMode()) {
writeNgcFilesToDisk(context, tsFile.path, tsFile.content, transpileOutput.outputText, transpileOutput.sourceMapText);
}
Logger.debug(`[AotCompiler] transpileFiles: Transpiling file ${tsFile.path} ... DONE`);
}
}

Expand Down
67 changes: 56 additions & 11 deletions src/deep-linking/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,47 @@ export function getSharedIonicModule() {
expect(results[2].name).toEqual('PageTwo');
});
});
/*describe('getDeepLinkData', () => {
it('should convert the paths to absolute paths', () => {

describe('getDeepLinkData', () => {
it('should return an empty list when no valid deep links are found', () => {

const fileContent = `
import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import * as Constants from '../util/constants';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
getSharedIonicModule()
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: []
})
export class AppModule {}
export function getSharedIonicModule() {
return IonicModule.forRoot(MyApp, {});
}
`;

const srcDir = '/Users/dan/Dev/myApp/src';
const result = util.getDeepLinkData(join(srcDir, 'app/app.module.ts'), fileContent);
expect(result).toBeTruthy();
expect(result.length).toEqual(0);
});

it('should return a hydrated deep link config', () => {

const fileContent = `
import { NgModule } from '@angular/core';
Expand Down Expand Up @@ -85,21 +124,27 @@ export class AppModule {}
export function getSharedIonicModule() {
return IonicModule.forRoot(MyApp, {}, {
links: [
{ path: '../pages/home/home.module', namedExport: 'HomePageModule', name: 'Home' },
{ path: '../pages/page-one/page-one.module', namedExport: 'PageOneModule', name: 'PageOne' },
{ path: '../pages/page-two/page-two.module', namedExport: 'PageTwoModule', name: 'PageTwo' }
{ loadChildren: '../pages/home/home.module#HomePageModule', name: 'Home' },
{ loadChildren: '../pages/page-one/page-one.module#PageOneModule', name: 'PageOne' },
{ loadChildren: '../pages/page-two/page-two.module#PageTwoModule', name: 'PageTwo' }
]
});
}
`;

const srcDir = '/Users/dan/Dev/myApp/src';
const result = util.getDeepLinkData(join(srcDir, 'app/app.module.ts'), fileContent);
expect(result).toBeTruthy();
expect(result[0].absolutePath).toEqual(join(srcDir, 'pages/home/home.module.ts'));
expect(result[1].absolutePath).toEqual(join(srcDir, 'page-one/page-one.module.ts'));
expect(result[2].absolutePath).toEqual(join(srcDir, 'page-two/page-two.module.ts'));
expect(result[0].modulePath).toEqual('../pages/home/home.module');
expect(result[0].name).toEqual('Home');
expect(result[0].absolutePath).toEqual('/Users/dan/Dev/myApp/src/pages/home/home.module.ts');

expect(result[1].modulePath).toEqual('../pages/page-one/page-one.module');
expect(result[1].name).toEqual('PageOne');
expect(result[1].absolutePath).toEqual('/Users/dan/Dev/myApp/src/pages/page-one/page-one.module.ts');

expect(result[2].modulePath).toEqual('../pages/page-two/page-two.module');
expect(result[2].name).toEqual('PageTwo');
expect(result[2].absolutePath).toEqual('/Users/dan/Dev/myApp/src/pages/page-two/page-two.module.ts');
});
});
*/
});
});
12 changes: 9 additions & 3 deletions src/deep-linking/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { DeepLinkConfigEntry, HydratedDeepLinkConfigEntry } from '../util/interf

function getLinksArrayContent(appNgModuleFileContent: string) {
const deepLinksContentMatches = LINKS_REGEX.exec(appNgModuleFileContent.toString());
if (!deepLinksContentMatches || deepLinksContentMatches.length !== 2) {
throw new Error('Could not locate links array in deep links config');
if (deepLinksContentMatches && deepLinksContentMatches.length === 2) {
return deepLinksContentMatches[1];
}
return deepLinksContentMatches[1];
return null;
}

export function extractDeepLinkPathData(appNgModuleFileContent: string) {
const linksInternalContent = getLinksArrayContent(appNgModuleFileContent);
if (!linksInternalContent) {
return null;
}
const pathsList = extractRegexContent(appNgModuleFileContent, LOAD_CHILDREN_REGEX);
const nameList = extractRegexContent(appNgModuleFileContent, NAME_REGEX);

Expand Down Expand Up @@ -48,6 +51,9 @@ function extractRegexContent(content: string, regex: RegExp) {

export function getDeepLinkData(appNgModuleFilePath: string, appNgModuleFileContent: string): HydratedDeepLinkConfigEntry[] {
const deepLinkConfigList = extractDeepLinkPathData(appNgModuleFileContent);
if (!deepLinkConfigList) {
return [];
}
const appDirectory = dirname(appNgModuleFilePath);
const hydratedDeepLinks = deepLinkConfigList.map(deepLinkConfigEntry => {
return Object.assign({}, deepLinkConfigEntry, {
Expand Down
4 changes: 3 additions & 1 deletion src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ export function generateContext(context?: BuildContext): BuildContext {

const appNgModulePath = resolve(getConfigValue(context, '--appNgModulePath', null, Constants.ENV_APP_NG_MODULE_PATH, Constants.ENV_APP_NG_MODULE_PATH.toLowerCase(), join(context.srcDir, 'app', 'app.module.ts')));
setProcessEnvVar(Constants.ENV_APP_NG_MODULE_PATH, appNgModulePath);
console.log('appNgModulePath: ', appNgModulePath);

const appNgModuleClass = resolve(getConfigValue(context, '--appNgModuleClass', null, Constants.ENV_APP_NG_MODULE_CLASS, Constants.ENV_APP_NG_MODULE_CLASS.toLowerCase(), 'AppModule'));
const appNgModuleClass = getConfigValue(context, '--appNgModuleClass', null, Constants.ENV_APP_NG_MODULE_CLASS, Constants.ENV_APP_NG_MODULE_CLASS.toLowerCase(), 'AppModule');
setProcessEnvVar(Constants.ENV_APP_NG_MODULE_CLASS, appNgModuleClass);
console.log('appNgModuleClass: ', appNgModuleClass);

setProcessEnvVar(Constants.ENV_GLOB_UTIL, join(getProcessEnvVar(Constants.ENV_VAR_APP_SCRIPTS_DIR), 'dist', 'util', 'glob-util.js'));

Expand Down

0 comments on commit 62f05fc

Please sign in to comment.