Skip to content

Commit 031425b

Browse files
committed
fix: resolve id in input
1 parent e6870ad commit 031425b

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/generate.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@ export function createGeneratePlugin({
4646
const inputAliasMap = new Map<string, string>()
4747
const resolver = createResolver()
4848
let programs: TsProgram[] = []
49+
let cwd: string
4950

5051
return {
5152
name: 'rolldown-plugin-dts:generate',
5253

54+
options(options) {
55+
cwd = options.cwd || process.cwd()
56+
},
57+
5358
buildStart(options) {
5459
if (!compilerOptions) {
5560
const { config } = getTsconfig(options.cwd) || {}
@@ -71,10 +76,9 @@ export function createGeneratePlugin({
7176
}
7277

7378
if (!Array.isArray(options.input)) {
74-
const cwd = options.cwd || process.cwd()
75-
for (const [fileName, inputFilePath] of Object.entries(options.input)) {
76-
const id = path.resolve(cwd, inputFilePath)
77-
inputAliasMap.set(id, fileName)
79+
for (const [name, id] of Object.entries(options.input)) {
80+
// `id` should be resolved in `resolveId`
81+
inputAliasMap.set(id, name)
7882
}
7983
}
8084
},
@@ -163,10 +167,28 @@ export function createGeneratePlugin({
163167
},
164168
},
165169

166-
async resolveId(id, importer) {
167-
// must be entry
170+
async resolveId(id, importer, extraOptions) {
168171
if (dtsMap.has(id)) {
172+
// must be dts entry
169173
return { id, meta }
174+
} else if (extraOptions.isEntry && inputAliasMap.has(id)) {
175+
// resolve id for inputAliasMap
176+
let resolution = await this.resolve(id, importer, {
177+
...extraOptions,
178+
skipSelf: true,
179+
})
180+
if (!resolution) {
181+
resolution = await this.resolve(path.resolve(cwd, id), importer, {
182+
...extraOptions,
183+
skipSelf: true,
184+
})
185+
if (!resolution) return
186+
}
187+
188+
const alias = inputAliasMap.get(id)!
189+
inputAliasMap.delete(id)
190+
inputAliasMap.set(resolution.id, alias)
191+
return resolution
170192
}
171193

172194
if (importer && this.getModuleInfo(importer)?.meta.dtsFile) {

tsdown.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig } from 'tsdown'
22
import { dts } from './src/index'
33

44
export default defineConfig({
5-
entry: ['./src/index.ts'],
5+
entry: ['./src/index.ts', './src/utils/filename.ts'],
66
target: 'node20.18',
77
clean: true,
88
platform: 'node',

0 commit comments

Comments
 (0)