UNPKG

4.41 kBMarkdownView Raw
1<div align="center">
2 <a href="https://github.com/webpack/webpack">
3 <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
4 </a>
5</div>
6
7[![npm][npm]][npm-url]
8[![node][node]][node-url]
9[![tests][tests]][tests-url]
10[![cover][cover]][cover-url]
11[![discussion][discussion]][discussion-url]
12[![size][size]][size-url]
13
14# json-minimizer-webpack-plugin
15
16This plugin uses [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) to minify your JSON.
17
18## Getting Started
19
20To begin, you'll need to install `json-minimizer-webpack-plugin`:
21
22```console
23npm install json-minimizer-webpack-plugin --save-dev
24```
25
26or
27
28```console
29yarn add -D json-minimizer-webpack-plugin
30```
31
32or
33
34```console
35pnpm add -D json-minimizer-webpack-plugin
36```
37
38Then add the plugin to your `webpack` configuration. For example:
39
40**webpack.config.js**
41
42```js
43const JsonMinimizerPlugin = require("json-minimizer-webpack-plugin");
44const CopyPlugin = require("copy-webpack-plugin");
45
46module.exports = {
47 module: {
48 rules: [
49 {
50 test: /\.json$/i,
51 type: "asset/resource",
52 },
53 ],
54 },
55 plugins: [
56 new CopyPlugin({
57 patterns: [
58 {
59 context: path.resolve(__dirname, "dist"),
60 from: "./src/*.json",
61 },
62 ],
63 }),
64 ],
65 optimization: {
66 minimize: true,
67 minimizer: [
68 // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
69 // `...`
70 new JsonMinimizerPlugin(),
71 ],
72 },
73};
74```
75
76And run `webpack` via your preferred method.
77
78## Options
79
80- **[`test`](#test)**
81- **[`include`](#include)**
82- **[`exclude`](#exclude)**
83- **[`minimizerOptions`](#minimizeroptions)**
84
85### `test`
86
87Type:
88
89```ts
90type test = string | RegExp | Array<string | RegExp>;
91```
92
93Default: `/\.json(\?.*)?$/i`
94
95Test to match files against.
96
97```js
98module.exports = {
99 optimization: {
100 minimize: true,
101 minimizer: [
102 new JsonMinimizerPlugin({
103 test: /\.foo\.json/i,
104 }),
105 ],
106 },
107};
108```
109
110### `include`
111
112Type:
113
114```ts
115type include = string | RegExp | Array<string | RegExp>;
116```
117
118Default: `undefined`
119
120Files to include.
121
122**webpack.config.js**
123
124```js
125module.exports = {
126 optimization: {
127 minimize: true,
128 minimizer: [
129 new JsonMinimizerPlugin({
130 include: /\/includes/,
131 }),
132 ],
133 },
134};
135```
136
137### `exclude`
138
139Type:
140
141```ts
142type exclude = string | RegExp | Array<string | RegExp>;
143```
144
145Default: `undefined`
146
147Files to exclude.
148
149**webpack.config.js**
150
151```js
152module.exports = {
153 optimization: {
154 minimize: true,
155 minimizer: [
156 new JsonMinimizerPlugin({
157 exclude: /\/excludes/,
158 }),
159 ],
160 },
161};
162```
163
164### `minimizerOptions`
165
166Type:
167
168```ts
169type minimizerOptions = {
170 space?: null | string | number;
171 replacer?: null | Function | Array<string | number>;
172};
173```
174
175Default: `{ replacer: null, space: null }`
176
177`JSON.stringify()` [options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).
178
179```js
180module.exports = {
181 optimization: {
182 minimize: true,
183 minimizer: [
184 new JsonMinimizerPlugin({
185 minimizerOptions: {
186 space: "\t",
187 },
188 }),
189 ],
190 },
191};
192```
193
194## Contributing
195
196Please take a moment to read our contributing guidelines if you haven't yet done so.
197
198[CONTRIBUTING](./.github/CONTRIBUTING.md)
199
200## License
201
202[MIT](./LICENSE)
203
204[npm]: https://img.shields.io/npm/v/json-minimizer-webpack-plugin.svg
205[npm-url]: https://npmjs.com/package/json-minimizer-webpack-plugin
206[node]: https://img.shields.io/node/v/json-minimizer-webpack-plugin.svg
207[node-url]: https://nodejs.org
208[tests]: https://github.com/webpack-contrib/json-minimizer-webpack-plugin/workflows/json-minimizer-webpack-plugin/badge.svg
209[tests-url]: https://github.com/webpack-contrib/json-minimizer-webpack-plugin/actions
210[cover]: https://codecov.io/gh/webpack-contrib/json-minimizer-webpack-plugin/branch/master/graph/badge.svg
211[cover-url]: https://codecov.io/gh/webpack-contrib/json-minimizer-webpack-plugin
212[discussion]: https://img.shields.io/github/discussions/webpack/webpack
213[discussion-url]: https://github.com/webpack/webpack/discussions
214[size]: https://packagephobia.now.sh/badge?p=json-minimizer-webpack-plugin
215[size-url]: https://packagephobia.now.sh/result?p=json-minimizer-webpack-plugin