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