|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { dirname, join } from 'path'; |
| 9 | +import * as ts from 'typescript'; |
| 10 | +import { findAstNodes, resolve } from './refactor'; |
| 11 | + |
| 12 | + |
| 13 | +function _getContentOfKeyLiteral(_source: ts.SourceFile, node: ts.Node): string | null { |
| 14 | + if (node.kind == ts.SyntaxKind.Identifier) { |
| 15 | + return (node as ts.Identifier).text; |
| 16 | + } else if (node.kind == ts.SyntaxKind.StringLiteral) { |
| 17 | + return (node as ts.StringLiteral).text; |
| 18 | + } else { |
| 19 | + return null; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +export interface LazyRouteMap { |
| 25 | + [path: string]: string; |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +export function findLazyRoutes( |
| 30 | + filePath: string, |
| 31 | + host: ts.CompilerHost, |
| 32 | + program?: ts.Program, |
| 33 | + compilerOptions?: ts.CompilerOptions, |
| 34 | +): LazyRouteMap { |
| 35 | + if (!compilerOptions) { |
| 36 | + if (!program) { |
| 37 | + throw new Error('Must pass either program or compilerOptions to findLazyRoutes.'); |
| 38 | + } |
| 39 | + compilerOptions = program.getCompilerOptions(); |
| 40 | + } |
| 41 | + const fileName = resolve(filePath, host, compilerOptions).replace(/\\/g, '/'); |
| 42 | + let sourceFile: ts.SourceFile | undefined; |
| 43 | + if (program) { |
| 44 | + sourceFile = program.getSourceFile(fileName); |
| 45 | + } |
| 46 | + |
| 47 | + if (!sourceFile) { |
| 48 | + const content = host.readFile(fileName); |
| 49 | + if (content) { |
| 50 | + sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (!sourceFile) { |
| 55 | + throw new Error(`Source file not found: '${fileName}'.`); |
| 56 | + } |
| 57 | + const sf: ts.SourceFile = sourceFile; |
| 58 | + |
| 59 | + return findAstNodes(null, sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true) |
| 60 | + // Get all their property assignments. |
| 61 | + .map((node: ts.ObjectLiteralExpression) => { |
| 62 | + return findAstNodes(node, sf, ts.SyntaxKind.PropertyAssignment, false); |
| 63 | + }) |
| 64 | + // Take all `loadChildren` elements. |
| 65 | + .reduce((acc: ts.PropertyAssignment[], props: ts.PropertyAssignment[]) => { |
| 66 | + return acc.concat(props.filter(literal => { |
| 67 | + return _getContentOfKeyLiteral(sf, literal.name) == 'loadChildren'; |
| 68 | + })); |
| 69 | + }, []) |
| 70 | + // Get only string values. |
| 71 | + .filter((node: ts.PropertyAssignment) => node.initializer.kind == ts.SyntaxKind.StringLiteral) |
| 72 | + // Get the string value. |
| 73 | + .map((node: ts.PropertyAssignment) => (node.initializer as ts.StringLiteral).text) |
| 74 | + // Map those to either [path, absoluteModulePath], or [path, null] if the module pointing to |
| 75 | + // does not exist. |
| 76 | + .map((routePath: string) => { |
| 77 | + const moduleName = routePath.split('#')[0]; |
| 78 | + const compOptions = (program && program.getCompilerOptions()) || compilerOptions || {}; |
| 79 | + const resolvedModuleName: ts.ResolvedModuleWithFailedLookupLocations = moduleName[0] == '.' |
| 80 | + ? ({ |
| 81 | + resolvedModule: { resolvedFileName: join(dirname(filePath), moduleName) + '.ts' }, |
| 82 | + } as ts.ResolvedModuleWithFailedLookupLocations) |
| 83 | + : ts.resolveModuleName(moduleName, filePath, compOptions, host); |
| 84 | + if (resolvedModuleName.resolvedModule |
| 85 | + && resolvedModuleName.resolvedModule.resolvedFileName |
| 86 | + && host.fileExists(resolvedModuleName.resolvedModule.resolvedFileName)) { |
| 87 | + return [routePath, resolvedModuleName.resolvedModule.resolvedFileName]; |
| 88 | + } else { |
| 89 | + return [routePath, null]; |
| 90 | + } |
| 91 | + }) |
| 92 | + // Reduce to the LazyRouteMap map. |
| 93 | + .reduce((acc: LazyRouteMap, [routePath, resolvedModuleName]: [string, string | null]) => { |
| 94 | + if (resolvedModuleName) { |
| 95 | + acc[routePath] = resolvedModuleName; |
| 96 | + } |
| 97 | + |
| 98 | + return acc; |
| 99 | + }, {}); |
| 100 | +} |
0 commit comments