This plugin usesJSON.stringify() to minify your JSON files during the build process.
To begin, you'll need to installjson-minimizer-webpack-plugin
:
npm install json-minimizer-webpack-plugin --save-dev
or
yarn add -D json-minimizer-webpack-plugin
or
pnpm add -D json-minimizer-webpack-plugin
Then add the plugin to yourwebpack
configuration. For example:
webpack.config.js
const JsonMinimizerPlugin=require("json-minimizer-webpack-plugin");const CopyPlugin=require("copy-webpack-plugin");module.exports={ module:{ rules:[{ test:/\.json$/i, type:"asset/resource",},],}, plugins:[newCopyPlugin({ patterns:[{ context: path.resolve(__dirname,"dist"),from:"./src/*.json",},],}),], optimization:{ minimize:true, minimizer:[// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line// `...`newJsonMinimizerPlugin(),],},};
Finally, runwebpack
using the method you normally use (e.g., via CLI or an npm script).
test
Type:
typetest=string| RegExp|Array<string| RegExp>;
Default:/\.json(\?.*)?$/i
Test to match files against.
module.exports={ optimization:{ minimize:true, minimizer:[newJsonMinimizerPlugin({ test:/\.foo\.json/i,}),],},};
include
Type:
typeinclude=string| RegExp|Array<string| RegExp>;
Default:undefined
Files to include for minimization.
webpack.config.js
module.exports={ optimization:{ minimize:true, minimizer:[newJsonMinimizerPlugin({ include:/\/includes/,}),],},};
exclude
Type:
typeexclude=string| RegExp|Array<string| RegExp>;
Default:undefined
Files to exclude from minimization.
webpack.config.js
module.exports={ optimization:{ minimize:true, minimizer:[newJsonMinimizerPlugin({ exclude:/\/excludes/,}),],},};
minimizerOptions
Type:
typeminimizerOptions={ space?:null|string|number; replacer?:null|Function|Array<string|number>;};
Default:{ replacer: null, space: null }
JSON.stringify()
options.
module.exports={ optimization:{ minimize:true, minimizer:[newJsonMinimizerPlugin({ minimizerOptions:{ space:"\t",},}),],},};
We welcome all contributions!If you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.