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

I want to build wasm for Deno. #672

Closed
kahirokunn opened this issue Jul 1, 2019 · 8 comments
Closed

I want to build wasm for Deno. #672

kahirokunn opened this issue Jul 1, 2019 · 8 comments

Comments

@kahirokunn
Copy link

💡 Feature description

I want to build wasm for Deno.
https://github.com/denoland/deno

I try write rust and do wasm-pack.
And then I got .wasm file, I put it file on the Deno.
But the wasm file never work....(´;ω;`)

💻 Basic example

$ wasm-pack build --target deno
@csmoe
Copy link
Member

csmoe commented Jul 3, 2019

wasm-pack's build target reiles on wasm-bindgen cc https://rustwasm.github.io/docs/wasm-bindgen/reference/cli.html?highlight=target#--target

@ibaryshnikov
Copy link
Member

@kahirokunn is there require function in deno?

@ashleygwilliams
Copy link
Member

related denoland/deno#1325

@ashleygwilliams
Copy link
Member

@kahirokunn in order to run wasm-packed wasm, you'll need to require the js wrapper generated by wasm-bindgen. can deno load JS?

@kevinkassimo
Copy link

kevinkassimo commented Nov 15, 2019

fyi Deno has resolved the original issue posted above and have also recently just introduced WASM imports (should be very similar to Node.js's --experimental-wasm-modules):

import { exportedFunc1, exportedFunc2 } from "./my.wasm";

If my.wasm imports from other JS/TS files, it could be done by specifying import names like

(module
  (import "./wasm-dep.js" "jsFn" (func $jsFn (result i32)))
  (import "http://127.0.0.1:4545/cli/tests/051_wasm_import/remote.ts" "jsRemoteFn" (func $jsRemoteFn (result i32)))
)

with corresponding local/remote file set up with correct named exports.

@ytoune
Copy link

ytoune commented Jul 1, 2020

I use below for a while

cargo generate --git https://github.com/rustwasm/wasm-pack-template -n myproject
cd myproject
# deno has not alert but has console.log.
sed -i -e "s/fn alert(s: &str);/#[wasm_bindgen(js_namespace = console, js_name = log)] fn alert(s: \\&str);/" src/lib.rs

wasm-pack build --target web --out-name index
# deno hasn't supported fetch('file:// ...') yet.
sed -i -e "s#input = import\\.meta\\.url\\.replace(/\\\\\\.js$/, '_bg\\.wasm');#input = import.meta.url.replace(/\\\\.js$/, '_bg.wasm'); if ('undefined' !== typeof Deno) input = new WebAssembly.Module(await Deno.readFile(new URL(input).pathname));#" pkg/index.js

echo "import * as pkg from './pkg/index.js'
await pkg.default()
pkg.greet()" >| app.js
deno run --allow-read app.js

denoland/deno#2150

@bsunderhus
Copy link

bsunderhus commented Apr 5, 2021

Just for future references of those who are still a little confused and end up in this opened issue.

To build for deno just build for web: wasm-pack build --target web

The only problem at the moment is that Deno doesn't support fetch for local files, and the web targeted build will try to get the .wasm binary with fetch, so to solve this when calling the initializing function from the generated package you'll have to manually pass the content extracted from the .wasm file

example:

import init, {greet} from "./pkg/greet.js";

await init(Deno.readFile('./pkg/greet_bg.wasm'));
greet();

@drager
Copy link
Member

drager commented Mar 19, 2023

Fixed in v0.11.0.

@drager drager closed this as completed Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants