UNPKG

1.83 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 const isI18nEnabled = JSON.parse(process.env.KOOT_I18N) === false ? false : true
16
17 if (isI18nEnabled && typeof localeId === 'undefined') {
18 try {
19 localeId = require('../index').localeId
20 } catch (e) { }
21 }
22
23 const i18nType = isI18nEnabled && JSON.parse(process.env.KOOT_I18N)
24 ? JSON.parse(process.env.KOOT_I18N_TYPE)
25 : undefined
26 const isI18nDefault = (isI18nEnabled && i18nType === 'default')
27
28 let chunkmap
29 if (typeof global.chunkmap === 'object') chunkmap = global.chunkmap
30 try {
31 chunkmap = JSON.parse(process.env.WEBPACK_CHUNKMAP)
32 } catch (e) {
33 chunkmap = false
34 }
35
36 if (typeof chunkmap !== 'object' && typeof getDistPath() === 'string') {
37 chunkmap = fs.readJsonSync(getChunkmapPath())
38 if (process.env.WEBPACK_BUILD_STAGE === 'server')
39 global.chunkmap = chunkmap
40 }
41
42 if (typeof chunkmap === 'object') {
43 // let chunkmap = fs.readJsonSync(pathChunckmap)
44 if (getFullResult) return chunkmap || {}
45 if (isI18nEnabled && isI18nDefault) chunkmap = chunkmap[`.${localeId}`] || {}
46 }
47
48 return chunkmap || {}
49}
50
51module.exports = getChunkmap