Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/create-module-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { clean_jsdoc, get_dts, is_declaration, is_reference, resolve_dts, walk }
* @returns {{
* content: string;
* mappings: Map<string, import('./types').Mapping>;
* ambient: string[];
* ambient: import('./types').ModuleReference[];
* }}
*/
export function create_module_declaration(id, entry, created, resolve) {
Expand All @@ -21,7 +21,7 @@ export function create_module_declaration(id, entry, created, resolve) {
/** @type {Map<string, import('./types').Mapping>} */
const mappings = new Map();

/** @type {string[]} */
/** @type {import('./types').ModuleReference[]} */
const ambient = [];

/** @type {Record<string, Record<string, import('./types').Declaration>>} */
Expand Down Expand Up @@ -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()) {
Expand Down
19 changes: 17 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export async function createBundle(options) {
/** @type {Set<string>} */
const ambient_modules = new Set();

/** @type {Set<string>} */
const external_ambient_modules = new Set();

let first = true;

/**
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions test/samples/ambient-imports/input/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'some-ambient-import';

export interface Foo {}
7 changes: 7 additions & 0 deletions test/samples/ambient-imports/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'some-ambient-import';

declare module 'ambient-imports' {
export interface Foo {}
}

//# sourceMappingURL=index.d.ts.map
14 changes: 14 additions & 0 deletions test/samples/ambient-imports/output/index.d.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 3,
"file": "index.d.ts",
"names": [
"Foo"
],
"sources": [
"../input/types.d.ts"
],
"sourcesContent": [
null
],
"mappings": ";;;kBAEiBA,GAAGA"
}