当前位置: 首页 > article >正文

webpack插件 --- webpack-bundle-analyzer【查看包体积】

const UglifyJsPlugin = require('uglifyjs-webpack-plugin') // 清除注释
const CompressionWebpackPlugin = require('compression-webpack-plugin'); // 开启压缩

// 是否为生产环境
const isProduction = process.env.NODE_ENV === 'production';
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
// 本地环境是否需要使用cdn
const devNeedCdn = true;

// cdn链接
const cdn = {}

module.exports={
  publicPath: '/',
  assetsDir: "assets",
  devServer: {
    open: true,  // npm run serve后自动打开页面
    host: '0.0.0.0',  // 匹配本机IP地址(默认是0.0.0.0)
    // port: 8066, // 开发服务器运行端口号
    proxy: {
      '/api': {                                //   以'/api'开头的请求会被代理进行转发
        target: 'http://localhost:6688',  
        changeOrigin: true
      }
    },
    disableHostCheck: true,
  },
  //去除生产环境的productionSourceMap
  productionSourceMap: false,

  chainWebpack: config => {
    // ============注入cdn start============
    config.plugin('html').tap(args => {
        // 生产环境或本地需要cdn时,才注入cdn
        if (isProduction || devNeedCdn) args[0].cdn = cdn
        return args
    })
    // config.plugin('webpack-bundle-analyzer') // todo 查看打包文件体积大小1
    //   .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    // ============注入cdn end============
  },
  configureWebpack: (config) => {
    // 用cdn方式引入,则构建时要忽略相关资源
    const plugins = [];
    if (isProduction || devNeedCdn){
      config.externals = cdn.externals
      config.mode = 'production';
      config["performance"] = {//打包文件大小配置
        "maxEntrypointSize": 10000000,
        "maxAssetSize": 30000000
      }
      config.plugins.push(
        new UglifyJsPlugin({
          uglifyOptions: {
            output: {
              comments: false, // 去掉注释
            },
            warnings: false,
            compress: {
              drop_console: false,
              drop_debugger: false,
              // pure_funcs: ['console.log']//移除console
            }
          }
        })
      )
       // 服务器也要相应开启gzip
       config.plugins.push(
        new CompressionWebpackPlugin({
            filename: '[path].gz[query]',
            algorithm: 'gzip',
            test: /\.(js|css)$/,// 匹配文件名
            threshold: 10000, // 对超过10k的数据压缩
            deleteOriginalAssets: false, // 不删除源文件
            minRatio: 0.8 // 压缩比
        })
      )

// todo 查看打包文件体积大小2
        config.plugins.push(
            new BundleAnalyzerPlugin({
                analyzerMode: 'server',
                analyzerHost: 'localhost',
                analyzerPort: 8889, // 修改为其他未被占用的端口
                openAnalyzer: true,
            })
        )
    }
  }

}

 


http://www.kler.cn/news/335378.html

相关文章:

  • 电影推荐系统
  • Python 代码编写规范
  • C++——模板进阶、继承
  • 【表分区】MySQL表分区(partition)创建、查询、删除以及重建分区等
  • windows C++-创建基于代理的应用程序(下)
  • leetcode-10/6
  • UGUI(六大UI根基组件)
  • Spring Boot新闻推荐:实时数据处理
  • 【C++】二叉搜索树+变身 = AVL树
  • redo log 和 bin log 的两阶段提交
  • 【MySQL】MySQL 数据库主从复制详解
  • Qt QWidget控件
  • Spring Boot项目使用MyBatis Plus的详细步骤
  • Apache POI 2024/10/2
  • 【TypeScript学习】TypeScript基础学习总结一
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-05
  • 在 Ubuntu 18.04 上安装 Syncthing
  • Mac通过ssh连接工具远程登录服务器( Royal TSX安装及使用)
  • sql-labs靶场第九关测试报告
  • GiliSoft Video Editor Pro专业视频编辑工具-视频剪辑/合并/字幕于一身的编辑器-供大家学习研究参考