UNPKG

2.71 kBMarkdownView Raw
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
16This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.
17
18## Install
19
20Using npm:
21
22```console
23npm install @rollup/plugin-json --save-dev
24```
25
26## Usage
27
28Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
29
30```js
31import json from '@rollup/plugin-json';
32
33export default {
34 input: 'src/index.js',
35 output: {
36 dir: 'output',
37 format: 'cjs'
38 },
39 plugins: [json()]
40};
41```
42
43Then 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
45With 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
49import { readFileSync } from 'fs';
50
51const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
52console.log(`running version ${pkg.version}`);
53```
54
55## Options
56
57### `compact`
58
59Type: `Boolean`<br>
60Default: `false`
61
62If `true`, instructs the plugin to ignore `indent` and generates the smallest code.
63
64### `exclude`
65
66Type: `String` | `Array[...String]`<br>
67Default: `null`
68
69A [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
73Type: `String` | `Array[...String]`<br>
74Default: `null`
75
76A [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
80Type: `String`<br>
81Default: `'\t'`
82
83Specifies the indentation for the generated default export.
84
85### `namedExports`
86
87Type: `Boolean`<br>
88Default: `true`
89
90If `true`, instructs the plugin to generate a named export for every property of the JSON object.
91
92### `preferConst`
93
94Type: `Boolean`<br>
95Default: `false`
96
97If `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)