-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Module Types Extra #2006
Merged
Merged
Module Types Extra #2006
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
90b0b47
fall back to eval for application/javascript on module types
guybedford 02a2b61
fixup npm run test
guybedford 4c24fde
add noBrowserTimeout for CI
guybedford 200af08
hmm
guybedford 7b2c8e3
handle timeouts
guybedford 2a0d7e9
reword
guybedford 4ce3e2b
create dedicated module-types extra
guybedford fd4769e
readme updates
guybedford 8b6ae18
wasm grouping
guybedford a01a01c
update extras links
guybedford 13b1607
update extras link
guybedford d08736e
wasm
guybedford 1fd6d26
Wasm example
guybedford File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,92 @@ | ||
/* | ||
* 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; | ||
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); | ||
}); | ||
} | ||
}; | ||
}]; | ||
}); | ||
} | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 I think that this is better as an extra than baked into s.js and script-load. And am also glad that the module-types extra is included by default into system.js