ECMAScript modules (ESM) for newer browsers
The ESM distribution target the latest browsers with support for ECMAScript modules introduced by ES6 introduced by ES6.
Compared with the Node.js version, this distribution bundles polyfills to run outside of the Node.js environment.
Addtionnal information are available in the project ECMAScript modules documentation.
Usage
The files can be manually imported. They dont require any external dependencies and they are located inside the packages/csv-generate/dist/esm
folder.
When using NPM to manage and load your modules, for example within webpack, use:
import {generate} from 'csv-generate/browser/esm';
// Or
import {generate} from 'csv-generate/browser/esm/sync';
Vanilla JavaScript
A working demo is available in the project demo/browser
directory.
With Express, expose the files with:
const app = express();
app.use('/lib/generate/',
express.static(`node_modules/csv-generate/dist/esm/`));
app.listen(3000);
The HTML code looks like:
<script type="module">
import {generate} from '/lib/generate/index.js';
generate(options, (err, data) => {
console.info(data)
});
</script>
If you wish to use the sync API, use:
<script type="module">
import {generate} from '/lib/generate/sync.js';
const data = generate(options);
</script>
Webpack module bundler
This distribution is compatible with webpack version 5. It comes with the Node.js polyfills. A working demo is shared on the project repository.
In your module, import the appropriate csv-generate
module:
./generate.js
:import {generate} from 'csv-generate/browser/esm';
./generate_sync.js
:import {generate} from 'csv-generate/browser/esm/sync';
The relevant webpack configuration looks like: