juanjoDiaz / json2csv

Flexible conversion between JSON and CSV
https://juanjodiaz.github.io/json2csv/
MIT License
306 stars 32 forks source link

could you please assist me with using json2csv in "vanilla" JS #24

Closed arashdalir closed 1 year ago

arashdalir commented 1 year ago

Hi! I assume, based on the documentation, that it's possible to use json2csv as a library in a project which is not using node, i.e. contains only old-school (like myself) js. I found this older 5.0.7 library, which is well, old and non-existing in your current release list. I'm trying to use the newest version of it inside our platform with no luck. is there any way to achieve this?

arashdalir commented 1 year ago

okay, I think I can now answer my own question:

using rollup, it's possible to create a library for those who are stuck in the 2000s - like myself. here is how I achieved this. I'll probably fork this library and create a PR in the next few days...

  1. you'll obviously need rollup for node, which I have installed globally for myself, but you can either have it installed globally or locally, dealer's choice...
    npm i -g rollup
  2. install the necessary rollup packages using npm:
    npm i --save-dev @rollup/plugin-commonjs @rollup/plugin-node-resolve rollup-plugin-polyfill-node
  3. add rollup.config.js to project root. please note that the output file will be stored in ./bundle.js
    
    //rollup.config.js
    import { nodeResolve } from '@rollup/plugin-node-resolve';

import nodePolyfills from 'rollup-plugin-polyfill-node';

import commonjs from '@rollup/plugin-commonjs';

export default { input: 'packages/plainjs/src/index.js', output: { file: './bundle.js', format: 'umd', name: "json2csv" }, plugins: [ nodeResolve({ browser: true }), commonjs(), nodePolyfills() ] };



4. run `rollup -c`, and voila!
   * Alternatively, one could also add `"rollup": "rollup -c"` to `scripts` in `package.json` for automation or other purposes.
juanjoDiaz commented 1 year ago

Hi @arashdalir ,

The current version of json2csv only support esm modules and cjs. I use esbuild instead fo rollup but it should be the same.

Can you elaborate a bit on how are you using the library and what error you are getting?

arashdalir commented 1 year ago

hi! sorry for the late reply; was really busy here.

we are using json2csv in our application to export large amounts of data (100k rows) with around 50 columns. the library is integrated into an old code which has no support for cjx or esm, so the library needed to be "converted" so we could use it in our tool. additionally, you are using lodash and json/streamparser which created additional issues for us because our app is in an offline environment and I couldn't find a way to load these libraries...

to achieve the result I needed, I rolluped the library and created an old-fashioned UMD-like bundle out of it, which resolves both of my issues: it automatically fetches the needed external resources and embeds them into the bundle, and the result is a plain javascript code which we are now using in our application.

juanjoDiaz commented 1 year ago

That makes sense.

I'll close this as it is resolved. Please let me know if there is anything else that you need.