forked from matthewp/script-type-module
-
Notifications
You must be signed in to change notification settings - Fork 3
/
polyfill.js
440 lines (379 loc) · 10.2 KB
/
polyfill.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
__scriptTypeModuleEval = function(__moduleSrc){
new Function(__moduleSrc)();
};
(function () {
'use strict';
function currentScript() {
return document.currentScript || document._currentScript || getCurrentScriptTheHardWay();
}
function getCurrentScriptTheHardWay() {
// Should be more complex than this.
var scripts = document.getElementsByTagName('script');
return scripts[scripts.length - 1];
}
class Cluster {
constructor(count){
this.count = count;
this.workerURL = new URL('./worker.js', document.currentScript.src);
this.workers = [];
this.spawn();
}
post(msg, handler) {
let worker = this.leastBusy();
worker.handlers[msg.url] = handler;
worker.postMessage(msg);
worker.inProgress++;
}
spawn() {
for(var i = 0; i < this.count; i++) {
let worker = new Worker(this.workerURL);
this.handleMessages(worker);
this.workers.push(worker);
}
}
leastBusy() {
this.workers.sort(function(a, b){
if(a.inProgress < b.inProgress) {
return -1;
} else {
return 1;
}
});
return this.workers[0];
}
handleMessages(worker) {
worker.inProgress = 0;
worker.handlers = {};
worker.onmessage = function(ev){
let msg = ev.data;
let handler = worker.handlers[msg.url];
handler(msg);
worker.inProgress--;
};
}
}
var addModuleTools = function(registry, dynamicImport){
self._importTypeModuleTools = function(url){
let moduleScript = registry.get(url);
let namespace = moduleScript.namespace;
return {
namespace: namespace,
staticImport: function(specifier){
let u = new URL(specifier, url).toString();
let moduleScript = registry.get(u);
return moduleScript.namespace;
},
dynamicImport: function(specifier){
let u = new URL(specifier, url).toString();
return dynamicImport(u);
},
set: function(name, value) {
if(typeof name === 'object') {
let moduleTools = this;
Object.keys(name).forEach(function(key){
moduleTools.set(key, name[key]);
});
return;
}
moduleScript.values[name] = value;
return value;
}
};
};
}
// TODO saving this space in case I want to support multiple workers
var execute = function({ url, code, map }){
if(map) {
code += encode$1(map);
} else {
code += '\n//# sourceURL=' + url;
}
__scriptTypeModuleEval(code);
}
const prefix = '\n//# source' + 'MappingURL=data:application/json;base64,';
function encode$1(map) {
return prefix + btoa(JSON.stringify(map));
}
class ModuleTree {
constructor() {
this.count = 0;
this.fetchPromise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
})
}
increment() {
this.count++;
}
decrement() {
this.count--;
if(this.count === 0) {
this.resolve();
}
}
}
class ModuleRecord {
constructor() {
this.requestedModules = null;
this.instantiationStatus = 'uninstantiated';
}
}
class ModuleScript {
constructor(url, resolve, reject){
this.moduleRecord = new ModuleRecord();
this.status = 'fetching';
this.baseTree = null;
this.trees = new Set();
this.url = url;
this.resolve = resolve;
this.reject = reject;
this._instantiationPromise = null;
this.fetchMessage = null;
this.deps = null;
this.code = null;
this.values = {};
this.namespace = {};
}
addToTree(tree) {
if(!this.trees.has(tree)) {
this.trees.add(tree);
if(this.status === 'fetching') {
tree.increment();
}
if(!this.baseTree) {
this.baseTree = tree;
}
}
}
addMessage(msg) {
this.status = 'fetched';
this.fetchMessage = msg;
this.code = msg.src;
this.map = msg.map;
this.deps = msg.deps;
}
complete() {
this.resolve(this);
this.trees.forEach(function(tree){
tree.decrement();
});
}
error(err) {
this.reject(err);
}
isDepOf(moduleScript) {
return moduleScript.deps.indexOf(this.url) !== -1;
}
instantiate() {
try {
execute(this);
this.moduleRecord.instantiationStatus = 'instantiated';
} catch(err) {
this.moduleRecord.instantiationStatus = 'errored';
this.moduleRecord.errorReason = err;
throw err;
}
}
instantiatePromise() {
if(this._instantiationPromise) {
return this._instantiationPromise;
}
return this._instantiationPromise = this._getInstantiatePromise();
}
_getInstantiatePromise() {
switch(this.moduleRecord.instantiationStatus) {
case 'instantiated':
return Promise.resolve();
case 'errored':
return Promise.reject(this.moduleRecord.errorReason);
default:
let tree = this.baseTree;
return tree.fetchPromise.then(() => {
// Wait for it to execute
return this._getInstantiatePromise();
});
}
}
}
const forEach$1 = Array.prototype.forEach;
function importExisting(importScript){
let tags = document.querySelectorAll('script[type=module-polyfill]');
forEach$1.call(tags, importScript);
}
function observe(importScript) {
let mo = new MutationObserver(function(mutations){
forEach$1.call(mutations, function(mutation){
forEach$1.call(mutation.addedNodes, function(el){
if(el.nodeName === 'SCRIPT' && el.type === 'module-polyfill') {
importScript(el);
}
});
});
});
mo.observe(document.documentElement, {
childList: true,
subtree: true
});
return mo;
}
var Registry = class {
constructor() {
this.moduleScriptMap = new Map();
this.fetchPromises = new Map();
}
get(url) {
return this.moduleScriptMap.get(url);
}
add(moduleScript) {
let url = moduleScript.url;
this.moduleScriptMap.set(url, moduleScript);
}
addExports(moduleScript) {
let msg = moduleScript.fetchMessage;
let exports = msg.exports;
let exportStars = msg.exportStars;
Object.keys(exports).forEach(name => {
let exp = exports[name];
if(exp.from) {
let parentModuleScript = this.moduleScriptMap.get(exp.from);
Object.defineProperty(moduleScript.namespace, name, {
get: getValue(parentModuleScript, exp.local)
});
} else {
Object.defineProperty(moduleScript.namespace, name, {
get: getValue(moduleScript, name)
});
}
});
exportStars.forEach(from => {
let parentModuleScript = this.moduleScriptMap.get(from);
let props = Object.getOwnPropertyNames(parentModuleScript.namespace);
props.forEach(function(prop){
Object.defineProperty(moduleScript.namespace, prop, {
get: getValue(parentModuleScript, prop)
});
});
});
}
link(moduleScript) {
moduleScript.status = 'linking';
let deps = moduleScript.deps;
deps.forEach(depUrl => {
let depModuleScript = this.get(depUrl);
if(depModuleScript.moduleRecord.instantiationStatus === 'uninstantiated') {
// Circular deps
if(depModuleScript.status !== 'linking') {
this.link(depModuleScript);
}
}
});
moduleScript.status = 'linked';
this.instantiate(moduleScript);
}
instantiate(moduleScript) {
if(moduleScript.moduleRecord.instantiationStatus === 'uninstantiated') {
this.addExports(moduleScript);
moduleScript.instantiate();
}
}
}
function getValue(moduleScript, name, par) {
return function(){
return moduleScript.values[name];
};
}
let cluster = new Cluster(1);
let registry = new Registry();
let anonCount = 0;
let pollyScript = currentScript();
let includeSourceMaps = pollyScript.dataset.noSm == null;
addModuleTools(registry, dynamicImport);
function importScript(script) {
let url = "" + (script.src || new URL('./!anonymous_' + anonCount++, document.baseURI));
let src = script.src ? undefined : script.textContent;
return internalImportModule(url, src)
.then(function(){
var ev = new Event('load');
script.dispatchEvent(ev);
})
.then(null, function(err){
console.error(err);
var ev = new ErrorEvent('error', {
message: err.message,
filename: url
});
script.dispatchEvent(ev);
});
}
function internalImportModule(url, src){
let entry = registry.get(url);
if(entry) {
return entry.instantiatePromise();
}
return importModuleWithTree(url, src);
}
function importModuleWithTree(url, src){
let tree = new ModuleTree();
return fetchModule(url, src, tree)
.then(function(moduleScript){
return tree.fetchPromise.then(function(){
return moduleScript;
});
})
.then(function(moduleScript){
registry.link(moduleScript);
});
}
function fetchModule(url, src, tree) {
var promise = registry.fetchPromises.get(url);
if(!promise) {
promise = new Promise(function(resolve, reject){
let moduleScript = new ModuleScript(url, resolve, reject);
moduleScript.addToTree(tree);
let handler = function(msg){
if(msg.type === 'error') {
let ErrorConstructor = self[msg.error.name] || Error;
let error = new ErrorConstructor(msg.error.message);
moduleScript.error(error);
return;
}
moduleScript.addMessage(msg);
fetchTree(moduleScript, tree);
moduleScript.complete();
};
cluster.post({
type: 'fetch',
url: url,
src: src,
includeSourceMaps: includeSourceMaps
}, handler);
registry.add(moduleScript);
});
registry.fetchPromises.set(url, promise);
} else {
// See if this ModuleScript is still being fetched
let moduleScript = registry.get(url);
moduleScript.addToTree(tree);
}
return promise;
}
function fetchTree(moduleScript, tree) {
let deps = moduleScript.deps;
let promises = deps.map(function(url){
let fetchPromise = fetchModule(url, null, tree);
let depModuleScript = registry.get(url);
moduleScript.trees.forEach(function(tree){
depModuleScript.addToTree(tree);
});
return fetchPromise;
});
return Promise.all(promises);
}
function dynamicImport(url, src){
return internalImportModule(url, src).then(function(){
return registry.get(url).namespace;
});
}
importExisting(importScript);
observe(importScript);
}());