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-devor
yarn add -D json-minimizer-webpack-pluginor
pnpm add -D json-minimizer-webpack-pluginThen add the plugin to yourwebpack configuration. For example:
webpack.config.js
const CopyPlugin=require("copy-webpack-plugin");const JsonMinimizerPlugin=require("json-minimizer-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).
testType:
typetest=string| RegExp|(string| RegExp)[];Default:/\.json(\?.*)?$/i
Test to match files against.
module.exports={ optimization:{ minimize:true, minimizer:[newJsonMinimizerPlugin({ test:/\.foo\.json/i,}),],},};includeType:
typeinclude=string| RegExp|(string| RegExp)[];Default:undefined
Files to include for minimization.
webpack.config.js
module.exports={ optimization:{ minimize:true, minimizer:[newJsonMinimizerPlugin({ include:/\/includes/,}),],},};excludeType:
typeexclude=string| RegExp|(string| RegExp)[];Default:undefined
Files to exclude from minimization.
webpack.config.js
module.exports={ optimization:{ minimize:true, minimizer:[newJsonMinimizerPlugin({ exclude:/\/excludes/,}),],},};minimizerOptionsType:
interfaceminimizerOptions{ space?:null|string|number; replacer?:null|Function|(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.