查看项目中无引用到的文件、函数

unimported 查看没有引用的文件

  1. 全局安装 unimported

    npm install -g unimported

  2. 在 package.json 的 scripts 增加

json 复制代码
"scripts": {   
  "analyze:deadcode": "unimported && webpack --profile --json > stats.json" 
} 
  1. 增加文件 .unimportedrc.json
json 复制代码
{
    "entry": [
      "src/app.{js,ts,jsx,tsx}",       
      "src/app.config.{js,ts}",        
      "src/pages/**/*.{js,ts,jsx,tsx}"
    ],
    "ignore": [
      "**/node_modules/**",
      "**/dist/**",
      "**/*.d.ts",
      "**/*.test.{js,ts}",
      "**/*.spec.{js,ts}",
      "**/__mocks__/**",
      "project.config.json",
      "config/**/*.js"                 
    ],
    "ignoreUnused": [
      "src/types/**/*.d.ts",           
      "src/assets/**/*",               
      "src/styles/**/*.{css,scss,less}" 
    ]
  }
  1. 然后运行
arduino 复制代码
npm run analyze:deadcode

运行后输出以下内容,列出未引用的文件

注意 有些小程序页面 A 也会有 /component 的组件,只给页面A用, 但是 "src/pages/**/*.{js,ts,jsx,tsx}" 会涵盖掉,不会把A的无用组件列出来, 这种情况下,小程序最好每个页面的路径都列出来,例如以下,这样可以更加精确的定位到每个页面ABCD里面的component是否真正有被使用

json 复制代码
{
    "entry": [
      "src/app.{js,ts,jsx,tsx}",       
      "src/app.config.{js,ts}",        
      "src/pages/index/index.{js,ts,jsx,tsx,config.ts}",
      "src/pages/home/index.{js,ts,jsx,tsx,config.ts}",
      "src/pages/category/index.{js,ts,jsx,tsx,config.ts}",
      "src/pages/cart/index.{js,ts,jsx,tsx,config.ts}",
      "src/pages/mine/index.{js,ts,jsx,tsx,config.ts}",
      "src/pages/product/detail/index.{js,ts,jsx,tsx,config.ts}",
      "src/pages/product/content-hub/index.{js,ts,jsx,tsx,config.ts}",
    ],
    "ignore": [ ],
    "ignoreUnused": [ ]
  }

使用Webpack插件 webpack-deadcode-plugin 查看未使用的函数和变量

  1. 安装 webpack-deadcode-plugin
css 复制代码
npm install webpack-deadcode-plugin --save-dev
  1. 在webpack配置中添加:
php 复制代码
const DeadCodePlugin = require('webpack-deadcode-plugin');

// 增强配置可检测具体方法
chain.plugin('deadcode')
  .use(DeadCodePlugin, [{
    detectUnusedExport: true,    // 检测未使用的导出
    detectUnusedClass: true,     // 检测未使用的类
    detectUnusedFunction: true,  // 检测未使用的函数
    detectUnusedVariable: true,  // 检测未使用的变量
    log: 'all'                   // 显示所有未使用项
  }]);
  1. 运行构建后会生成未使用文件的报告

通过这些方式可以 了解到 哪些文件、函数、变量没有使用到

如果要删除

  1. 一定要确认、确认、确认、多次确认 清楚再删
  2. 小规模的删,不要一次性删太多东西
  3. 删除之后,对于相关的流程做好自测,也要跟 QA 和 产品说明清楚,可能要做大版本的回测
  4. 对于已经上线的项目,不建议再做这样的操作,如果需要做也必须【提前】跟 PM、QA 商量一下是否有足够的时间安排来做代码优化和流程测试,避免出现生产事故
相关推荐
用户693717500138410 分钟前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦14 分钟前
Web 前端搜索文字高亮实现方法汇总
前端
用户693717500138415 分钟前
Room 3.0:这次不是升级,是重来
android·前端·google
漫随流水2 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
踩着两条虫3 小时前
VTJ.PRO 核心架构全公开!从设计稿到代码,揭秘AI智能体如何“听懂人话”
前端·vue.js·ai编程
jzlhll1234 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
蓝冰凌5 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛5 小时前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js
柳杉5 小时前
从动漫水面到赛博飞船:这位开发者的Three.js作品太惊艳了
前端·javascript·数据可视化