1 | const fs = require('fs-extra')
|
2 | const path = require('path')
|
3 |
|
4 | const generateFilemap = require('./generate-filemap-from-compilation')
|
5 | const getChunkmapPath = require('./get-chunkmap-path')
|
6 | const getDistPath = require('./get-dist-path')
|
7 |
|
8 | const times = n => f => {
|
9 | let iter = i => {
|
10 | if (i === n) return
|
11 | f(i)
|
12 | iter(i + 1)
|
13 | }
|
14 | return iter(0)
|
15 | }
|
16 |
|
17 | const isNotSourcemap = (filename) => (
|
18 | !/\.(js|css)\.map$/i.test(filename)
|
19 | )
|
20 |
|
21 | const getFilePathname = (dirname, file) => {
|
22 | if (process.env.WEBPACK_BUILD_ENV === 'dev') return file
|
23 | return `${dirname}/${file}`
|
24 | }
|
25 |
|
26 | const log = (obj, spaceCount = 1, deep = 2) => {
|
27 | if (typeof obj === 'object') {
|
28 | let spaces = ''
|
29 | times(spaceCount)(() => {
|
30 | spaces += ' '
|
31 | })
|
32 | for (let key in obj) {
|
33 | console.log(spaces + key)
|
34 | if (spaceCount < deep)
|
35 | log(obj[key], spaceCount + 1, deep)
|
36 | }
|
37 | }
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | module.exports = async (compilation, localeId) => {
|
47 | if (typeof compilation !== 'object') return {}
|
48 |
|
49 | const stats = compilation.getStats().toJson()
|
50 |
|
51 | const chunkmap = {}
|
52 | const entryChunks = {}
|
53 |
|
54 |
|
55 | const dirRelative = path.relative(getDistPath(), stats.outputPath).replace(`\\`, '/')
|
56 | const filepathname = getChunkmapPath()
|
57 |
|
58 |
|
59 | fs.ensureFileSync(filepathname)
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | if (typeof stats.entrypoints === 'object') {
|
81 | Object.keys(stats.entrypoints).forEach(key => {
|
82 | const { assets } = stats.entrypoints[key]
|
83 | if (!Array.isArray(assets)) return
|
84 | entryChunks[key] = []
|
85 | assets
|
86 | .filter(filename => isNotSourcemap(filename))
|
87 | .forEach(filename =>
|
88 | entryChunks[key].push(getFilePathname(dirRelative, filename))
|
89 | )
|
90 | })
|
91 | chunkmap['.entrypoints'] = entryChunks
|
92 | }
|
93 |
|
94 |
|
95 | chunkmap['.files'] = generateFilemap(compilation, dirRelative)
|
96 |
|
97 |
|
98 | if (Array.isArray(stats.chunks)) {
|
99 |
|
100 |
|
101 |
|
102 | for (let id in stats.chunks) {
|
103 | const o = stats.chunks[id]
|
104 | if (typeof o.name === 'undefined' || o.name === null) continue
|
105 | chunkmap[o.name] = o.files
|
106 |
|
107 | if (Array.isArray(o.files))
|
108 | chunkmap[o.name] = o.files
|
109 | .filter(filename => isNotSourcemap(filename))
|
110 | .map(filename => getFilePathname(dirRelative, filename))
|
111 | }
|
112 | }
|
113 |
|
114 | let json = {}
|
115 |
|
116 | if (localeId) {
|
117 | json = fs.readJsonSync(filepathname)
|
118 | json[`.${localeId}`] = chunkmap
|
119 | } else {
|
120 | json = chunkmap
|
121 | }
|
122 |
|
123 | await fs.writeJsonSync(
|
124 | filepathname,
|
125 | json,
|
126 | {
|
127 | spaces: 4
|
128 | }
|
129 | )
|
130 |
|
131 | return json
|
132 | }
|