fix: change format of output .d.ts files

This commit is contained in:
hyper 2026-03-31 16:12:17 +08:00
parent 525ae262fd
commit d056145462
4 changed files with 35 additions and 61 deletions

View File

@ -57,32 +57,25 @@ module.exports = {
`emitTypes: true`loader 会自动生成 `.d.ts` 类型定义文件:
```typescript
// data.schema.d.ts
declare module "./data.schema.csv" {
export interface data_schema {
// data.csv.d.ts
type Table = {
name: string;
age: number;
active: boolean;
scores: number[];
}
}[];
export type RowType = data_schema;
const data: data_schema[];
export default data;
}
declare const data: Table;
export default data;
```
### Importing in TypeScript
```typescript
import data from './data.schema.csv';
import data from './data.csv';
// TypeScript 会自动推断类型:
// data: { name: string; age: number; active: boolean; scores: number[] }[]
// 如果启用了 emitTypes也可以显式导入类型:
import type { RowType } from './data.schema.csv';
```
## Options

View File

@ -387,19 +387,14 @@ function schemaToTypeString(schema) {
return "unknown";
}
}
function generateTypeDefinition(resourceName, propertyConfigs, relativePath) {
const interfaceName = path.basename(resourceName, path.extname(resourceName)).replace(/[^a-zA-Z0-9_$]/g, "_").replace(/^(\d)/, "_$1");
function generateTypeDefinition(resourceName, propertyConfigs) {
const properties = propertyConfigs.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`).join("\n");
return `declare module "${relativePath}" {
export interface ${interfaceName} {
return `type Table = {
${properties}
}
}[];
export type RowType = ${interfaceName};
const data: ${interfaceName}[];
export default data;
}
declare const data: Table;
export default data;
`;
}
function csvLoader(content) {
@ -474,9 +469,9 @@ function csvLoader(content) {
relativePath = relativePath.substring(1);
}
relativePath = relativePath.replace(/\\/g, "/");
const dtsFileName = relativePath.replace(/\.csv$/, ".d.ts");
const dtsFileName = `${relativePath}.d.ts`;
const outputPath = typesOutputDir ? path.join(typesOutputDir, dtsFileName) : dtsFileName;
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs);
if (writeToDisk) {
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });

View File

@ -353,19 +353,14 @@ function schemaToTypeString(schema) {
return "unknown";
}
}
function generateTypeDefinition(resourceName, propertyConfigs, relativePath) {
const interfaceName = path.basename(resourceName, path.extname(resourceName)).replace(/[^a-zA-Z0-9_$]/g, "_").replace(/^(\d)/, "_$1");
function generateTypeDefinition(resourceName, propertyConfigs) {
const properties = propertyConfigs.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`).join("\n");
return `declare module "${relativePath}" {
export interface ${interfaceName} {
return `type Table = {
${properties}
}
}[];
export type RowType = ${interfaceName};
const data: ${interfaceName}[];
export default data;
}
declare const data: Table;
export default data;
`;
}
function csvLoader(content) {
@ -440,9 +435,9 @@ function csvLoader(content) {
relativePath = relativePath.substring(1);
}
relativePath = relativePath.replace(/\\/g, "/");
const dtsFileName = relativePath.replace(/\.csv$/, ".d.ts");
const dtsFileName = `${relativePath}.d.ts`;
const outputPath = typesOutputDir ? path.join(typesOutputDir, dtsFileName) : dtsFileName;
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs);
if (writeToDisk) {
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });

View File

@ -57,27 +57,18 @@ function schemaToTypeString(schema: Schema): string {
*/
function generateTypeDefinition(
resourceName: string,
propertyConfigs: PropertyConfig[],
relativePath: string
propertyConfigs: PropertyConfig[]
): string {
const interfaceName = path.basename(resourceName, path.extname(resourceName))
.replace(/[^a-zA-Z0-9_$]/g, '_')
.replace(/^(\d)/, '_$1');
const properties = propertyConfigs
.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`)
.join('\n');
return `declare module "${relativePath}" {
export interface ${interfaceName} {
return `type Table = {
${properties}
}
}[];
export type RowType = ${interfaceName};
const data: ${interfaceName}[];
export default data;
}
declare const data: Table;
export default data;
`;
}
@ -167,12 +158,12 @@ export default function csvLoader(
}
relativePath = relativePath.replace(/\\/g, '/');
// Replace .csv with .d.ts for the output filename
const dtsFileName = relativePath.replace(/\.csv$/, '.d.ts');
// Replace .csv with .csv.d.ts for the output filename
const dtsFileName = `${relativePath}.d.ts`;
const outputPath = typesOutputDir
? path.join(typesOutputDir, dtsFileName)
: dtsFileName;
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs);
if (writeToDisk) {
// Write directly to disk (useful for dev server)