UNPKG

4.09 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3
4const generateFilemap = require('./generate-filemap-from-compilation')
5const getChunkmapPath = require('./get-chunkmap-path')
6const getDistPath = require('./get-dist-path')
7
8const 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
17const isNotSourcemap = (filename) => (
18 !/\.(js|css)\.map$/i.test(filename)
19)
20
21const getFilePathname = (dirname, file) => {
22 if (process.env.WEBPACK_BUILD_ENV === 'dev') return file
23 return `${dirname}/${file}`
24}
25
26const 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 * 写入打包文件对应表 (chunkmap)
42 * @param {*} stats
43 * @param {*} localeId
44 * @returns {Object} 打包文件对应表 (chunkmap)
45 */
46module.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 // const dirRelative = path.relative(getDistPath(), stats.compilation.outputOptions.path).replace(`\\`, '/')
55 const dirRelative = path.relative(getDistPath(), stats.outputPath).replace(`\\`, '/')
56 const filepathname = getChunkmapPath()
57 // stats.compilation.outputOptions.path,
58
59 fs.ensureFileSync(filepathname)
60
61 // for (let key in stats.compilation) {
62 // console.log(key)
63 // }
64
65 // 生成入口对照表
66 // if (stats.compilation.entrypoints) {
67 // stats.compilation.entrypoints.forEach((value, key) => {
68 // entryChunks[key] = []
69 // value.chunks.forEach(chunk => {
70 // if (Array.isArray(chunk.files))
71 // chunk.files
72 // .filter(file => isNotSourcemap(file))
73 // .forEach(file =>
74 // entryChunks[key].push(getFilePathname(dirRelative, file))
75 // )
76 // })
77 // })
78 // chunkmap['.entrypoints'] = entryChunks
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 // console.log(stats.chunks)
100 // for (let id in stats.compilation.chunks) {
101 // const o = stats.compilation.chunks[id]
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}