1 | const fs = require('fs-extra');
|
2 |
|
3 | const getIsI18nEnabled = require('../i18n/is-enabled');
|
4 | const getI18nType = require('../i18n/get-type');
|
5 | const getChunkmapPath = require('./get-chunkmap-path');
|
6 | const getDistPath = require('./get-dist-path');
|
7 |
|
8 | let cachedChunkmap;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const 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 |
|
56 | if (getFullResult) return chunkmap || {};
|
57 | if (isI18nEnabled && isI18nDefault)
|
58 | return chunkmap[`.${localeId}`] || {};
|
59 | }
|
60 |
|
61 | return chunkmap || {};
|
62 | };
|
63 |
|
64 | module.exports = getChunkmap;
|