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-06-09 17:51:53 -04:00
|
|
|
export interface ReflectorHostContext {
|
|
|
|
exists(fileName: string): boolean;
|
|
|
|
read(fileName: string): string;
|
|
|
|
write(fileName: string, data: string): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
|
2016-05-01 14:22:39 -04:00
|
|
|
private metadataCollector = new MetadataCollector();
|
2016-06-09 17:51:53 -04:00
|
|
|
private context: ReflectorHostContext;
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
|
|
|
private program: ts.Program, private compilerHost: ts.CompilerHost,
|
|
|
|
private options: AngularCompilerOptions, context?: ReflectorHostContext) {
|
2016-06-09 17:51:53 -04:00
|
|
|
this.context = context || new NodeReflectorHostContext();
|
|
|
|
}
|
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-04-29 00:57:16 -04:00
|
|
|
private resolve(m: string, containingFile: string) {
|
|
|
|
const resolved =
|
|
|
|
ts.resolveModuleName(m, containingFile, this.options, this.compilerHost).resolvedModule;
|
2016-05-02 12:38:46 -04:00
|
|
|
return resolved ? resolved.resolvedFileName : null;
|
2016-04-29 00:57:16 -04:00
|
|
|
};
|
|
|
|
|
2016-05-03 20:31:40 -04:00
|
|
|
private normalizeAssetUrl(url: string): string {
|
|
|
|
let assetUrl = AssetUrl.parse(url);
|
|
|
|
return assetUrl ? `${assetUrl.packageName}/${assetUrl.modulePath}` : null;
|
|
|
|
}
|
2016-05-02 12:38:46 -04:00
|
|
|
|
|
|
|
private 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-05-03 20:31:40 -04:00
|
|
|
return 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.
|
|
|
|
* Relativize the paths by checking candidate prefixes of the absolute path, to see if
|
|
|
|
* they are resolvable by the moduleResolution strategy from the CompilerHost.
|
|
|
|
*/
|
2016-05-02 12:38:46 -04:00
|
|
|
getImportPath(containingFile: string, importedFile: 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-05-02 12:38:46 -04:00
|
|
|
// TODO(tbosch): if a file does not yet exist (because we compile it later),
|
|
|
|
// we still need to create it so that the `resolve` method works!
|
|
|
|
if (!this.compilerHost.fileExists(importedFile)) {
|
2016-05-24 13:53:48 -04:00
|
|
|
if (this.options.trace) {
|
2016-05-03 13:50:55 -04:00
|
|
|
console.log(`Generating empty file ${importedFile} to allow resolution of import`);
|
|
|
|
}
|
2016-05-02 12:38:46 -04:00
|
|
|
this.compilerHost.writeFile(importedFile, '', false);
|
2016-06-09 17:51:53 -04:00
|
|
|
this.context.write(importedFile, '');
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
2016-05-02 12:38:46 -04:00
|
|
|
|
2016-06-03 15:23:35 -04:00
|
|
|
const importModuleName = importedFile.replace(EXT, '');
|
|
|
|
const parts = importModuleName.split(path.sep).filter(p => !!p);
|
2016-05-02 12:38:46 -04:00
|
|
|
|
2016-04-29 00:57:16 -04:00
|
|
|
for (let index = parts.length - 1; index >= 0; index--) {
|
|
|
|
let candidate = parts.slice(index, parts.length).join(path.sep);
|
2016-05-02 12:38:46 -04:00
|
|
|
if (this.resolve('.' + path.sep + candidate, containingFile) === importedFile) {
|
|
|
|
return `./${candidate}`;
|
|
|
|
}
|
|
|
|
if (this.resolve(candidate, containingFile) === importedFile) {
|
|
|
|
return candidate;
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 15:23:35 -04:00
|
|
|
|
|
|
|
// Try a relative import
|
|
|
|
let candidate = path.relative(path.dirname(containingFile), importModuleName);
|
|
|
|
if (this.resolve(candidate, containingFile) === importedFile) {
|
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
|
2016-04-29 00:57:16 -04:00
|
|
|
throw new Error(
|
2016-05-02 12:38:46 -04:00
|
|
|
`Unable to find any resolvable import for ${importedFile} relative to ${containingFile}`);
|
2016-04-29 00:57:16 -04:00
|
|
|
}
|
|
|
|
|
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-05-24 13:53:48 -04:00
|
|
|
containingFile = path.join(this.options.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) {
|
|
|
|
throw new Error(`Could not resolve module ${module} relative to ${containingFile}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// the corresponding .ts files stored in the .metadata.json file. Just assume the
|
|
|
|
// symbol and file we resolved to be correct as we don't need this to be the
|
|
|
|
// cannonical reference as this reference could have only been generated by a
|
|
|
|
// .metadata.json file resolving values.
|
|
|
|
return this.getStaticSymbol(filePath, symbolName);
|
|
|
|
}
|
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];
|
|
|
|
const declarationFile = declaration.getSourceFile().fileName;
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-05-02 12:38:46 -04:00
|
|
|
getStaticSymbol(declarationFile: string, name: string): StaticSymbol {
|
2016-04-29 00:57:16 -04:00
|
|
|
let key = `"${declarationFile}".${name}`;
|
|
|
|
let result = this.typeCache.get(key);
|
|
|
|
if (!result) {
|
2016-05-02 12:38:46 -04:00
|
|
|
result = new StaticSymbol(declarationFile, name);
|
2016-04-29 00:57:16 -04:00
|
|
|
this.typeCache.set(key, result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(alexeagle): take a statictype
|
|
|
|
getMetadataFor(filePath: string): ModuleMetadata {
|
2016-06-09 17:51:53 -04:00
|
|
|
if (!this.context.exists(filePath)) {
|
2016-04-29 00:57:16 -04:00
|
|
|
throw new Error(`No such file '${filePath}'`);
|
|
|
|
}
|
|
|
|
if (DTS.test(filePath)) {
|
|
|
|
const metadataPath = filePath.replace(DTS, '.metadata.json');
|
2016-06-09 17:51:53 -04:00
|
|
|
if (this.context.exists(metadataPath)) {
|
2016-04-29 00:57:16 -04:00
|
|
|
return this.readMetadata(metadataPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let sf = this.program.getSourceFile(filePath);
|
|
|
|
if (!sf) {
|
|
|
|
throw new Error(`Source file ${filePath} not present in program.`);
|
|
|
|
}
|
2016-05-31 14:00:39 -04:00
|
|
|
const metadata = this.metadataCollector.getMetadata(sf);
|
2016-04-29 00:57:16 -04:00
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
readMetadata(filePath: string) {
|
|
|
|
try {
|
2016-06-09 17:51:53 -04:00
|
|
|
const result = JSON.parse(this.context.read(filePath));
|
2016-04-29 00:57:16 -04:00
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to read JSON file ${filePath}`);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
|
|
|
export class NodeReflectorHostContext implements ReflectorHostContext {
|
2016-06-08 19:38:52 -04:00
|
|
|
exists(fileName: string): boolean { return fs.existsSync(fileName); }
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
read(fileName: string): string { return fs.readFileSync(fileName, 'utf8'); }
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
write(fileName: string, data: string): void { fs.writeFileSync(fileName, data, 'utf8'); }
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|