UNPKG

4.25 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[![deps][deps]][deps-url]
10[![tests][tests]][tests-url]
11[![cover][cover]][cover-url]
12[![chat][chat]][chat-url]
13[![size][size]][size-url]
14
15# json-minimizer-webpack-plugin
16
17This plugin uses [JSON.stringify()](https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) to minify your JSON.
18
19## Getting Started
20
21To begin, you'll need to install `json-minimizer-webpack-plugin`:
22
23```console
24$ npm install json-minimizer-webpack-plugin --save-dev
25```
26
27Then add the plugin to your `webpack` configuration. For example:
28
29**webpack.config.js**
30
31```js
32const JsonMinimizerPlugin = require("json-minimizer-webpack-plugin");
33const CopyPlugin = require("copy-webpack-plugin");
34
35module.exports = {
36 module: {
37 rules: [
38 {
39 test: /\.json$/i,
40 type: "asset/resource",
41 },
42 ],
43 },
44 plugins: [
45 new CopyPlugin({
46 patterns: [
47 {
48 context: path.resolve(__dirname, "dist"),
49 from: "./src/*.json",
50 },
51 ],
52 }),
53 ],
54 optimization: {
55 minimize: true,
56 minimizer: [
57 // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
58 // `...`
59 new JsonMinimizerPlugin(),
60 ],
61 },
62};
63```
64
65This will enable JSON optimization only in production mode.
66If you want to run it also in development set the `optimization.minimize` option to `true`.
67
68And run `webpack` via your preferred method.
69
70## Options
71
72### `test`
73
74Type: `String|RegExp|Array<String|RegExp>` - default: `/\.json(\?.*)?$/i`
75
76Test to match files against.
77
78```js
79module.exports = {
80 optimization: {
81 minimize: true,
82 minimizer: [
83 new JsonMinimizerPlugin({
84 test: /\.foo\.json/i,
85 }),
86 ],
87 },
88};
89```
90
91### `include`
92
93Type: `String|RegExp|Array<String|RegExp>`
94Default: `undefined`
95
96Files to include.
97
98**webpack.config.js**
99
100```js
101module.exports = {
102 optimization: {
103 minimize: true,
104 minimizer: [
105 new JsonMinimizerPlugin({
106 include: /\/includes/,
107 }),
108 ],
109 },
110};
111```
112
113### `exclude`
114
115Type: `String|RegExp|Array<String|RegExp>`
116Default: `undefined`
117
118Files to exclude.
119
120**webpack.config.js**
121
122```js
123module.exports = {
124 optimization: {
125 minimize: true,
126 minimizer: [
127 new JsonMinimizerPlugin({
128 exclude: /\/excludes/,
129 }),
130 ],
131 },
132};
133```
134
135### `minimizerOptions`
136
137Type: `Object`
138Default: `{ replacer: null, space: null }`
139
140`JSON.stringify()` [options](https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).
141
142```js
143module.exports = {
144 optimization: {
145 minimize: true,
146 minimizer: [
147 new JsonMinimizerPlugin({
148 minimizerOptions: {
149 space: "\t",
150 },
151 }),
152 ],
153 },
154};
155```
156
157## Contributing
158
159Please take a moment to read our contributing guidelines if you haven't yet done so.
160
161[CONTRIBUTING](./.github/CONTRIBUTING.md)
162
163## License
164
165[MIT](./LICENSE)
166
167[npm]: https://img.shields.io/npm/v/json-minimizer-webpack-plugin.svg
168[npm-url]: https://npmjs.com/package/json-minimizer-webpack-plugin
169[node]: https://img.shields.io/node/v/json-minimizer-webpack-plugin.svg
170[node-url]: https://nodejs.org
171[deps]: https://david-dm.org/webpack-contrib/json-minimizer-webpack-plugin.svg
172[deps-url]: https://david-dm.org/webpack-contrib/json-minimizer-webpack-plugin
173[tests]: https://github.com/webpack-contrib/json-minimizer-webpack-plugin/workflows/json-minimizer-webpack-plugin/badge.svg
174[tests-url]: https://github.com/webpack-contrib/json-minimizer-webpack-plugin/actions
175[cover]: https://codecov.io/gh/webpack-contrib/json-minimizer-webpack-plugin/branch/master/graph/badge.svg
176[cover-url]: https://codecov.io/gh/webpack-contrib/json-minimizer-webpack-plugin
177[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
178[chat-url]: https://gitter.im/webpack/webpack
179[size]: https://packagephobia.now.sh/badge?p=json-minimizer-webpack-plugin
180[size-url]: https://packagephobia.now.sh/result?p=json-minimizer-webpack-plugin