t-comm
Version:
专业、稳定、纯粹的工具库
76 lines (73 loc) • 2 kB
JavaScript
import { a as __spreadArray } from '../tslib.es6-096fffdd.js';
import * as fs from 'fs';
import * as path from 'path';
import { getMatchListFromReg } from '../regexp/regexp.mjs';
function getHtmlContent(buildPath) {
var htmlPath = path.resolve(buildPath, 'index.html');
var content = fs.readFileSync(htmlPath, {
encoding: 'utf-8'
});
return content;
}
/**
*
* result示例:
*
* [
* 'static/js/chunk-vendors.59912eed.js',
* 'static/js/index.8ec239e5.js',
* 'static/index.b0707a6a.css'
* ]
*
* @ignore
*/
function getEntryFiles(content, domain) {
var scriptReg = new RegExp("<script .*?src=\"".concat(domain, "/?(.+?)\".*?/?>"), 'g');
var cssReg = new RegExp("<link .*?href=\"".concat(domain, "/?(.+?)\".*?/?>"), 'g');
var result = __spreadArray(__spreadArray([], getMatchListFromReg(content, scriptReg), true), getMatchListFromReg(content, cssReg), true);
console.log('[getEntryFiles] result', result);
return result;
}
function getBundleSize(list, buildPath) {
return list.map(function (item) {
var filePath = path.resolve(buildPath, item);
console.log('[getBundleSize] filePath', filePath);
var isExist = fs.existsSync(filePath);
if (isExist) {
var stat = fs.statSync(filePath);
return {
file: item,
size: stat.size,
time: new Date(stat.ctime).getTime()
};
}
}).filter(function (item) {
return item;
});
}
/**
* 分析首页Bundle信息
*
* @export
* @param config 配置
* @param {string} config.domain 域名
* @param {string} config.buildPath 打包路径
* @returns {*}
*
* @example
* ```ts
* analyzeIndexBundle({
* domain: '',
* buildPath: '',
* })
* ```
*/
function analyzeIndexBundle(_a) {
var domain = _a.domain,
buildPath = _a.buildPath;
var content = getHtmlContent(buildPath);
var fileList = getEntryFiles(content, domain);
var bundleSizeList = getBundleSize(fileList, buildPath);
return bundleSizeList;
}
export { analyzeIndexBundle };