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:
Vendored
+2
@@ -11,6 +11,8 @@ 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;
|
||||
}
|
||||
declare function csvLoader(this: LoaderContext<CsvLoaderOptions>, content: string): string | Buffer;
|
||||
|
||||
|
||||
Vendored
+2
@@ -11,6 +11,8 @@ 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;
|
||||
}
|
||||
declare function csvLoader(this: LoaderContext<CsvLoaderOptions>, content: string): string | Buffer;
|
||||
|
||||
|
||||
Vendored
+9
-1
@@ -365,6 +365,7 @@ function createValidator(schema) {
|
||||
|
||||
// src/csv-loader/loader.ts
|
||||
var path = __toESM(require("path"));
|
||||
var fs = __toESM(require("fs"));
|
||||
function schemaToTypeString(schema) {
|
||||
switch (schema.type) {
|
||||
case "string":
|
||||
@@ -411,6 +412,7 @@ function csvLoader(content) {
|
||||
const trim = options?.trim ?? true;
|
||||
const emitTypes = options?.emitTypes ?? true;
|
||||
const typesOutputDir = options?.typesOutputDir ?? "";
|
||||
const writeToDisk = options?.writeToDisk ?? false;
|
||||
const records = (0, import_sync.parse)(content, {
|
||||
delimiter,
|
||||
quote,
|
||||
@@ -475,7 +477,13 @@ function csvLoader(content) {
|
||||
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);
|
||||
if (writeToDisk) {
|
||||
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
|
||||
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
||||
fs.writeFileSync(absolutePath, dtsContent);
|
||||
} else {
|
||||
this.emitFile?.(outputPath, dtsContent);
|
||||
}
|
||||
}
|
||||
return `export default ${json};`;
|
||||
}
|
||||
|
||||
Vendored
+9
-1
@@ -331,6 +331,7 @@ function createValidator(schema) {
|
||||
|
||||
// src/csv-loader/loader.ts
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
function schemaToTypeString(schema) {
|
||||
switch (schema.type) {
|
||||
case "string":
|
||||
@@ -377,6 +378,7 @@ function csvLoader(content) {
|
||||
const trim = options?.trim ?? true;
|
||||
const emitTypes = options?.emitTypes ?? true;
|
||||
const typesOutputDir = options?.typesOutputDir ?? "";
|
||||
const writeToDisk = options?.writeToDisk ?? false;
|
||||
const records = parse(content, {
|
||||
delimiter,
|
||||
quote,
|
||||
@@ -441,7 +443,13 @@ function csvLoader(content) {
|
||||
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);
|
||||
if (writeToDisk) {
|
||||
const absolutePath = path.join(this.context || process.cwd(), typesOutputDir || "", dtsFileName);
|
||||
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
||||
fs.writeFileSync(absolutePath, dtsContent);
|
||||
} else {
|
||||
this.emitFile?.(outputPath, dtsContent);
|
||||
}
|
||||
}
|
||||
return `export default ${json};`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user