mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2025-01-06 04:07:47 +03:00
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:
parent
a521c9012c
commit
e16f7e41bf
@ -167,7 +167,10 @@ impl<'a> Context<'a> {
|
|||||||
Ok(())
|
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
|
// 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
|
// glue for all classes as well as finish up a few final imports like
|
||||||
// `__wrap` and such.
|
// `__wrap` and such.
|
||||||
@ -273,9 +276,10 @@ impl<'a> Context<'a> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
module_name: &str,
|
module_name: &str,
|
||||||
needs_manual_start: bool,
|
needs_manual_start: bool,
|
||||||
) -> Result<(String, String), Error> {
|
) -> Result<(String, String, Option<String>), Error> {
|
||||||
let mut ts = self.typescript.clone();
|
let mut ts = self.typescript.clone();
|
||||||
let mut js = String::new();
|
let mut js = String::new();
|
||||||
|
let mut start = None;
|
||||||
|
|
||||||
if let OutputMode::NoModules { global } = &self.config.mode {
|
if let OutputMode::NoModules { global } = &self.config.mode {
|
||||||
js.push_str(&format!("let {};\n(function() {{\n", global));
|
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) {
|
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
|
||||||
let import = self.module.imports.get_mut(*id);
|
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("\nexport const ");
|
||||||
footer.push_str(&import.name);
|
footer.push_str(&import.name);
|
||||||
footer.push_str(" = ");
|
footer.push_str(" = ");
|
||||||
@ -348,7 +352,7 @@ impl<'a> Context<'a> {
|
|||||||
footer.push_str(";\n");
|
footer.push_str(";\n");
|
||||||
}
|
}
|
||||||
if needs_manual_start {
|
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");
|
js = js.replace("\n\n\n", "\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((js, ts))
|
Ok((js, ts, start))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn js_import_header(&self) -> Result<String, Error> {
|
fn js_import_header(&self) -> Result<String, Error> {
|
||||||
|
@ -61,6 +61,7 @@ struct JsGenerated {
|
|||||||
mode: OutputMode,
|
mode: OutputMode,
|
||||||
js: String,
|
js: String,
|
||||||
ts: String,
|
ts: String,
|
||||||
|
start: Option<String>,
|
||||||
snippets: HashMap<String, Vec<String>>,
|
snippets: HashMap<String, Vec<String>>,
|
||||||
local_modules: HashMap<String, String>,
|
local_modules: HashMap<String, String>,
|
||||||
npm_dependencies: HashMap<String, (PathBuf, String)>,
|
npm_dependencies: HashMap<String, (PathBuf, String)>,
|
||||||
@ -421,7 +422,7 @@ impl Bindgen {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
|
let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
|
||||||
cx.generate()?;
|
cx.generate()?;
|
||||||
let (js, ts) = cx.finalize(stem)?;
|
let (js, ts, start) = cx.finalize(stem)?;
|
||||||
Generated::Js(JsGenerated {
|
Generated::Js(JsGenerated {
|
||||||
snippets: aux.snippets.clone(),
|
snippets: aux.snippets.clone(),
|
||||||
local_modules: aux.local_modules.clone(),
|
local_modules: aux.local_modules.clone(),
|
||||||
@ -430,6 +431,7 @@ impl Bindgen {
|
|||||||
npm_dependencies: cx.npm_dependencies.clone(),
|
npm_dependencies: cx.npm_dependencies.clone(),
|
||||||
js,
|
js,
|
||||||
ts,
|
ts,
|
||||||
|
start,
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -568,6 +570,16 @@ impl OutputMode {
|
|||||||
_ => false,
|
_ => 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,
|
/// 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::InterfaceTypes => self.stem.clone(),
|
||||||
Generated::Js(_) => format!("{}_bg", self.stem),
|
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)?;
|
fs::create_dir_all(out_dir)?;
|
||||||
let wasm_bytes = self.module.emit_wasm();
|
let wasm_bytes = self.module.emit_wasm();
|
||||||
fs::write(&wasm_path, wasm_bytes)
|
fs::write(&wasm_path, wasm_bytes)
|
||||||
@ -660,9 +672,35 @@ impl Output {
|
|||||||
} else {
|
} else {
|
||||||
"js"
|
"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);
|
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 {
|
if gen.typescript {
|
||||||
let ts_path = js_path.with_extension("d.ts");
|
let ts_path = js_path.with_extension("d.ts");
|
||||||
|
@ -112,7 +112,7 @@ fn runtest(test: &Path) -> Result<()> {
|
|||||||
let wat = sanitize_wasm(&wasm)?;
|
let wat = sanitize_wasm(&wasm)?;
|
||||||
assert_same(&wat, &test.with_extension("wat"))?;
|
assert_same(&wat, &test.with_extension("wat"))?;
|
||||||
} else {
|
} 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"))?;
|
assert_same(&js, &test.with_extension("js"))?;
|
||||||
let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?;
|
let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?;
|
||||||
assert_same(&wat, &test.with_extension("wat"))?;
|
assert_same(&wat, &test.with_extension("wat"))?;
|
||||||
|
@ -11,5 +11,3 @@ export const __wbindgen_init_anyref_table = function() {
|
|||||||
;
|
;
|
||||||
};
|
};
|
||||||
|
|
||||||
wasm.__wbindgen_start();
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
(module
|
(module
|
||||||
(type (;0;) (func))
|
(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)
|
(table (;0;) 32 anyref)
|
||||||
(memory (;0;) 16)
|
(memory (;0;) 16)
|
||||||
(export "memory" (memory 0))
|
(export "memory" (memory 0))
|
||||||
|
@ -64,5 +64,3 @@ export const __wbindgen_init_anyref_table = function() {
|
|||||||
;
|
;
|
||||||
};
|
};
|
||||||
|
|
||||||
wasm.__wbindgen_start();
|
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
(type (;0;) (func))
|
(type (;0;) (func))
|
||||||
(type (;1;) (func (result i32)))
|
(type (;1;) (func (result i32)))
|
||||||
(type (;2;) (func (param 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 $__wbindgen_exn_store (type 2) (param i32))
|
||||||
(func $__anyref_table_alloc (type 1) (result i32))
|
(func $__anyref_table_alloc (type 1) (result i32))
|
||||||
(func $exported (type 0))
|
(func $exported (type 0))
|
||||||
|
@ -17,5 +17,3 @@ export const __wbindgen_init_anyref_table = function() {
|
|||||||
;
|
;
|
||||||
};
|
};
|
||||||
|
|
||||||
wasm.__wbindgen_start();
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
(module
|
(module
|
||||||
(type (;0;) (func))
|
(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))
|
(func $foo (type 0))
|
||||||
(table (;0;) 32 anyref)
|
(table (;0;) 32 anyref)
|
||||||
(memory (;0;) 17)
|
(memory (;0;) 17)
|
||||||
|
Loading…
Reference in New Issue
Block a user