forked from barbatus/meteor-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-service.js
160 lines (135 loc) · 4.16 KB
/
compile-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import assert from "assert";
import ts from "typescript";
import _ from "underscore";
import logger from "./logger";
import sourceHost from "./files-source-host";
import {
normalizePath,
prepareSourceMap,
isSourceMap,
getDepsAndRefs,
getRefs,
createDiagnostics,
getRootedPath,
TsDiagnostics,
} from "./ts-utils";
export default class CompileService {
constructor(serviceHost) {
this.serviceHost = serviceHost;
this.service = ts.createLanguageService(serviceHost);
}
compile(filePath, moduleName) {
const sourceFile = this.getSourceFile(filePath);
assert.ok(sourceFile);
if (moduleName) {
sourceFile.moduleName = moduleName;
}
const result = this.service.getEmitOutput(filePath);
let code, sourceMap;
_.each(result.outputFiles, function(file) {
if (normalizePath(filePath) !==
normalizePath(file.name)) return;
const text = file.text;
if (isSourceMap(file.name)) {
const source = sourceHost.get(filePath);
sourceMap = prepareSourceMap(text, source, filePath);
} else {
code = text;
}
}, this);
const checker = this.getTypeChecker();
const pcs = logger.newProfiler("process csresult");
const deps = getDepsAndRefs(sourceFile, checker);
const meteorizedCode = this.rootifyPaths(code, deps.mappings);
const csResult = createCSResult(filePath, {
code: meteorizedCode,
sourceMap,
version: this.serviceHost.getScriptVersion(filePath),
isExternal: ts.isExternalModule(sourceFile),
dependencies: deps,
diagnostics: this.getDiagnostics(filePath),
});
pcs.end();
return csResult;
}
getHost() {
return this.serviceHost;
}
getDocRegistry() {
return this.registry;
}
getSourceFile(filePath) {
const program = this.service.getProgram();
return program.getSourceFile(filePath);
}
getDepsAndRefs(filePath) {
const checker = this.getTypeChecker();
return getDepsAndRefs(this.getSourceFile(filePath), checker);
}
getRefTypings(filePath) {
const refs = getRefs(this.getSourceFile(filePath));
return refs.refTypings;
}
getTypeChecker() {
return this.service.getProgram().getTypeChecker();
}
getDiagnostics(filePath) {
return createDiagnostics(
this.service.getSyntacticDiagnostics(filePath),
this.service.getSemanticDiagnostics(filePath)
);
}
rootifyPaths(code, mappings) {
function buildPathRegExp(modulePath) {
const regExp = new RegExp("(require\\(\"|\')(" + modulePath + ")(\"|\'\\))", "g");
return regExp;
}
mappings = mappings.filter(module => module.resolved && !module.external);
logger.assert("process mappings %s", mappings.map((module) => module.resolvedPath));
for (const module of mappings) {
const usedPath = module.modulePath;
const resolvedPath = module.resolvedPath;
// Fix some weird v2.1.x bug where
// LanguageService converts dotted paths
// to relative in the code.
const regExp = buildPathRegExp(resolvedPath);
code = code.replace(regExp, function(match, p1, p2, p3, offset) {
return p1 + getRootedPath(resolvedPath) + p3;
});
// Skip path replacement for dotted paths.
if (! usedPath.startsWith(".")) {
const regExp = buildPathRegExp(usedPath);
code = code.replace(regExp, function(match, p1, p2, p3, offset) {
return p1 + getRootedPath(resolvedPath) + p3;
});
}
}
return code;
}
}
export function createCSResult(filePath, result) {
const props = [
"code", "sourceMap", "version",
"isExternal", "dependencies", "diagnostics",
];
const len = props.length;
for (let i = 0; i < len; i++) {
if (!_.has(result, props[i])) {
const msg = `file result ${filePath} doesn't contain ${props[i]}`;
logger.debug(msg);
throw new Error(msg);
}
}
result.diagnostics = new TsDiagnostics(
result.diagnostics);
return new CSResult(result);
}
export class CSResult {
constructor(result) {
assert.ok(this instanceof CSResult);
_.extend(this, result);
}
upDiagnostics(diagnostics) {
this.diagnostics = new TsDiagnostics(diagnostics);
}
}