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