UNPKG

2.66 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3
4module.exports = async (stats, localeId) => {
5 const {
6 WEBPACK_BUILD_ENV: ENV,
7 SUPER_DIST_DIR: dist,
8 } = process.env
9
10 const chunks = {}
11
12 const times = n => f => {
13 let iter = i => {
14 if (i === n) return
15 f(i)
16 iter(i + 1)
17 }
18 return iter(0)
19 }
20
21 const getFilePathname = (file) => {
22 if (ENV === 'dev') return file
23 return `${dirRelative}/${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 const isSourcemap = (filename) => (
41 !/\.(js|css)\.map$/i.test(filename)
42 )
43
44 const dirRelative = path.relative(
45 dist,
46 stats.compilation.outputOptions.path
47 ).replace(`\\`, '/')
48
49 // for (let key in stats.compilation) {
50 // console.log(key)
51 // }
52
53 if (stats.compilation.entrypoints) {
54 const entryChunks = {}
55 stats.compilation.entrypoints.forEach((value, key) => {
56 // console.log(value, key, map)
57 entryChunks[key] = []
58 value.chunks.forEach(chunk => {
59 if (Array.isArray(chunk.files))
60 chunk.files
61 .filter(file => isSourcemap(file))
62 .forEach(file => entryChunks[key].push(getFilePathname(file)))
63 })
64 })
65 chunks['.entrypoints'] = entryChunks
66 }
67 for (let id in stats.compilation.chunks) {
68 const o = stats.compilation.chunks[id]
69 if (typeof o.name === 'undefined' || o.name === null) continue
70 chunks[o.name] = o.files
71
72 if (Array.isArray(chunks[o.name]))
73 chunks[o.name] = chunks[o.name]
74 .filter(file => isSourcemap(file))
75 .map(file => (
76 getFilePathname(file)
77 ))
78 }
79
80 const file = path.resolve(
81 // stats.compilation.outputOptions.path,
82 dist,
83 `.public-chunkmap.json`
84 )
85 let json = {}
86
87 fs.ensureFileSync(file)
88
89 if (localeId) {
90 json = fs.readJsonSync(file)
91 json[`.${localeId}`] = chunks
92 } else {
93 json = chunks
94 }
95
96 await fs.writeJsonSync(
97 file,
98 json,
99 {
100 spaces: 4
101 }
102 )
103
104 return json
105}