此外掛程式使用 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",
},
}),
],
},
};
如果您尚未閱讀,請花點時間閱讀我們的貢獻指南。