UNPKG

6.49 kBJavaScriptView Raw
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"use strict";
9var __extends = (this && this.__extends) || function (d, b) {
10 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13};
14var path = require('path');
15var ts = require('typescript');
16var reflector_host_1 = require('./reflector_host');
17var EXT = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
18var DTS = /\.d\.ts$/;
19/**
20 * This version of the reflector host expects that the program will be compiled
21 * and executed with a "path mapped" directory structure, where generated files
22 * are in a parallel tree with the sources, and imported using a `./` relative
23 * import. This requires using TS `rootDirs` option and also teaching the module
24 * loader what to do.
25 */
26var PathMappedReflectorHost = (function (_super) {
27 __extends(PathMappedReflectorHost, _super);
28 function PathMappedReflectorHost(program, compilerHost, options, context) {
29 _super.call(this, program, compilerHost, options, context);
30 }
31 PathMappedReflectorHost.prototype.getCanonicalFileName = function (fileName) {
32 if (!fileName)
33 return fileName;
34 // NB: the rootDirs should have been sorted longest-first
35 for (var _i = 0, _a = this.options.rootDirs || []; _i < _a.length; _i++) {
36 var dir = _a[_i];
37 if (fileName.indexOf(dir) === 0) {
38 fileName = fileName.substring(dir.length);
39 }
40 }
41 return fileName;
42 };
43 PathMappedReflectorHost.prototype.resolve = function (m, containingFile) {
44 for (var _i = 0, _a = this.options.rootDirs || ['']; _i < _a.length; _i++) {
45 var root = _a[_i];
46 var rootedContainingFile = path.join(root, containingFile);
47 var resolved = ts.resolveModuleName(m, rootedContainingFile, this.options, this.context).resolvedModule;
48 if (resolved) {
49 if (this.options.traceResolution) {
50 console.error('resolve', m, containingFile, '=>', resolved.resolvedFileName);
51 }
52 return resolved.resolvedFileName;
53 }
54 }
55 };
56 /**
57 * We want a moduleId that will appear in import statements in the generated code.
58 * These need to be in a form that system.js can load, so absolute file paths don't work.
59 * Relativize the paths by checking candidate prefixes of the absolute path, to see if
60 * they are resolvable by the moduleResolution strategy from the CompilerHost.
61 */
62 PathMappedReflectorHost.prototype.getImportPath = function (containingFile, importedFile) {
63 var _this = this;
64 importedFile = this.resolveAssetUrl(importedFile, containingFile);
65 containingFile = this.resolveAssetUrl(containingFile, '');
66 if (this.options.traceResolution) {
67 console.error('getImportPath from containingFile', containingFile, 'to importedFile', importedFile);
68 }
69 // If a file does not yet exist (because we compile it later), we still need to
70 // assume it exists so that the `resolve` method works!
71 if (!this.context.fileExists(importedFile)) {
72 if (this.options.rootDirs && this.options.rootDirs.length > 0) {
73 this.context.assumeFileExists(path.join(this.options.rootDirs[0], importedFile));
74 }
75 else {
76 this.context.assumeFileExists(importedFile);
77 }
78 }
79 var resolvable = function (candidate) {
80 var resolved = _this.getCanonicalFileName(_this.resolve(candidate, importedFile));
81 return resolved && resolved.replace(EXT, '') === importedFile.replace(EXT, '');
82 };
83 var importModuleName = importedFile.replace(EXT, '');
84 var parts = importModuleName.split(path.sep).filter(function (p) { return !!p; });
85 var foundRelativeImport;
86 for (var index = parts.length - 1; index >= 0; index--) {
87 var candidate_1 = parts.slice(index, parts.length).join(path.sep);
88 if (resolvable(candidate_1)) {
89 return candidate_1;
90 }
91 candidate_1 = '.' + path.sep + candidate_1;
92 if (resolvable(candidate_1)) {
93 foundRelativeImport = candidate_1;
94 }
95 }
96 if (foundRelativeImport)
97 return foundRelativeImport;
98 // Try a relative import
99 var candidate = path.relative(path.dirname(containingFile), importModuleName);
100 if (resolvable(candidate)) {
101 return candidate;
102 }
103 throw new Error("Unable to find any resolvable import for " + importedFile + " relative to " + containingFile);
104 };
105 PathMappedReflectorHost.prototype.getMetadataFor = function (filePath) {
106 for (var _i = 0, _a = this.options.rootDirs || []; _i < _a.length; _i++) {
107 var root = _a[_i];
108 var rootedPath = path.join(root, filePath);
109 if (!this.compilerHost.fileExists(rootedPath)) {
110 // If the file doesn't exists then we cannot return metadata for the file.
111 // This will occur if the user refernced a declared module for which no file
112 // exists for the module (i.e. jQuery or angularjs).
113 continue;
114 }
115 if (DTS.test(rootedPath)) {
116 var metadataPath = rootedPath.replace(DTS, '.metadata.json');
117 if (this.context.fileExists(metadataPath)) {
118 var metadata = this.readMetadata(metadataPath);
119 return (Array.isArray(metadata) && metadata.length == 0) ? undefined : metadata;
120 }
121 }
122 else {
123 var sf = this.program.getSourceFile(rootedPath);
124 if (!sf) {
125 throw new Error("Source file " + rootedPath + " not present in program.");
126 }
127 sf.fileName = this.getCanonicalFileName(sf.fileName);
128 return this.metadataCollector.getMetadata(sf);
129 }
130 }
131 };
132 return PathMappedReflectorHost;
133}(reflector_host_1.ReflectorHost));
134exports.PathMappedReflectorHost = PathMappedReflectorHost;
135//# sourceMappingURL=path_mapped_reflector_host.js.map
\No newline at end of file