Skip to content

Commit

Permalink
Move WASM building to onLoad
Browse files Browse the repository at this point in the history
We don't want to use onResolve because we can resolve the same WASM
multiple times (i.e. multiple files importing the same WASM), leading to
the WASM being built multiple times, possibly in parallel (which may
lead to build failure).
  • Loading branch information
paw-hub committed Dec 12, 2024
1 parent 967c4df commit 5ac754a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/tuta-wasm-loader/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function createOutputFolderStructure(output: string) {
export function esbuildWasmLoader(options: PluginOptions) {
const normalizedOptions = parseOptions(options)
const fallbackOptions = normalizedOptions.fallback

return {
name: "wasm",
setup(build: PluginBuild) {
Expand All @@ -112,15 +113,11 @@ export function esbuildWasmLoader(options: PluginOptions) {
path: args.path.replaceAll("wasm-fallback:", ""),
namespace: "wasm-fallback",
}
}

const lib = findLib(normalizedOptions, args.path)

await generateWasm(lib.command, lib)

return {
path: path.join("wasm", args.path),
namespace: "wasm-loader",
} else {
return {
path: path.join("wasm", args.path),
namespace: "wasm-loader",
}
}
})

Expand All @@ -134,6 +131,13 @@ export function esbuildWasmLoader(options: PluginOptions) {
})
}

build.onLoad({ filter: /\.wasm$/ }, async (args): Promise<OnLoadResult | undefined> => {
const lib = findLib(normalizedOptions, path.basename(args.path))
await generateWasm(lib.command, lib)
// don't try to read the wasm since we use wasm-loader or wasm-fallback to use the wasm
return undefined
})

build.onLoad({ filter: /.*/, namespace: "wasm-loader" }, async (args): Promise<OnLoadResult> => {
return {
contents: await generateImportCode(args.path, fallbackOptions != null),
Expand All @@ -152,6 +156,7 @@ export function esbuildWasmLoader(options: PluginOptions) {
}
})
}

build.onResolve({ filter: /node:*/, namespace: "wasm-loader" }, async (_) => {
return {
external: true,
Expand Down

0 comments on commit 5ac754a

Please sign in to comment.