Skip to content

Commit e16f7e4

Browse files
authored
Adding in wrapper file to fix circular dependency with Webpack 5 (#2110)
* Adding in wrapper file to fix circular dependency with Webpack 5 * Running rustfmt * Fixing unit tests
1 parent a521c90 commit e16f7e4

File tree

9 files changed

+55
-19
lines changed

9 files changed

+55
-19
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ impl<'a> Context<'a> {
167167
Ok(())
168168
}
169169

170-
pub fn finalize(&mut self, module_name: &str) -> Result<(String, String), Error> {
170+
pub fn finalize(
171+
&mut self,
172+
module_name: &str,
173+
) -> Result<(String, String, Option<String>), Error> {
171174
// Finalize all bindings for JS classes. This is where we'll generate JS
172175
// glue for all classes as well as finish up a few final imports like
173176
// `__wrap` and such.
@@ -273,9 +276,10 @@ impl<'a> Context<'a> {
273276
&mut self,
274277
module_name: &str,
275278
needs_manual_start: bool,
276-
) -> Result<(String, String), Error> {
279+
) -> Result<(String, String, Option<String>), Error> {
277280
let mut ts = self.typescript.clone();
278281
let mut js = String::new();
282+
let mut start = None;
279283

280284
if let OutputMode::NoModules { global } = &self.config.mode {
281285
js.push_str(&format!("let {};\n(function() {{\n", global));
@@ -340,15 +344,15 @@ impl<'a> Context<'a> {
340344
));
341345
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
342346
let import = self.module.imports.get_mut(*id);
343-
import.module = format!("./{}.js", module_name);
347+
import.module = format!("./{}_bg.js", module_name);
344348
footer.push_str("\nexport const ");
345349
footer.push_str(&import.name);
346350
footer.push_str(" = ");
347351
footer.push_str(js.trim());
348352
footer.push_str(";\n");
349353
}
350354
if needs_manual_start {
351-
footer.push_str("\nwasm.__wbindgen_start();\n");
355+
start = Some("\nwasm.__wbindgen_start();\n".to_string());
352356
}
353357
}
354358

@@ -394,7 +398,7 @@ impl<'a> Context<'a> {
394398
js = js.replace("\n\n\n", "\n\n");
395399
}
396400

397-
Ok((js, ts))
401+
Ok((js, ts, start))
398402
}
399403

400404
fn js_import_header(&self) -> Result<String, Error> {

crates/cli-support/src/lib.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct JsGenerated {
6161
mode: OutputMode,
6262
js: String,
6363
ts: String,
64+
start: Option<String>,
6465
snippets: HashMap<String, Vec<String>>,
6566
local_modules: HashMap<String, String>,
6667
npm_dependencies: HashMap<String, (PathBuf, String)>,
@@ -421,7 +422,7 @@ impl Bindgen {
421422
.unwrap();
422423
let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
423424
cx.generate()?;
424-
let (js, ts) = cx.finalize(stem)?;
425+
let (js, ts, start) = cx.finalize(stem)?;
425426
Generated::Js(JsGenerated {
426427
snippets: aux.snippets.clone(),
427428
local_modules: aux.local_modules.clone(),
@@ -430,6 +431,7 @@ impl Bindgen {
430431
npm_dependencies: cx.npm_dependencies.clone(),
431432
js,
432433
ts,
434+
start,
433435
})
434436
};
435437

@@ -568,6 +570,16 @@ impl OutputMode {
568570
_ => false,
569571
}
570572
}
573+
574+
fn esm_integration(&self) -> bool {
575+
match self {
576+
OutputMode::Bundler { .. }
577+
| OutputMode::Node {
578+
experimental_modules: true,
579+
} => true,
580+
_ => false,
581+
}
582+
}
571583
}
572584

573585
/// Remove a number of internal exports that are synthesized by Rust's linker,
@@ -613,7 +625,7 @@ impl Output {
613625
Generated::InterfaceTypes => self.stem.clone(),
614626
Generated::Js(_) => format!("{}_bg", self.stem),
615627
};
616-
let wasm_path = out_dir.join(wasm_name).with_extension("wasm");
628+
let wasm_path = out_dir.join(&wasm_name).with_extension("wasm");
617629
fs::create_dir_all(out_dir)?;
618630
let wasm_bytes = self.module.emit_wasm();
619631
fs::write(&wasm_path, wasm_bytes)
@@ -660,9 +672,35 @@ impl Output {
660672
} else {
661673
"js"
662674
};
675+
676+
fn write<P, C>(path: P, contents: C) -> Result<(), anyhow::Error>
677+
where
678+
P: AsRef<Path>,
679+
C: AsRef<[u8]>,
680+
{
681+
fs::write(&path, contents)
682+
.with_context(|| format!("failed to write `{}`", path.as_ref().display()))
683+
}
684+
663685
let js_path = out_dir.join(&self.stem).with_extension(extension);
664-
fs::write(&js_path, reset_indentation(&gen.js))
665-
.with_context(|| format!("failed to write `{}`", js_path.display()))?;
686+
687+
if gen.mode.esm_integration() {
688+
let js_name = format!("{}_bg.{}", self.stem, extension);
689+
690+
let start = gen.start.as_deref().unwrap_or("");
691+
692+
write(
693+
&js_path,
694+
format!(
695+
"import * as wasm from \"./{}.wasm\";\nexport * from \"./{}\";{}",
696+
wasm_name, js_name, start
697+
),
698+
)?;
699+
700+
write(&out_dir.join(&js_name), reset_indentation(&gen.js))?;
701+
} else {
702+
write(&js_path, reset_indentation(&gen.js))?;
703+
}
666704

667705
if gen.typescript {
668706
let ts_path = js_path.with_extension("d.ts");

crates/cli/tests/reference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn runtest(test: &Path) -> Result<()> {
112112
let wat = sanitize_wasm(&wasm)?;
113113
assert_same(&wat, &test.with_extension("wat"))?;
114114
} else {
115-
let js = fs::read_to_string(td.path().join("reference_test.js"))?;
115+
let js = fs::read_to_string(td.path().join("reference_test_bg.js"))?;
116116
assert_same(&js, &test.with_extension("js"))?;
117117
let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?;
118118
assert_same(&wat, &test.with_extension("wat"))?;

crates/cli/tests/reference/anyref-empty.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ export const __wbindgen_init_anyref_table = function() {
1111
;
1212
};
1313

14-
wasm.__wbindgen_start();
15-

crates/cli/tests/reference/anyref-empty.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type (;0;) (func))
3-
(import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0)))
3+
(import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0)))
44
(table (;0;) 32 anyref)
55
(memory (;0;) 16)
66
(export "memory" (memory 0))

crates/cli/tests/reference/anyref-import-catch.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,3 @@ export const __wbindgen_init_anyref_table = function() {
6464
;
6565
};
6666

67-
wasm.__wbindgen_start();
68-

crates/cli/tests/reference/anyref-import-catch.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type (;0;) (func))
33
(type (;1;) (func (result i32)))
44
(type (;2;) (func (param i32)))
5-
(import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0)))
5+
(import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0)))
66
(func $__wbindgen_exn_store (type 2) (param i32))
77
(func $__anyref_table_alloc (type 1) (result i32))
88
(func $exported (type 0))

crates/cli/tests/reference/anyref-nop.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,3 @@ export const __wbindgen_init_anyref_table = function() {
1717
;
1818
};
1919

20-
wasm.__wbindgen_start();
21-

crates/cli/tests/reference/anyref-nop.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type (;0;) (func))
3-
(import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0)))
3+
(import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0)))
44
(func $foo (type 0))
55
(table (;0;) 32 anyref)
66
(memory (;0;) 17)

0 commit comments

Comments
 (0)