forked from systemjs/systemjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-types.js
99 lines (95 loc) · 3.56 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
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
(function(){function errMsg(errCode, msg) {
return (msg || "") + " (SystemJS Error#" + errCode + " " + "https://git.io/JvFET#" + errCode + ")";
}/*
* Loads JSON, CSS, Wasm module types based on file extensions
* Supports application/javascript falling back to JS eval
*/
(function(global) {
var systemJSPrototype = global.System.constructor.prototype;
var instantiate = systemJSPrototype.instantiate;
var moduleTypesRegEx = /\.(css|html|json|wasm)$/;
systemJSPrototype.shouldFetch = function (url) {
var path = url.split('?')[0].split('#')[0];
var ext = path.slice(path.lastIndexOf('.'));
return ext.match(moduleTypesRegEx);
};
systemJSPrototype.fetch = function (url) {
return fetch(url);
};
systemJSPrototype.instantiate = function (url, parent) {
var loader = this;
if (this.shouldFetch(url)) {
return this.fetch(url)
.then(function (res) {
if (!res.ok)
throw Error(errMsg(7, res.status + ' ' + res.statusText + ', loading ' + url + (parent ? ' from ' + parent : '')));
var contentType = res.headers.get('content-type');
if (contentType.match(/^(text|application)\/(x-)?javascript(;|$)/)) {
return res.text().then(function (source) {
(0, eval)(source);
return loader.getRegister();
});
}
else if (contentType.match(/^application\/json(;|$)/)) {
return res.text().then(function (source) {
return [[], function (_export) {
return {
execute: function () {
_export('default', JSON.parse(source));
}
};
}];
});
}
else if (contentType.match(/^text\/css(;|$)/)) {
return res.text().then(function (source) {
return [[], function (_export) {
return {
execute: function () {
// Relies on a Constructable Stylesheet polyfill
var stylesheet = new CSSStyleSheet();
stylesheet.replaceSync(source);
_export('default', stylesheet);
}
};
}];
});
}
else if (contentType.match(/^application\/wasm(;|$)/)) {
return (WebAssembly.compileStreaming ? WebAssembly.compileStreaming(res) : res.arrayBuffer().then(WebAssembly.compile))
.then(function (module) {
var deps = [];
var setters = [];
var importObj = {};
// we can only set imports if supported (eg early Safari doesnt support)
if (WebAssembly.Module.imports)
WebAssembly.Module.imports(module).forEach(function (impt) {
var key = impt.module;
if (deps.indexOf(key) === -1) {
deps.push(key);
setters.push(function (m) {
importObj[key] = m;
});
}
});
return [deps, function (_export) {
return {
setters: setters,
execute: function () {
return WebAssembly.instantiate(module, importObj)
.then(function (instance) {
_export(instance.exports);
});
}
};
}];
});
}
else {
throw Error(errMsg(4, 'Unknown module type "' + contentType + '"'));
}
});
}
return instantiate.apply(this, arguments);
};
})(typeof self !== 'undefined' ? self : global);}());