feat: add writeToDisk option for dev server support
- Add writeToDisk option to write .d.ts files directly to disk - Useful for rsbuild/rspack dev server which uses in-memory FS - Set writeToDisk: true in dev mode to see generated types Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import { parse } from 'csv-parse/sync';
|
||||
import { parseSchema, createValidator, parseValue } from '../index.js';
|
||||
import type { Schema } from '../types.js';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
|
||||
export interface CsvLoaderOptions {
|
||||
delimiter?: string;
|
||||
@@ -15,6 +16,8 @@ export interface CsvLoaderOptions {
|
||||
emitTypes?: boolean;
|
||||
/** Output directory for generated type files (relative to output path) */
|
||||
typesOutputDir?: string;
|
||||
/** Write .d.ts files to disk (useful for dev server) */
|
||||
writeToDisk?: boolean;
|
||||
}
|
||||
|
||||
interface PropertyConfig {
|
||||
@@ -91,6 +94,7 @@ export default function csvLoader(
|
||||
const trim = options?.trim ?? true;
|
||||
const emitTypes = options?.emitTypes ?? true;
|
||||
const typesOutputDir = options?.typesOutputDir ?? '';
|
||||
const writeToDisk = options?.writeToDisk ?? false;
|
||||
|
||||
const records = parse(content, {
|
||||
delimiter,
|
||||
@@ -162,15 +166,23 @@ export default function csvLoader(
|
||||
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
|
||||
const outputPath = typesOutputDir
|
||||
? path.join(typesOutputDir, dtsFileName)
|
||||
: dtsFileName;
|
||||
const dtsContent = generateTypeDefinition(this.resourcePath, propertyConfigs, `./${relativePath}`);
|
||||
|
||||
this.emitFile?.(outputPath, dtsContent);
|
||||
if (writeToDisk) {
|
||||
// Write directly to disk (useful for dev server)
|
||||
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || '', dtsFileName);
|
||||
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
||||
fs.writeFileSync(absolutePath, dtsContent);
|
||||
} else {
|
||||
// Emit to in-memory filesystem (for production build)
|
||||
this.emitFile?.(outputPath, dtsContent);
|
||||
}
|
||||
}
|
||||
|
||||
return `export default ${json};`;
|
||||
|
||||
Reference in New Issue
Block a user