Skip to content
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

feat(wasm): option for initWasm() to return the instance (#3847 #5615) #7181

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/vite/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,20 @@ declare module '*.otf' {

// other
declare module '*.wasm' {
const initWasm: (options: WebAssembly.Imports) => Promise<WebAssembly.Exports>
function initWasm(
options: WebAssembly.Imports,
getInstance: true
): Promise<WebAssembly.Instance>
function initWasm(
options: WebAssembly.Imports,
getInstance: false
): Promise<WebAssembly.Exports>
function initWasm(options: WebAssembly.Imports): Promise<WebAssembly.Exports>

function initWasm(
options: WebAssembly.Imports,
getInstance?: boolean
): Promise<WebAssembly.Instance | WebAssembly.Exports>
export default initWasm
}
declare module '*.webmanifest' {
Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/node/plugins/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fileToUrl } from './asset'

const wasmHelperId = '/__vite-wasm-helper'

const wasmHelper = async (opts = {}, url: string) => {
const wasmHelper = async (opts = {}, url: string, getInstance = false) => {
let result
if (url.startsWith('data:')) {
// @ts-ignore
Expand Down Expand Up @@ -37,7 +37,7 @@ const wasmHelper = async (opts = {}, url: string) => {
result = await WebAssembly.instantiate(buffer, opts)
}
}
return result.instance.exports
return getInstance ? result.instance : result.instance.exports
}

const wasmHelperCode = wasmHelper.toString()
Expand Down Expand Up @@ -65,7 +65,9 @@ export const wasmPlugin = (config: ResolvedConfig): Plugin => {

return `
import initWasm from "${wasmHelperId}"
export default opts => initWasm(opts, ${JSON.stringify(url)})
export default (opts, getInstance) => initWasm(opts, ${JSON.stringify(
url
)}, getInstance)
`
}
}
Expand Down