Skip to content

Commit 6363c8a

Browse files
committed
fix: inline import as namespace
1 parent a9fe569 commit 6363c8a

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

src/index.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import { walk } from 'estree-walker'
3-
import { MagicStringAST } from 'magic-string-ast'
3+
import { MagicStringAST, type MagicString } from 'magic-string-ast'
44
import {
55
parseAsync,
66
type BindingPattern,
@@ -54,6 +54,18 @@ export function dts(): Plugin {
5454
return symbolMap.get(symbolId)!
5555
}
5656

57+
const importsMap: Record<string, string> = {}
58+
function importNamespace(s: MagicString, source: string) {
59+
if (importsMap[source]) return importsMap[source]
60+
let local = source.replaceAll(/[^a-z0-9]/gi, '_')
61+
if (/^\d/.test(local[0])) {
62+
local = `_${local}`
63+
}
64+
importsMap[source] = local
65+
s.prepend(`import * as ${local} from ${JSON.stringify(source)};\n`)
66+
return local
67+
}
68+
5769
return {
5870
name: 'rolldown-plugin-dts',
5971
options(options) {
@@ -119,7 +131,7 @@ export function dts(): Plugin {
119131
continue
120132
}
121133

122-
const deps = collectDependencies(s, node)
134+
const deps = collectDependencies(s, node, importNamespace)
123135
const depsString = stringifyDependencies(s, deps)
124136
const depsRanges: Range[] = deps.map((dep) => [
125137
dep.start - offset,
@@ -223,11 +235,10 @@ export function dts(): Plugin {
223235
}
224236
}
225237

226-
let i = 0
227-
228238
function collectDependencies(
229239
s: MagicStringAST,
230240
node: Node,
241+
importNamespace: (s: MagicString, source: string) => string,
231242
): (Partial<Node> & Span)[] {
232243
const deps: Set<Partial<Node> & Span> = new Set()
233244

@@ -260,21 +271,20 @@ function collectDependencies(
260271
} else if (node.type === 'TSTypeQuery') {
261272
addDependency(node.exprName)
262273
} else if (node.type === 'TSImportType') {
263-
if (node.argument.type !== 'TSLiteralType') return
264-
const source = s.sliceNode(node.argument)
265-
const local = `_${i++}`
266-
const specifiers = node.qualifier
267-
? `{ ${s.sliceNode(node.qualifier)} as ${local} }`
268-
: `* as ${local}`
269-
s.prepend(`import ${specifiers} from ${source};\n`)
270-
if (node.qualifier) {
271-
addDependency({
272-
type: 'Identifier',
273-
name: local,
274-
start: node.start + (node.isTypeOf ? 7 : 0),
275-
end: node.typeArguments ? node.typeArguments.start : node.end,
276-
})
277-
}
274+
if (
275+
node.argument.type !== 'TSLiteralType' ||
276+
node.argument.literal.type !== 'Literal' ||
277+
typeof node.argument.literal.value !== 'string'
278+
)
279+
return
280+
const source = node.argument.literal.value
281+
const local = importNamespace(s, source)
282+
addDependency({
283+
type: 'Identifier',
284+
name: local,
285+
start: node.start + (node.isTypeOf ? 7 : 0),
286+
end: node.argument.end + 1,
287+
})
278288
}
279289
},
280290
})

tests/fixtures/inline-import-typeof-members/diff.patch

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/fixtures/inline-import-typeof-members/snapshot.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// index.d.ts
2-
import { RollupOptions } from "rollup";
2+
import * as rollup from "rollup";
3+
import * as typescript from "typescript";
34

45
//#region tests/fixtures/inline-import-typeof-members/index.d.ts
5-
type TypeScript = typeof import("typescript");
6+
type TypeScript = typeof typescript;
67
interface Test {
7-
rollup: RollupOptions;
8+
rollup: rollup.RollupOptions;
89
}
910

1011
//#endregion

0 commit comments

Comments
 (0)