fix: change format of output .d.ts files
This commit is contained in:
parent
525ae262fd
commit
d056145462
|
|
@ -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[];
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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}[];
|
||||
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 });
|
||||
|
|
|
|||
|
|
@ -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}[];
|
||||
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 });
|
||||
|
|
|
|||
|
|
@ -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}[];
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue