A Vite plugin that generates TypeScript declaration files from source files in library mode
—
Source map manipulation and path handling utilities for advanced declaration file processing and development tooling integration.
Utility function for modifying source map directory references when declaration files are output to different directories.
/**
* Edit source map directory references to maintain correct paths
* @param content - Source map content as JSON string
* @param fromDir - Original source directory path
* @param toDir - Target destination directory path
* @returns Modified source map content, true if no change needed, or false if parsing fails
*/
function editSourceMapDir(content: string, fromDir: string, toDir: string): string | boolean;Usage Examples:
import { editSourceMapDir } from "vite-plugin-dts";
// Edit source map when moving files between directories
const sourceMapContent = JSON.stringify({
version: 3,
sources: ["../src/index.ts", "../src/utils.ts"],
mappings: "AAAA,..."
});
const updatedSourceMap = editSourceMapDir(
sourceMapContent,
"/project/src",
"/project/dist/types"
);
if (updatedSourceMap && typeof updatedSourceMap === 'string') {
// Source map successfully updated
const parsedMap = JSON.parse(updatedSourceMap);
console.log(parsedMap.sources); // Updated relative paths
} else {
console.error("Failed to update source map");
}
// Using in beforeWriteFile hook
dts({
beforeWriteFile: async (filePath, content) => {
if (filePath.endsWith('.d.ts.map')) {
const result = editSourceMapDir(content, 'src', 'dist/types');
if (typeof result === 'string') {
return { content: result };
}
}
}
});Note: The editSourceMapDir function is the only public utility function exported by the plugin. All other utilities are internal implementation details and are not part of the public API.
When working with source maps, the plugin handles the following structure:
interface SourceMapData {
version: number;
sources: string[];
mappings: string;
names?: string[];
sourceRoot?: string;
sourcesContent?: (string | null)[];
}Usage in Plugin Context:
// Example of source map processing within the plugin
dts({
afterBuild: async (emittedFiles) => {
for (const [filePath, content] of emittedFiles) {
if (filePath.endsWith('.d.ts.map')) {
try {
const sourceMap: SourceMapData = JSON.parse(content);
console.log(`Source map for ${filePath}:`);
console.log(`- Sources: ${sourceMap.sources.length}`);
console.log(`- Mappings length: ${sourceMap.mappings.length}`);
} catch (error) {
console.warn(`Invalid source map in ${filePath}`);
}
}
}
}
});Common patterns for working with paths in plugin configuration and utilities:
// Normalize paths across platforms
const normalizedPath = normalizePath("/path\\to\\file"); // "/path/to/file"
// Ensure absolute paths
const absolutePath = ensureAbsolute("./src", "/project"); // "/project/src"
// Work with arrays consistently
const fileList = ensureArray("single-file.ts"); // ["single-file.ts"]
const multipleFiles = ensureArray(["file1.ts", "file2.ts"]); // ["file1.ts", "file2.ts"]
// Convert TypeScript paths to Vite aliases
const aliases = parseTsAliases("/project/src", {
"@/*": ["./src/*"],
"@utils/*": ["./src/utils/*"]
});Install with Tessl CLI
npx tessl i tessl/npm-vite-plugin-dts