|
| 1 | +[npm]: https://img.shields.io/npm/v/@rollup/plugin-json |
| 2 | +[npm-url]: https://www.npmjs.com/package/@rollup/plugin-json |
| 3 | +[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-json |
| 4 | +[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-json |
| 5 | + |
| 6 | +[![npm][npm]][npm-url] |
| 7 | +[![size][size]][size-url] |
| 8 | +[](https://liberamanifesto.com) |
| 9 | + |
| 10 | +# @rollup/plugin-json |
| 11 | + |
| 12 | +🍣 A Rollup which Converts .json files to ES6 modules. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+. |
| 17 | + |
| 18 | +## Install |
| 19 | + |
| 20 | +Using npm: |
| 21 | + |
| 22 | +```console |
| 23 | +npm install @rollup/plugin-json --save-dev |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin: |
| 29 | + |
| 30 | +```js |
| 31 | +import json from '@rollup/plugin-json'; |
| 32 | + |
| 33 | +export default { |
| 34 | + input: 'src/index.js', |
| 35 | + output: { |
| 36 | + dir: 'output', |
| 37 | + format: 'cjs' |
| 38 | + }, |
| 39 | + plugins: [json()] |
| 40 | +}; |
| 41 | +``` |
| 42 | + |
| 43 | +Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api). |
| 44 | + |
| 45 | +With an accompanying file `src/index.js`, the local `package.json` file would now be importable as seen below: |
| 46 | + |
| 47 | +```js |
| 48 | +// src/index.js |
| 49 | +import pkg from './package.json'; |
| 50 | +console.log(`running version ${pkg.version}`); |
| 51 | +``` |
| 52 | + |
| 53 | +## Options |
| 54 | + |
| 55 | +### `compact` |
| 56 | + |
| 57 | +Type: `Boolean`<br> |
| 58 | +Default: `false` |
| 59 | + |
| 60 | +If `true`, instructs the plugin to ignore `indent` and generates the smallest code. |
| 61 | + |
| 62 | +### `exclude` |
| 63 | + |
| 64 | +Type: `String` | `Array[...String]`<br> |
| 65 | +Default: `null` |
| 66 | + |
| 67 | +A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored. |
| 68 | + |
| 69 | +### `include` |
| 70 | + |
| 71 | +Type: `String` | `Array(String)`<br> |
| 72 | +Default: `null` |
| 73 | + |
| 74 | +A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted. |
| 75 | + |
| 76 | +### `indent` |
| 77 | + |
| 78 | +Type: `String`<br> |
| 79 | +Default: `'\t'` |
| 80 | + |
| 81 | +Specifies the indentation for the generated default export. |
| 82 | + |
| 83 | +### `namedExports` |
| 84 | + |
| 85 | +Type: `Boolean`<br> |
| 86 | +Default: `true` |
| 87 | + |
| 88 | +If `true`, instructs the plugin to generate a named export for every property of the JSON object. |
| 89 | + |
| 90 | +### `preferConst` |
| 91 | + |
| 92 | +Type: `Boolean`<br> |
| 93 | +Default: `false` |
| 94 | + |
| 95 | +If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking. |
| 96 | + |
| 97 | +## Meta |
| 98 | + |
| 99 | +[CONTRIBUTING](/.github/CONTRIBUTING.md) |
| 100 | + |
| 101 | +[LICENSE (MIT)](/LICENSE) |
0 commit comments