Skip to content

Commit c172197

Browse files
committed
refactor: simplify resolveId
1 parent 8a1d1cc commit c172197

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

src/generate.ts

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -171,45 +171,44 @@ export function createGeneratePlugin({
171171
return { id, meta }
172172
}
173173

174-
if (importer && this.getModuleInfo(importer)?.meta.dtsFile) {
175-
// in dts file
176-
177-
// resolve dependency
178-
if (!isRelative(id)) {
179-
let shouldResolve: boolean
180-
if (typeof resolve === 'boolean') {
181-
shouldResolve = resolve
182-
} else {
183-
shouldResolve = resolve.some((pattern) =>
184-
typeof pattern === 'string' ? id === pattern : pattern.test(id),
185-
)
186-
}
187-
if (shouldResolve) {
188-
const resolution = resolver(id, importer)
189-
if (resolution) return { id: resolution, meta }
190-
} else {
191-
return { id, external: true, meta }
192-
}
193-
}
174+
if (!importer || !this.getModuleInfo(importer)?.meta.dtsFile) {
175+
return
176+
}
194177

195-
// link to the original module
196-
const resolution = await this.resolve(
197-
id,
198-
filename_dts_to(importer!, 'ts'),
199-
)
200-
if (!resolution || resolution.external) return
178+
// in dts file
201179

202-
const dtsId = filename_ts_to_dts(resolution.id)
203-
if (dtsMap.has(dtsId)) {
204-
return { id: dtsId, meta }
180+
// resolve dependency
181+
if (!isRelative(id)) {
182+
let shouldResolve: boolean
183+
if (typeof resolve === 'boolean') {
184+
shouldResolve = resolve
185+
} else {
186+
shouldResolve = resolve.some((pattern) =>
187+
typeof pattern === 'string' ? id === pattern : pattern.test(id),
188+
)
205189
}
206-
207-
// pre-load original module if not already loaded
208-
await this.load(resolution)
209-
if (dtsMap.has(dtsId)) {
210-
return { id: dtsId, meta }
190+
if (shouldResolve) {
191+
const resolution = resolver(id, importer)
192+
if (resolution) return { id: resolution, meta }
193+
} else {
194+
return { id, external: true, meta }
211195
}
212196
}
197+
198+
// link to the original module
199+
const resolution = await this.resolve(id, filename_dts_to(importer, 'ts'))
200+
if (!resolution || resolution.external) return
201+
202+
const dtsId = filename_ts_to_dts(resolution.id)
203+
if (dtsMap.has(dtsId)) {
204+
return { id: dtsId, meta }
205+
}
206+
207+
// pre-load original module if not already loaded
208+
await this.load(resolution)
209+
if (dtsMap.has(dtsId)) {
210+
return { id: dtsId, meta }
211+
}
213212
},
214213

215214
load: {

0 commit comments

Comments
 (0)