-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (41 loc) · 1.68 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
const path = require('path');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
class VersionInjectorPlugin {
constructor(className = 'version-panel') {
this.className = className;
if (this.className === 'version-panel') {
fs.readFile(path.join(__dirname, 'styles.css'), 'utf8', (err, data) => {
if (err) {
return console.log(err);
}
this.styles = `<style>${data}</style>`;
});
}
}
apply(compiler) {
const packageJSON = require(path.join(compiler.options.context, 'package.json'));
this.version = packageJSON.version;
compiler.hooks.compilation.tap('VersionInjectorPlugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
'ReactRootPlugin',
(data, cb) => {
const htmlString = data.html;
const bodyIndex = htmlString.indexOf('<script');
const firstHalf = htmlString.slice(0, bodyIndex);
const secondHalf = htmlString.slice(bodyIndex, htmlString.length);
const returnData = {
html: `${firstHalf}
${this.styles || ''}
<div class="${this.className}">
${this.version}
</div>
${secondHalf}`,
};
cb(null, returnData);
}
);
});
}
}
module.exports = VersionInjectorPlugin;