Skip to content

Commit

Permalink
fix(app): support for backtick in Routing lazy-loading syntax
Browse files Browse the repository at this point in the history
fix #1164
  • Loading branch information
vogloblinsky committed Dec 15, 2021
1 parent a4d95cf commit cff2ca5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/utils/router-parser.util.ts
Expand Up @@ -23,9 +23,9 @@ export class RouterParserUtil {
private cleanModulesTree;
private modulesWithRoutes = [];
private transformAngular8ImportSyntax =
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>"import\((\\'|'|")([^'"]+?)(\\'|'|")\)\.then\(\(?\w+?\)?=>\S+?\.([^)]+?)\)(\\'|'|")/g;
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>"import\((\\'|'|"|`)([^'"]+?)(\\'|'|"|`)\)\.then\(\(?\w+?\)?=>\S+?\.([^)]+?)\)(\\'|'|")/g;
private transformAngular8ImportSyntaxAsyncAwait =
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>\("import\((\\'|'|")([^'"]+?)(\\'|'|")\)"\)\.['"]([^)]+?)['"]/g;
/(['"]loadChildren['"]:)\(\)(:[^)]+?)?=>\("import\((\\'|'|"|`)([^'"]+?)(\\'|'|"|`)\)"\)\.['"]([^)]+?)['"]/g;

private static instance: RouterParserUtil;
private constructor() {}
Expand Down Expand Up @@ -594,8 +594,7 @@ export class RouterParserUtil {

if (foundWithAliasInImports) {
if (typeof searchedImport !== 'undefined') {

const routePathIsBad = (path) => {
const routePathIsBad = path => {
return typeof ast.getSourceFile(path) == 'undefined';
};

Expand All @@ -604,7 +603,9 @@ export class RouterParserUtil {
if (searchStrLen == 0) {
return [];
}
var startIndex = 0, index, indices = [];
var startIndex = 0,
index,
indices = [];
if (!caseSensitive) {
str = str.toLowerCase();
searchStr = searchStr.toLowerCase();
Expand All @@ -614,22 +615,28 @@ export class RouterParserUtil {
startIndex = index + searchStrLen;
}
return indices;
}
};

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

let importPath = path.resolve(dirNamePath + '/' + searchedImport.getModuleSpecifierValue() + '.ts');
let importPath = path.resolve(
dirNamePath + '/' + searchedImport.getModuleSpecifierValue() + '.ts'
);

if (routePathIsBad(importPath)) {
let leadingIndices = getIndicesOf(leadingFilePath, importPath, true);
if (leadingIndices.length > 1) { // Nested route fixes
if (leadingIndices.length > 1) {
// Nested route fixes
let startIndex = leadingIndices[0];
let endIndex = leadingIndices[leadingIndices.length -1];
importPath = importPath.slice(0, startIndex) + importPath.slice(endIndex);
} else { // Top level route fixes
importPath = path.dirname(dirNamePath) + '/' + searchedImportPath + '.ts';
let endIndex = leadingIndices[leadingIndices.length - 1];
importPath =
importPath.slice(0, startIndex) + importPath.slice(endIndex);
} else {
// Top level route fixes
importPath =
path.dirname(dirNamePath) + '/' + searchedImportPath + '.ts';
}
}
const sourceFileImport =
Expand Down
Expand Up @@ -3,6 +3,7 @@ import { Routes, RouterModule } from '@angular/router';

export const APP_ROUTES: Routes = [
{ path: 'about', loadChildren: async () => (await import('./about/about.module')).AboutModule },
{ path: 'toto', loadChildren: async () => (await import(`./toto/toto.module`)).TotoModule },
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', redirectTo: 'home', pathMatch: 'full' }
];
Expand Down

0 comments on commit cff2ca5

Please sign in to comment.