|
1 | 1 | // Copyright 2018-2024 the Deno authors. MIT license.
|
2 | 2 |
|
3 | 3 | use capacity_builder::StringBuilder;
|
4 |
| -use indexmap::IndexSet; |
| 4 | +use indexmap::IndexMap; |
5 | 5 | use wasm_dep_analyzer::ValueType;
|
6 | 6 |
|
7 | 7 | pub fn wasm_module_to_dts(
|
@@ -45,15 +45,30 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
|
45 | 45 | .iter()
|
46 | 46 | .map(|export| is_valid_ident(export.name))
|
47 | 47 | .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 | + } |
54 | 54 | 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 \""); |
57 | 72 | builder.append(*import_module);
|
58 | 73 | builder.append("\";\n");
|
59 | 74 | }
|
@@ -146,14 +161,31 @@ mod test {
|
146 | 161 | use pretty_assertions::assert_eq;
|
147 | 162 | use wasm_dep_analyzer::Export;
|
148 | 163 | use wasm_dep_analyzer::FunctionSignature;
|
| 164 | + use wasm_dep_analyzer::Import; |
149 | 165 | use wasm_dep_analyzer::WasmDeps;
|
150 | 166 |
|
151 | 167 | use super::*;
|
152 | 168 |
|
153 | 169 | #[test]
|
154 | 170 | fn test_output() {
|
155 | 171 | 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 | + ], |
157 | 189 | exports: vec![
|
158 | 190 | Export {
|
159 | 191 | name: "name--1",
|
@@ -228,7 +260,9 @@ mod test {
|
228 | 260 | });
|
229 | 261 | assert_eq!(
|
230 | 262 | 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; |
232 | 266 | export { __deno_wasm_export_0__ as \"name--1\" };
|
233 | 267 | export declare function name2(arg0: number, arg1: bigint | number): bigint;
|
234 | 268 | export declare const name3: unknown;
|
|
0 commit comments