此插件使用 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
(例如,通过 CLI 或 npm 脚本)。
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",
},
}),
],
},
};
我们欢迎所有贡献!如果您是新用户,请在提交问题或拉取请求之前花一些时间阅读我们的贡献指南。