50行代碼實現(xiàn)Webpack組件使用次數(shù)統(tǒng)計
背景
最近有個領導想讓我們搭組件庫,然后我就想知道目前項目中使用的三方組件庫哪些組件使用頻率最高。本來想去咨詢小伙伴,但是小伙伴太忙了,只能自己弄了。我就想能不能通過 webpack 來實現(xiàn)我的想法
效果
我們是用的 @material-ui,下面是組件使用情況

實現(xiàn)
我們知道 loader 的 source是文件的靜態(tài)字符串如下圖

最快的方案通過字符串統(tǒng)計用正則的方式一把梭,但是這樣會有問題就是如果注釋部分有的話也會被統(tǒng)計進去就不準確,所以我們可以通過 AST 的方式來實現(xiàn),關于 ast 的概念有很多大佬都講過了我就不啰嗦了
分析 AST
我這邊是通過 @babel/parser 來分析的,我們先看下面這段代碼在網(wǎng)站上的構成
import { Box } from '@material-ui/core';
import Autocomplete from '@material-ui/lab/Autocomplete';

我們可以看出路徑 program => body ,然后聲明類型是 type: ImportDeclaration,繼續(xù)看如下構成
"source": {
"type": "StringLiteral",
"value": "@material-ui/core"
},
// 第二段
"source": {
type": "StringLiteral",
"value": "@material-ui/lab/Autocomplete"
},
我們發(fā)下這個字段里面的 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 節(jié)點類型,這里我們去過濾 import 聲明類型 同時去過濾
(i) => i.type === getImport && i.source.value.includes(getMaterialImport),
);
拿到相關的 ast 數(shù)組下一步就要去拿到組件名字了, 通過觀察我們發(fā)現(xiàn) specifiers 標識符這個字段里面有兩個字段包含組件名:imported、local

- imported 表示從導出模塊導出的變量
- local 表示導入后當前模塊的變量
這里我取的 local, 因為下面這種方式并不會出現(xiàn) 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 節(jié)點類型,這里我們去過濾 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',
},
},
],
},
一個簡單的統(tǒng)計功能就完成了,當然可能還有其他更好的方式我只是提供這個想法,歡迎大家討論
最后
做這個的意義是什么呢,比如是我們自己的組件庫上線以后可以統(tǒng)計組件引用次數(shù),并且以某個時間為維度比如說周。通過數(shù)據(jù)來分析我們組件庫下個版本的優(yōu)化方向,而且也可以做 kpi 匯報手段畢竟有數(shù)據(jù)支撐
到此這篇關于50行代碼實現(xiàn)Webpack組件使用次數(shù)統(tǒng)計的文章就介紹到這了,更多相關Webpack組件次數(shù)統(tǒng)計內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript iframe中打開文件,并檢測iframe存在否
從iframe中打開文件,并檢測iframe存在否如果說只是檢測頁面存在否,直接設置target用偽協(xié)議就可以解決了...2008-12-12
JS判斷數(shù)組是否包含某元素實現(xiàn)方法匯總
這篇文章主要介紹了JS判斷數(shù)組是否包含某元素實現(xiàn)方法匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
基于javascript實現(xiàn)判斷移動終端瀏覽器版本信息
這篇文章主要介紹了基于javascript實現(xiàn)判斷移動終端瀏覽器版本信息,需要的朋友可以參考下2014-12-12
分步解析JavaScript實現(xiàn)tab選項卡自動切換功能
這篇文章主要分步解析JavaScript實現(xiàn)tab選項卡自動切換功能代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
js函數(shù)使用技巧之 setTimeout(function(){},0)
setTimeout的作用是將函數(shù)推遲第二參數(shù)設定的毫秒數(shù)后再執(zhí)行,如果是0,就意味著瀏覽器要馬上執(zhí)行該函數(shù),但是瀏覽器解析到setTimeout,雖然會"立刻"執(zhí)行2009-02-02

