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
This commit is contained in:
Pauan 2020-04-29 17:55:28 +02:00 committed by GitHub
parent a521c9012c
commit e16f7e41bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 19 deletions

View File

@ -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.
@ -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));
@ -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<String, Error> {

View File

@ -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)>,
@ -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<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");

View File

@ -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"))?;

View File

@ -11,5 +11,3 @@ export const __wbindgen_init_anyref_table = function() {
;
};
wasm.__wbindgen_start();

View File

@ -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))

View File

@ -64,5 +64,3 @@ export const __wbindgen_init_anyref_table = function() {
;
};
wasm.__wbindgen_start();

View File

@ -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))

View File

@ -17,5 +17,3 @@ export const __wbindgen_init_anyref_table = function() {
;
};
wasm.__wbindgen_start();

View File

@ -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)