This document explains how web-csv-toolbox uses Node.js package exports to provide environment-specific implementations and optimized resource loading.
This library provides multiple import paths for different use cases. The appropriate implementation is automatically selected based on your environment (Node.js, Browser, Deno, etc.) - you don't need to worry about platform differences.
What you need to know:
web-csv-toolbox): Full features with automatic WASM initialization
import { parseStringToArraySyncWASM } from 'web-csv-toolbox'; // Auto-initializes on first use; optionally call loadWASM() at startup to reduce first‑parse latency const records = parseStringToArraySyncWASM(csv);
2. **Slim library** (`web-csv-toolbox/slim`): Smaller bundle size, manual WASM initialization required
```typescript
import { loadWASM, parseStringToArraySyncWASM } from 'web-csv-toolbox/slim';
await loadWASM(); // Must call before using WASM functions
const records = parseStringToArraySyncWASM(csv);
Worker file (web-csv-toolbox/worker): Only needed when using bundlers with worker-based parsing
// Vite
import workerUrl from 'web-csv-toolbox/worker?url';
// Webpack
const workerUrl = new URL('web-csv-toolbox/worker', import.meta.url);
WASM module (web-csv-toolbox/csv.wasm): For advanced deployment scenarios
loadWASM() to reduce first‑parse latencyThe library handles environment detection and optimizations automatically using Conditional Exports.
| Import Path | Purpose | Bundle Size | Typical Usage |
|---|---|---|---|
web-csv-toolbox |
Full features (auto-initialization) | Larger (WASM embedded in JS) | ✅ Default choice |
web-csv-toolbox/slim |
Smaller bundle (manual initialization) | Smaller (WASM as separate file) | ✅ Bundle size optimization |
web-csv-toolbox/worker |
Worker implementation | - | ✅ Via bundler |
web-csv-toolbox/csv.wasm |
WebAssembly module | WASM binary only | ⚠️ Advanced scenarios |
Note: Bundle sizes vary depending on the bundler, tree-shaking, and build configuration. The slim entry reduces the main JavaScript bundle size by not embedding the WASM binary.
Feature parity: Both Main and Slim export the same public API. Choose based on WASM initialization strategy and bundle packaging, not features.
The library provides two entry points with different trade-offs:
web-csv-toolbox)Best for: Most users who want automatic WASM initialization and convenience
import { parseStringToArraySyncWASM } from 'web-csv-toolbox';
// Auto-initialization occurs on first use; optional preloading recommended for faster first parse
const records = parseStringToArraySyncWASM(csv);
Characteristics:
loadWASM() at startup to reduce first‑parse latencyWhen to use:
web-csv-toolbox/slim)Best for: Bundle size-sensitive applications
import { loadWASM, parseStringToArraySyncWASM } from 'web-csv-toolbox/slim';
// Manual initialization required
await loadWASM();
const records = parseStringToArraySyncWASM(csv);
Characteristics:
loadWASM() callWhen to use:
Bundle Size Comparison:
| Entry Point | Main Bundle | WASM Binary | Total (Approx.) |
|---|---|---|---|
web-csv-toolbox |
Larger (WASM embedded) | - | Larger |
web-csv-toolbox/slim |
Smaller | Separate file | Smaller overall |
Note: Actual bundle sizes depend on bundler configuration, tree-shaking, and compression. The slim entry reduces the main JavaScript bundle size by not embedding the WASM binary (which is instead loaded as a separate asset).
Both entry points export the same public API:
import { parseString } from 'web-csv-toolbox';
// or
import { parseString } from 'web-csv-toolbox/slim';
The main exports resolve to ./dist/main.* (main) or ./dist/slim.* (slim) respectively.
web-csv-toolbox/worker)The worker entry point provides environment-specific implementations:
import workerUrl from 'web-csv-toolbox/worker?url'; // Vite
// or
const workerUrl = new URL('web-csv-toolbox/worker', import.meta.url); // Webpack
Resolution logic:
Node.js environment ("node" condition):
./dist/worker.node.jsworker_threads moduleparentPortBrowser environment ("browser" condition):
./dist/worker.web.jsself.addEventListener('message', ...)Default fallback:
worker.web.js)web-csv-toolbox/csv.wasm)What is it?
This exports the pre-compiled WebAssembly module used for high-performance CSV parsing.
Do you need to use this directly?
No, in most cases. The library automatically loads the WASM module when you use WASM-enabled features.
import { parse, loadWASM } from 'web-csv-toolbox';
// WASM module is automatically loaded
await loadWASM();
// Just use the API - WASM file is handled internally
for await (const record of parse(csv, { engine: { wasm: true } })) {
console.log(record);
}
Current limitations and future improvements:
⚠️ Currently, this export has limited practical use. The WASM module is embedded as base64 in the JavaScript bundle for automatic initialization, so importing csv.wasm separately does not reduce bundle size.
Potential use cases (when combined with future distribution improvements):
Future considerations:
We are considering improvements to the distribution method to enable:
These improvements would make the csv.wasm export more useful for bundle size optimization. For transparency, we acknowledge that the current architecture does not fully support these scenarios, but we are exploring options for future releases.
Key points:
Key characteristics:
node:worker_threads moduleparentPort.on("message", ...)Key characteristics:
self as worker contextself.addEventListener("message", ...)Both implementations share the same parsing logic and behavior, ensuring consistent results across environments.
Modern bundlers understand package exports and handle them correctly:
// Automatically resolves based on build target
import workerUrl from 'web-csv-toolbox/worker?url';
Vite's ?url suffix tells the bundler to:
// Automatically resolves based on target
const workerUrl = new URL('web-csv-toolbox/worker', import.meta.url);
Webpack's new URL() + import.meta.url syntax:
Requires @rollup/plugin-url or similar plugin to process Worker imports.