t-comm
Version:
专业、稳定、纯粹的工具库
81 lines (78 loc) • 2.43 kB
JavaScript
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { getMatchListFromReg } from '../regexp/regexp.mjs';
import { getFs } from '../nodejs/fs.mjs';
import { getPath } from '../nodejs/path.mjs';
function getHtmlContent(buildPath) {
var htmlPath = getPath().resolve(buildPath, 'index.html');
var content = getFs().readFileSync(htmlPath, {
encoding: 'utf-8'
});
return content;
}
/**
* 从 HTML 内容中提取入口 script/css 文件路径
* @param content HTML 文本内容
* @param domain 域名(如 https://cdn.example.com)
* @returns 入口文件路径列表
* @example
* ```ts
* const html = '<script src="https://cdn.example.com/static/js/app.js"></script>';
* getEntryFiles(html, 'https://cdn.example.com');
* // [
* // '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 = [].concat(_toConsumableArray(getMatchListFromReg(content, scriptReg)), _toConsumableArray(getMatchListFromReg(content, cssReg)));
console.log('[getEntryFiles] result', result);
return result;
}
function getBundleSize(list, buildPath) {
return list.map(function (item) {
var filePath = getPath().resolve(buildPath, item);
console.log('[getBundleSize] filePath', filePath);
var isExist = getFs().existsSync(filePath);
if (isExist) {
var stat = getFs().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(_ref) {
var domain = _ref.domain,
buildPath = _ref.buildPath;
var content = getHtmlContent(buildPath);
var fileList = getEntryFiles(content, domain);
var bundleSizeList = getBundleSize(fileList, buildPath);
return bundleSizeList;
}
export { analyzeIndexBundle, getEntryFiles };