Skip to content

Commit

Permalink
fix: add named imports to wasm output (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 3, 2025
1 parent 53d9399 commit 5eedb60
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
56 changes: 45 additions & 11 deletions src/source/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use capacity_builder::StringBuilder;
use indexmap::IndexSet;
use indexmap::IndexMap;
use wasm_dep_analyzer::ValueType;

pub fn wasm_module_to_dts(
Expand Down Expand Up @@ -45,15 +45,30 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
.iter()
.map(|export| is_valid_ident(export.name))
.collect::<Vec<_>>();
let unique_import_modules = wasm_deps
.imports
.iter()
.map(|import| import.module)
.collect::<IndexSet<_>>();

let mut unique_import_modules: IndexMap<&str, Vec<&str>> =
IndexMap::with_capacity(wasm_deps.imports.len());
for import in &wasm_deps.imports {
let entry = unique_import_modules.entry(import.module).or_default();
entry.push(import.name);
}
StringBuilder::build(|builder| {
for import_module in &unique_import_modules {
builder.append("import \"");
let mut count = 0;
for (import_module, named_imports) in &unique_import_modules {
builder.append("import { ");
// we add the named imports in order to cause a type checking error if
// the importing module does not have it as an export
for (i, named_import) in named_imports.iter().enumerate() {
if i > 0 {
builder.append(", ");
}
builder.append('"');
builder.append(*named_import);
builder.append("\" as __deno_wasm_import_");
builder.append(count);
builder.append("__");
count += 1;
}
builder.append(" } from \"");
builder.append(*import_module);
builder.append("\";\n");
}
Expand Down Expand Up @@ -146,14 +161,31 @@ mod test {
use pretty_assertions::assert_eq;
use wasm_dep_analyzer::Export;
use wasm_dep_analyzer::FunctionSignature;
use wasm_dep_analyzer::Import;
use wasm_dep_analyzer::WasmDeps;

use super::*;

#[test]
fn test_output() {
let text = wasm_module_deps_to_dts(&WasmDeps {
imports: vec![],
imports: vec![
Import {
name: "name1",
module: "./mod.ts",
import_type: wasm_dep_analyzer::ImportType::Function(0),
},
Import {
name: "name1",
module: "./other.ts",
import_type: wasm_dep_analyzer::ImportType::Function(0),
},
Import {
name: "name2",
module: "./mod.ts",
import_type: wasm_dep_analyzer::ImportType::Function(0),
},
],
exports: vec![
Export {
name: "name--1",
Expand Down Expand Up @@ -228,7 +260,9 @@ mod test {
});
assert_eq!(
text,
"declare function __deno_wasm_export_0__(): void;
"import { \"name1\" as __deno_wasm_import_0__, \"name2\" as __deno_wasm_import_1__ } from \"./mod.ts\";
import { \"name1\" as __deno_wasm_import_2__ } from \"./other.ts\";
declare function __deno_wasm_export_0__(): void;
export { __deno_wasm_export_0__ as \"name--1\" };
export declare function name2(arg0: number, arg1: bigint | number): bigint;
export declare const name3: unknown;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ async fn test_wasm_math_with_import() {
deno_graph::Module::Wasm(wasm_module) => {
assert_eq!(
wasm_module.source_dts.to_string(),
"import \"./math.ts\";
"import { \"js_add\" as __deno_wasm_import_0__, \"js_subtract\" as __deno_wasm_import_1__ } from \"./math.ts\";
export declare const memory: WebAssembly.Memory;
export declare function add(arg0: number, arg1: number): number;
export declare function subtract(arg0: number, arg1: number): number;
Expand Down

0 comments on commit 5eedb60

Please sign in to comment.