JsonMinimizerPlugin
raw JSON → 5.0.1 verified Sat Apr 25 auth: no javascript
A Webpack plugin that minifies JSON assets using JSON.stringify(). Version 5.0.1 (stable). Maintained by the webpack-contrib team with regular updates; breaking changes follow major Node.js and Webpack version bumps. Differentiators: lightweight alternative to heavier minifiers, integrates seamlessly with Webpack's optimization minimizer array, supports include/exclude patterns and custom JSON.stringify options (space/replacer). Ships TypeScript definitions. Requires Node.js >= 18.12.0 and webpack >= 5.1.0.
Common errors
error Error: '...' is not a valid minimizer. Use '...' to extend default minimizers. ↓
cause Forgetting to include the spread operator when adding custom minimizers alongside defaults
fix
Add
'...' as an entry in the minimizer array before your custom plugin error TypeError: Cannot read properties of undefined (reading 'test') ↓
cause Plugin instantiated without `new` keyword or imported incorrectly
fix
Use
new JsonMinimizerPlugin({...}) and ensure default import Warnings
breaking Node.js version < 18.12.0 is not supported ↓
fix Upgrade Node.js to >= 18.12.0
breaking Minimum webpack version changed to 5.1.0 ↓
fix Update webpack to >= 5.1.0
breaking TypeScript type exports changed: removed CJS wrapper and namespace exports ↓
fix Switch to default import for the plugin class; use type imports for options
gotcha Plugin only minifies JSON that passes through Webpack as assets (not import/require of .json files which are inlined) ↓
fix Use type: 'asset/resource' in module rules or copy-plugin to treat JSON as separate files
gotcha The plugin uses JSON.stringify which may produce output that is not strictly valid JSON if replacer is incorrectly typed ↓
fix Ensure replacer is null, a function, or an array of strings/numbers
Install
npm install json-minimizer-webpack-plugin yarn add json-minimizer-webpack-plugin pnpm add json-minimizer-webpack-plugin Imports
- JsonMinimizerPlugin wrong
const JsonMinimizerPlugin = require('json-minimizer-webpack-plugin')correctimport JsonMinimizerPlugin from 'json-minimizer-webpack-plugin' - JsonMinimizerPlugin (type) wrong
const { JsonMinimizerPluginOptions } = require('json-minimizer-webpack-plugin')correctimport type { JsonMinimizerPluginOptions } from 'json-minimizer-webpack-plugin' - constructor options wrong
new JsonMinimizerPlugin() // no optionscorrectnew JsonMinimizerPlugin({ test: /\.json$/i, include: [...], exclude: [...], minimizerOptions: { space: 0 } })
Quickstart
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const JsonMinimizerPlugin = require('json-minimizer-webpack-plugin');
module.exports = {
mode: 'production',
entry: './src/index.js',
output: { path: path.resolve(__dirname, 'dist') },
module: {
rules: [
{ test: /\.json$/i, type: 'asset/resource' }
]
},
plugins: [
new CopyPlugin({
patterns: [{ from: './src/*.json', to: '[name][ext]' }]
})
],
optimization: {
minimize: true,
minimizer: [
`...`,
new JsonMinimizerPlugin({
test: /\.json$/i,
minimizerOptions: { space: 0 }
})
]
}
};