From e16f7e41bf9ee3446ce1727b9d8826b97f00f266 Mon Sep 17 00:00:00 2001 From: Pauan Date: Wed, 29 Apr 2020 17:55:28 +0200 Subject: [PATCH] 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 --- crates/cli-support/src/js/mod.rs | 14 ++++-- crates/cli-support/src/lib.rs | 46 +++++++++++++++++-- crates/cli/tests/reference.rs | 2 +- crates/cli/tests/reference/anyref-empty.js | 2 - crates/cli/tests/reference/anyref-empty.wat | 2 +- .../tests/reference/anyref-import-catch.js | 2 - .../tests/reference/anyref-import-catch.wat | 2 +- crates/cli/tests/reference/anyref-nop.js | 2 - crates/cli/tests/reference/anyref-nop.wat | 2 +- 9 files changed, 55 insertions(+), 19 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index b5437a19..ed305ff9 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -167,7 +167,10 @@ impl<'a> Context<'a> { Ok(()) } - pub fn finalize(&mut self, module_name: &str) -> Result<(String, String), Error> { + pub fn finalize( + &mut self, + module_name: &str, + ) -> Result<(String, String, Option), Error> { // Finalize all bindings for JS classes. This is where we'll generate JS // glue for all classes as well as finish up a few final imports like // `__wrap` and such. @@ -273,9 +276,10 @@ impl<'a> Context<'a> { &mut self, module_name: &str, needs_manual_start: bool, - ) -> Result<(String, String), Error> { + ) -> Result<(String, String, Option), Error> { let mut ts = self.typescript.clone(); let mut js = String::new(); + let mut start = None; if let OutputMode::NoModules { global } = &self.config.mode { js.push_str(&format!("let {};\n(function() {{\n", global)); @@ -340,7 +344,7 @@ impl<'a> Context<'a> { )); for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) { let import = self.module.imports.get_mut(*id); - import.module = format!("./{}.js", module_name); + import.module = format!("./{}_bg.js", module_name); footer.push_str("\nexport const "); footer.push_str(&import.name); footer.push_str(" = "); @@ -348,7 +352,7 @@ impl<'a> Context<'a> { footer.push_str(";\n"); } if needs_manual_start { - footer.push_str("\nwasm.__wbindgen_start();\n"); + start = Some("\nwasm.__wbindgen_start();\n".to_string()); } } @@ -394,7 +398,7 @@ impl<'a> Context<'a> { js = js.replace("\n\n\n", "\n\n"); } - Ok((js, ts)) + Ok((js, ts, start)) } fn js_import_header(&self) -> Result { diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index dd47a620..0a47c51f 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -61,6 +61,7 @@ struct JsGenerated { mode: OutputMode, js: String, ts: String, + start: Option, snippets: HashMap>, local_modules: HashMap, npm_dependencies: HashMap, @@ -421,7 +422,7 @@ impl Bindgen { .unwrap(); let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?; cx.generate()?; - let (js, ts) = cx.finalize(stem)?; + let (js, ts, start) = cx.finalize(stem)?; Generated::Js(JsGenerated { snippets: aux.snippets.clone(), local_modules: aux.local_modules.clone(), @@ -430,6 +431,7 @@ impl Bindgen { npm_dependencies: cx.npm_dependencies.clone(), js, ts, + start, }) }; @@ -568,6 +570,16 @@ impl OutputMode { _ => false, } } + + fn esm_integration(&self) -> bool { + match self { + OutputMode::Bundler { .. } + | OutputMode::Node { + experimental_modules: true, + } => true, + _ => false, + } + } } /// Remove a number of internal exports that are synthesized by Rust's linker, @@ -613,7 +625,7 @@ impl Output { Generated::InterfaceTypes => self.stem.clone(), Generated::Js(_) => format!("{}_bg", self.stem), }; - let wasm_path = out_dir.join(wasm_name).with_extension("wasm"); + let wasm_path = out_dir.join(&wasm_name).with_extension("wasm"); fs::create_dir_all(out_dir)?; let wasm_bytes = self.module.emit_wasm(); fs::write(&wasm_path, wasm_bytes) @@ -660,9 +672,35 @@ impl Output { } else { "js" }; + + fn write(path: P, contents: C) -> Result<(), anyhow::Error> + where + P: AsRef, + C: AsRef<[u8]>, + { + fs::write(&path, contents) + .with_context(|| format!("failed to write `{}`", path.as_ref().display())) + } + let js_path = out_dir.join(&self.stem).with_extension(extension); - fs::write(&js_path, reset_indentation(&gen.js)) - .with_context(|| format!("failed to write `{}`", js_path.display()))?; + + if gen.mode.esm_integration() { + let js_name = format!("{}_bg.{}", self.stem, extension); + + let start = gen.start.as_deref().unwrap_or(""); + + write( + &js_path, + format!( + "import * as wasm from \"./{}.wasm\";\nexport * from \"./{}\";{}", + wasm_name, js_name, start + ), + )?; + + write(&out_dir.join(&js_name), reset_indentation(&gen.js))?; + } else { + write(&js_path, reset_indentation(&gen.js))?; + } if gen.typescript { let ts_path = js_path.with_extension("d.ts"); diff --git a/crates/cli/tests/reference.rs b/crates/cli/tests/reference.rs index 4df55ec0..c4524981 100644 --- a/crates/cli/tests/reference.rs +++ b/crates/cli/tests/reference.rs @@ -112,7 +112,7 @@ fn runtest(test: &Path) -> Result<()> { let wat = sanitize_wasm(&wasm)?; assert_same(&wat, &test.with_extension("wat"))?; } else { - let js = fs::read_to_string(td.path().join("reference_test.js"))?; + let js = fs::read_to_string(td.path().join("reference_test_bg.js"))?; assert_same(&js, &test.with_extension("js"))?; let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?; assert_same(&wat, &test.with_extension("wat"))?; diff --git a/crates/cli/tests/reference/anyref-empty.js b/crates/cli/tests/reference/anyref-empty.js index adc1a1ce..440a088a 100644 --- a/crates/cli/tests/reference/anyref-empty.js +++ b/crates/cli/tests/reference/anyref-empty.js @@ -11,5 +11,3 @@ export const __wbindgen_init_anyref_table = function() { ; }; -wasm.__wbindgen_start(); - diff --git a/crates/cli/tests/reference/anyref-empty.wat b/crates/cli/tests/reference/anyref-empty.wat index 22baefbf..42c8e96c 100644 --- a/crates/cli/tests/reference/anyref-empty.wat +++ b/crates/cli/tests/reference/anyref-empty.wat @@ -1,6 +1,6 @@ (module (type (;0;) (func)) - (import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) + (import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) (table (;0;) 32 anyref) (memory (;0;) 16) (export "memory" (memory 0)) diff --git a/crates/cli/tests/reference/anyref-import-catch.js b/crates/cli/tests/reference/anyref-import-catch.js index 4f5f48aa..0ad9ccc5 100644 --- a/crates/cli/tests/reference/anyref-import-catch.js +++ b/crates/cli/tests/reference/anyref-import-catch.js @@ -64,5 +64,3 @@ export const __wbindgen_init_anyref_table = function() { ; }; -wasm.__wbindgen_start(); - diff --git a/crates/cli/tests/reference/anyref-import-catch.wat b/crates/cli/tests/reference/anyref-import-catch.wat index 49e199e8..236e0902 100644 --- a/crates/cli/tests/reference/anyref-import-catch.wat +++ b/crates/cli/tests/reference/anyref-import-catch.wat @@ -2,7 +2,7 @@ (type (;0;) (func)) (type (;1;) (func (result i32))) (type (;2;) (func (param i32))) - (import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) + (import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) (func $__wbindgen_exn_store (type 2) (param i32)) (func $__anyref_table_alloc (type 1) (result i32)) (func $exported (type 0)) diff --git a/crates/cli/tests/reference/anyref-nop.js b/crates/cli/tests/reference/anyref-nop.js index e3f060a3..43bbe06c 100644 --- a/crates/cli/tests/reference/anyref-nop.js +++ b/crates/cli/tests/reference/anyref-nop.js @@ -17,5 +17,3 @@ export const __wbindgen_init_anyref_table = function() { ; }; -wasm.__wbindgen_start(); - diff --git a/crates/cli/tests/reference/anyref-nop.wat b/crates/cli/tests/reference/anyref-nop.wat index 44fad1e4..76b2e259 100644 --- a/crates/cli/tests/reference/anyref-nop.wat +++ b/crates/cli/tests/reference/anyref-nop.wat @@ -1,6 +1,6 @@ (module (type (;0;) (func)) - (import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) + (import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) (func $foo (type 0)) (table (;0;) 32 anyref) (memory (;0;) 17)