"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDynamicKey = getDynamicKey; exports.getChunkGroupData = exports.getChunkAssetsMaps = exports.isAssetsType = exports.patchDataWithRoutes = void 0; function _react() { const data = _interopRequireDefault(require("react")); _react = function _react() { return data; }; return data; } function _lodash() { const data = require("lodash"); _lodash = function _lodash() { return data; }; return data; } var _routesToJSON = require("../routes/routesToJSON"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function getDynamicKey(route) { return route.path || '__404'; // __404 是为了配置路由的情况下的 404 页面 } /** * Patch chunkMap static resources according to routes * {} => { '/': ['.css', '.js', ...], '/bar': ['.js', '.css'] } * @param dynamicMap * @param routes * @param chunkGroupData * @param parentChunks */ const patchDataWithRoutes = (dynamicMap, routes = [], chunkGroupData, parentChunks = []) => { routes.forEach(route => { const key = getDynamicKey(route); dynamicMap[key] = dynamicMap[key] || []; const webpackChunkName = (0, _routesToJSON.normalizeEntry)(route.component || 'common_component').replace(/^src__/, '').replace(/^pages__/, 'p__').replace(/^page__/, 'p__').replace(/\$/g, '_'); const chunks = (0, _lodash().flatten)(chunkGroupData.filter(group => group.name === webpackChunkName || group.name === 'umi').map(group => group.chunks)); dynamicMap[key] = (0, _lodash().uniq)(dynamicMap[key].concat(parentChunks).concat(chunks)); patchDataWithRoutes(dynamicMap, route.routes, chunkGroupData, dynamicMap[key]); }); }; /** * is current assets Type? * @param type * @param filename */ exports.patchDataWithRoutes = patchDataWithRoutes; const isAssetsType = (type, filename) => { // for type convert const regexpMap = { js: /\.js$/, css: /\.css$/ }; const expType = typeof type === 'string' ? regexpMap[type] : type; if (!expType) { return false; } return expType.test(filename); }; /** * classify assets type * { '/': ['.css', '.js'] } => { '/': js: [], css: [] } * @param dynamicMaps */ exports.isAssetsType = isAssetsType; const getChunkAssetsMaps = dynamicMaps => Object.entries(dynamicMaps).reduce((prev, curr) => { const _curr = _slicedToArray(curr, 2), route = _curr[0], chunks = _curr[1]; if (route) { prev[route] = chunks.reduce((prevChunk, currChunk) => { if (isAssetsType('js', currChunk)) { prevChunk.js.push(currChunk); } if (isAssetsType('css', currChunk)) { prevChunk.css.push(currChunk); } return prevChunk; }, { js: [], css: [] }); } return prev; }, {}); /** * make route chunks into group * chunks => [{ name: 'umi', chunks: ['.css', '.js']* }, { name: 'p_bar', chunks: ['.css', '.js'] }] * @param chunkGroups */ exports.getChunkAssetsMaps = getChunkAssetsMaps; const getChunkGroupData = chunkGroups => chunkGroups.map(chunkGroup => { return { name: chunkGroup.name, chunks: (0, _lodash().flatten)(chunkGroup.chunks.map(chunk => { return chunk.files.filter(file => !/(\.map$)|(hot\-update\.js)/.test(file)).map(file => file); })) }; }); exports.getChunkGroupData = getChunkGroupData;