Skip to content

Commit 84c7cf0

Browse files
address pr comments
1 parent 79f96af commit 84c7cf0

File tree

13 files changed

+151
-36
lines changed

13 files changed

+151
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ members = [
6565
"examples/char",
6666
"examples/closures",
6767
"examples/console_log",
68+
"examples/deno",
6869
"examples/dom",
6970
"examples/duck-typed-interfaces",
7071
"examples/fetch",

azure-pipelines.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ jobs:
167167
- script: mv _package.json package.json && npm install && rm package.json
168168
displayName: "run npm install"
169169
- script: |
170-
for dir in `ls examples | grep -v README | grep -v asm.js | grep -v raytrace | grep -v without-a-bundler | grep -v websockets | grep -v webxr`; do
170+
for dir in `ls examples | grep -v README | grep -v asm.js | grep -v raytrace | grep -v without-a-bundler | grep -v websockets | grep -v webxr | grep -v deno`; do
171171
(cd examples/$dir &&
172172
ln -fs ../../node_modules . &&
173173
npm run build -- --output-path $BUILD_ARTIFACTSTAGINGDIRECTORY/exbuild/$dir) || exit 1;
@@ -178,6 +178,16 @@ jobs:
178178
artifactName: examples1
179179
targetPath: '$(Build.ArtifactStagingDirectory)'
180180

181+
- job: test_deno
182+
displayName: "Build and test the deno example"
183+
steps:
184+
- template: ci/azure-install-rust.yml
185+
- script: rustup target add wasm32-unknown-unknown
186+
displayName: "install wasm target"
187+
- template: ci/azure-install-deno.yml
188+
- script: cd examples/deno && ./build.sh && $HOME/.deno/bin/deno run --allow-read test.ts
189+
displayName: "build and run deno example"
190+
181191
- job: build_raytrace
182192
displayName: "Build raytrace examples"
183193
steps:

ci/azure-install-deno.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
steps:
2+
- script: curl -fsSL https://deno.land/x/install/install.sh | sh
3+
displayName: "install deno"

crates/cli-support/src/js/mod.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ impl<'a> Context<'a> {
294294
}
295295

296296
fn generate_deno_wasm_loading(&self, module_name: &str) -> String {
297+
// Deno removed support for .wasm imports in https://github.com/denoland/deno/pull/5135
298+
// the issue for bringing it back is https://github.com/denoland/deno/issues/5609.
297299
format!(
298300
"const file = new URL(import.meta.url).pathname;
299301
const wasmFile = file.substring(0, file.lastIndexOf(Deno.build.os === 'windows' ? '\\\\' : '/') + 1) + '{}_bg.wasm';
@@ -1283,27 +1285,36 @@ impl<'a> Context<'a> {
12831285
}
12841286

12851287
fn expose_text_processor(&mut self, s: &str, args: &str) -> Result<(), Error> {
1286-
if self.config.mode.nodejs() {
1287-
let name = self.import_name(&JsImport {
1288-
name: JsImportName::Module {
1289-
module: "util".to_string(),
1290-
name: s.to_string(),
1291-
},
1292-
fields: Vec::new(),
1293-
})?;
1294-
self.global(&format!("let cached{} = new {}{};", s, name, args));
1295-
} else if !self.config.mode.always_run_in_browser() && !self.config.mode.deno() {
1296-
self.global(&format!(
1297-
"
1288+
match &self.config.mode {
1289+
OutputMode::Node { .. } => {
1290+
let name = self.import_name(&JsImport {
1291+
name: JsImportName::Module {
1292+
module: "util".to_string(),
1293+
name: s.to_string(),
1294+
},
1295+
fields: Vec::new(),
1296+
})?;
1297+
self.global(&format!("let cached{} = new {}{};", s, name, args));
1298+
}
1299+
OutputMode::Bundler {
1300+
browser_only: false,
1301+
} => {
1302+
self.global(&format!(
1303+
"
12981304
const l{0} = typeof {0} === 'undefined' ? \
12991305
(0, module.require)('util').{0} : {0};\
13001306
",
1301-
s
1302-
));
1303-
self.global(&format!("let cached{0} = new l{0}{1};", s, args));
1304-
} else {
1305-
self.global(&format!("let cached{0} = new {0}{1};", s, args));
1306-
}
1307+
s
1308+
));
1309+
self.global(&format!("let cached{0} = new l{0}{1};", s, args));
1310+
}
1311+
OutputMode::Deno
1312+
| OutputMode::Web
1313+
| OutputMode::NoModules { .. }
1314+
| OutputMode::Bundler { browser_only: true } => {
1315+
self.global(&format!("let cached{0} = new {0}{1};", s, args))
1316+
}
1317+
};
13071318

13081319
Ok(())
13091320
}

crates/cli-support/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -564,29 +564,13 @@ impl OutputMode {
564564
}
565565
}
566566

567-
fn always_run_in_browser(&self) -> bool {
568-
match self {
569-
OutputMode::Web => true,
570-
OutputMode::NoModules { .. } => true,
571-
OutputMode::Bundler { browser_only } => *browser_only,
572-
_ => false,
573-
}
574-
}
575-
576567
fn web(&self) -> bool {
577568
match self {
578569
OutputMode::Web => true,
579570
_ => false,
580571
}
581572
}
582573

583-
fn deno(&self) -> bool {
584-
match self {
585-
OutputMode::Deno { .. } => true,
586-
_ => false,
587-
}
588-
}
589-
590574
fn esm_integration(&self) -> bool {
591575
match self {
592576
OutputMode::Bundler { .. }

examples/deno/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "deno"
3+
version = "0.1.0"
4+
authors = ["The wasm-bindgen Developers"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
wasm-bindgen = "0.2.63"

examples/deno/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Using deno
2+
3+
You can build the example with
4+
5+
```sh
6+
$ ./build.sh
7+
```
8+
9+
and test it with
10+
11+
```sh
12+
$ deno run --allow-read test.ts
13+
```
14+
15+
The `--allow-read` flag is needed because the wasm file is read during runtime.
16+
This will be fixed when https://github.com/denoland/deno/issues/5609 is resolved.

examples/deno/build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
5+
cargo build --target wasm32-unknown-unknown --release
6+
cargo run --package wasm-bindgen-cli --bin wasm-bindgen -- \
7+
--out-dir pkg --target deno ${CARGO_TARGET_DIR:-../../target}/wasm32-unknown-unknown/release/deno.wasm

examples/deno/crate/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/deno.wasm

examples/deno/defined-in-js.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export class MyClass {
2+
constructor() {
3+
this._number = 42;
4+
}
5+
6+
get number() {
7+
return this._number;
8+
}
9+
10+
set number(n) {
11+
return (this._number = n);
12+
}
13+
14+
render() {
15+
return `My number is: ${this.number}`;
16+
}
17+
}

0 commit comments

Comments
 (0)