fix: improve path handling for .d.ts file emission

- Fix leading slash in relative path
- Use .d.ts extension instead of .csv.d.ts
- Support typesOutputDir option properly
- Use ./ prefix for module declaration path

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
hyper
2026-03-31 15:49:05 +08:00
co-authored by Qwen-Coder
parent 452b600487
commit 377a47e49f
5 changed files with 136 additions and 6 deletions
+16 -6
View File
@@ -152,16 +152,26 @@ export default function csvLoader(
});
const json = JSON.stringify(objects, null, 2);
// Emit type definition file if enabled
if (emitTypes) {
const relativePath = this.resourcePath.replace(this.context, '').replace(/\\/g, '/');
const dtsFileName = relativePath.replace(/\.csv$/, '.d.ts');
const outputPath = path.join(typesOutputDir, dtsFileName);
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, relativePath);
const context = this.context || '';
// Get relative path from context, normalize to forward slashes
let relativePath = this.resourcePath.replace(context, '');
if (relativePath.startsWith('\\') || relativePath.startsWith('/')) {
relativePath = relativePath.substring(1);
}
relativePath = relativePath.replace(/\\/g, '/');
// Replace .csv with .d.ts for the output filename
const dtsFileName = relativePath.replace(/\.csv$/, '.d.ts');
const outputPath = typesOutputDir
? path.join(typesOutputDir, dtsFileName)
: dtsFileName;
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
this.emitFile?.(outputPath, dtsContent);
}
return `export default ${json};`;
}