Skip to content

Use Webpack 5 style URL for Wasm import #12

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions verifying.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class Verifier {
try {
// TODO: switch to TinyGo when math/big works for smaller wasm file and non-global exports.
const go = new Go()
const url = `${import.meta.url.split('/').slice(0, -1).join('/')}/wasm/drand.wasm`
// Note: in Webpack >= v5.0.0-beta.30 the form `new URL("./relative_path.xyz", import.meta.url)`
// is treated as dependency and creates an asset module. This allows us to create a package that
// works in node, browsers and Webpack.
const url = new URL("./wasm/drand.wasm", import.meta.url)
let result
if (typeof fs !== 'undefined' && fs.promises) { // wasm_exec puts fs on global object in Node.js
const dirname = new URL(import.meta.url).pathname.split('/').slice(0, -1).join('/')
const data = new Uint8Array(await fs.promises.readFile(`${dirname}/wasm/drand.wasm`))
const data = new Uint8Array(await fs.promises.readFile(url))
result = await WebAssembly.instantiate(data, go.importObject)
} else if (WebAssembly.instantiateStreaming) {
result = await WebAssembly.instantiateStreaming(fetch(url), go.importObject)
Expand Down