このプラグインは、JSONを最小化するためにJSON.stringify()を使用します。
まず、json-minimizer-webpack-plugin
をインストールする必要があります
npm install json-minimizer-webpack-plugin --save-dev
または
yarn add -D json-minimizer-webpack-plugin
または
pnpm add -D json-minimizer-webpack-plugin
次に、プラグインをwebpack
の設定に追加します。例:
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: [
new CopyPlugin({
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
// `...`
new JsonMinimizerPlugin(),
],
},
};
そして、お好みの方法でwebpack
を実行します。
test
型
type test = string | RegExp | Array<string | RegExp>;
デフォルト:/\.json(\?.*)?$/i
ファイルと照合するテスト。
module.exports = {
optimization: {
minimize: true,
minimizer: [
new JsonMinimizerPlugin({
test: /\.foo\.json/i,
}),
],
},
};
include
型
type include = string | RegExp | Array<string | RegExp>;
デフォルト:undefined
含めるファイル。
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new JsonMinimizerPlugin({
include: /\/includes/,
}),
],
},
};
exclude
型
type exclude = string | RegExp | Array<string | RegExp>;
デフォルト:undefined
除外するファイル。
webpack.config.js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new JsonMinimizerPlugin({
exclude: /\/excludes/,
}),
],
},
};
minimizerOptions
型
type minimizerOptions = {
space?: null | string | number;
replacer?: null | Function | Array<string | number>;
};
デフォルト:{ replacer: null, space: null }
JSON.stringify()
オプション。
module.exports = {
optimization: {
minimize: true,
minimizer: [
new JsonMinimizerPlugin({
minimizerOptions: {
space: "\t",
},
}),
],
},
};
まだ読んでいない場合は、貢献ガイドラインを読む時間を取ってください。