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` 类型定义文件:
|
当 `emitTypes: true` 时,loader 会自动生成 `.d.ts` 类型定义文件:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// data.schema.d.ts
|
// data.csv.d.ts
|
||||||
declare module "./data.schema.csv" {
|
type Table = {
|
||||||
export interface data_schema {
|
|
||||||
name: string;
|
name: string;
|
||||||
age: number;
|
age: number;
|
||||||
active: boolean;
|
active: boolean;
|
||||||
scores: number[];
|
scores: number[];
|
||||||
}
|
}[];
|
||||||
|
|
||||||
export type RowType = data_schema;
|
declare const data: Table;
|
||||||
|
|
||||||
const data: data_schema[];
|
|
||||||
export default data;
|
export default data;
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Importing in TypeScript
|
### Importing in TypeScript
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import data from './data.schema.csv';
|
import data from './data.csv';
|
||||||
|
|
||||||
// TypeScript 会自动推断类型:
|
// TypeScript 会自动推断类型:
|
||||||
// data: { name: string; age: number; active: boolean; scores: number[] }[]
|
// data: { name: string; age: number; active: boolean; scores: number[] }[]
|
||||||
|
|
||||||
// 如果启用了 emitTypes,也可以显式导入类型:
|
|
||||||
import type { RowType } from './data.schema.csv';
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
|
|
|
||||||
|
|
@ -387,19 +387,14 @@ function schemaToTypeString(schema) {
|
||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function generateTypeDefinition(resourceName, propertyConfigs, relativePath) {
|
function generateTypeDefinition(resourceName, propertyConfigs) {
|
||||||
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");
|
const properties = propertyConfigs.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`).join("\n");
|
||||||
return `declare module "${relativePath}" {
|
return `type Table = {
|
||||||
export interface ${interfaceName} {
|
|
||||||
${properties}
|
${properties}
|
||||||
}
|
}[];
|
||||||
|
|
||||||
export type RowType = ${interfaceName};
|
declare const data: Table;
|
||||||
|
|
||||||
const data: ${interfaceName}[];
|
|
||||||
export default data;
|
export default data;
|
||||||
}
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
function csvLoader(content) {
|
function csvLoader(content) {
|
||||||
|
|
@ -474,9 +469,9 @@ function csvLoader(content) {
|
||||||
relativePath = relativePath.substring(1);
|
relativePath = relativePath.substring(1);
|
||||||
}
|
}
|
||||||
relativePath = relativePath.replace(/\\/g, "/");
|
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 outputPath = typesOutputDir ? path.join(typesOutputDir, dtsFileName) : dtsFileName;
|
||||||
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
|
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs);
|
||||||
if (writeToDisk) {
|
if (writeToDisk) {
|
||||||
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
|
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
|
||||||
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
||||||
|
|
|
||||||
|
|
@ -353,19 +353,14 @@ function schemaToTypeString(schema) {
|
||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function generateTypeDefinition(resourceName, propertyConfigs, relativePath) {
|
function generateTypeDefinition(resourceName, propertyConfigs) {
|
||||||
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");
|
const properties = propertyConfigs.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`).join("\n");
|
||||||
return `declare module "${relativePath}" {
|
return `type Table = {
|
||||||
export interface ${interfaceName} {
|
|
||||||
${properties}
|
${properties}
|
||||||
}
|
}[];
|
||||||
|
|
||||||
export type RowType = ${interfaceName};
|
declare const data: Table;
|
||||||
|
|
||||||
const data: ${interfaceName}[];
|
|
||||||
export default data;
|
export default data;
|
||||||
}
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
function csvLoader(content) {
|
function csvLoader(content) {
|
||||||
|
|
@ -440,9 +435,9 @@ function csvLoader(content) {
|
||||||
relativePath = relativePath.substring(1);
|
relativePath = relativePath.substring(1);
|
||||||
}
|
}
|
||||||
relativePath = relativePath.replace(/\\/g, "/");
|
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 outputPath = typesOutputDir ? path.join(typesOutputDir, dtsFileName) : dtsFileName;
|
||||||
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
|
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs);
|
||||||
if (writeToDisk) {
|
if (writeToDisk) {
|
||||||
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
|
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
|
||||||
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
||||||
|
|
|
||||||
|
|
@ -57,27 +57,18 @@ function schemaToTypeString(schema: Schema): string {
|
||||||
*/
|
*/
|
||||||
function generateTypeDefinition(
|
function generateTypeDefinition(
|
||||||
resourceName: string,
|
resourceName: string,
|
||||||
propertyConfigs: PropertyConfig[],
|
propertyConfigs: PropertyConfig[]
|
||||||
relativePath: string
|
|
||||||
): string {
|
): string {
|
||||||
const interfaceName = path.basename(resourceName, path.extname(resourceName))
|
|
||||||
.replace(/[^a-zA-Z0-9_$]/g, '_')
|
|
||||||
.replace(/^(\d)/, '_$1');
|
|
||||||
|
|
||||||
const properties = propertyConfigs
|
const properties = propertyConfigs
|
||||||
.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`)
|
.map((config) => ` ${config.name}: ${schemaToTypeString(config.schema)};`)
|
||||||
.join('\n');
|
.join('\n');
|
||||||
|
|
||||||
return `declare module "${relativePath}" {
|
return `type Table = {
|
||||||
export interface ${interfaceName} {
|
|
||||||
${properties}
|
${properties}
|
||||||
}
|
}[];
|
||||||
|
|
||||||
export type RowType = ${interfaceName};
|
declare const data: Table;
|
||||||
|
|
||||||
const data: ${interfaceName}[];
|
|
||||||
export default data;
|
export default data;
|
||||||
}
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,12 +158,12 @@ export default function csvLoader(
|
||||||
}
|
}
|
||||||
relativePath = relativePath.replace(/\\/g, '/');
|
relativePath = relativePath.replace(/\\/g, '/');
|
||||||
|
|
||||||
// Replace .csv with .d.ts for the output filename
|
// Replace .csv with .csv.d.ts for the output filename
|
||||||
const dtsFileName = relativePath.replace(/\.csv$/, '.d.ts');
|
const dtsFileName = `${relativePath}.d.ts`;
|
||||||
const outputPath = typesOutputDir
|
const outputPath = typesOutputDir
|
||||||
? path.join(typesOutputDir, dtsFileName)
|
? path.join(typesOutputDir, dtsFileName)
|
||||||
: dtsFileName;
|
: dtsFileName;
|
||||||
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
|
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs);
|
||||||
|
|
||||||
if (writeToDisk) {
|
if (writeToDisk) {
|
||||||
// Write directly to disk (useful for dev server)
|
// Write directly to disk (useful for dev server)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue