forked from systemjs/systemjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-types.js
70 lines (67 loc) · 2.78 KB
/
module-types.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
(function(){/*
* Loads JSON, CSS, Wasm module types based on file extension
* filters and content type verifications
*/
(function(global) {
var systemJSPrototype = global.System.constructor.prototype;
var moduleTypesRegEx = /^[^#?]+\.(css|html|json|wasm)([?#].*)?$/;
systemJSPrototype.shouldFetch = function (url) {
return moduleTypesRegEx.test(url);
};
var jsonContentType = /^application\/json(;|$)/;
var cssContentType = /^text\/css(;|$)/;
var wasmContentType = /^application\/wasm(;|$)/;
var fetch = systemJSPrototype.fetch;
systemJSPrototype.fetch = function (url, options) {
return fetch(url, options)
.then(function (res) {
if (!res.ok)
return res;
var contentType = res.headers.get('content-type');
if (jsonContentType.test(contentType))
return res.json()
.then(function (json) {
return new Response(new Blob([
'System.register([],function(e){return{execute:function(){e("default",' + JSON.stringify(json) + ')}}})'
], {
type: 'application/javascript'
}));
});
if (cssContentType.test(contentType))
return res.text()
.then(function (source) {
return new Response(new Blob([
'System.register([],function(e){return{execute:function(){var s=new CSSStyleSheet();s.replaceSync(' + JSON.stringify(source) + ');e("default",s)}}})'
], {
type: 'application/javascript'
}));
});
if (wasmContentType.test(contentType))
return (WebAssembly.compileStreaming ? WebAssembly.compileStreaming(res) : res.arrayBuffer().then(WebAssembly.compile))
.then(function (module) {
if (!global.System.wasmModules)
global.System.wasmModules = Object.create(null);
global.System.wasmModules[url] = module;
// we can only set imports if supported (eg early Safari doesnt support)
var deps = [];
var setterSources = [];
if (WebAssembly.Module.imports)
WebAssembly.Module.imports(module).forEach(function (impt) {
var key = JSON.stringify(impt.module);
if (deps.indexOf(key) === -1) {
deps.push(key);
setterSources.push('function(m){i[' + key + ']=m}');
}
});
return new Response(new Blob([
'System.register([' + deps.join(',') + '],function(e){var i={};return{setters:[' + setterSources.join(',') +
'],execute:function(){return WebAssembly.instantiate(System.wasmModules[' + JSON.stringify(url) +
'],i).then(function(m){e(m.exports)})}}})'
], {
type: 'application/javascript'
}));
});
return res;
});
};
})(typeof self !== 'undefined' ? self : global);}());