2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-05-27 19:22:16 -04:00
|
|
|
import {AngularCompilerOptions, MetadataCollector, ModuleMetadata} from '@angular/tsc-wrapped';
|
2016-04-29 00:57:16 -04:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2016-06-08 19:38:52 -04:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
|
|
import {AssetUrl, ImportGenerator} from './compiler_private';
|
|
|
|
import {StaticReflectorHost, StaticSymbol} from './static_reflector';
|
2016-05-24 13:53:48 -04:00
|
|
|
|
2016-04-29 00:57:16 -04:00
|
|
|
const EXT = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
|
|
|
|
const DTS = /\.d\.ts$/;
|
2016-08-26 18:41:50 -04:00
|
|
|
const NODE_MODULES = '/node_modules/';
|
2016-08-12 20:38:29 -04:00
|
|
|
const IS_GENERATED = /\.(ngfactory|css(\.shim)?)$/;
|
2016-04-29 00:57:16 -04:00
|
|
|
|
2016-06-09 17:51:53 -04:00
|
|
|
export interface ReflectorHostContext {
|
2016-07-13 14:15:23 -04:00
|
|
|
fileExists(fileName: string): boolean;
|
|
|
|
directoryExists(directoryName: string): boolean;
|
|
|
|
readFile(fileName: string): string;
|
|
|
|
assumeFileExists(fileName: string): void;
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
|
2016-08-22 14:48:33 -04:00
|
|
|
protected metadataCollector = new MetadataCollector();
|
|
|
|
protected context: ReflectorHostContext;
|
2016-08-04 00:34:03 -04:00
|
|
|
private isGenDirChildOfRootDir: boolean;
|
2016-08-22 14:48:33 -04:00
|
|
|
protected basePath: string;
|
2016-08-05 14:11:24 -04:00
|
|
|
private genDir: string;
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
2016-08-22 14:48:33 -04:00
|
|
|
protected program: ts.Program, protected compilerHost: ts.CompilerHost,
|
|
|
|
protected options: AngularCompilerOptions, context?: ReflectorHostContext) {
|
2016-08-05 14:11:24 -04:00
|
|
|
// normalize the path so that it never ends with '/'.
|
2016-08-26 18:41:50 -04:00
|
|
|
this.basePath = path.normalize(path.join(this.options.basePath, '.')).replace(/\\/g, '/');
|
|
|
|
this.genDir = path.normalize(path.join(this.options.genDir, '.')).replace(/\\/g, '/');
|
2016-08-05 14:11:24 -04:00
|
|
|
|
2016-06-09 17:51:53 -04:00
|
|
|
this.context = context || new NodeReflectorHostContext();
|
2016-08-05 14:11:24 -04:00
|
|
|
var genPath: string = path.relative(this.basePath, this.genDir);
|
2016-08-04 00:34:03 -04:00
|
|
|
this.isGenDirChildOfRootDir = genPath === '' || !genPath.startsWith('..');
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
2016-05-01 14:22:39 -04:00
|
|
|
|
|
|
|
angularImportLocations() {
|
2016-05-24 13:53:48 -04:00
|
|
|
return {
|
|
|
|
coreDecorators: '@angular/core/src/metadata',
|
|
|
|
diDecorators: '@angular/core/src/di/decorators',
|
|
|
|
diMetadata: '@angular/core/src/di/metadata',
|
2016-06-13 18:56:51 -04:00
|
|
|
diOpaqueToken: '@angular/core/src/di/opaque_token',
|
2016-05-31 12:15:17 -04:00
|
|
|
animationMetadata: '@angular/core/src/animation/metadata',
|
2016-05-24 13:53:48 -04:00
|
|
|
provider: '@angular/core/src/di/provider'
|
|
|
|
};
|
2016-05-01 14:22:39 -04:00
|
|
|
}
|
2016-08-02 14:45:14 -04:00
|
|
|
|
2016-08-22 14:48:33 -04:00
|
|
|
// We use absolute paths on disk as canonical.
|
|
|
|
getCanonicalFileName(fileName: string): string { return fileName; }
|
|
|
|
|
|
|
|
protected resolve(m: string, containingFile: string) {
|
2016-04-29 00:57:16 -04:00
|
|
|
const resolved =
|
2016-08-26 18:41:50 -04:00
|
|
|
ts.resolveModuleName(m, containingFile.replace(/\\/g, '/'), this.options, this.context)
|
|
|
|
.resolvedModule;
|
2016-05-02 12:38:46 -04:00
|
|
|
return resolved ? resolved.resolvedFileName : null;
|
2016-04-29 00:57:16 -04:00
|
|
|
};
|
|
|
|
|
2016-08-22 14:48:33 -04:00
|
|
|
protected normalizeAssetUrl(url: string): string {
|
2016-05-03 20:31:40 -04:00
|
|
|
let assetUrl = AssetUrl.parse(url);
|
2016-08-22 14:48:33 -04:00
|
|
|
const path = assetUrl ? `${assetUrl.packageName}/${assetUrl.modulePath}` : null;
|
|
|
|
return this.getCanonicalFileName(path);
|
2016-05-03 20:31:40 -04:00
|
|
|
}
|
2016-05-02 12:38:46 -04:00
|
|
|
|
2016-08-22 14:48:33 -04:00
|
|
|
protected resolveAssetUrl(url: string, containingFile: string): string {
|
2016-05-03 20:31:40 -04:00
|
|
|
let assetUrl = this.normalizeAssetUrl(url);
|
2016-05-02 12:38:46 -04:00
|
|
|
if (assetUrl) {
|
2016-08-22 14:48:33 -04:00
|
|
|
return this.getCanonicalFileName(this.resolve(assetUrl, containingFile));
|
2016-05-02 12:38:46 -04:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2016-04-29 00:57:16 -04:00
|
|
|
/**
|
|
|
|
* We want a moduleId that will appear in import statements in the generated code.
|
|
|
|
* These need to be in a form that system.js can load, so absolute file paths don't work.
|
2016-08-04 00:34:03 -04:00
|
|
|
*
|
|
|
|
* The `containingFile` is always in the `genDir`, where as the `importedFile` can be in
|
2016-08-12 20:38:29 -04:00
|
|
|
* `genDir`, `node_module` or `basePath`. The `importedFile` is either a generated file or
|
|
|
|
* existing file.
|
2016-08-04 00:34:03 -04:00
|
|
|
*
|
|
|
|
* | genDir | node_module | rootDir
|
|
|
|
* --------------+----------+-------------+----------
|
|
|
|
* generated | relative | relative | n/a
|
|
|
|
* existing file | n/a | absolute | relative(*)
|
|
|
|
*
|
|
|
|
* NOTE: (*) the relative path is computed depending on `isGenDirChildOfRootDir`.
|
2016-04-29 00:57:16 -04:00
|
|
|
*/
|
2016-08-04 00:34:03 -04:00
|
|
|
getImportPath(containingFile: string, importedFile: string): string {
|
2016-05-03 00:21:10 -04:00
|
|
|
importedFile = this.resolveAssetUrl(importedFile, containingFile);
|
2016-05-02 12:38:46 -04:00
|
|
|
containingFile = this.resolveAssetUrl(containingFile, '');
|
2016-04-29 00:57:16 -04:00
|
|
|
|
2016-08-02 14:45:14 -04:00
|
|
|
// If a file does not yet exist (because we compile it later), we still need to
|
|
|
|
// assume it exists it so that the `resolve` method works!
|
2016-05-02 12:38:46 -04:00
|
|
|
if (!this.compilerHost.fileExists(importedFile)) {
|
2016-07-13 14:15:23 -04:00
|
|
|
this.context.assumeFileExists(importedFile);
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
2016-05-02 12:38:46 -04:00
|
|
|
|
2016-08-12 20:38:29 -04:00
|
|
|
containingFile = this.rewriteGenDirPath(containingFile);
|
|
|
|
const containingDir = path.dirname(containingFile);
|
|
|
|
// drop extension
|
|
|
|
importedFile = importedFile.replace(EXT, '');
|
|
|
|
|
|
|
|
var nodeModulesIndex = importedFile.indexOf(NODE_MODULES);
|
|
|
|
const importModule = nodeModulesIndex === -1 ?
|
|
|
|
null :
|
|
|
|
importedFile.substring(nodeModulesIndex + NODE_MODULES.length);
|
|
|
|
const isGeneratedFile = IS_GENERATED.test(importedFile);
|
|
|
|
|
|
|
|
if (isGeneratedFile) {
|
|
|
|
// rewrite to genDir path
|
|
|
|
if (importModule) {
|
|
|
|
// it is generated, therefore we do a relative path to the factory
|
|
|
|
return this.dotRelative(containingDir, this.genDir + NODE_MODULES + importModule);
|
|
|
|
} else {
|
|
|
|
// assume that import is also in `genDir`
|
|
|
|
importedFile = this.rewriteGenDirPath(importedFile);
|
|
|
|
return this.dotRelative(containingDir, importedFile);
|
2016-05-02 12:38:46 -04:00
|
|
|
}
|
2016-08-12 20:38:29 -04:00
|
|
|
} else {
|
|
|
|
// user code import
|
|
|
|
if (importModule) {
|
|
|
|
return importModule;
|
|
|
|
} else {
|
|
|
|
if (!this.isGenDirChildOfRootDir) {
|
|
|
|
// assume that they are on top of each other.
|
|
|
|
importedFile = importedFile.replace(this.basePath, this.genDir);
|
2016-08-04 00:34:03 -04:00
|
|
|
}
|
2016-08-12 20:38:29 -04:00
|
|
|
return this.dotRelative(containingDir, importedFile);
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-04 00:34:03 -04:00
|
|
|
}
|
2016-06-03 15:23:35 -04:00
|
|
|
|
2016-08-04 00:34:03 -04:00
|
|
|
private dotRelative(from: string, to: string): string {
|
2016-08-26 18:41:50 -04:00
|
|
|
var rPath: string = path.relative(from, to).replace(/\\/g, '/');
|
2016-08-04 00:34:03 -04:00
|
|
|
return rPath.startsWith('.') ? rPath : './' + rPath;
|
|
|
|
}
|
2016-06-03 15:23:35 -04:00
|
|
|
|
2016-08-12 20:38:29 -04:00
|
|
|
/**
|
|
|
|
* Moves the path into `genDir` folder while preserving the `node_modules` directory.
|
|
|
|
*/
|
|
|
|
private rewriteGenDirPath(filepath: string) {
|
|
|
|
var nodeModulesIndex = filepath.indexOf(NODE_MODULES);
|
|
|
|
if (nodeModulesIndex !== -1) {
|
|
|
|
// If we are in node_modulse, transplant them into `genDir`.
|
|
|
|
return path.join(this.genDir, filepath.substring(nodeModulesIndex));
|
|
|
|
} else {
|
|
|
|
// pretend that containing file is on top of the `genDir` to normalize the paths.
|
|
|
|
// we apply the `genDir` => `rootDir` delta through `rootDirPrefix` later.
|
|
|
|
return filepath.replace(this.basePath, this.genDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
findDeclaration(
|
|
|
|
module: string, symbolName: string, containingFile: string,
|
|
|
|
containingModule?: string): StaticSymbol {
|
2016-04-29 00:57:16 -04:00
|
|
|
if (!containingFile || !containingFile.length) {
|
2016-06-08 19:38:52 -04:00
|
|
|
if (module.indexOf('.') === 0) {
|
|
|
|
throw new Error('Resolution of relative paths requires a containing file.');
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
// Any containing file gives the same result for absolute imports
|
2016-08-05 14:11:24 -04:00
|
|
|
containingFile = path.join(this.basePath, 'index.ts');
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-05-03 20:31:40 -04:00
|
|
|
let assetUrl = this.normalizeAssetUrl(module);
|
|
|
|
if (assetUrl) {
|
|
|
|
module = assetUrl;
|
|
|
|
}
|
2016-04-29 00:57:16 -04:00
|
|
|
const filePath = this.resolve(module, containingFile);
|
|
|
|
|
|
|
|
if (!filePath) {
|
2016-07-11 20:26:35 -04:00
|
|
|
// If the file cannot be found the module is probably referencing a declared module
|
|
|
|
// for which there is no disambiguating file and we also don't need to track
|
|
|
|
// re-exports. Just use the module name.
|
|
|
|
return this.getStaticSymbol(module, symbolName);
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const tc = this.program.getTypeChecker();
|
|
|
|
const sf = this.program.getSourceFile(filePath);
|
2016-06-09 17:51:53 -04:00
|
|
|
if (!sf || !(<any>sf).symbol) {
|
|
|
|
// The source file was not needed in the compile but we do need the values from
|
2016-08-02 14:45:14 -04:00
|
|
|
// the corresponding .ts files stored in the .metadata.json file. Check the file
|
|
|
|
// for exports to see if the file is exported.
|
|
|
|
return this.resolveExportedSymbol(filePath, symbolName) ||
|
|
|
|
this.getStaticSymbol(filePath, symbolName);
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
2016-04-29 00:57:16 -04:00
|
|
|
|
|
|
|
let symbol = tc.getExportsOfModule((<any>sf).symbol).find(m => m.name === symbolName);
|
|
|
|
if (!symbol) {
|
|
|
|
throw new Error(`can't find symbol ${symbolName} exported from module ${filePath}`);
|
|
|
|
}
|
2016-04-29 00:58:51 -04:00
|
|
|
if (symbol &&
|
|
|
|
symbol.flags & ts.SymbolFlags.Alias) { // This is an alias, follow what it aliases
|
2016-04-29 00:57:16 -04:00
|
|
|
symbol = tc.getAliasedSymbol(symbol);
|
|
|
|
}
|
|
|
|
const declaration = symbol.getDeclarations()[0];
|
2016-08-22 14:48:33 -04:00
|
|
|
const declarationFile = this.getCanonicalFileName(declaration.getSourceFile().fileName);
|
2016-04-29 00:57:16 -04:00
|
|
|
|
2016-05-02 12:38:46 -04:00
|
|
|
return this.getStaticSymbol(declarationFile, symbol.getName());
|
2016-04-29 00:57:16 -04:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`can't resolve module ${module} from ${containingFile}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private typeCache = new Map<string, StaticSymbol>();
|
2016-08-02 14:45:14 -04:00
|
|
|
private resolverCache = new Map<string, ModuleMetadata>();
|
2016-04-29 00:57:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getStaticSymbol produces a Type whose metadata is known but whose implementation is not loaded.
|
|
|
|
* All types passed to the StaticResolver should be pseudo-types returned by this method.
|
|
|
|
*
|
|
|
|
* @param declarationFile the absolute path of the file where the symbol is declared
|
|
|
|
* @param name the name of the type.
|
|
|
|
*/
|
2016-08-23 14:58:12 -04:00
|
|
|
getStaticSymbol(declarationFile: string, name: string, members?: string[]): StaticSymbol {
|
|
|
|
const memberSuffix = members ? `.${ members.join('.')}` : '';
|
|
|
|
const key = `"${declarationFile}".${name}${memberSuffix}`;
|
2016-04-29 00:57:16 -04:00
|
|
|
let result = this.typeCache.get(key);
|
|
|
|
if (!result) {
|
2016-08-23 14:58:12 -04:00
|
|
|
result = new StaticSymbol(declarationFile, name, members);
|
2016-04-29 00:57:16 -04:00
|
|
|
this.typeCache.set(key, result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMetadataFor(filePath: string): ModuleMetadata {
|
2016-07-13 14:15:23 -04:00
|
|
|
if (!this.context.fileExists(filePath)) {
|
2016-07-11 20:26:35 -04:00
|
|
|
// If the file doesn't exists then we cannot return metadata for the file.
|
|
|
|
// This will occur if the user refernced a declared module for which no file
|
|
|
|
// exists for the module (i.e. jQuery or angularjs).
|
|
|
|
return;
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
if (DTS.test(filePath)) {
|
|
|
|
const metadataPath = filePath.replace(DTS, '.metadata.json');
|
2016-07-13 14:15:23 -04:00
|
|
|
if (this.context.fileExists(metadataPath)) {
|
2016-08-10 14:52:56 -04:00
|
|
|
const metadata = this.readMetadata(metadataPath);
|
|
|
|
return (Array.isArray(metadata) && metadata.length == 0) ? undefined : metadata;
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
2016-07-06 17:26:31 -04:00
|
|
|
} else {
|
|
|
|
const sf = this.program.getSourceFile(filePath);
|
|
|
|
if (!sf) {
|
|
|
|
throw new Error(`Source file ${filePath} not present in program.`);
|
|
|
|
}
|
|
|
|
return this.metadataCollector.getMetadata(sf);
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
readMetadata(filePath: string) {
|
|
|
|
try {
|
2016-08-02 14:45:14 -04:00
|
|
|
return this.resolverCache.get(filePath) || JSON.parse(this.context.readFile(filePath));
|
2016-04-29 00:57:16 -04:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to read JSON file ${filePath}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 14:45:14 -04:00
|
|
|
|
|
|
|
private getResolverMetadata(filePath: string): ModuleMetadata {
|
|
|
|
let metadata = this.resolverCache.get(filePath);
|
|
|
|
if (!metadata) {
|
|
|
|
metadata = this.getMetadataFor(filePath);
|
|
|
|
this.resolverCache.set(filePath, metadata);
|
|
|
|
}
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
2016-08-22 14:48:33 -04:00
|
|
|
protected resolveExportedSymbol(filePath: string, symbolName: string): StaticSymbol {
|
2016-08-02 14:45:14 -04:00
|
|
|
const resolveModule = (moduleName: string): string => {
|
2016-08-22 14:48:33 -04:00
|
|
|
const resolvedModulePath = this.getCanonicalFileName(this.resolve(moduleName, filePath));
|
2016-08-02 14:45:14 -04:00
|
|
|
if (!resolvedModulePath) {
|
|
|
|
throw new Error(`Could not resolve module '${moduleName}' relative to file ${filePath}`);
|
|
|
|
}
|
|
|
|
return resolvedModulePath;
|
|
|
|
};
|
|
|
|
let metadata = this.getResolverMetadata(filePath);
|
|
|
|
if (metadata) {
|
|
|
|
// If we have metadata for the symbol, this is the original exporting location.
|
|
|
|
if (metadata.metadata[symbolName]) {
|
|
|
|
return this.getStaticSymbol(filePath, symbolName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no, try to find the symbol in one of the re-export location
|
|
|
|
if (metadata.exports) {
|
|
|
|
// Try and find the symbol in the list of explicitly re-exported symbols.
|
|
|
|
for (const moduleExport of metadata.exports) {
|
|
|
|
if (moduleExport.export) {
|
|
|
|
const exportSymbol = moduleExport.export.find(symbol => {
|
|
|
|
if (typeof symbol === 'string') {
|
|
|
|
return symbol == symbolName;
|
|
|
|
} else {
|
|
|
|
return symbol.as == symbolName;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (exportSymbol) {
|
|
|
|
let symName = symbolName;
|
|
|
|
if (typeof exportSymbol !== 'string') {
|
|
|
|
symName = exportSymbol.name;
|
|
|
|
}
|
|
|
|
return this.resolveExportedSymbol(resolveModule(moduleExport.from), symName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the symbol via export * directives.
|
|
|
|
for (const moduleExport of metadata.exports) {
|
|
|
|
if (!moduleExport.export) {
|
|
|
|
const resolvedModule = resolveModule(moduleExport.from);
|
|
|
|
const candidateSymbol = this.resolveExportedSymbol(resolvedModule, symbolName);
|
|
|
|
if (candidateSymbol) return candidateSymbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
|
|
|
export class NodeReflectorHostContext implements ReflectorHostContext {
|
2016-07-13 14:15:23 -04:00
|
|
|
private assumedExists: {[fileName: string]: boolean} = {};
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2016-07-13 14:15:23 -04:00
|
|
|
fileExists(fileName: string): boolean {
|
|
|
|
return this.assumedExists[fileName] || fs.existsSync(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
directoryExists(directoryName: string): boolean {
|
|
|
|
try {
|
|
|
|
return fs.statSync(directoryName).isDirectory();
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
readFile(fileName: string): string { return fs.readFileSync(fileName, 'utf8'); }
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2016-07-13 14:15:23 -04:00
|
|
|
assumeFileExists(fileName: string): void { this.assumedExists[fileName] = true; }
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|