-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw-ts-compiler.js
278 lines (256 loc) · 8.03 KB
/
sw-ts-compiler.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
class TsCompiler {
constructor({
verbose,
compileOptions,
} = {
verbose: 0,
compileOptions: {
noEmitOnError: true,
noImplicitAny: true,
strictNullChecks: true,
experimentalDecorators: true,
},
}) {
this.verbose = verbose;
this.compileOptions = compileOptions;
this.sourceFiles = {};
this.compiledFiles = {};
this.tsLibs = {};
this.tsReady = this.loadTs();
}
handleFetch(event) {
event.respondWith((async () => {
const compile = await this.compileUrl(event.request.url);
if (compile) {
this.log(3, 'TsCompiler serving: ', event.request.url, compile);
return new Response(compile, {
headers: {'content-type': 'application/javascript'},
});
}
return fetch(event.request);
})());
}
async compileUrl(fetchUrl) {
const isBrowserFetchingTs = fetchUrl.endsWith('.ts');
if (isBrowserFetchingTs) {
const changed = await this.downloadImports(fetchUrl);
if (changed)
return await this.compileSourceFile(fetchUrl);
}
const compiledUrl = tsUrlToCompiledUrl(fetchUrl);
this.log(3, 'Compile cache check for: ', compiledUrl);
const compiled = this.compiledFiles[compiledUrl];
if (compiled) {
this.log(1, 'Compile cache hit: ', fetchUrl);
return compiled;
}
if (isBrowserFetchingTs)
return await this.compileSourceFile(fetchUrl);
return null;
}
async downloadImports(rootUrl) {
this.log(1, 'Downloading imports for: ', rootUrl);
let changed = false;
const pendingUrls = new Set([rootUrl]);
const seenUrls = new Set();
const downloads = new Map();
while (true) {
for (const url of pendingUrls) {
this.log(3, 'Downloading: ', url);
seenUrls.add(url);
const download = (async () => {
const response = await fetch(url);
this.log(3, 'Downloaded: ', url);
return {
url,
data: response.ok ? await response.text() : null,
};
})();
downloads.set(url, download.finally(() => downloads.delete(url)));
}
pendingUrls.clear();
if (downloads.size == 0)
break;
const {url, data} = await Promise.race(downloads.values());
if (data == null) {
this.log(1, 'Download failed: ', url);
continue;
}
if (this.sourceFiles[url] != data) {
this.log(2, 'Source changed: ', url)
changed = true;
}
this.sourceFiles[url] = data;
for (const importUrl of this.getModulePaths(url, data)) {
if (!seenUrls.has(importUrl)) {
this.log(3, 'New import: ', importUrl);
pendingUrls.add(importUrl);
}
}
}
return changed;
}
*getModulePaths(url, source) {
const sourceFile = ts.createSourceFile('', source, ts.ScriptTarget.ESNext);
for (const statement of sourceFile.statements) {
if (!ts.isAnyImportOrReExport(statement)) {
continue;
}
const modulePathExpr = ts.getExternalModuleName(statement);
if (modulePathExpr && ts.isStringLiteral(modulePathExpr) && modulePathExpr.text) {
let sourceUrl = tsUrlToSourceUrl(modulePathExpr.text);
if (sourceUrl.startsWith('.'))
sourceUrl = new URL(url + '/../' + sourceUrl).href;
yield sourceUrl;
}
}
}
async compileSourceFile(url) {
await this.tsReady;
this.log(1, 'Compiling: ', url);
const compiledUrl = tsUrlToCompiledUrl(url);
delete this.compiledFiles[compiledUrl];
const self = this;
ts.sys = {
useCaseSensitiveFileNames: true,
newLine: '\n',
getExecutingFilePath() {
return '';
},
getCurrentDirectory() {
return '';
},
directoryExists(dir) {
return true;
},
getDirectories(dir) {
return [];
},
fileExists(path) {
path = resolveOriginRelative(path);
self.log(3, 'File exists check: ', path);
return path in self.tsLibs || path in self.sourceFiles;
},
readFile(path) {
path = resolveOriginRelative(path);
if (path in self.tsLibs) {
self.log(3, 'Read lib: ', path);
return self.tsLibs[path];
}
if (path in self.sourceFiles) {
self.log(3, 'Read source: ', path);
return self.sourceFiles[path];
}
self.log(1, 'Missing file: ', path);
return null;
},
writeFile(path, text) {
path = resolveOriginRelative(path);
self.log(2, 'Write file: ', path);
self.compiledFiles[path] = text;
},
};
let program = ts.createProgram([url], {
...this.compileOptions,
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.ESNext,
});
let emitResult = program.emit();
this.log(2, 'Compile result: ', emitResult);
if (emitResult.emitSkipped) {
return emitResult.diagnostics.map(diagnostic => {
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
return `console.error(${JSON.stringify(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)});`;
}).join('\n');
}
return this.compiledFiles[compiledUrl];
}
log(level, ...args) {
if (level <= this.verbose)
console.log(...args);
}
async loadTs() {
this.log(2, 'Loading core TypeScript.');
const tsLibRoot = 'https://unpkg.com/[email protected]/';
const tsLibFilenames = [
'lib.d.ts',
'lib.dom.d.ts',
'lib.dom.iterable.d.ts',
'lib.es2015.collection.d.ts',
'lib.es2015.core.d.ts',
'lib.es2015.d.ts',
'lib.es2015.generator.d.ts',
'lib.es2015.iterable.d.ts',
'lib.es2015.promise.d.ts',
'lib.es2015.proxy.d.ts',
'lib.es2015.reflect.d.ts',
'lib.es2015.symbol.d.ts',
'lib.es2015.symbol.wellknown.d.ts',
'lib.es2016.array.include.d.ts',
'lib.es2016.d.ts',
'lib.es2017.d.ts',
'lib.es2017.intl.d.ts',
'lib.es2017.object.d.ts',
'lib.es2017.sharedmemory.d.ts',
'lib.es2017.string.d.ts',
'lib.es2017.typedarrays.d.ts',
'lib.es2018.asyncgenerator.d.ts',
'lib.es2018.asynciterable.d.ts',
'lib.es2018.d.ts',
'lib.es2018.intl.d.ts',
'lib.es2018.promise.d.ts',
'lib.es2018.regexp.d.ts',
'lib.es2019.array.d.ts',
'lib.es2019.d.ts',
'lib.es2019.object.d.ts',
'lib.es2019.string.d.ts',
'lib.es2019.symbol.d.ts',
'lib.es2020.bigint.d.ts',
'lib.es2020.d.ts',
'lib.es2020.intl.d.ts',
'lib.es2020.promise.d.ts',
'lib.es2020.string.d.ts',
'lib.es2020.symbol.wellknown.d.ts',
'lib.es5.d.ts',
'lib.esnext.d.ts',
'lib.esnext.full.d.ts',
'lib.esnext.intl.d.ts',
'lib.esnext.promise.d.ts',
'lib.esnext.string.d.ts',
'lib.scripthost.d.ts',
'lib.webworker.importscripts.d.ts',
];
const libsLoaded = Promise.all(tsLibFilenames.map(async filename => {
this.tsLibs[filename] = await (await fetch(tsLibRoot + 'lib/' + filename)).text();
}));
globalThis.process = {env: {}};
globalThis.require = () => undefined;
importScripts(tsLibRoot + 'lib/typescriptServices.js');
this.log(2, 'TypeScript services loaded.');
await libsLoaded;
this.log(2, 'TypeScript libraries loaded.');
}
}
function resolveOriginRelative(url) {
return url.startsWith('/') ? location.origin + url : url;
}
function tsUrlToCompiledUrl(tsUrl) {
// ./test -> ./test.js
// ./test.ts -> ./test.js
// /test -> https://origin.com/test.js
tsUrl = resolveOriginRelative(tsUrl);
return tsUrl.replace(/\.ts$/, '') + '.js';
}
function tsUrlToSourceUrl(tsUrl) {
// ./test -> ./test.ts
// ./test.ts -> ./test.ts
// ./test.js -> ./test.d.ts
// /test -> https://origin.com/test.ts
tsUrl = resolveOriginRelative(tsUrl);
if (tsUrl.endsWith('.ts'))
return tsUrl;
if (tsUrl.endsWith('.js'))
return tsUrl.replace(/\.js$/, '.d.ts');
return tsUrl + '.ts';
}