-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
28 lines (23 loc) · 766 Bytes
/
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
const { Compilation } = require('webpack');
function GenerateJsonPlugin(filename, value, replacer, space) {
Object.assign(this, { filename, value, replacer, space });
this.plugin = { name: 'GenerateJsonPlugin' };
}
GenerateJsonPlugin.prototype.apply = function apply(compiler) {
compiler.hooks.compilation.tap(this.plugin, (compilation) => {
const json = JSON.stringify(this.value, this.replacer, this.space);
compilation.hooks.processAssets.tap(
{
name: this.plugin.name,
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
},
(assets) => {
assets[this.filename] = {
source: () => json,
size: () => json.length,
};
}
);
});
};
module.exports = GenerateJsonPlugin;