A Rollup plugin which converts .json
files to ES6 modules. Recognizes standard JSON, CJSON (JSON with comments) and JSON5 (further more flexible JSON).
This plugin started as an overwrite of @rollup/plugin-json
. It supports all the original options. It leverages the parser from @prantlf/jsonlint
to provide better error information in case or invalid input and to parse JSON extensions like CJSON and JSON5. If no extension is enabled, the standard JSON.parse
will be used for the best performance.
This plugin requires Node.js LTS (currently 18, at least 14.8) and Rollup 1.20 or newer.
Using npm:
npm i -D rollup-plugin-jsonlint
Create a rollup.config.js
configuration file and import the plugin:
import jsonlint from 'rollup-plugin-jsonlint'
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [jsonlint({ mode: 'cjson' })]
}
Then call rollup
either via the command-line or the programmatically.
With an accompanying file src/index.js
, the local package.json
file would now be importable as seen below:
import pkg from './package.json';
console.log(`Running version ${pkg.version}.`);
The following options customize the parser. If no default value is changed, only the standard JSON will be accepted.
Type: String
(json
|cjson
|json5
)
Default: json
A shortcut for setting the three boolean flags below.
json
- expects the standard JSON (uses defaults)cjson
- expects CJSON (setsignoreComments
totrue
)json5
- expects JSON5 (setsignoreComments
,ignoreTrailingCommas
andallowSingleQuotedStrings
totrue
)
Type: Boolean
Default: false
If true
, the leading UTF-8 byte-order mark will be ignored.
Type: Boolean
Default: false
If true
, single-line and multi-line JavaScript-style comments will be ignored as another "whitespace". (CJSON and JSON5 feature)
Type: Boolean
Default: false
If true
, trailing commas after the last object entry and array items will be ignored without throwing an error. (JSON5 feature)
Type: Boolean
Default: false
If true
, single quotes will be accepted in addition to double quotes as characters for enclosing string literals within. (JSON5 feature)
Type: Boolean
Default: true
If false
, duplicate object keys will be reported as an error. Useful for detecting mistakes in configuration files, where duplicate keys usually mean old properties forgotten to be deleted.
The following options customize the ES6 module generator.
Type: Boolean
Default: false
If true
, instructs the plugin to ignore indent
and generate the smallest code.
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.
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.
Type: String
Default: '\t'
Specifies the indentation for the generated default export.
Type: Boolean
Default: true
If true
, instructs the plugin to generate a named export for every property of the JSON object.
Type: Boolean
Default: false
If true
, instructs the plugin to declare properties as variables, using either var
or const
. This pertains to tree-shaking.
Copyright (C) 2019-2024 Ferdinand Prantl
Licensed under the MIT License.