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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465import path from 'node:path'import { pathToFileURL } from 'node:url'import ts from 'typescript'import type { ExistingRawSourceMap } from 'rolldown'
export const formatHost: ts.FormatDiagnosticsHost = { getCurrentDirectory: () => ts.sys.getCurrentDirectory(), getNewLine: () => ts.sys.newLine, getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase(),}
// fix #77const stripPrivateFields: ts.TransformerFactory<ts.SourceFile | ts.Bundle> = ( ctx,) => { const visitor = (node: ts.Node) => { if (ts.isPropertySignature(node) && ts.isPrivateIdentifier(node.name)) { return ctx.factory.updatePropertySignature( node, node.modifiers, ctx.factory.createStringLiteral(node.name.text), node.questionToken, node.type, ) } return ts.visitEachChild(node, visitor, ctx) } return (sourceFile) => ts.visitNode(sourceFile, visitor, ts.isSourceFile) ?? sourceFile}
export const customTransformers: ts.CustomTransformers = { afterDeclarations: [stripPrivateFields],}
// Since the output directory of tsc and rolldown-plugin-dts might be different,// we need to explicitly set the `sourceRoot` of the source map so that the// final sourcemap has correct paths in `sources` field.export function setSourceMapRoot( map: ExistingRawSourceMap | undefined, // The original path of the source map file (e.g. configured by tsconfig.json `outDir` and emitted by tsc) originalFilePath: string, // The final path of the source map file (e.g. emitted by rolldown-plugin-dts) finalFilePath: string,): void { if (!map) { return }
// Don't override the sourceRoot if it's already set. if (map.sourceRoot) { return }
const originalDir = path.posix.dirname( pathToFileURL(originalFilePath).pathname, ) const finalDir = path.posix.dirname(pathToFileURL(finalFilePath).pathname) if (originalDir !== finalDir) { map.sourceRoot = path.posix.relative(finalDir, originalDir) }}