-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
90 lines (83 loc) · 2.47 KB
/
webpack.config.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* This config is for creating a production JS bundle for
* running in a browser as a user script.
*/
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const TerserPlugin = require("terser-webpack-plugin");
const CleanPlugin = require("clean-webpack-plugin").CleanWebpackPlugin;
const stripIndents = require("common-tags").stripIndents;
const process = require("process");
// Read the license and convert it to a JS comment
const licenseComment = fs
.readFileSync("./LICENSE", { encoding: "utf8" })
.replace(/^/gm, "// ");
// Define the user script headers
let headerDoc = yaml.safeLoad(
fs.readFileSync(path.resolve(__dirname, "headers.yaml"), {
encoding: "utf8",
})
);
// Dynamically add the source header using an environment variable.
// Intended to be used after retrieving the URL of the gist
// dynamically.
if (process.env.gistUrl) {
headerDoc.source = process.env.gistUrl;
}
// When releasing, use the GITHUB_REF env var to set the version header of the
// userscript. See:
// https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#release
if (process.env.GITHUB_REF) {
// Throws an error if GITHUB_REF doesn't include a number. It should, since
// it will be a version-based tag.
headerDoc.version = process.env.GITHUB_REF.match(/[0-9\.]+/)[0];
}
// Build the user script headers from headerDoc
// See: https://wiki.greasespot.net/Metadata_Block#Syntax
const headers = Object.entries(headerDoc).reduce((accum, entry) => {
return stripIndents`${accum}
// @${entry[0]} ${entry[1]}`;
}, "");
module.exports = {
target: "web", // The default, but included here for clarity
mode: "production",
entry: "./src/index.ts",
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
},
],
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
output: {
// Don't remove whitespace
beautify: true,
// Add the user script banner.
preamble: stripIndents`
// ==UserScript==
${headers}
// ==/UserScript==
//
${licenseComment}`,
},
},
extractComments: false,
}),
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "static", "js_dist"),
},
plugins: [new CleanPlugin()],
};