小程序vant組件按需加載

let fs = require('fs');
let path = require('path');

const vantComponentList = [
  'button',
  'cell',
  'col',
  'cell-group',
  'config-provider',
  'icon',
  'image',
  'layout',
  'popup',
  'style',
  'toast',
  'transition',
  'calendar',
  'checkbox',
  'checkbox-group',
  'datetime-picker',
  'field',
  'picker',
  'picker-column',
  'radio',
  'radio-group',
  'rate',
  'search',
  'slider',
  'stepper',
  'switch',
  'uploader',
  'action-sheet',
  'dialog',
  'dropdown-menu',
  'dropdown-item',
  'loading',
  'notify',
  'overlay',
  'share-sheet',
  'swipe-cell',
  'circle',
  'collapse',
  'collapse-item',
  'count-down',
  'divider',
  'empty',
  'notice-bar',
  'progress',
  'skeleton',
  'steps',
  'sticky',
  'tag',
  'grid',
  'index-bar',
  'index-anchor',
  'nav-bar',
  'sidebar',
  'sidebar-item',
  'tab',
  'tabbar',
  'tabbar-item',
  'tree-select',
  'area',
  'card',
  'submit-bar',
  'goods-action',
  'goods-action-button',
  'goods-action-icon',
  'panel',
];

function walkSync(currentDirPath, callback) {
  fs.readdirSync(currentDirPath, { withFileTypes: true }).forEach(function (dirent) {
    const filePath = path.join(currentDirPath, dirent.name);
    if (dirent.isFile()) {
      callback(filePath, dirent);
    } else if (dirent.isDirectory()) {
      walkSync(filePath, callback);
    }
  });
}
const useVantList = new Set();
function addUseList(filePath, stat, type = 0) {
  if (stat.name === 'index.json') {
    if (type === 0) {
      const data = fs.readFileSync(filePath, 'utf-8');
      data.match(/\@vant\/weapp\/([^\/]*)\/index/g)?.forEach(item => {
        const res = item.match(/(?<=weapp\/)[^\/]*(?=\/index)/g)[0];
        res && useVantList.add(res);
        return;
      });
    }
    if (type === 1) {
      const data = fs.readFileSync(filePath, 'utf-8');
      data.match(/\.\.\/([^\/]*)\/index/g)?.forEach(item => {
        const res = item.match(/(?<=\.\.\/)[^\/]*(?=\/index)/g)[0];
        res && useVantList.add(res);
        return;
      });
    }
  }
}
const deletePath = `./miniprogram_npm/@vant/weapp/`;
walkSync(path.join(__dirname, './pages'), addUseList);
walkSync(path.join(__dirname, './components'), addUseList);
const appData = require(path.join(__dirname, './app.json'));
appData.subpackages?.forEach(function (item) {
  walkSync(path.join(__dirname, `./${item.root}`), addUseList);
});

for (const useVantItem of useVantList) {
  if (!fs.existsSync(path.join(__dirname, deletePath + useVantItem))) {
    console.error(`缺少${useVantItem } ?? ~ 請重新構(gòu)建`);
    process.exit(0);
  }

  // 遍歷Set
  walkSync(path.join(__dirname, deletePath + useVantItem), (path, dirent) => {
    addUseList(path, dirent, 1);
  });
}
console.log('使用的vant組件', useVantList);

function deleteall(path) {
  let files = [];
  if (fs.existsSync(path)) {
    files = fs.readdirSync(path);
    files.forEach(function (file, index) {
      let curPath = `${path}/${file}`;
      if (fs.statSync(curPath).isDirectory()) {
        // recurse
        deleteall(curPath);
      } else {
        // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
}

const deleteList = vantComponentList.filter(function (item) {
  return !useVantList.has(item);
});

deleteList.forEach(item => {
  deleteall(path.join(__dirname, deletePath + item));
});

作用:運行腳本作用是刪除miniprogram_npm中未使用到的vant組件。

使用方法: 代碼保存成test.js 放到項目的根目錄下,打開對應(yīng)目錄的cmd執(zhí)行: node test.js

需要注意的是凭峡,如果添加新組件了就要重新構(gòu)建npm, 重新執(zhí)行 node test.js

轉(zhuǎn)自:https://blog.csdn.net/lgldl/article/details/127605555?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-127605555-blog-128645892.235%5Ev35%5Epc_relevant_default_base3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-127605555-blog-128645892.235%5Ev35%5Epc_relevant_default_base3&utm_relevant_index=2

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末固阁,一起剝皮案震驚了整個濱河市芝发,隨后出現(xiàn)的幾起案子泼诱,更是在濱河造成了極大的恐慌,老刑警劉巖咪奖,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盗忱,死亡現(xiàn)場離奇詭異,居然都是意外死亡羊赵,警方通過查閱死者的電腦和手機趟佃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昧捷,“玉大人闲昭,你說我怎么就攤上這事∶一樱” “怎么了序矩?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長跋破。 經(jīng)常有香客問我簸淀,道長瓶蝴,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任租幕,我火速辦了婚禮舷手,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘劲绪。我一直安慰自己男窟,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布珠叔。 她就那樣靜靜地躺著蝎宇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪祷安。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天兔乞,我揣著相機與錄音汇鞭,去河邊找鬼。 笑死庸追,一個胖子當(dāng)著我的面吹牛霍骄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播淡溯,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼读整,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了咱娶?” 一聲冷哼從身側(cè)響起米间,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎膘侮,沒想到半個月后屈糊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡琼了,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年逻锐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片雕薪。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡昧诱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出所袁,到底是詐尸還是另有隱情盏档,我是刑警寧澤,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布纲熏,位于F島的核電站妆丘,受9級特大地震影響锄俄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勺拣,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一奶赠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧药有,春花似錦毅戈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宦言,卻和暖如春扇单,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奠旺。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工蜘澜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人响疚。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓鄙信,卻偏偏與公主長得像,于是被迫代替她去往敵國和親忿晕。 傳聞我的和親對象是個殘疾皇子装诡,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容