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 |
|
16 | This 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 |
|
20 | To begin, you'll need to install `json-minimizer-webpack-plugin`:
|
21 |
|
22 | ```console
|
23 | npm install json-minimizer-webpack-plugin --save-dev
|
24 | ```
|
25 |
|
26 | or
|
27 |
|
28 | ```console
|
29 | yarn add -D json-minimizer-webpack-plugin
|
30 | ```
|
31 |
|
32 | or
|
33 |
|
34 | ```console
|
35 | pnpm add -D json-minimizer-webpack-plugin
|
36 | ```
|
37 |
|
38 | Then add the plugin to your `webpack` configuration. For example:
|
39 |
|
40 | **webpack.config.js**
|
41 |
|
42 | ```js
|
43 | const JsonMinimizerPlugin = require("json-minimizer-webpack-plugin");
|
44 | const CopyPlugin = require("copy-webpack-plugin");
|
45 |
|
46 | module.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 |
|
76 | And 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 |
|
87 | Type:
|
88 |
|
89 | ```ts
|
90 | type test = string | RegExp | Array<string | RegExp>;
|
91 | ```
|
92 |
|
93 | Default: `/\.json(\?.*)?$/i`
|
94 |
|
95 | Test to match files against.
|
96 |
|
97 | ```js
|
98 | module.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 |
|
112 | Type:
|
113 |
|
114 | ```ts
|
115 | type include = string | RegExp | Array<string | RegExp>;
|
116 | ```
|
117 |
|
118 | Default: `undefined`
|
119 |
|
120 | Files to include.
|
121 |
|
122 | **webpack.config.js**
|
123 |
|
124 | ```js
|
125 | module.exports = {
|
126 | optimization: {
|
127 | minimize: true,
|
128 | minimizer: [
|
129 | new JsonMinimizerPlugin({
|
130 | include: /\/includes/,
|
131 | }),
|
132 | ],
|
133 | },
|
134 | };
|
135 | ```
|
136 |
|
137 | ### `exclude`
|
138 |
|
139 | Type:
|
140 |
|
141 | ```ts
|
142 | type exclude = string | RegExp | Array<string | RegExp>;
|
143 | ```
|
144 |
|
145 | Default: `undefined`
|
146 |
|
147 | Files to exclude.
|
148 |
|
149 | **webpack.config.js**
|
150 |
|
151 | ```js
|
152 | module.exports = {
|
153 | optimization: {
|
154 | minimize: true,
|
155 | minimizer: [
|
156 | new JsonMinimizerPlugin({
|
157 | exclude: /\/excludes/,
|
158 | }),
|
159 | ],
|
160 | },
|
161 | };
|
162 | ```
|
163 |
|
164 | ### `minimizerOptions`
|
165 |
|
166 | Type:
|
167 |
|
168 | ```ts
|
169 | type minimizerOptions = {
|
170 | space?: null | string | number;
|
171 | replacer?: null | Function | Array<string | number>;
|
172 | };
|
173 | ```
|
174 |
|
175 | Default: `{ 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
|
180 | module.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 |
|
196 | Please 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
|