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 | [![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
|
9 |
|
10 | # @rollup/plugin-json
|
11 |
|
12 | 🍣 A Rollup plugin which Converts .json files to ES6 modules.
|
13 |
|
14 | ## Requirements
|
15 |
|
16 | This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.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 { readFileSync } from 'fs';
|
50 |
|
51 | const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
52 | console.log(`running version ${pkg.version}`);
|
53 | ```
|
54 |
|
55 | ## Options
|
56 |
|
57 | ### `compact`
|
58 |
|
59 | Type: `Boolean`<br>
|
60 | Default: `false`
|
61 |
|
62 | If `true`, instructs the plugin to ignore `indent` and generates the smallest code.
|
63 |
|
64 | ### `exclude`
|
65 |
|
66 | Type: `String` | `Array[...String]`<br>
|
67 | Default: `null`
|
68 |
|
69 | A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.
|
70 |
|
71 | ### `include`
|
72 |
|
73 | Type: `String` | `Array[...String]`<br>
|
74 | Default: `null`
|
75 |
|
76 | A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
|
77 |
|
78 | ### `indent`
|
79 |
|
80 | Type: `String`<br>
|
81 | Default: `'\t'`
|
82 |
|
83 | Specifies the indentation for the generated default export.
|
84 |
|
85 | ### `namedExports`
|
86 |
|
87 | Type: `Boolean`<br>
|
88 | Default: `true`
|
89 |
|
90 | If `true`, instructs the plugin to generate a named export for every property of the JSON object.
|
91 |
|
92 | ### `preferConst`
|
93 |
|
94 | Type: `Boolean`<br>
|
95 | Default: `false`
|
96 |
|
97 | If `true`, instructs the plugin to declare properties as variables, using either `var` or `const`. This pertains to tree-shaking.
|
98 |
|
99 | ## Meta
|
100 |
|
101 | [CONTRIBUTING](/.github/CONTRIBUTING.md)
|
102 |
|
103 | [LICENSE (MIT)](/LICENSE)
|