rollup-plugin-jsonlines
v0.0.1
Published
Import jsonl (JSON lines) files as JSON array
Downloads
4
Readme
rollup-plugin-jsonlines
🍣 A Rollup plugin which imports .jsonl
(JSON Lines) files as JSON arrays.
Requirements
This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.
Install
Using npm:
npm install rollup-plugin-jsonlines --save-dev
Usage
Create a rollup.config.js configuration file and import the plugin:
import jsonl from 'rollup-plugin-jsonlines';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [jsonl()]
};
Then call rollup either via the CLI or the API.
Usage with vite
vite.config.js
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import jsonl from 'rollup-plugin-jsonlines';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte(), jsonl()]
});
Example
fruits.jsonl
{ "type": "apples", "count": 7 }
{ "type": "pears", "count": 4 }
{ "type": "bananas", "count": 5 }
index.js
import fruit from './fruits.jsonl';
console.log(fruit);
log
[
{ "type": "apples", "count": 7 },
{ "type": "pears", "count": 4 },
{ "type": "bananas", "count": 5 }
]
Options
ignoreErrors
Type: Boolean
Default: false
If set to false (default), an exception will be thrown in case of invalid JSON on a line.
If set to true, invalid lines will be omitted.
processRow
Type: Function
Default: null
Specifies a function which processes each row in the parsed array. The function can either manipulate the passed row, or return an entirely new row object.
This option could be used for converting numeric string values into Number values. – for example turning numeric values into numbers, e.g.
jsonl({
processRow: (row, id) => {
Object.keys(row).forEach((key) => {
var value = row[key];
row[key] = isNaN(+value) ? value : +value;
});
}
});