rollup-plugin-fast-json

Convert .json files to ES6 modules and it is parsed quickly

Usage no npm install needed!

<script type="module">
  import rollupPluginFastJson from 'https://cdn.skypack.dev/rollup-plugin-fast-json';
</script>

README

npm

rollup-plugin-fast-json

🔥 A Rollup plugin which Converts .json files to ES6 modules
this plugin use JSON.parse() because it is much faster than original
https://github.com/GoogleChromeLabs/json-parse-benchmark

Requirements

This plugin requires an LTS Node version (v8.0.0+) and Rollup v1.20.0+.

Install

Using npm:

yarn add rollup-plugin-fast-json

Usage

Create a rollup.config.js configuration file and import the plugin:

import fastJson from "rollup-plugin-fast-json";

export default {
  input: "src/index.js",
  output: {
    dir: "output",
    format: "cjs",
  },
  plugins: [fastJson()],
};

Then call rollup either via the CLI or the API.

Input

// src/index.js
import pkg from "./package.json";
import { version } from "./package.json"; //statically
// pkg.version === version
console.log(`running version ${version}`);

const data = await import("./data.json").default; // dynamically
doFuncWithData(data);

Options

compact

Type: Boolean
Default: false

If true, instructs the plugin to ignore indent and generates the smallest code.

exclude

Type: String | Array[...String]
Default: null

A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.

include

Type: String | Array[...String]
Default: null

A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

useOriginal

Type: String | Array[...String]
Default: ['**/package.json']

Exclude optimizing some JSON file because terser cannot optimize.

A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

indent

Type: String
Default: ''

Specifies the indentation for the generated default export.

namedExports

Type: Boolean
Default: true

If true, instructs the plugin to generate a named export for every property of the JSON object.

preferConst

Type: Boolean
Default: true

If true, instructs the plugin to declare properties as variables, using either var or const. This pertains to tree-shaking.