mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-25 22:22:12 +00:00
fix deno import logic to include non-placeholder-module imports
This commit is contained in:
parent
84c7cf01ce
commit
addb0824d1
@ -272,25 +272,46 @@ impl<'a> Context<'a> {
|
|||||||
|
|
||||||
// generates somthing like
|
// generates somthing like
|
||||||
// ```js
|
// ```js
|
||||||
|
// import * as import0 from './snippets/.../inline1.js';
|
||||||
|
// ```,
|
||||||
|
//
|
||||||
|
// ```js
|
||||||
// const imports = {
|
// const imports = {
|
||||||
// __wbindgen_placeholder__: {
|
// __wbindgen_placeholder__: {
|
||||||
// __wbindgen_throw: function(..) { .. },
|
// __wbindgen_throw: function(..) { .. },
|
||||||
// ..
|
// ..
|
||||||
// }
|
// },
|
||||||
|
// './snippets/deno-65e2634a84cc3c14/inline1.js': import0,
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
fn generate_deno_imports(&self) -> String {
|
fn generate_deno_imports(&self) -> (String, String) {
|
||||||
let mut imports = "const imports = {\n".to_string();
|
let mut imports = String::new();
|
||||||
imports.push_str(&format!(" {}: {{\n", crate::PLACEHOLDER_MODULE));
|
let mut wasm_import_object = "const imports = {\n".to_string();
|
||||||
|
|
||||||
|
wasm_import_object.push_str(&format!(" {}: {{\n", crate::PLACEHOLDER_MODULE));
|
||||||
|
|
||||||
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(*id);
|
let import = self.module.imports.get(*id);
|
||||||
imports.push_str(&format!("{}: {},\n", &import.name, js.trim()));
|
wasm_import_object.push_str(&format!("{}: {},\n", &import.name, js.trim()));
|
||||||
}
|
}
|
||||||
|
|
||||||
imports.push_str("\t}\n};\n\n");
|
wasm_import_object.push_str("\t},\n");
|
||||||
|
|
||||||
imports
|
// e.g. snippets without parameters
|
||||||
|
let import_modules = self
|
||||||
|
.module
|
||||||
|
.imports
|
||||||
|
.iter()
|
||||||
|
.map(|import| &import.module)
|
||||||
|
.filter(|module| module.as_str() != PLACEHOLDER_MODULE);
|
||||||
|
for (i, module) in import_modules.enumerate() {
|
||||||
|
imports.push_str(&format!("import * as import{} from '{}'\n", i, module));
|
||||||
|
wasm_import_object.push_str(&format!(" '{}': import{},", module, i))
|
||||||
|
}
|
||||||
|
|
||||||
|
wasm_import_object.push_str("\n};\n\n");
|
||||||
|
|
||||||
|
(imports, wasm_import_object)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_deno_wasm_loading(&self, module_name: &str) -> String {
|
fn generate_deno_wasm_loading(&self, module_name: &str) -> String {
|
||||||
@ -369,7 +390,10 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
OutputMode::Deno => {
|
OutputMode::Deno => {
|
||||||
footer.push_str(&self.generate_deno_imports());
|
let (js_imports, wasm_import_object) = self.generate_deno_imports();
|
||||||
|
imports.push_str(&js_imports);
|
||||||
|
footer.push_str(&wasm_import_object);
|
||||||
|
|
||||||
footer.push_str(&self.generate_deno_wasm_loading(module_name));
|
footer.push_str(&self.generate_deno_wasm_loading(module_name));
|
||||||
|
|
||||||
if needs_manual_start {
|
if needs_manual_start {
|
||||||
|
@ -26,6 +26,10 @@ extern "C" {
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
fn add(a: u32, b: u32) -> u32;
|
fn add(a: u32, b: u32) -> u32;
|
||||||
}
|
}
|
||||||
|
#[wasm_bindgen(inline_js = "export function test() {}")]
|
||||||
|
extern "C" {
|
||||||
|
fn test();
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn greet(name: String) {
|
pub fn greet(name: String) {
|
||||||
@ -33,5 +37,6 @@ pub fn greet(name: String) {
|
|||||||
|
|
||||||
let x = MyClass::new();
|
let x = MyClass::new();
|
||||||
assert_eq!(x.number(), add(40, 2));
|
assert_eq!(x.number(), add(40, 2));
|
||||||
|
test();
|
||||||
log(&x.render());
|
log(&x.render());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user