背景
最近有個領導想讓我們搭組件庫,然后我就想知道目前項目中使用的三方組件庫哪些組件使用頻率最高。本來想去咨詢小伙伴,但是小伙伴太忙了,只能自己弄了。我就想能不能通過 webpack 來實現我的想法
效果
我們是用的 @material-ui,下面是組件使用情況
實現
我們知道 loader 的 source是文件的靜態字符串如下圖
最快的方案通過字符串統計用正則的方式一把梭,但是這樣會有問題就是如果注釋部分有的話也會被統計進去就不準確,所以我們可以通過 AST 的方式來實現,關于 ast 的概念有很多大佬都講過了我就不啰嗦了
分析 AST
我這邊是通過 @babel/parser 來分析的,我們先看下面這段代碼在網站上的構成
import { Box } from '@material-ui/core'; import Autocomplete from '@material-ui/lab/Autocomplete';
我們可以看出路徑 program => body ,然后聲明類型是 type: ImportDeclaration,繼續看如下構成
"source": { "type": "StringLiteral", "value": "@material-ui/core" }, // 第二段 "source": { type": "StringLiteral", "value": "@material-ui/lab/Autocomplete" },
我們發下這個字段里面的 value 有我們想要的包名所以第一段代碼就是
const ast = parser.parse(source, { sourceType: 'module', plugins: ['jsx'], }); const getImport = 'ImportDeclaration'; const getMaterialImport = packageName || '@material-ui'; const importAst = ast.program.body.filter( // type 節點類型,這里我們去過濾 import 聲明類型 同時去過濾 (i) => i.type === getImport && i.source.value.includes(getMaterialImport), );
拿到相關的 ast 數組下一步就要去拿到組件名字了, 通過觀察我們發現 specifiers 標識符這個字段里面有兩個字段包含組件名:imported、local
- imported 表示從導出模塊導出的變量
- local 表示導入后當前模塊的變量
這里我取的 local, 因為下面這種方式并不會出現 imported 這個字段
import Autocomplete from '@material-ui/lab/Autocomplete';
這個時候包的名字也拿到了后面就簡單了直奔主題貼完整代碼了
demo
const parser = require('@babel/parser'); const loaderUtils = require('loader-utils'); const total = { len: 0, components: {}, }; // 對象排序 const sortable = (obj) => Object.fromEntries(Object.entries(obj).sort(([, a], [, b]) => b - a)); module.exports = function(source) { console.log(source, '--'); const options = loaderUtils.getOptions(this); const { packageName = '' } = options; const callback = this.async(); if (!packageName) return callback(null, source); try { // 解析成 ast const ast = parser.parse(source, { sourceType: 'module', plugins: ['jsx'], }); if (ast) { setTimeout(() => { const getImport = 'ImportDeclaration'; const getMaterialImport = packageName; const importAst = ast.program.body.filter( // type 節點類型,這里我們去過濾 import 聲明類型 同時去過濾 (i) => i.type === getImport && i.source.value.includes(getMaterialImport), ); total.len = total.len + importAst.length; for (let i of importAst) { const { specifiers = [] } = i; for (let s of specifiers) { if (s.local) { const { name } = s.local; total.components[name] = total.components[name] ? total.components[name] + 1 : 1; } } } total.components = sortable(total.components); console.log(total, 'total'); callback(null, source); }, 0); } else callback(null, source); } catch (error) { callback(null, source); } };
調用 loader
{ test: /\.(jsx|)$/, exclude: /node_modules/, include: [appConfig.eslintEntry], use: [ { loader: path.resolve(__dirname, './loader/total.js'), options: { packageName: '@material-ui', }, }, ], },
一個簡單的統計功能就完成了,當然可能還有其他更好的方式我只是提供這個想法,歡迎大家討論
最后
做這個的意義是什么呢,比如是我們自己的組件庫上線以后可以統計組件引用次數,并且以某個時間為維度比如說周。通過數據來分析我們組件庫下個版本的優化方向,而且也可以做 kpi 匯報手段畢竟有數據支撐
到此這篇關于50行代碼實現Webpack組件使用次數統計的文章就介紹到這了,更多相關Webpack組件次數統計內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://juejin.cn/post/6936084555476500488