-
Notifications
You must be signed in to change notification settings - Fork 455
/
write-json-webpack-plugin.js
51 lines (44 loc) · 1.3 KB
/
write-json-webpack-plugin.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
/**
* Author: DrowsyFlesh
* Create: 2019/10/25
* Description:
*/
const path = require("path");
class WriteJsonWebpackPlugin {
constructor(options) {
this.options = options || {};
this.options.object = options.object || {};
this.options.path = options.path || "";
this.options.filename = options.filename || "timestamp.json";
this.options.pretty = options.pretty;
this.createObj = this.createObj.bind(this);
}
apply(compiler) {
if (compiler.hooks) {
compiler.hooks.emit.tapAsync(this.constructor.name, this.createObj);
} else {
// Fallback for webpack under version 4
compiler.plugin("emit", this.createObj);
}
}
createObj(compilation, callback) {
const json = JSON.stringify(
this.options.object,
null,
this.options.pretty ? 4 : null
);
const output = path.join(this.options.path, this.options.filename);
Object.assign(compilation.assets, {
[output]: {
source() {
return json;
},
size() {
return json.length;
},
},
});
callback();
}
}
module.exports = WriteJsonWebpackPlugin;