JsonMinimizerWebpackPlugin

免責聲明: JsonMinimizerWebpackPlugin 是由社群成員維護的第三方套件,它可能沒有與 webpack 相同的支援、安全政策或授權,且並非由 webpack 維護。

npm node tests cover discussion size

此外掛程式使用 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",
        },
      }),
    ],
  },
};

貢獻

如果您尚未閱讀,請花點時間閱讀我們的貢獻指南。

貢獻

授權

MIT