• webpack-bundle-analyzer 打包分析工具


     

    移动端项目优化的时候,需要这个插件来可视化查看打包大小,在此记录下

    插件官网地址

    一.安装

    npm install --save-dev webpack-bundle-analyzer

    二.使用方式:


    1. 作为插件使用(推荐)

    在webpack配置文件,如果有生产配置文件放入生产配置文件里
    require方式引入进来,然后在plugins配置项new一个实例即可

     

    1. const webpack = require('webpack');
    2. const { merge } = require('webpack-merge');
    3. const common = require('./webpack.common.js');
    4. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
    5. module.exports = merge(common, {
    6. // devtool:'source-map',//开启将会生成map文件
    7. mode: 'production',
    8. plugins: [
    9. new webpack.DefinePlugin({
    10. 'process.env.NODE_ENV': JSON.stringify('production')
    11. }),
    12. new BundleAnalyzerPlugin(),
    13. ],
    14. //代码分离 防止多入口重复打包bundle
    15. optimization: {
    16. splitChunks: {
    17. minChunks: 2, //模块至少使用次数
    18. cacheGroups: {
    19. vendor: {
    20. name: 'vendor',
    21. test: /[\\/]node_modules[\\/]/,
    22. chunks: 'all',
    23. priority: 2, //2>0 nodulesmodules里的模块将优先打包进vendor
    24. },
    25. commons: {
    26. name: "commons", //async异步代码分割 initial同步代码分割 all同步异步分割都开启
    27. chunks: "all",
    28. minSize: 30000, //字节 引入的文件大于30kb才进行分割
    29. priority: 0, //优先级,先打包到哪个组里面,值越大,优先级越高
    30. }
    31. }
    32. }
    33. }
    34. });

    然后执行npm run build 既可

    2. 作为一个命令使用

    You can generate it using BundleAnalyzerPlugin with generateStatsFile option set to true or with this simple command:(项目目录执行下面命令)

    webpack --profile --json > stats.json

    If you're on Windows and using PowerShell, you can generate the stats file with this command to avoid BOM issues:(windwos10用户可以用powershell执行下面命令)

    webpack --profile --json | Out-file 'stats.json' -Encoding OEM
    

    再执行该命令即可

    webpack-bundle-analyzer bundle/output/path/stats.json

     

  • 相关阅读:
    Python Day3 爬虫-数据接口和selenium基础
    评价指标篇——IOU(交并比)
    C++中string的用法
    每天一道算法题(十一)——滑动窗口最大值_困难(中等)
    VB 语言介绍以及VBA、宏(Macro)的关系
    有隙可乘 - Android 序列化漏洞分析实战
    如何在JVM中基于引用计数法实现GC
    RocketMQ高级特性
    Android问题笔记 - NoSuchmethodException: could not find Fragment constructor
    如何解决Java 中的精度问题
  • 原文地址:https://blog.csdn.net/cddcj/article/details/126598339