Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/rolldown-plugin-dts. A Rolldown plugin to generate and bundle dts files.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798import { createResolver } from 'dts-resolver'import { filename_dts_to, filename_js_to_dts, filename_ts_to_dts, isRelative, RE_DTS, RE_JS, RE_NODE_MODULES, RE_TS,} from './utils/filename'import type { OptionsResolved } from '.'import type { Plugin } from 'rolldown'
export const meta = { dtsFile: true } as const
export function createDtsResolvePlugin({ tsconfig, resolve,}: Pick<OptionsResolved, 'tsconfig' | 'resolve'>): Plugin { const resolver = createResolver({ tsconfig: tsconfig ? (tsconfig as string) : undefined, })
return { name: 'rolldown-plugin-dts:resolve',
resolveId: { order: 'pre', async handler(id, importer, options) { // only resolve in dts file if ( !importer || (!RE_DTS.test(importer) && !this.getModuleInfo(importer)?.meta.dtsFile) ) { return }
if (RE_NODE_MODULES.test(importer)) { const resolution = resolver(id, importer) if (resolution) return { id: resolution, meta } }
// link to the original module const tsImporter = filename_dts_to(importer, 'ts') let resolution = await this.resolve(id, tsImporter, options) if (!resolution && !id.endsWith('.d')) { resolution = await this.resolve(`${id}.d`, tsImporter, options) }
// resolve dependency if ( RE_NODE_MODULES.test(resolution?.id || id) || !isRelative(resolution?.id || id) ) { let shouldResolve: boolean if (typeof resolve === 'boolean') { shouldResolve = resolve } else { shouldResolve = resolve.some((pattern) => typeof pattern === 'string' ? id === pattern : pattern.test(id), ) } if (shouldResolve) { const resolution = resolver(id, importer) if (resolution) return { id: resolution, meta } } else { return { id, external: true, meta } } }
if (!resolution || resolution.external) { return resolution }
if (RE_JS.test(resolution.id)) { // resolve js to dts resolution = await this.resolve( filename_js_to_dts(resolution.id), importer, options, ) if (!resolution) return } else if (RE_TS.test(resolution.id) && !RE_DTS.test(resolution.id)) { await this.load(resolution) // redirect ts to dts resolution.id = filename_ts_to_dts(resolution.id) }
if (RE_DTS.test(resolution.id)) { return { ...resolution, meta } } }, }, }}