🍣 A Rollup plugin which imports .jsonl
(JSON Lines) files as JSON arrays.
This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.
Using npm:
npm install rollup-plugin-jsonlines --save-dev
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.
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()]
});
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 }
]
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.
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;
});
}
});