UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

90 lines (83 loc) 2.88 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray'); var nodejs_fs = require('../nodejs/fs.js'); var nodejs_path = require('../nodejs/path.js'); var regexp_regexp = require('../regexp/regexp.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray); function getHtmlContent(buildPath) { var htmlPath = nodejs_path.getPath().resolve(buildPath, 'index.html'); var content = nodejs_fs.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__default["default"](regexp_regexp.getMatchListFromReg(content, scriptReg)), _toConsumableArray__default["default"](regexp_regexp.getMatchListFromReg(content, cssReg))); console.log('[getEntryFiles] result', result); return result; } function getBundleSize(list, buildPath) { return list.map(function (item) { var filePath = nodejs_path.getPath().resolve(buildPath, item); console.log('[getBundleSize] filePath', filePath); var isExist = nodejs_fs.getFs().existsSync(filePath); if (isExist) { var stat = nodejs_fs.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; } exports.analyzeIndexBundle = analyzeIndexBundle; exports.getEntryFiles = getEntryFiles;