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

Commit 62f05fc

Browse files
committed
fix(deep-linking): works when there isn't a valid deep link config
1 parent 05b981d commit 62f05fc

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

src/aot/aot-compiler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,14 @@ export class AotCompiler {
112112
modifiedFileContent = getFallbackMainContent();
113113
}
114114

115-
116115
Logger.debug(`[AotCompiler] compile: Modified File Content: ${modifiedFileContent}`);
117116
this.context.fileCache.set(this.options.entryPoint, { path: this.options.entryPoint, content: modifiedFileContent});
118117
Logger.debug('[AotCompiler] compile: Starting to process and modify entry point ... DONE');
119118
})
120119
.then(() => {
121-
Logger.debug('[AotCompiler] compile: Removing decorators from program files ...');
120+
Logger.debug('[AotCompiler] compile: Transpiling files ...');
122121
transpileFiles(this.context, this.tsConfig, this.fileSystem);
123-
Logger.debug('[AotCompiler] compile: Removing decorators from program files ... DONE');
122+
Logger.debug('[AotCompiler] compile: Transpiling files ... DONE');
124123
}).then(() => {
125124
return {
126125
lazyLoadedModuleDictionary: this.lazyLoadedModuleDictionary
@@ -154,6 +153,7 @@ function errorCheckProgram(context: BuildContext, tsConfig: ParsedTsConfig, comp
154153
function transpileFiles(context: BuildContext, tsConfig: ParsedTsConfig, fileSystem: HybridFileSystem) {
155154
const tsFiles = context.fileCache.getAll().filter(file => extname(file.path) === '.ts' && file.path.indexOf('.d.ts') === -1);
156155
for (const tsFile of tsFiles) {
156+
Logger.debug(`[AotCompiler] transpileFiles: Transpiling file ${tsFile.path} ...`);
157157
const transpileOutput = transpileFileContent(tsFile.path, tsFile.content, tsConfig.parsed.options);
158158
const diagnostics = runTypeScriptDiagnostics(context, transpileOutput.diagnostics);
159159
if (diagnostics.length) {
@@ -167,9 +167,10 @@ function transpileFiles(context: BuildContext, tsConfig: ParsedTsConfig, fileSys
167167
fileSystem.addVirtualFile(jsFilePath + '.map', transpileOutput.sourceMapText);
168168

169169
// write files to disk here if debug is enabled
170-
if (isDebugMode() || true) {
170+
if (isDebugMode()) {
171171
writeNgcFilesToDisk(context, tsFile.path, tsFile.content, transpileOutput.outputText, transpileOutput.sourceMapText);
172172
}
173+
Logger.debug(`[AotCompiler] transpileFiles: Transpiling file ${tsFile.path} ... DONE`);
173174
}
174175
}
175176

src/deep-linking/util.spec.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,47 @@ export function getSharedIonicModule() {
5454
expect(results[2].name).toEqual('PageTwo');
5555
});
5656
});
57-
/*describe('getDeepLinkData', () => {
58-
it('should convert the paths to absolute paths', () => {
57+
58+
describe('getDeepLinkData', () => {
59+
it('should return an empty list when no valid deep links are found', () => {
60+
61+
const fileContent = `
62+
import { NgModule } from '@angular/core';
63+
import { IonicApp, IonicModule } from 'ionic-angular';
64+
import { MyApp } from './app.component';
65+
import { HomePage } from '../pages/home/home';
66+
67+
import * as Constants from '../util/constants';
68+
69+
@NgModule({
70+
declarations: [
71+
MyApp,
72+
HomePage
73+
],
74+
imports: [
75+
getSharedIonicModule()
76+
],
77+
bootstrap: [IonicApp],
78+
entryComponents: [
79+
MyApp,
80+
HomePage
81+
],
82+
providers: []
83+
})
84+
export class AppModule {}
85+
86+
export function getSharedIonicModule() {
87+
return IonicModule.forRoot(MyApp, {});
88+
}
89+
`;
90+
91+
const srcDir = '/Users/dan/Dev/myApp/src';
92+
const result = util.getDeepLinkData(join(srcDir, 'app/app.module.ts'), fileContent);
93+
expect(result).toBeTruthy();
94+
expect(result.length).toEqual(0);
95+
});
96+
97+
it('should return a hydrated deep link config', () => {
5998

6099
const fileContent = `
61100
import { NgModule } from '@angular/core';
@@ -85,21 +124,27 @@ export class AppModule {}
85124
export function getSharedIonicModule() {
86125
return IonicModule.forRoot(MyApp, {}, {
87126
links: [
88-
{ path: '../pages/home/home.module', namedExport: 'HomePageModule', name: 'Home' },
89-
{ path: '../pages/page-one/page-one.module', namedExport: 'PageOneModule', name: 'PageOne' },
90-
{ path: '../pages/page-two/page-two.module', namedExport: 'PageTwoModule', name: 'PageTwo' }
127+
{ loadChildren: '../pages/home/home.module#HomePageModule', name: 'Home' },
128+
{ loadChildren: '../pages/page-one/page-one.module#PageOneModule', name: 'PageOne' },
129+
{ loadChildren: '../pages/page-two/page-two.module#PageTwoModule', name: 'PageTwo' }
91130
]
92131
});
93132
}
94133
`;
95134

96135
const srcDir = '/Users/dan/Dev/myApp/src';
97136
const result = util.getDeepLinkData(join(srcDir, 'app/app.module.ts'), fileContent);
98-
expect(result).toBeTruthy();
99-
expect(result[0].absolutePath).toEqual(join(srcDir, 'pages/home/home.module.ts'));
100-
expect(result[1].absolutePath).toEqual(join(srcDir, 'page-one/page-one.module.ts'));
101-
expect(result[2].absolutePath).toEqual(join(srcDir, 'page-two/page-two.module.ts'));
137+
expect(result[0].modulePath).toEqual('../pages/home/home.module');
138+
expect(result[0].name).toEqual('Home');
139+
expect(result[0].absolutePath).toEqual('/Users/dan/Dev/myApp/src/pages/home/home.module.ts');
140+
141+
expect(result[1].modulePath).toEqual('../pages/page-one/page-one.module');
142+
expect(result[1].name).toEqual('PageOne');
143+
expect(result[1].absolutePath).toEqual('/Users/dan/Dev/myApp/src/pages/page-one/page-one.module.ts');
144+
145+
expect(result[2].modulePath).toEqual('../pages/page-two/page-two.module');
146+
expect(result[2].name).toEqual('PageTwo');
147+
expect(result[2].absolutePath).toEqual('/Users/dan/Dev/myApp/src/pages/page-two/page-two.module.ts');
102148
});
103149
});
104-
*/
105-
});
150+
});

src/deep-linking/util.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import { DeepLinkConfigEntry, HydratedDeepLinkConfigEntry } from '../util/interf
55

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

1414
export function extractDeepLinkPathData(appNgModuleFileContent: string) {
1515
const linksInternalContent = getLinksArrayContent(appNgModuleFileContent);
16+
if (!linksInternalContent) {
17+
return null;
18+
}
1619
const pathsList = extractRegexContent(appNgModuleFileContent, LOAD_CHILDREN_REGEX);
1720
const nameList = extractRegexContent(appNgModuleFileContent, NAME_REGEX);
1821

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

4952
export function getDeepLinkData(appNgModuleFilePath: string, appNgModuleFileContent: string): HydratedDeepLinkConfigEntry[] {
5053
const deepLinkConfigList = extractDeepLinkPathData(appNgModuleFileContent);
54+
if (!deepLinkConfigList) {
55+
return [];
56+
}
5157
const appDirectory = dirname(appNgModuleFilePath);
5258
const hydratedDeepLinks = deepLinkConfigList.map(deepLinkConfigEntry => {
5359
return Object.assign({}, deepLinkConfigEntry, {

src/util/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ export function generateContext(context?: BuildContext): BuildContext {
9090

9191
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')));
9292
setProcessEnvVar(Constants.ENV_APP_NG_MODULE_PATH, appNgModulePath);
93+
console.log('appNgModulePath: ', appNgModulePath);
9394

94-
const appNgModuleClass = resolve(getConfigValue(context, '--appNgModuleClass', null, Constants.ENV_APP_NG_MODULE_CLASS, Constants.ENV_APP_NG_MODULE_CLASS.toLowerCase(), 'AppModule'));
95+
const appNgModuleClass = getConfigValue(context, '--appNgModuleClass', null, Constants.ENV_APP_NG_MODULE_CLASS, Constants.ENV_APP_NG_MODULE_CLASS.toLowerCase(), 'AppModule');
9596
setProcessEnvVar(Constants.ENV_APP_NG_MODULE_CLASS, appNgModuleClass);
97+
console.log('appNgModuleClass: ', appNgModuleClass);
9698

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

0 commit comments

Comments
 (0)