webpack处理其它资源

85 阅读1分钟

修改打包输出路径

const path = require("path");

module.exports = {
  entry: "./src/main.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "static/js/main.js", // 将 js 文件输出到 static/js 目录中
    //下次打包前自动清空
    clean:true
  },
  module: {
    rules: [
      {
        test: /\.(png|jpe?g|gif|webp)$/,
        //转换成base64
        type: "asset",
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024, // 小于10kb的图片会被base64处理
          },
        },
        generator: {
          // 将图片文件输出到 static/imgs 目录中
          // 将图片文件命名 [hash:8][ext][query]
          // [hash:8]: hash值取8位
          // [ext]: 使用之前的文件扩展名
          // [query]: 添加之前的query参数
          filename: "static/imgs/[hash:8][ext][query]",
        },
      },
    ],
  },
  mode: "development",
};

自动清空上一次内容

output输出中设置 - clean:true

字体图标

{
    test: /.(ttf|woff2?)$/,
    //不转换橙base64
    type: "asset/resource",
    generator: {
        filename: "static/media/[hash:8][ext][query]",
    },
}

其他资源

{
    //处理其它资源只需要兼容
    test: /.(ttf|woff2?|mp3|mp4)$/,
    //不转换base64
    type: "asset/resource",
    generator: {
        filename: "static/media/[hash:8][ext][query]",
    },
}