UNPKG

1.67 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const getChunkmapPath = require('./get-chunkmap-path')
3const getDistPath = require('./get-dist-path')
4
5/**
6 * 获取打包文件对应表 (chunkmap)
7 *
8 * @param {String} [localeId] 当前语言,默认为当前语言 (i18n开启时) 或未指定 (i18n未开启时)
9 * @param {Boolean} [getFullResult = false] 仅 i18n 开启时:获取 chunkmap 全文,而非当前语言的片段
10 * @returns {Object}
11 */
12const getChunkmap = (localeId, getFullResult = false) => {
13 if (localeId === true) return getChunkmap(getFullResult || undefined, true)
14
15 if (typeof localeId === 'undefined') {
16 try {
17 localeId = require('../i18n').localeId
18 } catch (e) { }
19 }
20
21 const i18nType = JSON.parse(process.env.KOOT_I18N)
22 ? JSON.parse(process.env.KOOT_I18N_TYPE)
23 : undefined
24 const isI18nDefault = (i18nType === 'default')
25
26 let chunkmap
27 if (typeof global.chunkmap === 'object') chunkmap = global.chunkmap
28 try {
29 chunkmap = JSON.parse(process.env.WEBPACK_CHUNKMAP)
30 } catch (e) {
31 chunkmap = false
32 }
33
34 if (typeof chunkmap !== 'object' && typeof getDistPath() === 'string') {
35 chunkmap = fs.readJsonSync(getChunkmapPath())
36 if (process.env.WEBPACK_BUILD_STAGE === 'server')
37 global.chunkmap = chunkmap
38 }
39
40 if (typeof chunkmap === 'object') {
41 // let chunkmap = fs.readJsonSync(pathChunckmap)
42 if (getFullResult) return chunkmap || {}
43 if (isI18nDefault) chunkmap = chunkmap[`.${localeId}`] || {}
44 }
45
46 return chunkmap || {}
47}
48
49module.exports = getChunkmap