|
| 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 | + |
| 9 | +import * as path from 'canonical-path'; |
| 10 | +import * as fs from 'fs'; |
| 11 | +import * as ts from 'typescript'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Helper functions for computing dependencies. |
| 15 | + */ |
| 16 | +export class DependencyHost { |
| 17 | + /** |
| 18 | + * Get a list of the resolved paths to all the dependencies of this entry point. |
| 19 | + * @param from An absolute path to the file whose dependencies we want to get. |
| 20 | + * @param resolved A set that will have the resolved dependencies added to it. |
| 21 | + * @param missing A set that will have the dependencies that could not be found added to it. |
| 22 | + * @param internal A set that is used to track internal dependencies to prevent getting stuck in a |
| 23 | + * circular dependency loop. |
| 24 | + * @returns an object containing an array of absolute paths to `resolved` depenendencies and an |
| 25 | + * array of import specifiers for dependencies that were `missing`. |
| 26 | + */ |
| 27 | + computeDependencies( |
| 28 | + from: string, resolved: Set<string>, missing: Set<string>, |
| 29 | + internal: Set<string> = new Set()): void { |
| 30 | + const fromContents = fs.readFileSync(from, 'utf8'); |
| 31 | + if (!this.hasImportOrReeportStatements(fromContents)) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // Parse the source into a TypeScript AST and then walk it looking for imports and re-exports. |
| 36 | + const sf = |
| 37 | + ts.createSourceFile(from, fromContents, ts.ScriptTarget.ES2015, false, ts.ScriptKind.JS); |
| 38 | + sf.statements |
| 39 | + // filter out statements that are not imports or reexports |
| 40 | + .filter(this.isStringImportOrReexport) |
| 41 | + // Grab the id of the module that is being imported |
| 42 | + .map(stmt => stmt.moduleSpecifier.text) |
| 43 | + // Resolve this module id into an absolute path |
| 44 | + .forEach(importPath => { |
| 45 | + if (importPath.startsWith('.')) { |
| 46 | + // This is an internal import so follow it |
| 47 | + const internalDependency = this.resolveInternal(from, importPath); |
| 48 | + // Avoid circular dependencies |
| 49 | + if (!internal.has(internalDependency)) { |
| 50 | + internal.add(internalDependency); |
| 51 | + this.computeDependencies(internalDependency, resolved, missing, internal); |
| 52 | + } |
| 53 | + } else { |
| 54 | + const externalDependency = this.tryResolveExternal(from, importPath); |
| 55 | + if (externalDependency !== null) { |
| 56 | + resolved.add(externalDependency); |
| 57 | + } else { |
| 58 | + missing.add(importPath); |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Resolve an internal module import. |
| 66 | + * @param from the absolute file path from where to start trying to resolve this module |
| 67 | + * @param to the module specifier of the internal dependency to resolve |
| 68 | + * @returns the resolved path to the import. |
| 69 | + */ |
| 70 | + resolveInternal(from: string, to: string): string { |
| 71 | + const fromDirectory = path.dirname(from); |
| 72 | + // `fromDirectory` is absolute so we don't need to worry about telling `require.resolve` |
| 73 | + // about it - unlike `tryResolve` below. |
| 74 | + return require.resolve(path.resolve(fromDirectory, to)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * We don't want to resolve external dependencies directly because if it is a path to a |
| 79 | + * sub-entry-point (e.g. @angular/animations/browser rather than @angular/animations) |
| 80 | + * then `require.resolve()` may return a path to a UMD bundle, which may actually live |
| 81 | + * in the folder containing the sub-entry-point |
| 82 | + * (e.g. @angular/animations/bundles/animations-browser.umd.js). |
| 83 | + * |
| 84 | + * Instead we try to resolve it as a package, which is what we would need anyway for it to be |
| 85 | + * compilable by ngcc. |
| 86 | + * |
| 87 | + * If `to` is actually a path to a file then this will fail, which is what we want. |
| 88 | + * |
| 89 | + * @param from the file path from where to start trying to resolve this module |
| 90 | + * @param to the module specifier of the dependency to resolve |
| 91 | + * @returns the resolved path to the entry point directory of the import or null |
| 92 | + * if it cannot be resolved. |
| 93 | + */ |
| 94 | + tryResolveExternal(from: string, to: string): string|null { |
| 95 | + const externalDependency = this.tryResolve(from, `${to}/package.json`); |
| 96 | + return externalDependency && path.dirname(externalDependency); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Resolve the absolute path of a module from a particular starting point. |
| 101 | + * |
| 102 | + * @param from the file path from where to start trying to resolve this module |
| 103 | + * @param to the module specifier of the dependency to resolve |
| 104 | + * @returns an absolute path to the entry-point of the dependency or null if it could not be |
| 105 | + * resolved. |
| 106 | + */ |
| 107 | + tryResolve(from: string, to: string): string|null { |
| 108 | + try { |
| 109 | + return require.resolve(to, {paths: [from]}); |
| 110 | + } catch (e) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Check whether the given statement is an import with a string literal module specifier. |
| 117 | + * @param stmt the statement node to check. |
| 118 | + * @returns true if the statement is an import with a string literal module specifier. |
| 119 | + */ |
| 120 | + isStringImportOrReexport(stmt: ts.Statement): stmt is ts.ImportDeclaration& |
| 121 | + {moduleSpecifier: ts.StringLiteral} { |
| 122 | + return ts.isImportDeclaration(stmt) || |
| 123 | + ts.isExportDeclaration(stmt) && !!stmt.moduleSpecifier && |
| 124 | + ts.isStringLiteral(stmt.moduleSpecifier); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check whether a source file needs to be parsed for imports. |
| 129 | + * This is a performance short-circuit, which saves us from creating |
| 130 | + * a TypeScript AST unnecessarily. |
| 131 | + * |
| 132 | + * @param source The content of the source file to check. |
| 133 | + * |
| 134 | + * @returns false if there are definitely no import or re-export statements |
| 135 | + * in this file, true otherwise. |
| 136 | + */ |
| 137 | + hasImportOrReeportStatements(source: string): boolean { |
| 138 | + return /(import|export)\s.+from/.test(source); |
| 139 | + } |
| 140 | +} |
0 commit comments