forked from PlaceNL/Userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.cjs
50 lines (45 loc) · 1.49 KB
/
webpack.config.cjs
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
const {Compilation, DefinePlugin} = require('webpack');
const {ConcatSource} = require('webpack-sources');
const fs = require('node:fs');
const commit = require('node:child_process')
.execSync('git rev-parse HEAD')
.toString().trim();
const HEADER = fs.readFileSync(`${__dirname}/src/USERSCRIPT_HEADER.txt`, 'utf-8').replace(/___GIT_HASH___/g, commit);
const UserScriptHeaderAppender = function () {
this.apply = function (compiler) {
compiler.hooks.compilation.tap('userscript-header', (compilation) => {
compilation.hooks.afterProcessAssets.tap({
name: 'userscript-header',
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
}, () => {
for (const chunk of compilation.chunks) {
for (const file of chunk.files) {
compilation.updateAsset(file, (old) => {
return new ConcatSource(HEADER, '\n', old)
});
}
}
});
});
};
}
module.exports = {
plugins: [
new DefinePlugin({
___GIT_HASH___: JSON.stringify(commit),
___WS_ENDPOINT___: JSON.stringify(process.env.WS_ENDPOINT)
}),
new UserScriptHeaderAppender()
],
module: {
rules: [
{
test: /\.css$/i,
use: ['css-loader']
}
]
},
output: {
filename: 'placenl-userscript.user.js'
}
};