-
-
Notifications
You must be signed in to change notification settings - Fork 615
refact(wasm): use helper module instead of banner #607
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,55 @@ | ||
| export const HELPERS_ID = '\0wasmHelpers.js'; | ||
|
|
||
| export const getHelpersModule = () => ` | ||
| function _loadWasmModule (sync, filepath, src, imports) { | ||
| function _instantiateOrCompile(source, imports, stream) { | ||
| var instantiateFunc = stream ? WebAssembly.instantiateStreaming : WebAssembly.instantiate; | ||
| var compileFunc = stream ? WebAssembly.compileStreaming : WebAssembly.compile; | ||
|
|
||
| if (imports) { | ||
| return instantiateFunc(source, imports) | ||
| } else { | ||
| return compileFunc(source) | ||
| } | ||
| } | ||
|
|
||
| var buf = null | ||
| var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null | ||
|
|
||
| if (filepath && isNode) { | ||
| var fs = eval('require("fs")') | ||
| var path = eval('require("path")') | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| fs.readFile(path.resolve(__dirname, filepath), (error, buffer) => { | ||
| if (error != null) { | ||
| reject(error) | ||
| } | ||
|
|
||
| resolve(_instantiateOrCompile(buffer, imports, false)) | ||
| }); | ||
| }); | ||
| } else if (filepath) { | ||
| return _instantiateOrCompile(fetch(filepath), imports, true) | ||
| } | ||
|
|
||
| if (isNode) { | ||
| buf = Buffer.from(src, 'base64') | ||
| } else { | ||
| var raw = globalThis.atob(src) | ||
| var rawLength = raw.length | ||
| buf = new Uint8Array(new ArrayBuffer(rawLength)) | ||
| for(var i = 0; i < rawLength; i++) { | ||
| buf[i] = raw.charCodeAt(i) | ||
| } | ||
| } | ||
|
|
||
| if(sync) { | ||
| var mod = new WebAssembly.Module(buf) | ||
| return imports ? new WebAssembly.Instance(mod, imports) : mod | ||
| } else { | ||
| return _instantiateOrCompile(buf, imports, false) | ||
| } | ||
| } | ||
| export { _loadWasmModule }; | ||
| `; | ||
This file contains hidden or 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 hidden or 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 @@ | ||
| export default 'foo'; |
This file contains hidden or 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,8 @@ | ||
| import sample from './sample.wasm'; | ||
| import foo from './foo'; | ||
|
|
||
| const instance = sample({ env: {} }); | ||
|
|
||
| t.is(instance.exports.main(), 3, 'wasm loaded'); | ||
|
|
||
| t.is(foo, 'foo'); |
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -121,3 +121,31 @@ try { | |||||
| } catch (err) { | ||||||
| // worker threads aren't fully supported in Node versions before 11.7.0 | ||||||
| } | ||||||
|
|
||||||
| test('injectHelper', async (t) => { | ||||||
| t.plan(4); | ||||||
|
|
||||||
| const injectImport = `import { _loadWasmModule } from ${JSON.stringify('\0wasmHelpers.js')};`; | ||||||
|
|
||||||
| const bundle = await rollup({ | ||||||
| input: 'fixtures/injectHelper.js', | ||||||
| plugins: [ | ||||||
| wasm({ | ||||||
| sync: ['fixtures/sample.wasm'] | ||||||
| }), | ||||||
| { | ||||||
| name: 'test-detect', | ||||||
| transform: (code, id) => { | ||||||
| if (id.endsWith('sample.wasm')) { | ||||||
| t.true(code.includes(injectImport)); | ||||||
| } | ||||||
| if (id.endsWith('foo.js')) { | ||||||
| t.true(!code.includes(injectImport)); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| return code; | ||||||
| } | ||||||
| } | ||||||
| ] | ||||||
| }); | ||||||
| await testBundle(t, bundle); | ||||||
| }); | ||||||
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.
What does this null byte prefix do?
Uh oh!
There was an error while loading. Please reload this page.
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 am not 100 percent sure where to find the documentation, but "\0" seems to be a convention when writing rollup plugin to indicate the module comes from a plugin. and if it should not be taken by this plugin,
resolveIdreturns null, otherwise returns the current resolveIdThere 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.
It indicates a "virtual module" in the Rollup ecosystem.
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.
More precisely it means: Other plugins, please ignore this module and please do not apply any transformations etc. https://rollupjs.org/guide/en/#conventions