1 | /* eslint-disable no-console */
|
2 | const fs = require('fs-extra');
|
3 | const path = require('path');
|
4 |
|
5 | const generateFilemap = require('./generate-filemap-from-compilation');
|
6 | const getChunkmapPath = require('./get-chunkmap-path');
|
7 | const getDistPath = require('./get-dist-path');
|
8 |
|
9 | // const times = n => f => {
|
10 | // const iter = i => {
|
11 | // if (i === n) return;
|
12 | // f(i);
|
13 | // iter(i + 1);
|
14 | // };
|
15 | // return iter(0);
|
16 | // };
|
17 |
|
18 | const isNotSourcemap = filename => !/\.(js|css)\.map$/i.test(filename);
|
19 |
|
20 | // const log = (obj, spaceCount = 1, deep = 2) => {
|
21 | // if (typeof obj === 'object') {
|
22 | // let spaces = '';
|
23 | // times(spaceCount)(() => {
|
24 | // spaces += ' ';
|
25 | // });
|
26 | // for (const key in obj) {
|
27 | // console.log(spaces + key);
|
28 | // if (spaceCount < deep) log(obj[key], spaceCount + 1, deep);
|
29 | // }
|
30 | // }
|
31 | // };
|
32 |
|
33 | /**
|
34 | * 写入打包文件对应表 (chunkmap)
|
35 | * @param {*} stats
|
36 | * @param {*} localeId
|
37 | * @param {string} [pathPublic]
|
38 | * @param {string} [serviceWorkerPathname]
|
39 | * @returns {Object} 打包文件对应表 (chunkmap)
|
40 | */
|
41 | module.exports = async (
|
42 | compilation,
|
43 | localeId,
|
44 | pathPublic,
|
45 | serviceWorkerPathname
|
46 | ) => {
|
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
|
56 | .relative(getDistPath(), stats.outputPath)
|
57 | .replace(/\\/g, '/');
|
58 | const filepathname = getChunkmapPath();
|
59 | // stats.compilation.outputOptions.path,
|
60 |
|
61 | if (pathPublic) {
|
62 | const relative = path
|
63 | .relative(getDistPath(), pathPublic)
|
64 | .replace(/\\/g, '/');
|
65 | chunkmap['.public'] =
|
66 | relative +
|
67 | (relative.substr(0, relative.length - 1) === '/' ? '' : '/');
|
68 | }
|
69 |
|
70 | fs.ensureFileSync(filepathname);
|
71 |
|
72 | const getFilePathname = file => {
|
73 | if (process.env.WEBPACK_BUILD_ENV === 'dev') return file;
|
74 | // const r = path
|
75 | // .relative(
|
76 | // path.resolve(getDistPath(), '..'),
|
77 | // path.resolve(dirname, file)
|
78 | // )
|
79 | // .replace(/\\/g, '/');
|
80 | // console.log('\n', getDistPath(), stats.outputPath, {dirname, file, r})
|
81 | return path
|
82 | .relative(getDistPath(), path.resolve(stats.outputPath, file))
|
83 | .replace(/\\/g, '/');
|
84 | // return r
|
85 | };
|
86 |
|
87 | // for (let key in stats.compilation) {
|
88 | // console.log(key)
|
89 | // }
|
90 |
|
91 | // 生成入口对照表
|
92 | // if (stats.compilation.entrypoints) {
|
93 | // stats.compilation.entrypoints.forEach((value, key) => {
|
94 | // entryChunks[key] = []
|
95 | // value.chunks.forEach(chunk => {
|
96 | // if (Array.isArray(chunk.files))
|
97 | // chunk.files
|
98 | // .filter(file => isNotSourcemap(file))
|
99 | // .forEach(file =>
|
100 | // entryChunks[key].push(getFilePathname(dirRelative, file))
|
101 | // )
|
102 | // })
|
103 | // })
|
104 | // chunkmap['.entrypoints'] = entryChunks
|
105 | // }
|
106 |
|
107 | if (typeof stats.entrypoints === 'object') {
|
108 | Object.keys(stats.entrypoints).forEach(key => {
|
109 | const { assets } = stats.entrypoints[key];
|
110 | if (!Array.isArray(assets)) return;
|
111 | entryChunks[key] = [];
|
112 | assets
|
113 | .filter(filename => isNotSourcemap(filename))
|
114 | .forEach(filename =>
|
115 | entryChunks[key].push(getFilePathname(filename))
|
116 | );
|
117 | });
|
118 | chunkmap['.entrypoints'] = entryChunks;
|
119 | }
|
120 |
|
121 | // 生成文件对照表
|
122 | chunkmap['.files'] = generateFilemap(compilation, dirRelative);
|
123 |
|
124 | // 生成所有入口和代码片段所输出的文件的对照表
|
125 | if (Array.isArray(stats.chunks)) {
|
126 | // console.log(stats.chunks)
|
127 | // for (let id in stats.compilation.chunks) {
|
128 | // const o = stats.compilation.chunks[id]
|
129 | for (const id in stats.chunks) {
|
130 | const o = stats.chunks[id];
|
131 | if (typeof o.name === 'undefined' || o.name === null) continue;
|
132 | chunkmap[o.name] = o.files;
|
133 |
|
134 | if (Array.isArray(o.files))
|
135 | chunkmap[o.name] = o.files
|
136 | .filter(filename => isNotSourcemap(filename))
|
137 | .map(filename => getFilePathname(filename));
|
138 | }
|
139 | }
|
140 |
|
141 | // 添加 service-worker
|
142 | if (serviceWorkerPathname) {
|
143 | chunkmap['service-worker'] = [getFilePathname(serviceWorkerPathname)];
|
144 | }
|
145 |
|
146 | let json = {};
|
147 |
|
148 | if (localeId) {
|
149 | json = fs.readJsonSync(filepathname);
|
150 | json[`.${localeId}`] = chunkmap;
|
151 | } else {
|
152 | json = chunkmap;
|
153 | }
|
154 |
|
155 | await fs.writeJsonSync(filepathname, json, {
|
156 | spaces: 4
|
157 | });
|
158 |
|
159 | return json;
|
160 | };
|