Skip to content

Commit d54c1c5

Browse files
authored
Merge pull request #27 from Rich-Harris/ambient-imports
add external ambient imports
2 parents 6502f1b + 1f6314e commit d54c1c5

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

src/create-module-declaration.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { clean_jsdoc, get_dts, is_declaration, is_reference, resolve_dts, walk }
1212
* @returns {{
1313
* content: string;
1414
* mappings: Map<string, import('./types').Mapping>;
15-
* ambient: string[];
15+
* ambient: import('./types').ModuleReference[];
1616
* }}
1717
*/
1818
export function create_module_declaration(id, entry, created, resolve) {
@@ -21,7 +21,7 @@ export function create_module_declaration(id, entry, created, resolve) {
2121
/** @type {Map<string, import('./types').Mapping>} */
2222
const mappings = new Map();
2323

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

2727
/** @type {Record<string, Record<string, import('./types').Declaration>>} */
@@ -73,9 +73,7 @@ export function create_module_declaration(id, entry, created, resolve) {
7373
}
7474

7575
for (const dep of module.ambient_imports) {
76-
if (!dep.external) {
77-
ambient.push(dep.id);
78-
}
76+
ambient.push(dep);
7977
}
8078

8179
for (const binding of module.imports.values()) {

src/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ export async function createBundle(options) {
9090
/** @type {Set<string>} */
9191
const ambient_modules = new Set();
9292

93+
/** @type {Set<string>} */
94+
const external_ambient_modules = new Set();
95+
9396
let first = true;
9497

9598
/**
@@ -125,8 +128,12 @@ export async function createBundle(options) {
125128

126129
types += content;
127130
all_mappings.set(id, mappings);
128-
for (const id of ambient) {
129-
ambient_modules.add(id);
131+
for (const dep of ambient) {
132+
if (dep.external) {
133+
external_ambient_modules.add(dep.id);
134+
} else {
135+
ambient_modules.add(dep.id);
136+
}
130137
}
131138
}
132139

@@ -152,6 +159,14 @@ export async function createBundle(options) {
152159
types += result.trim().toString();
153160
}
154161

162+
if (external_ambient_modules.size > 0) {
163+
const imports = Array.from(external_ambient_modules)
164+
.map((id) => `import '${id}';`)
165+
.join('\n');
166+
167+
types = `${imports}\n\n${types}`;
168+
}
169+
155170
// finally, add back exports as appropriate
156171
const ast = ts.createSourceFile(output, types, ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
157172
const magic_string = new MagicString(types);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'some-ambient-import';
2+
3+
export interface Foo {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'some-ambient-import';
2+
3+
declare module 'ambient-imports' {
4+
export interface Foo {}
5+
}
6+
7+
//# sourceMappingURL=index.d.ts.map
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": 3,
3+
"file": "index.d.ts",
4+
"names": [
5+
"Foo"
6+
],
7+
"sources": [
8+
"../input/types.d.ts"
9+
],
10+
"sourcesContent": [
11+
null
12+
],
13+
"mappings": ";;;kBAEiBA,GAAGA"
14+
}

0 commit comments

Comments
 (0)