|
| 1 | +# rollup-plugin-jsonlint |
| 2 | + |
| 3 | +[](https://badge.fury.io/js/rollup-plugin-jsonlint) |
| 4 | +[](https://travis-ci.com/prantlf/rollup-plugin-jsonlint) |
| 5 | +[](https://codecov.io/gh/prantlf/rollup-plugin-jsonlint) |
| 6 | +[](https://david-dm.org/prantlf/rollup-plugin-jsonlint) |
| 7 | +[](https://david-dm.org/prantlf/rollup-plugin-jsonlint#info=devDependencies) |
| 8 | +[](https://standardjs.com) |
| 9 | + |
| 10 | +A [Rollup] plugin which converts `.json` files to ES6 modules. Recognizes standard [JSON], CJSON (JSON with comments) and [JSON5] (further more flexible JSON). |
| 11 | + |
| 12 | +This plugin is based on [`@rollup/plugin-json`]. It supports all 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. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +This plugin requires [Node.js] [LTS] (currently 12, at least 8) and Rollup 1.20 or newer. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Using npm: |
| 21 | + |
| 22 | + npm i -D rollup-plugin-jsonlint |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +Create a `rollup.config.js` [configuration file] and import the plugin: |
| 27 | + |
| 28 | +```js |
| 29 | +import jsonlint from 'rollup-plugin-jsonlint' |
| 30 | + |
| 31 | +export default { |
| 32 | + input: 'src/index.js', |
| 33 | + output: { |
| 34 | + dir: 'output', |
| 35 | + format: 'cjs' |
| 36 | + }, |
| 37 | + plugins: [jsonlint({ mode: 'cjson' })] |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Then call `rollup` either via the [command-line] or the [programmatically]. |
| 42 | + |
| 43 | +With an accompanying file `src/index.js`, the local `package.json` file would now be importable as seen below: |
| 44 | + |
| 45 | +```js |
| 46 | +import pkg from './package.json'; |
| 47 | +console.log(`Running version ${pkg.version}.`); |
| 48 | +``` |
| 49 | + |
| 50 | +## Input Parsing Options |
| 51 | + |
| 52 | +The following options customize the parser. If no default value is changed, only the standard JSON will be accepted. |
| 53 | + |
| 54 | +### `mode` |
| 55 | + |
| 56 | +Type: `String` (`json`|`cjson`|`json5`)<br> |
| 57 | +Default: `json` |
| 58 | + |
| 59 | +A shortcut for setting the three boolean flags below. |
| 60 | + |
| 61 | +* `json` - expects the standard JSON (uses defaults) |
| 62 | +* `cjson` - expects CJSON (sets `ignoreComments` to `true`) |
| 63 | +* `json5` - expects JSON5 (sets `ignoreComments`, `ignoreTrailingCommas` and `allowSingleQuotedStrings` to `true`) |
| 64 | + |
| 65 | +### `ignoreComments` |
| 66 | + |
| 67 | +Type: `Boolean`<br> |
| 68 | +Default: `false` |
| 69 | + |
| 70 | +If `true`, single-line and multi-line JavaScript-style comments will be ignored as another "whitespace". (CJSON and JSON5 feature) |
| 71 | + |
| 72 | +### `ignoreTrailingCommas` |
| 73 | + |
| 74 | +Type: `Boolean`<br> |
| 75 | +Default: `false` |
| 76 | + |
| 77 | +If `true`, trailing commas after the last object entry and array items will be ignored without throwing an error. (JSON5 feature) |
| 78 | + |
| 79 | +### `allowSingleQuotedStrings` |
| 80 | + |
| 81 | +Type: `Boolean`<br> |
| 82 | +Default: `false` |
| 83 | + |
| 84 | +If `true`, single quotes will be accepted in addition to double quotes as characters for enclosing string literals within. (JSON5 feature) |
| 85 | + |
| 86 | +### `allowDuplicateObjectKeys` |
| 87 | + |
| 88 | +Type: `Boolean`<br> |
| 89 | +Default: `true` |
| 90 | + |
| 91 | +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. |
| 92 | + |
| 93 | +## Module Generation Options |
| 94 | + |
| 95 | +The following options customize the ES6 module generator. |
| 96 | + |
| 97 | +### `compact` |
| 98 | + |
| 99 | +Type: `Boolean`<br> |
| 100 | +Default: `false` |
| 101 | + |
| 102 | +If `true`, instructs the plugin to ignore `indent` and generate the smallest code. |
| 103 | + |
| 104 | +### `exclude` |
| 105 | + |
| 106 | +Type: `String` | `Array[...String]`<br> |
| 107 | +Default: `null` |
| 108 | + |
| 109 | +A [minimatch pattern], or array of patterns, which specifies the files in the build the plugin should *ignore*. By default no files are ignored. |
| 110 | + |
| 111 | +### `include` |
| 112 | + |
| 113 | +Type: `String` | `Array[...String]`<br> |
| 114 | +Default: `null` |
| 115 | + |
| 116 | +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. |
| 117 | + |
| 118 | +### `indent` |
| 119 | + |
| 120 | +Type: `String`<br> |
| 121 | +Default: `'\t'` |
| 122 | + |
| 123 | +Specifies the indentation for the generated default export. |
| 124 | + |
| 125 | +### `namedExports` |
| 126 | + |
| 127 | +Type: `Boolean`<br> |
| 128 | +Default: `true` |
| 129 | + |
| 130 | +If `true`, instructs the plugin to generate a named export for every property of the JSON object. |
| 131 | + |
| 132 | +### `preferConst` |
| 133 | + |
| 134 | +Type: `Boolean`<br> |
| 135 | +Default: `false` |
| 136 | + |
| 137 | +If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking. |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +Copyright (C) 2019 Ferdinand Prantl |
| 142 | + |
| 143 | +Licensed under the [MIT License]. |
| 144 | + |
| 145 | +[MIT License]: http://en.wikipedia.org/wiki/MIT_License |
| 146 | +[Rollup]: https://rollupjs.org/ |
| 147 | +[JSON]: https://tools.ietf.org/html/rfc8259 |
| 148 | +[JSON5]: https://spec.json5.org |
| 149 | +[`@prantlf/jsonlint`]: https://github.com/prantlf/jsonlint#readme |
| 150 | +[`@rollup/plugin-json`]: https://github.com/rollup/plugins/tree/master/packages/json#rollupplugin-json |
| 151 | +[Node.js]: https://nodejs.org/ |
| 152 | +[LTS]: https://github.com/nodejs/Release |
| 153 | +[configuration file]: https://www.rollupjs.org/guide/en/#configuration-files |
| 154 | +[command-line]: https://www.rollupjs.org/guide/en/#command-line-reference |
| 155 | +[programmatically]: https://www.rollupjs.org/guide/en/#javascript-api |
| 156 | +[minimatch pattern]: https://github.com/isaacs/minimatch |
0 commit comments