此插件使用 JSON.stringify() 来最小化你的 JSON。
首先,你需要安装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",
},
}),
],
},
};
如果你还没有这样做,请花点时间阅读我们的贡献指南。