Compare commits
2 Commits
cf55295ce7
...
16d88d6108
| Author | SHA1 | Date |
|---|---|---|
|
|
16d88d6108 | |
|
|
b7e65ebc1d |
|
|
@ -14,6 +14,41 @@ interface CsvLoaderOptions {
|
||||||
/** Write .d.ts files to disk (useful for dev server) */
|
/** Write .d.ts files to disk (useful for dev server) */
|
||||||
writeToDisk?: boolean;
|
writeToDisk?: boolean;
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
};
|
||||||
declare function csvLoader(this: LoaderContext<CsvLoaderOptions>, content: string): string | Buffer;
|
declare function csvLoader(this: LoaderContext<CsvLoaderOptions>, content: string): string | Buffer;
|
||||||
|
|
||||||
export { type CsvLoaderOptions, csvLoader as default };
|
export { type CsvLoaderOptions, type CsvParseResult, csvToModule, csvLoader as default, parseCsv };
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,41 @@ interface CsvLoaderOptions {
|
||||||
/** Write .d.ts files to disk (useful for dev server) */
|
/** Write .d.ts files to disk (useful for dev server) */
|
||||||
writeToDisk?: boolean;
|
writeToDisk?: boolean;
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
};
|
||||||
declare function csvLoader(this: LoaderContext<CsvLoaderOptions>, content: string): string | Buffer;
|
declare function csvLoader(this: LoaderContext<CsvLoaderOptions>, content: string): string | Buffer;
|
||||||
|
|
||||||
export { type CsvLoaderOptions, csvLoader as default };
|
export { type CsvLoaderOptions, type CsvParseResult, csvToModule, csvLoader as default, parseCsv };
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
||||||
// src/csv-loader/loader.ts
|
// src/csv-loader/loader.ts
|
||||||
var loader_exports = {};
|
var loader_exports = {};
|
||||||
__export(loader_exports, {
|
__export(loader_exports, {
|
||||||
default: () => csvLoader
|
csvToModule: () => csvToModule,
|
||||||
|
default: () => csvLoader,
|
||||||
|
parseCsv: () => parseCsv
|
||||||
});
|
});
|
||||||
module.exports = __toCommonJS(loader_exports);
|
module.exports = __toCommonJS(loader_exports);
|
||||||
var import_sync = require("csv-parse/sync");
|
var import_sync = require("csv-parse/sync");
|
||||||
|
|
@ -435,17 +437,14 @@ declare const data: Table;
|
||||||
export default data;
|
export default data;
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
function csvLoader(content) {
|
function parseCsv(content, options = {}) {
|
||||||
const options = this.getOptions();
|
const delimiter = options.delimiter ?? ",";
|
||||||
const delimiter = options?.delimiter ?? ",";
|
const quote = options.quote ?? '"';
|
||||||
const quote = options?.quote ?? '"';
|
const escape = options.escape ?? "\\";
|
||||||
const escape = options?.escape ?? "\\";
|
const bom = options.bom ?? true;
|
||||||
const bom = options?.bom ?? true;
|
const comment = options.comment === false ? void 0 : options.comment ?? "#";
|
||||||
const comment = options?.comment === false ? void 0 : options?.comment ?? "#";
|
const trim = options.trim ?? true;
|
||||||
const trim = options?.trim ?? true;
|
const emitTypes = options.emitTypes ?? true;
|
||||||
const emitTypes = options?.emitTypes ?? true;
|
|
||||||
const typesOutputDir = options?.typesOutputDir ?? "";
|
|
||||||
const writeToDisk = options?.writeToDisk ?? false;
|
|
||||||
const records = (0, import_sync.parse)(content, {
|
const records = (0, import_sync.parse)(content, {
|
||||||
delimiter,
|
delimiter,
|
||||||
quote,
|
quote,
|
||||||
|
|
@ -499,8 +498,31 @@ function csvLoader(content) {
|
||||||
});
|
});
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
const json = JSON.stringify(objects, null, 2);
|
const result = {
|
||||||
|
data: objects,
|
||||||
|
propertyConfigs
|
||||||
|
};
|
||||||
if (emitTypes) {
|
if (emitTypes) {
|
||||||
|
result.typeDefinition = generateTypeDefinition("", propertyConfigs);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
function csvToModule(content, options = {}) {
|
||||||
|
const result = parseCsv(content, options);
|
||||||
|
const json = JSON.stringify(result.data, null, 2);
|
||||||
|
const js = `export default ${json};`;
|
||||||
|
return {
|
||||||
|
js,
|
||||||
|
dts: result.typeDefinition
|
||||||
|
};
|
||||||
|
}
|
||||||
|
function csvLoader(content) {
|
||||||
|
const options = this.getOptions();
|
||||||
|
const emitTypes = options?.emitTypes ?? true;
|
||||||
|
const typesOutputDir = options?.typesOutputDir ?? "";
|
||||||
|
const writeToDisk = options?.writeToDisk ?? false;
|
||||||
|
const result = parseCsv(content, options);
|
||||||
|
if (emitTypes && result.typeDefinition) {
|
||||||
const context = this.context || "";
|
const context = this.context || "";
|
||||||
let relativePath = this.resourcePath.replace(context, "");
|
let relativePath = this.resourcePath.replace(context, "");
|
||||||
if (relativePath.startsWith("\\") || relativePath.startsWith("/")) {
|
if (relativePath.startsWith("\\") || relativePath.startsWith("/")) {
|
||||||
|
|
@ -509,14 +531,18 @@ function csvLoader(content) {
|
||||||
relativePath = relativePath.replace(/\\/g, "/");
|
relativePath = relativePath.replace(/\\/g, "/");
|
||||||
const dtsFileName = `${relativePath}.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);
|
|
||||||
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 });
|
||||||
fs.writeFileSync(absolutePath, dtsContent);
|
fs.writeFileSync(absolutePath, result.typeDefinition);
|
||||||
} else {
|
} else {
|
||||||
this.emitFile?.(outputPath, dtsContent);
|
this.emitFile?.(outputPath, result.typeDefinition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return `export default ${json};`;
|
return `export default ${JSON.stringify(result.data, null, 2)};`;
|
||||||
}
|
}
|
||||||
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
|
0 && (module.exports = {
|
||||||
|
csvToModule,
|
||||||
|
parseCsv
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -401,17 +401,14 @@ declare const data: Table;
|
||||||
export default data;
|
export default data;
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
function csvLoader(content) {
|
function parseCsv(content, options = {}) {
|
||||||
const options = this.getOptions();
|
const delimiter = options.delimiter ?? ",";
|
||||||
const delimiter = options?.delimiter ?? ",";
|
const quote = options.quote ?? '"';
|
||||||
const quote = options?.quote ?? '"';
|
const escape = options.escape ?? "\\";
|
||||||
const escape = options?.escape ?? "\\";
|
const bom = options.bom ?? true;
|
||||||
const bom = options?.bom ?? true;
|
const comment = options.comment === false ? void 0 : options.comment ?? "#";
|
||||||
const comment = options?.comment === false ? void 0 : options?.comment ?? "#";
|
const trim = options.trim ?? true;
|
||||||
const trim = options?.trim ?? true;
|
const emitTypes = options.emitTypes ?? true;
|
||||||
const emitTypes = options?.emitTypes ?? true;
|
|
||||||
const typesOutputDir = options?.typesOutputDir ?? "";
|
|
||||||
const writeToDisk = options?.writeToDisk ?? false;
|
|
||||||
const records = parse(content, {
|
const records = parse(content, {
|
||||||
delimiter,
|
delimiter,
|
||||||
quote,
|
quote,
|
||||||
|
|
@ -465,8 +462,31 @@ function csvLoader(content) {
|
||||||
});
|
});
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
const json = JSON.stringify(objects, null, 2);
|
const result = {
|
||||||
|
data: objects,
|
||||||
|
propertyConfigs
|
||||||
|
};
|
||||||
if (emitTypes) {
|
if (emitTypes) {
|
||||||
|
result.typeDefinition = generateTypeDefinition("", propertyConfigs);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
function csvToModule(content, options = {}) {
|
||||||
|
const result = parseCsv(content, options);
|
||||||
|
const json = JSON.stringify(result.data, null, 2);
|
||||||
|
const js = `export default ${json};`;
|
||||||
|
return {
|
||||||
|
js,
|
||||||
|
dts: result.typeDefinition
|
||||||
|
};
|
||||||
|
}
|
||||||
|
function csvLoader(content) {
|
||||||
|
const options = this.getOptions();
|
||||||
|
const emitTypes = options?.emitTypes ?? true;
|
||||||
|
const typesOutputDir = options?.typesOutputDir ?? "";
|
||||||
|
const writeToDisk = options?.writeToDisk ?? false;
|
||||||
|
const result = parseCsv(content, options);
|
||||||
|
if (emitTypes && result.typeDefinition) {
|
||||||
const context = this.context || "";
|
const context = this.context || "";
|
||||||
let relativePath = this.resourcePath.replace(context, "");
|
let relativePath = this.resourcePath.replace(context, "");
|
||||||
if (relativePath.startsWith("\\") || relativePath.startsWith("/")) {
|
if (relativePath.startsWith("\\") || relativePath.startsWith("/")) {
|
||||||
|
|
@ -475,17 +495,18 @@ function csvLoader(content) {
|
||||||
relativePath = relativePath.replace(/\\/g, "/");
|
relativePath = relativePath.replace(/\\/g, "/");
|
||||||
const dtsFileName = `${relativePath}.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);
|
|
||||||
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 });
|
||||||
fs.writeFileSync(absolutePath, dtsContent);
|
fs.writeFileSync(absolutePath, result.typeDefinition);
|
||||||
} else {
|
} else {
|
||||||
this.emitFile?.(outputPath, dtsContent);
|
this.emitFile?.(outputPath, result.typeDefinition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return `export default ${json};`;
|
return `export default ${JSON.stringify(result.data, null, 2)};`;
|
||||||
}
|
}
|
||||||
export {
|
export {
|
||||||
csvLoader as default
|
csvToModule,
|
||||||
|
csvLoader as default,
|
||||||
|
parseCsv
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,15 @@ export interface CsvLoaderOptions {
|
||||||
writeToDisk?: boolean;
|
writeToDisk?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export 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 {
|
interface PropertyConfig {
|
||||||
name: string;
|
name: string;
|
||||||
schema: any;
|
schema: any;
|
||||||
|
|
@ -78,20 +87,25 @@ export default data;
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function csvLoader(
|
/**
|
||||||
this: LoaderContext<CsvLoaderOptions>,
|
* Parse CSV content string into structured data with schema validation.
|
||||||
content: string
|
* This is a standalone function that doesn't depend on webpack/rspack LoaderContext.
|
||||||
): string | Buffer {
|
*
|
||||||
const options = this.getOptions() as CsvLoaderOptions | undefined;
|
* @param content - CSV content string (must have at least headers + schema row + 1 data row)
|
||||||
const delimiter = options?.delimiter ?? ',';
|
* @param options - Parsing options
|
||||||
const quote = options?.quote ?? '"';
|
* @returns CsvParseResult containing parsed data and optional type definitions
|
||||||
const escape = options?.escape ?? '\\';
|
*/
|
||||||
const bom = options?.bom ?? true;
|
export function parseCsv(
|
||||||
const comment = options?.comment === false ? undefined : (options?.comment ?? '#');
|
content: string,
|
||||||
const trim = options?.trim ?? true;
|
options: CsvLoaderOptions = {}
|
||||||
const emitTypes = options?.emitTypes ?? true;
|
): CsvParseResult {
|
||||||
const typesOutputDir = options?.typesOutputDir ?? '';
|
const delimiter = options.delimiter ?? ',';
|
||||||
const writeToDisk = options?.writeToDisk ?? false;
|
const quote = options.quote ?? '"';
|
||||||
|
const escape = options.escape ?? '\\';
|
||||||
|
const bom = options.bom ?? true;
|
||||||
|
const comment = options.comment === false ? undefined : (options.comment ?? '#');
|
||||||
|
const trim = options.trim ?? true;
|
||||||
|
const emitTypes = options.emitTypes ?? true;
|
||||||
|
|
||||||
const records = parse(content, {
|
const records = parse(content, {
|
||||||
delimiter,
|
delimiter,
|
||||||
|
|
@ -152,10 +166,54 @@ export default function csvLoader(
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
|
|
||||||
const json = JSON.stringify(objects, null, 2);
|
const result: CsvParseResult = {
|
||||||
|
data: objects,
|
||||||
|
propertyConfigs,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (emitTypes) {
|
||||||
|
result.typeDefinition = generateTypeDefinition('', propertyConfigs);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
export function csvToModule(
|
||||||
|
content: string,
|
||||||
|
options: CsvLoaderOptions = {}
|
||||||
|
): { js: string; dts?: string } {
|
||||||
|
const result = parseCsv(content, options);
|
||||||
|
|
||||||
|
const json = JSON.stringify(result.data, null, 2);
|
||||||
|
const js = `export default ${json};`;
|
||||||
|
|
||||||
|
return {
|
||||||
|
js,
|
||||||
|
dts: result.typeDefinition,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function csvLoader(
|
||||||
|
this: LoaderContext<CsvLoaderOptions>,
|
||||||
|
content: string
|
||||||
|
): string | Buffer {
|
||||||
|
const options = this.getOptions() as CsvLoaderOptions | undefined;
|
||||||
|
const emitTypes = options?.emitTypes ?? true;
|
||||||
|
const typesOutputDir = options?.typesOutputDir ?? '';
|
||||||
|
const writeToDisk = options?.writeToDisk ?? false;
|
||||||
|
|
||||||
|
const result = parseCsv(content, options);
|
||||||
|
|
||||||
// Emit type definition file if enabled
|
// Emit type definition file if enabled
|
||||||
if (emitTypes) {
|
if (emitTypes && result.typeDefinition) {
|
||||||
const context = this.context || '';
|
const context = this.context || '';
|
||||||
// Get relative path from context, normalize to forward slashes
|
// Get relative path from context, normalize to forward slashes
|
||||||
let relativePath = this.resourcePath.replace(context, '');
|
let relativePath = this.resourcePath.replace(context, '');
|
||||||
|
|
@ -169,18 +227,17 @@ export default function csvLoader(
|
||||||
const outputPath = typesOutputDir
|
const outputPath = typesOutputDir
|
||||||
? path.join(typesOutputDir, dtsFileName)
|
? path.join(typesOutputDir, dtsFileName)
|
||||||
: dtsFileName;
|
: dtsFileName;
|
||||||
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)
|
||||||
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 });
|
||||||
fs.writeFileSync(absolutePath, dtsContent);
|
fs.writeFileSync(absolutePath, result.typeDefinition);
|
||||||
} else {
|
} else {
|
||||||
// Emit to in-memory filesystem (for production build)
|
// Emit to in-memory filesystem (for production build)
|
||||||
this.emitFile?.(outputPath, dtsContent);
|
this.emitFile?.(outputPath, result.typeDefinition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return `export default ${json};`;
|
return `export default ${JSON.stringify(result.data, null, 2)};`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,9 @@
|
||||||
"declarationMap": true,
|
"declarationMap": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"rootDir": "./src"
|
"rootDir": "./src",
|
||||||
|
"types": ["node"]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*"],
|
"include": ["src/**/*"],
|
||||||
"exclude": ["node_modules", "dist", "src/csv-loader"]
|
"exclude": ["node_modules", "dist"]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue