Skip to content

Commit fff8df0

Browse files
authored
Support the web Target of wasm-bindgen (#852)
This emits the bindings in a way such that they are compatible with not only the `bundler` target of `wasm-bindgen`, but also the `web` target. This allows us to circumvent `webpack`, which means we are not limited by the features it supports anymore. In particular, this unblocks the usage of reference types.
1 parent a1a130b commit fff8df0

File tree

2 files changed

+92
-11
lines changed

2 files changed

+92
-11
lines changed

capi/bind_gen/src/main.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,38 @@ fn write_files(classes: &BTreeMap<String, Class>, opt: &Opt) -> Result<()> {
312312
path.push("wasm_bindgen");
313313
create_dir_all(&path)?;
314314
{
315-
path.push("index.js");
316-
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, false)?;
315+
path.push("bundler");
316+
create_dir_all(&path)?;
317+
{
318+
path.push("index.js");
319+
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, false, true)?;
320+
path.pop();
321+
322+
path.push("index.ts");
323+
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, true, true)?;
324+
path.pop();
325+
}
317326
path.pop();
318327

319-
path.push("index.ts");
320-
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, true)?;
328+
path.push("web");
329+
create_dir_all(&path)?;
330+
{
331+
path.push("index.js");
332+
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, false, false)?;
333+
path.pop();
334+
335+
path.push("index.ts");
336+
wasm_bindgen::write(BufWriter::new(File::create(&path)?), classes, true, false)?;
337+
path.pop();
338+
339+
path.push("preload.js");
340+
wasm_bindgen::write_preload(BufWriter::new(File::create(&path)?), false)?;
341+
path.pop();
342+
343+
path.push("preload.ts");
344+
wasm_bindgen::write_preload(BufWriter::new(File::create(&path)?), true)?;
345+
path.pop();
346+
}
321347
path.pop();
322348
}
323349
path.pop();

capi/bind_gen/src/wasm_bindgen.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,60 @@ fn write_fn<W: Write>(mut writer: W, function: &Function, type_script: bool) ->
336336
Ok(())
337337
}
338338

339+
pub fn write_preload<W: Write>(mut writer: W, type_script: bool) -> Result<()> {
340+
let content = if type_script {
341+
r#"import init, { InitOutput } from "./livesplit_core.js";
342+
343+
let promise: Promise<InitOutput> | null = null;
344+
export async function preloadWasm(): Promise<InitOutput> {
345+
if (!promise) {
346+
promise = init();
347+
}
348+
return await promise;
349+
}"#
350+
} else {
351+
r#"import init from "./livesplit_core.js
352+
353+
let promise = null;
354+
export async function preloadWasm() {
355+
if (!promise) {
356+
promise = init();
357+
}
358+
return await promise;
359+
}"#
360+
};
361+
writeln!(writer, "{content}")
362+
}
363+
339364
pub fn write<W: Write>(
340365
mut writer: W,
341366
classes: &BTreeMap<String, Class>,
342367
type_script: bool,
368+
bundler: bool,
343369
) -> Result<()> {
344370
if type_script {
371+
if bundler {
372+
writeln!(
373+
writer,
374+
"{}",
375+
r#"// tslint:disable
376+
import * as wasm from "./livesplit_core_bg.wasm";
377+
import "./livesplit_core.js";"#
378+
)?;
379+
} else {
380+
writeln!(
381+
writer,
382+
"{}",
383+
r#"// tslint:disable
384+
import { preloadWasm } from "./preload";
385+
386+
const wasm = await preloadWasm();"#
387+
)?;
388+
}
345389
writeln!(
346390
writer,
347391
"{}{}",
348-
r#"// tslint:disable
349-
import * as wasm from "./livesplit_core_bg.wasm";
350-
import "./livesplit_core.js";
351-
392+
r#"
352393
const encoder = new TextEncoder();
353394
const decoder = new TextDecoder();
354395
@@ -402,12 +443,26 @@ function dealloc(slice: Slice) {
402443
typescript::HEADER,
403444
)?;
404445
} else {
446+
if bundler {
447+
writeln!(
448+
writer,
449+
"{}",
450+
r#"import * as wasm from "./livesplit_core_bg.wasm";
451+
import "./livesplit_core.js";"#
452+
)?;
453+
} else {
454+
writeln!(
455+
writer,
456+
"{}",
457+
r#"import { preloadWasm } from "./preload";
458+
459+
const wasm = await preloadWasm();"#
460+
)?;
461+
}
405462
writeln!(
406463
writer,
407464
"{}",
408-
r#"import * as wasm from "./livesplit_core_bg.wasm";
409-
import "./livesplit_core.js";
410-
465+
r#"
411466
const encoder = new TextEncoder();
412467
const decoder = new TextDecoder();
413468

0 commit comments

Comments
 (0)