Skip to content

Commit cff2ca5

Browse files
committed
fix(app): support for backtick in Routing lazy-loading syntax
fix #1164
1 parent a4d95cf commit cff2ca5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/utils/router-parser.util.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class RouterParserUtil {
2323
private cleanModulesTree;
2424
private modulesWithRoutes = [];
2525
private transformAngular8ImportSyntax =
26-
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>"import\((\\'|'|")([^'"]+?)(\\'|'|")\)\.then\(\(?\w+?\)?=>\S+?\.([^)]+?)\)(\\'|'|")/g;
26+
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>"import\((\\'|'|"|`)([^'"]+?)(\\'|'|"|`)\)\.then\(\(?\w+?\)?=>\S+?\.([^)]+?)\)(\\'|'|")/g;
2727
private transformAngular8ImportSyntaxAsyncAwait =
28-
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>\("import\((\\'|'|")([^'"]+?)(\\'|'|")\)"\)\.['"]([^)]+?)['"]/g;
28+
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>\("import\((\\'|'|"|`)([^'"]+?)(\\'|'|"|`)\)"\)\.['"]([^)]+?)['"]/g;
2929

3030
private static instance: RouterParserUtil;
3131
private constructor() {}
@@ -594,8 +594,7 @@ export class RouterParserUtil {
594594

595595
if (foundWithAliasInImports) {
596596
if (typeof searchedImport !== 'undefined') {
597-
598-
const routePathIsBad = (path) => {
597+
const routePathIsBad = path => {
599598
return typeof ast.getSourceFile(path) == 'undefined';
600599
};
601600

@@ -604,7 +603,9 @@ export class RouterParserUtil {
604603
if (searchStrLen == 0) {
605604
return [];
606605
}
607-
var startIndex = 0, index, indices = [];
606+
var startIndex = 0,
607+
index,
608+
indices = [];
608609
if (!caseSensitive) {
609610
str = str.toLowerCase();
610611
searchStr = searchStr.toLowerCase();
@@ -614,22 +615,28 @@ export class RouterParserUtil {
614615
startIndex = index + searchStrLen;
615616
}
616617
return indices;
617-
}
618+
};
618619

619620
const dirNamePath = path.dirname(file.getFilePath());
620621
const searchedImportPath = searchedImport.getModuleSpecifierValue();
621622
const leadingFilePath = searchedImportPath.split('/').shift();
622623

623-
let importPath = path.resolve(dirNamePath + '/' + searchedImport.getModuleSpecifierValue() + '.ts');
624+
let importPath = path.resolve(
625+
dirNamePath + '/' + searchedImport.getModuleSpecifierValue() + '.ts'
626+
);
624627

625628
if (routePathIsBad(importPath)) {
626629
let leadingIndices = getIndicesOf(leadingFilePath, importPath, true);
627-
if (leadingIndices.length > 1) { // Nested route fixes
630+
if (leadingIndices.length > 1) {
631+
// Nested route fixes
628632
let startIndex = leadingIndices[0];
629-
let endIndex = leadingIndices[leadingIndices.length -1];
630-
importPath = importPath.slice(0, startIndex) + importPath.slice(endIndex);
631-
} else { // Top level route fixes
632-
importPath = path.dirname(dirNamePath) + '/' + searchedImportPath + '.ts';
633+
let endIndex = leadingIndices[leadingIndices.length - 1];
634+
importPath =
635+
importPath.slice(0, startIndex) + importPath.slice(endIndex);
636+
} else {
637+
// Top level route fixes
638+
importPath =
639+
path.dirname(dirNamePath) + '/' + searchedImportPath + '.ts';
633640
}
634641
}
635642
const sourceFileImport =

test/fixtures/todomvc-ng2-simple-routing-standard-async/src/app/app-routing.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Routes, RouterModule } from '@angular/router';
33

44
export const APP_ROUTES: Routes = [
55
{ path: 'about', loadChildren: async () => (await import('./about/about.module')).AboutModule },
6+
{ path: 'toto', loadChildren: async () => (await import(`./toto/toto.module`)).TotoModule },
67
{ path: '', redirectTo: 'home', pathMatch: 'full' },
78
{ path: '**', redirectTo: 'home', pathMatch: 'full' }
89
];

0 commit comments

Comments
 (0)