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

Adding in wrapper file to fix circular dependency with Webpack 5 #2110

Merged
merged 3 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>), 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.
Expand Down Expand Up @@ -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<String>), 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));
Expand Down Expand Up @@ -340,15 +344,15 @@ 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(" = ");
footer.push_str(js.trim());
footer.push_str(";\n");
}
if needs_manual_start {
footer.push_str("\nwasm.__wbindgen_start();\n");
start = Some("\nwasm.__wbindgen_start();\n".to_string());
}
}

Expand Down Expand Up @@ -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<String, Error> {
Expand Down
46 changes: 42 additions & 4 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct JsGenerated {
mode: OutputMode,
js: String,
ts: String,
start: Option<String>,
snippets: HashMap<String, Vec<String>>,
local_modules: HashMap<String, String>,
npm_dependencies: HashMap<String, (PathBuf, String)>,
Expand Down Expand Up @@ -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(),
Expand All @@ -430,6 +431,7 @@ impl Bindgen {
npm_dependencies: cx.npm_dependencies.clone(),
js,
ts,
start,
})
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -660,9 +672,35 @@ impl Output {
} else {
"js"
};

fn write<P, C>(path: P, contents: C) -> Result<(), anyhow::Error>
where
P: AsRef<Path>,
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");
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))?;
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/tests/reference/anyref-empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ export const __wbindgen_init_anyref_table = function() {
;
};

wasm.__wbindgen_start();

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/anyref-empty.wat
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,3 @@ export const __wbindgen_init_anyref_table = function() {
;
};

wasm.__wbindgen_start();

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/anyref-import-catch.wat
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/tests/reference/anyref-nop.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@ export const __wbindgen_init_anyref_table = function() {
;
};

wasm.__wbindgen_start();

2 changes: 1 addition & 1 deletion crates/cli/tests/reference/anyref-nop.wat
Original file line number Diff line number Diff line change
@@ -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)
Expand Down