Skip to content

Commit addb082

Browse files
fix deno import logic to include non-placeholder-module imports
1 parent 84c7cf0 commit addb082

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,25 +272,46 @@ impl<'a> Context<'a> {
272272

273273
// generates somthing like
274274
// ```js
275+
// import * as import0 from './snippets/.../inline1.js';
276+
// ```,
277+
//
278+
// ```js
275279
// const imports = {
276280
// __wbindgen_placeholder__: {
277281
// __wbindgen_throw: function(..) { .. },
278282
// ..
279-
// }
283+
// },
284+
// './snippets/deno-65e2634a84cc3c14/inline1.js': import0,
280285
// }
281286
// ```
282-
fn generate_deno_imports(&self) -> String {
283-
let mut imports = "const imports = {\n".to_string();
284-
imports.push_str(&format!(" {}: {{\n", crate::PLACEHOLDER_MODULE));
287+
fn generate_deno_imports(&self) -> (String, String) {
288+
let mut imports = String::new();
289+
let mut wasm_import_object = "const imports = {\n".to_string();
290+
291+
wasm_import_object.push_str(&format!(" {}: {{\n", crate::PLACEHOLDER_MODULE));
285292

286293
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
287294
let import = self.module.imports.get(*id);
288-
imports.push_str(&format!("{}: {},\n", &import.name, js.trim()));
295+
wasm_import_object.push_str(&format!("{}: {},\n", &import.name, js.trim()));
296+
}
297+
298+
wasm_import_object.push_str("\t},\n");
299+
300+
// e.g. snippets without parameters
301+
let import_modules = self
302+
.module
303+
.imports
304+
.iter()
305+
.map(|import| &import.module)
306+
.filter(|module| module.as_str() != PLACEHOLDER_MODULE);
307+
for (i, module) in import_modules.enumerate() {
308+
imports.push_str(&format!("import * as import{} from '{}'\n", i, module));
309+
wasm_import_object.push_str(&format!(" '{}': import{},", module, i))
289310
}
290311

291-
imports.push_str("\t}\n};\n\n");
312+
wasm_import_object.push_str("\n};\n\n");
292313

293-
imports
314+
(imports, wasm_import_object)
294315
}
295316

296317
fn generate_deno_wasm_loading(&self, module_name: &str) -> String {
@@ -369,7 +390,10 @@ impl<'a> Context<'a> {
369390
}
370391

371392
OutputMode::Deno => {
372-
footer.push_str(&self.generate_deno_imports());
393+
let (js_imports, wasm_import_object) = self.generate_deno_imports();
394+
imports.push_str(&js_imports);
395+
footer.push_str(&wasm_import_object);
396+
373397
footer.push_str(&self.generate_deno_wasm_loading(module_name));
374398

375399
if needs_manual_start {

examples/deno/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ extern "C" {
2626
extern "C" {
2727
fn add(a: u32, b: u32) -> u32;
2828
}
29+
#[wasm_bindgen(inline_js = "export function test() {}")]
30+
extern "C" {
31+
fn test();
32+
}
2933

3034
#[wasm_bindgen]
3135
pub fn greet(name: String) {
3236
log(&format!("Hello from {}!", name)); // should output "Hello from Rust!"
3337

3438
let x = MyClass::new();
3539
assert_eq!(x.number(), add(40, 2));
40+
test();
3641
log(&x.render());
3742
}

0 commit comments

Comments
 (0)