-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (167 loc) · 5.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const fs = require('fs').promises;
const path = require('path');
const {
tap,
tapPromise,
} = require('./lib/tap');
const getGeneratedSources = require('./lib/get-generated-source');
const getParsedModules = require('./lib/get-parsed-modules');
const getSources = require('./lib/get-sources');
const { getStatIdentifier, getParsedIdentifier } = require('./lib/identifier');
function enhanceModules(modules = [], enhancedModulesMap) {
return modules.map(({
modules: submodules,
...module
}) => {
const enhancedSubmodules = enhanceModules(submodules, enhancedModulesMap);
if (!('source' in module)) {
return {
...module,
modules: enhancedSubmodules,
};
}
const enhancedModule = enhancedModulesMap.get(module.identifier) || {};
return {
...module,
...enhancedModule,
modules: enhancedSubmodules,
};
});
}
function getCompilationHooks(compilation) {
// eslint-disable-next-line global-require, import/no-unresolved
const { NormalModule } = module.exports.webpack || require('webpack');
if (NormalModule) {
const { loader } = NormalModule.getCompilationHooks(compilation);
const { afterProcessAssets } = compilation.hooks;
return {
loader,
afterProcessAssets,
};
}
const {
normalModuleLoader: loader,
afterOptimizeChunkAssets: afterProcessAssets,
} = compilation.hooks;
return {
loader,
afterProcessAssets,
};
}
module.exports = class WebpackEnhancedStatsPlugin {
constructor({
filename = 'stats.json',
} = {}) {
this.options = {
filename,
};
this.originalSource = new Map();
this.parsedSource = new Map();
}
async apply({
hooks: {
compilation,
done,
},
context,
}) {
const plugin = {
name: 'Assets logger',
};
const [enhancedModulesMap, statsJson] = await Promise.all([
tap(
plugin,
compilation,
async (comp) => {
const { loader, afterProcessAssets } = getCompilationHooks(comp);
tap(
plugin,
loader,
(loaderContext, mod) => {
// eslint-disable-next-line no-param-reassign
loaderContext.saveOriginalSource = (source) => {
const identifier = path.relative(context, mod.resource);
this.originalSource.set(identifier, {
source,
size: source.length,
});
};
},
);
await tap(
plugin,
afterProcessAssets,
async (chunks) => {
const sources = getSources(chunks, comp);
const modules = await Promise.all(
sources.map(async ({ source: generatedSource, map }) => {
try {
const parsed = await getGeneratedSources(map, generatedSource);
return Object.entries(parsed).map(([key, { text, ranges, boundaries }]) => {
const identifier = getParsedIdentifier(key, context);
return {
identifier,
source: text,
size: text.length,
boundaries,
ranges,
};
});
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
return [];
}
}),
);
const regeneratedSourcesMap = new Map();
modules
.reduce((acc, item) => acc.concat(item), [])
.forEach((source) => {
regeneratedSourcesMap.set(source.identifier, source);
});
getParsedModules({
sources,
context,
assets: comp.assets,
regeneratedSourcesMap,
parsedModules: this.parsedSource,
});
},
);
return {
get: (key) => {
const identifier = getStatIdentifier(key, context);
const parsed = this.parsedSource.get(identifier);
const original = this.originalSource.get(identifier);
return {
parsedSize: parsed ? parsed.size : null,
parsedSource: parsed ? parsed.source : null,
originalSize: original ? original.size : null,
originalSource: original ? original.source : null,
};
},
};
},
),
tapPromise(plugin, done, (stats) => stats.toJson()),
]);
const enhancedStats = {
...statsJson,
modules: enhanceModules(statsJson.modules, enhancedModulesMap),
chunks: statsJson.chunks.map(({
modules,
...chunk
}) => ({
...chunk,
modules: enhanceModules(modules, enhancedModulesMap),
})),
};
await fs.mkdir(path.dirname(statsJson.outputPath), { recursive: true });
await fs.writeFile(
path.join(statsJson.outputPath, this.options.filename),
JSON.stringify(enhancedStats, null, 2),
);
}
};
module.exports.loader = require.resolve('./loader');