-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
40 lines (33 loc) · 1.18 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
const fs = require('fs');
class ManifestVersionSyncPlugin {
constructor(options) {
const defaultOptions = {
manifestPath: 'manifest.json',
packagePath: 'package.json',
};
this.options = { ...defaultOptions, ...options };
}
apply(compiler) {
compiler.hooks.compilation.tap('ManifestVersionSyncPlugin', (compilation, compilationParams) => {
compilation.hooks.afterProcessAssets.tap('ManifestVersionSyncPlugin', () => {
const { packagePath, manifestPath } = this.options;
// Skip watch mode
if (compilation.options.watch || compilation.assets[manifestPath] == undefined) {
return;
}
const { version } = JSON.parse(fs.readFileSync(packagePath).toString());
const manifest = JSON.parse(compilation.assets[manifestPath].source().toString());
const content = JSON.stringify({ ...manifest, version }, undefined, 2);
compilation.assets[manifestPath] = {
source: function() {
return content;
},
size: function() {
return Buffer.byteLength(content);
},
};
});
});
}
}
module.exports = ManifestVersionSyncPlugin;