2026-03-31 14:55:46 +08:00
|
|
|
import { LoaderContext } from '@rspack/core';
|
|
|
|
|
|
|
|
|
|
interface CsvLoaderOptions {
|
|
|
|
|
delimiter?: string;
|
|
|
|
|
quote?: string;
|
|
|
|
|
escape?: string;
|
|
|
|
|
bom?: boolean;
|
|
|
|
|
comment?: string | false;
|
|
|
|
|
trim?: boolean;
|
2026-03-31 15:49:05 +08:00
|
|
|
/** Generate TypeScript declaration file (.d.ts) */
|
|
|
|
|
emitTypes?: boolean;
|
|
|
|
|
/** Output directory for generated type files (relative to output path) */
|
|
|
|
|
typesOutputDir?: string;
|
2026-03-31 15:54:38 +08:00
|
|
|
/** Write .d.ts files to disk (useful for dev server) */
|
|
|
|
|
writeToDisk?: boolean;
|
2026-03-31 14:55:46 +08:00
|
|
|
}
|
2026-04-02 17:34:11 +08:00
|
|
|
interface CsvParseResult {
|
|
|
|
|
/** Parsed CSV data as array of objects */
|
|
|
|
|
data: Record<string, unknown>[];
|
|
|
|
|
/** Generated TypeScript type definition string (if emitTypes is true) */
|
|
|
|
|
typeDefinition?: string;
|
|
|
|
|
/** Property configurations for the CSV columns */
|
|
|
|
|
propertyConfigs: PropertyConfig[];
|
|
|
|
|
}
|
|
|
|
|
interface PropertyConfig {
|
|
|
|
|
name: string;
|
|
|
|
|
schema: any;
|
|
|
|
|
validator: (value: unknown) => boolean;
|
|
|
|
|
parser: (valueString: string) => unknown;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Parse CSV content string into structured data with schema validation.
|
|
|
|
|
* This is a standalone function that doesn't depend on webpack/rspack LoaderContext.
|
|
|
|
|
*
|
|
|
|
|
* @param content - CSV content string (must have at least headers + schema row + 1 data row)
|
|
|
|
|
* @param options - Parsing options
|
|
|
|
|
* @returns CsvParseResult containing parsed data and optional type definitions
|
|
|
|
|
*/
|
|
|
|
|
declare function parseCsv(content: string, options?: CsvLoaderOptions): CsvParseResult;
|
|
|
|
|
/**
|
|
|
|
|
* Generate JavaScript module code from CSV content.
|
|
|
|
|
* Returns a string that can be used as a module export.
|
|
|
|
|
*
|
|
|
|
|
* @param content - CSV content string
|
|
|
|
|
* @param options - Parsing options
|
|
|
|
|
* @returns JavaScript module code string
|
|
|
|
|
*/
|
|
|
|
|
declare function csvToModule(content: string, options?: CsvLoaderOptions): {
|
|
|
|
|
js: string;
|
|
|
|
|
dts?: string;
|
|
|
|
|
};
|
2026-03-31 14:55:46 +08:00
|
|
|
declare function csvLoader(this: LoaderContext<CsvLoaderOptions>, content: string): string | Buffer;
|
|
|
|
|
|
2026-04-02 17:34:11 +08:00
|
|
|
export { type CsvLoaderOptions, type CsvParseResult, csvToModule, csvLoader as default, parseCsv };
|