UNPKG

t-comm

Version:

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

101 lines (94 loc) 2.81 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib_es6 = require('../tslib.es6-01322ba9.js'); var fs = require('fs'); var path = require('path'); var regexp_regexp = require('../regexp/regexp.js'); function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var fs__namespace = /*#__PURE__*/_interopNamespace(fs); var path__namespace = /*#__PURE__*/_interopNamespace(path); function getHtmlContent(buildPath) { var htmlPath = path__namespace.resolve(buildPath, 'index.html'); var content = fs__namespace.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 = tslib_es6.__spreadArray(tslib_es6.__spreadArray([], regexp_regexp.getMatchListFromReg(content, scriptReg), true), regexp_regexp.getMatchListFromReg(content, cssReg), true); console.log('[getEntryFiles] result', result); return result; } function getBundleSize(list, buildPath) { return list.map(function (item) { var filePath = path__namespace.resolve(buildPath, item); console.log('[getBundleSize] filePath', filePath); var isExist = fs__namespace.existsSync(filePath); if (isExist) { var stat = fs__namespace.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; } exports.analyzeIndexBundle = analyzeIndexBundle;