diff --git a/src/create-module-declaration.js b/src/create-module-declaration.js index dcb940d..4336df7 100644 --- a/src/create-module-declaration.js +++ b/src/create-module-declaration.js @@ -12,7 +12,7 @@ import { clean_jsdoc, get_dts, is_declaration, is_reference, resolve_dts, walk } * @returns {{ * content: string; * mappings: Map; - * ambient: string[]; + * ambient: import('./types').ModuleReference[]; * }} */ export function create_module_declaration(id, entry, created, resolve) { @@ -21,7 +21,7 @@ export function create_module_declaration(id, entry, created, resolve) { /** @type {Map} */ const mappings = new Map(); - /** @type {string[]} */ + /** @type {import('./types').ModuleReference[]} */ const ambient = []; /** @type {Record>} */ @@ -73,9 +73,7 @@ export function create_module_declaration(id, entry, created, resolve) { } for (const dep of module.ambient_imports) { - if (!dep.external) { - ambient.push(dep.id); - } + ambient.push(dep); } for (const binding of module.imports.values()) { diff --git a/src/index.js b/src/index.js index 67c808b..4ae51b8 100644 --- a/src/index.js +++ b/src/index.js @@ -90,6 +90,9 @@ export async function createBundle(options) { /** @type {Set} */ const ambient_modules = new Set(); + /** @type {Set} */ + const external_ambient_modules = new Set(); + let first = true; /** @@ -125,8 +128,12 @@ export async function createBundle(options) { types += content; all_mappings.set(id, mappings); - for (const id of ambient) { - ambient_modules.add(id); + for (const dep of ambient) { + if (dep.external) { + external_ambient_modules.add(dep.id); + } else { + ambient_modules.add(dep.id); + } } } @@ -152,6 +159,14 @@ export async function createBundle(options) { types += result.trim().toString(); } + if (external_ambient_modules.size > 0) { + const imports = Array.from(external_ambient_modules) + .map((id) => `import '${id}';`) + .join('\n'); + + types = `${imports}\n\n${types}`; + } + // finally, add back exports as appropriate const ast = ts.createSourceFile(output, types, ts.ScriptTarget.Latest, false, ts.ScriptKind.TS); const magic_string = new MagicString(types); diff --git a/test/samples/ambient-imports/input/types.d.ts b/test/samples/ambient-imports/input/types.d.ts new file mode 100644 index 0000000..73ff6d7 --- /dev/null +++ b/test/samples/ambient-imports/input/types.d.ts @@ -0,0 +1,3 @@ +import 'some-ambient-import'; + +export interface Foo {} diff --git a/test/samples/ambient-imports/output/index.d.ts b/test/samples/ambient-imports/output/index.d.ts new file mode 100644 index 0000000..9bb8f94 --- /dev/null +++ b/test/samples/ambient-imports/output/index.d.ts @@ -0,0 +1,7 @@ +import 'some-ambient-import'; + +declare module 'ambient-imports' { + export interface Foo {} +} + +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/test/samples/ambient-imports/output/index.d.ts.map b/test/samples/ambient-imports/output/index.d.ts.map new file mode 100644 index 0000000..ff6baf1 --- /dev/null +++ b/test/samples/ambient-imports/output/index.d.ts.map @@ -0,0 +1,14 @@ +{ + "version": 3, + "file": "index.d.ts", + "names": [ + "Foo" + ], + "sources": [ + "../input/types.d.ts" + ], + "sourcesContent": [ + null + ], + "mappings": ";;;kBAEiBA,GAAGA" +} \ No newline at end of file