UNPKG

2.22 kBJavaScriptView Raw
1const fs = require('fs-extra');
2
3const getIsI18nEnabled = require('../i18n/is-enabled');
4const getI18nType = require('../i18n/get-type');
5const getChunkmapPath = require('./get-chunkmap-path');
6const getDistPath = require('./get-dist-path');
7
8let cachedChunkmap;
9
10/**
11 * 获取打包文件对应表 (chunkmap)
12 *
13 * @param {String} [localeId] 当前语言,默认为当前语言 (i18n开启时) 或未指定 (i18n未开启时)
14 * @param {Boolean} [getFullResult = false] 仅 i18n 开启时:获取 chunkmap 全文,而非当前语言的片段
15 * @param {Boolean} [ignoreCache = false] 忽略结果缓存,强制重新获取对照表
16 * @returns {Object}
17 */
18const getChunkmap = (localeId, getFullResult = false, ignoreCache = false) => {
19 if (localeId === true) return getChunkmap(getFullResult || undefined, true);
20
21 const isI18nEnabled = getIsI18nEnabled();
22
23 if (isI18nEnabled && typeof localeId === 'undefined') {
24 try {
25 localeId = require('../index').localeId;
26 } catch (e) {}
27 }
28
29 const i18nType = getI18nType();
30 const isI18nDefault = isI18nEnabled && i18nType === 'default';
31
32 const chunkmap = (() => {
33 if (!ignoreCache && cachedChunkmap) return cachedChunkmap;
34
35 let chunkmap;
36
37 if (typeof global.chunkmap === 'object') chunkmap = global.chunkmap;
38 try {
39 chunkmap = JSON.parse(process.env.WEBPACK_CHUNKMAP);
40 } catch (e) {
41 chunkmap = false;
42 }
43 if (typeof chunkmap !== 'object' && typeof getDistPath() === 'string') {
44 chunkmap = fs.readJsonSync(getChunkmapPath());
45 if (process.env.WEBPACK_BUILD_STAGE === 'server')
46 global.chunkmap = chunkmap;
47 }
48
49 if (process.env.WEBPACK_BUILD_ENV === 'prod') cachedChunkmap = chunkmap;
50
51 return chunkmap;
52 })();
53
54 if (typeof chunkmap === 'object') {
55 // let chunkmap = fs.readJsonSync(pathChunckmap)
56 if (getFullResult) return chunkmap || {};
57 if (isI18nEnabled && isI18nDefault)
58 return chunkmap[`.${localeId}`] || {};
59 }
60
61 return chunkmap || {};
62};
63
64module.exports = getChunkmap;