Skip to content

Commit 5eedb60

Browse files
authored
fix: add named imports to wasm output (#558)
1 parent 53d9399 commit 5eedb60

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/source/wasm.rs

+45-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. MIT license.
22

33
use capacity_builder::StringBuilder;
4-
use indexmap::IndexSet;
4+
use indexmap::IndexMap;
55
use wasm_dep_analyzer::ValueType;
66

77
pub fn wasm_module_to_dts(
@@ -45,15 +45,30 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
4545
.iter()
4646
.map(|export| is_valid_ident(export.name))
4747
.collect::<Vec<_>>();
48-
let unique_import_modules = wasm_deps
49-
.imports
50-
.iter()
51-
.map(|import| import.module)
52-
.collect::<IndexSet<_>>();
53-
48+
let mut unique_import_modules: IndexMap<&str, Vec<&str>> =
49+
IndexMap::with_capacity(wasm_deps.imports.len());
50+
for import in &wasm_deps.imports {
51+
let entry = unique_import_modules.entry(import.module).or_default();
52+
entry.push(import.name);
53+
}
5454
StringBuilder::build(|builder| {
55-
for import_module in &unique_import_modules {
56-
builder.append("import \"");
55+
let mut count = 0;
56+
for (import_module, named_imports) in &unique_import_modules {
57+
builder.append("import { ");
58+
// we add the named imports in order to cause a type checking error if
59+
// the importing module does not have it as an export
60+
for (i, named_import) in named_imports.iter().enumerate() {
61+
if i > 0 {
62+
builder.append(", ");
63+
}
64+
builder.append('"');
65+
builder.append(*named_import);
66+
builder.append("\" as __deno_wasm_import_");
67+
builder.append(count);
68+
builder.append("__");
69+
count += 1;
70+
}
71+
builder.append(" } from \"");
5772
builder.append(*import_module);
5873
builder.append("\";\n");
5974
}
@@ -146,14 +161,31 @@ mod test {
146161
use pretty_assertions::assert_eq;
147162
use wasm_dep_analyzer::Export;
148163
use wasm_dep_analyzer::FunctionSignature;
164+
use wasm_dep_analyzer::Import;
149165
use wasm_dep_analyzer::WasmDeps;
150166

151167
use super::*;
152168

153169
#[test]
154170
fn test_output() {
155171
let text = wasm_module_deps_to_dts(&WasmDeps {
156-
imports: vec![],
172+
imports: vec![
173+
Import {
174+
name: "name1",
175+
module: "./mod.ts",
176+
import_type: wasm_dep_analyzer::ImportType::Function(0),
177+
},
178+
Import {
179+
name: "name1",
180+
module: "./other.ts",
181+
import_type: wasm_dep_analyzer::ImportType::Function(0),
182+
},
183+
Import {
184+
name: "name2",
185+
module: "./mod.ts",
186+
import_type: wasm_dep_analyzer::ImportType::Function(0),
187+
},
188+
],
157189
exports: vec![
158190
Export {
159191
name: "name--1",
@@ -228,7 +260,9 @@ mod test {
228260
});
229261
assert_eq!(
230262
text,
231-
"declare function __deno_wasm_export_0__(): void;
263+
"import { \"name1\" as __deno_wasm_import_0__, \"name2\" as __deno_wasm_import_1__ } from \"./mod.ts\";
264+
import { \"name1\" as __deno_wasm_import_2__ } from \"./other.ts\";
265+
declare function __deno_wasm_export_0__(): void;
232266
export { __deno_wasm_export_0__ as \"name--1\" };
233267
export declare function name2(arg0: number, arg1: bigint | number): bigint;
234268
export declare const name3: unknown;

tests/integration_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ async fn test_wasm_math_with_import() {
711711
deno_graph::Module::Wasm(wasm_module) => {
712712
assert_eq!(
713713
wasm_module.source_dts.to_string(),
714-
"import \"./math.ts\";
714+
"import { \"js_add\" as __deno_wasm_import_0__, \"js_subtract\" as __deno_wasm_import_1__ } from \"./math.ts\";
715715
export declare const memory: WebAssembly.Memory;
716716
export declare function add(arg0: number, arg1: number): number;
717717
export declare function subtract(arg0: number, arg1: number): number;

0 commit comments

Comments
 (0)