-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a0d7e9
commit 4ce3e2b
Showing
8 changed files
with
132 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Loads JSON, CSS, Wasm module types based on file extensions | ||
* Supports application/javascript falling back to JS eval | ||
*/ | ||
import { systemJSPrototype } from '../system-core'; | ||
const instantiate = systemJSPrototype.instantiate; | ||
systemJSPrototype.instantiate = function (url, parent) { | ||
const loader = this; | ||
const ext = url.slice(url.lastIndexOf('.')); | ||
switch (ext) { | ||
case '.css': | ||
return loadDynamicModule(function (_export, source) { | ||
// Relies on a Constructable Stylesheet polyfill | ||
const stylesheet = new CSSStyleSheet(); | ||
stylesheet.replaceSync(source); | ||
_export('default', stylesheet); | ||
}); | ||
case '.html': | ||
return getSourceRes().then(function (res) { | ||
return maybeJSFallback(res) || loadError("'.html' modules not implemented"); | ||
}); | ||
case '.json': | ||
return loadDynamicModule(function (_export, source) { | ||
_export('default', JSON.parse(source)); | ||
}); | ||
case '.wasm': | ||
return getSourceRes().then(function (res) { | ||
return maybeJSFallback(res) || | ||
(WebAssembly.compileStreaming ? WebAssembly.compileStreaming(res) : res.arrayBuffer().then(WebAssembly.compile)); | ||
}) | ||
.then(function (module) { | ||
const deps = []; | ||
const setters = []; | ||
const importObj = {}; | ||
|
||
// we can only set imports if supported (eg early Safari doesnt support) | ||
if (WebAssembly.Module.imports) | ||
WebAssembly.Module.imports(module).forEach(function (impt) { | ||
const key = impt.module; | ||
setters.push(function (m) { | ||
importObj[key] = m; | ||
}); | ||
if (deps.indexOf(key) === -1) | ||
deps.push(key); | ||
}); | ||
|
||
return [deps, function (_export) { | ||
return { | ||
setters: setters, | ||
execute: function () { | ||
return WebAssembly.instantiate(module, importObj) | ||
.then(function (instance) { | ||
_export(instance.exports); | ||
}); | ||
} | ||
}; | ||
}]; | ||
}); | ||
} | ||
return instantiate.apply(this, arguments); | ||
|
||
function getSourceRes () { | ||
return fetch(url).then(function (res) { | ||
if (!res.ok) | ||
loadError(res.status + ' ' + res.statusText); | ||
return res; | ||
}); | ||
} | ||
function maybeJSFallback (res) { | ||
const contentType = res.headers.get('content-type'); | ||
// if the resource is sent as application/javascript, support eval-based execution | ||
if (contentType && contentType.match(/^application\/javascript(;|$)/)) { | ||
return res.text().then(function (source) { | ||
(0, eval)(source); | ||
return loader.getRegister(); | ||
}); | ||
} | ||
} | ||
function loadDynamicModule (createExec) { | ||
return getSourceRes().then(function (res) { | ||
return maybeJSFallback(res) || res.text().then(function (source) { | ||
return [[], function (_export) { | ||
return { execute: createExec(_export, source) }; | ||
}]; | ||
}); | ||
}); | ||
} | ||
function loadError (msg) { | ||
throw Error(msg + ', loading ' + url + (parent ? ' from ' + parent : '')); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ if (hasSelf && typeof importScripts === 'function') | |
} | ||
resolve(loader.getRegister()); | ||
}); | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import './features/script-load.js'; | ||
import './features/worker-load.js'; | ||
import './extras/global.js'; | ||
import './features/wasm-load.js'; | ||
import './extras/module-types.js'; | ||
import './features/import-map.js'; | ||
import './features/registry.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters