generated from Sv443/Userscript.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
126 lines (117 loc) · 3.57 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { exec } from "child_process";
import dotenv from "dotenv";
import { resolve } from "path";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import pkg from "./package.json" with { type: "json" };
import deps from "./dependencies.json" with { type: "json" };
dotenv.config();
/** Set to true to suppress all webpack output but errors */
const silent = true;
/** If no "mode" argument is passed to the webpack command, the value of NODE_ENV or "development" is used as a fallback */
const defaultMode = ["development", "production"].includes(process.env.NODE_ENV) ? String(process.env.NODE_ENV) : "development";
export const output = {
filename: `${pkg.userscriptName}.user.js`,
path: resolve("./dist"),
clean: true,
module: true,
};
/** @param {({ "mode": boolean })} env */
export default (env) => {
const mode = env.mode ?? defaultMode;
const externals = Object.entries(deps)
.reduce((acc, [name, value]) => {
acc[name] = value.global;
return acc;
}, {});
/** @type {import("webpack-cli").WebpackConfiguration} */
const cfg = {
entry: "./src/index.ts",
output,
mode,
target: "web",
experiments: {
outputModule: true,
},
externalsType: "var",
externals,
optimization: {
moduleIds: "named",
// since sites like greasyfork don't allow minified userscripts:
minimize: false,
minimizer: [
`...`,
new CssMinimizerPlugin(),
],
},
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
use: "ts-loader",
exclude: /node_modules/,
},
{
test: /\.(htm|html)$/i,
loader: "html-loader",
},
{
test: /\.md$/i,
use: [
{
loader: "html-loader",
},
{
loader: "markdown-loader",
},
],
},
{
test: /\.css$/i,
// test: /\.(scss|css)$/i, // replace above with this when using sass-loader
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader" },
// { loader: "sass-loader" }, // uncomment if you want to be able to import and compile .scss files (also install with `npm i -D sass-loader`)
],
},
// uncomment to import any file as a string (install with `npm i -D raw-loader`):
// {
// test: /\.txt$/i,
// use: "raw-loader",
// },
],
},
resolve: {
extensions: [
".ts",
".tsx",
".js",
],
},
// enable sourcemaps if NODE_ENV === "development"
...(mode === "development" ? { devtool: "source-map" } : {}),
...(silent ? { stats: "errors-only", } : {}),
plugins: [
new MiniCssExtractPlugin({
// name of the emitted css bundle
// if this is changed, the value of globalStylePath in post-build.ts must be changed too
filename: "global.css",
}),
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap("AfterEmitPlugin", () => {
console.log("Running post-build script...");
exec(`npm run --silent post-build -- mode=${env.mode ?? defaultMode}`, (err, stdout, stderr) => {
if(err)
console.error(`\x1b[31mError while calling post-build script:\x1b[0m\n${err}`);
stdout && process.stdout.write(stdout);
stderr && process.stderr.write(stderr);
});
});
},
},
],
};
return cfg;
};