forked from django-webpack/webpack-bundle-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
228 lines (201 loc) · 7.19 KB
/
index.js
File metadata and controls
228 lines (201 loc) · 7.19 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// @ts-check
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").Stats} Stats */
/** @typedef {import("webpack").compilation.Compilation} Compilation */
/** @typedef {import("webpack").compilation.ContextModuleFactory} ContextModuleFactory */
/** @typedef {import("webpack").ChunkData} ChunkData */
/** @typedef {import("../typings").Contents} Contents */
/** @typedef {import("../typings").Options} Options */
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const stripAnsi = require('./utils/stripAnsi');
function getAssetPath(compilation, name) {
return path.join(compilation.getPath(compilation.compiler.outputPath), name.split('?')[0]);
}
function getSource(compilation, name) {
const path = getAssetPath(compilation, name);
return fs.readFileSync(path, { encoding: 'utf-8' });
}
/**
* Merges the provided objects, ensuring that the resulting object has its properties in sorted order.
* @template T
* @param {T} obj1 First object to merge.
* @param {Partial<T> | undefined} obj2 Second object to merge, can be undefined.
* @returns {*} A new object containing the merged properties of obj1 and obj2, with keys sorted.
*/
function mergeObjectsAndSortKeys(obj1, obj2) {
const mergedObj = Object.assign({}, obj1, obj2);
// Generates a new object with the same keys and values as mergedObj but in sorted order
const sortedKeys = Object.keys(mergedObj).sort();
return sortedKeys.reduce((acc, key) => {
acc[key] = mergedObj[key];
return acc;
}, {});
}
class BundleTrackerPlugin {
/**
* Track assets file location per bundle
* @param {Options} options
*/
constructor(options) {
/** @type {Options} */
this.options = options;
/** @type {Contents} */
this.contents = {
status: 'initialization',
assets: {},
chunks: {},
};
this.name = 'BundleTrackerPlugin';
this.outputChunkDir = '';
this.outputTrackerFile = '';
this.outputTrackerDir = '';
}
/**
* Setup parameter from compiler data
* @param {Compiler} compiler
* @returns this
*/
_setParamsFromCompiler(compiler) {
this.options = Object.assign(
{},
{
path: compiler.options.output?.path ?? process.cwd(),
publicPath: compiler.options.output?.publicPath ?? '',
filename: 'webpack-stats.json',
logTime: false,
relativePath: false,
integrity: false,
indent: 2,
// https://www.w3.org/TR/SRI/#cryptographic-hash-functions
integrityHashes: ['sha256', 'sha384', 'sha512'],
},
this.options,
);
if (this.options.filename?.includes('/')) {
throw Error(
"The `filename` shouldn't include a `/`. Please use the `path` parameter to " +
"build the directory path and use `filename` only for the file's name itself.\n" +
'TIP: you can use `path.join` to build the directory path in your config file.',
);
}
// Set output directories
const outputPath = compiler.options.output?.path ?? process.cwd();
this.outputChunkDir = path.resolve(outputPath);
// @ts-ignore: TS2345 this.options.path can't be undefined here because we set a default value above
// @ts-ignore: TS2345 this.options.filename can't be undefined here because we set a default value above
this.outputTrackerFile = path.resolve(path.join(this.options.path, this.options.filename));
this.outputTrackerDir = path.dirname(this.outputTrackerFile);
return this;
}
/**
* Write bundle tracker stats file
*
* @param {Compiler} _compiler
* @param {Partial<Contents>} contents
*/
_writeOutput(_compiler, contents) {
Object.assign(this.contents, contents, {
assets: mergeObjectsAndSortKeys(this.contents.assets, contents.assets),
chunks: mergeObjectsAndSortKeys(this.contents.chunks, contents.chunks),
});
if (this.options.publicPath) {
this.contents.publicPath = this.options.publicPath;
}
fs.mkdirSync(this.outputTrackerDir, { recursive: true, mode: 0o755 });
fs.writeFileSync(this.outputTrackerFile, JSON.stringify(this.contents, null, this.options.indent));
}
/**
* Compute hash for a content
* @param {string} content
*/
_computeIntegrity(content) {
// @ts-ignore: TS2532 this.options.integrityHashes can't be undefined here because
// we set a default value on _setParamsFromCompiler
return this.options.integrityHashes
.map(algorithm => {
const hash = crypto
.createHash(algorithm)
.update(content, 'utf8')
.digest('base64');
return `${algorithm}-${hash}`;
})
.join(' ');
}
/**
* Handle compile hook
* @param {Compiler} compiler
*/
_handleCompile(compiler) {
this._writeOutput(compiler, { status: 'compile' });
}
/**
* Handle compile hook
* @param {Compiler} compiler
* @param {Stats} stats
*/
_handleDone(compiler, stats) {
if (stats.hasErrors()) {
const findError = compilation => {
if (compilation.errors.length > 0) {
return compilation.errors[0];
}
return compilation.children.find(child => findError(child));
};
const error = findError(stats.compilation);
this._writeOutput(compiler, {
status: 'error',
error: error?.name ?? 'unknown-error',
message: stripAnsi(error['message']),
});
return;
}
/** @type {Contents} */
const output = { status: 'done', assets: {}, chunks: {} };
Object.entries(stats.compilation.assets).map(([assetName, _]) => {
const fileInfo = {
name: assetName,
path: getAssetPath(stats.compilation, assetName),
};
if (this.options.integrity === true) {
fileInfo.integrity = this._computeIntegrity(getSource(stats.compilation, assetName));
}
if (this.options.publicPath) {
if (this.options.publicPath === 'auto') {
fileInfo.publicPath = 'auto';
} else {
fileInfo.publicPath = this.options.publicPath + assetName;
}
}
if (this.options.relativePath === true) {
fileInfo.path = path.relative(this.outputChunkDir, fileInfo.path);
}
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
if (stats.compilation.assetsInfo) {
// @ts-ignore: TS2339: Property 'assetsInfo' does not exist on type 'Compilation'.
fileInfo.sourceFilename = stats.compilation.assetsInfo.get(assetName).sourceFilename;
}
output.assets[assetName] = fileInfo;
});
stats.compilation.chunkGroups.forEach(chunkGroup => {
if (!chunkGroup.isInitial()) return;
output.chunks[chunkGroup.name] = chunkGroup.getFiles();
});
if (this.options.logTime === true) {
output.startTime = stats.startTime;
output.endTime = stats.endTime;
}
this._writeOutput(compiler, output);
}
/**
* Method called by webpack to apply plugin hook
* @param {Compiler} compiler
*/
apply(compiler) {
this._setParamsFromCompiler(compiler);
compiler.hooks.compile.tap(this.name, this._handleCompile.bind(this, compiler));
compiler.hooks.done.tap(this.name, this._handleDone.bind(this, compiler));
}
}
module.exports = BundleTrackerPlugin;