-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
165 lines (147 loc) · 4.36 KB
/
webpack.config.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable unicorn/prefer-module */
import { HeadersProps, UserscriptPlugin } from "webpack-userscript";
import { Configuration as WebpackDevelopmentServerConfiguration } from "webpack-dev-server";
import CircularDependencyPlugin from "circular-dependency-plugin";
import ESLintPlugin from "eslint-webpack-plugin";
import ForkTSCheckerPlugin from "fork-ts-checker-webpack-plugin";
import manifest from "package.json";
import path from "node:path";
import webpack, { DefinePlugin } from "webpack";
interface Configuration extends webpack.Configuration {
devServer?: WebpackDevelopmentServerConfiguration;
}
type NodeEnvironment = "development" | "production" | undefined;
const mode: webpack.Configuration["mode"] =
(process.env.NODE_ENV as NodeEnvironment) || "production";
const isProduction = mode === "production";
const isDevelopment = process.env.NODE_ENV === "development";
const PORT = 3333;
console.log(`BUILDING IN ${mode} MODE`);
const config: Configuration = {
mode,
// don't allow any errors in production
bail: isProduction,
// use filesystem cache for speed in development
cache: isDevelopment ? { type: "filesystem" } : false,
// build fast source maps in dev
devtool: isDevelopment ? "eval-cheap-module-source-map" : "source-map",
devServer: {
port: PORT,
},
watchOptions: {
ignored: ["**/node_modules", "**/dist"],
},
entry: path.resolve(__dirname, "src", "index.ts"),
output: {
path: path.resolve(__dirname, "dist/"),
filename: "farmrpg-farmhand.user.js",
},
optimization: {
minimize: false,
splitChunks: false,
removeAvailableModules: true,
removeEmptyChunks: false,
},
resolve: {
extensions: [".js", ".ts", ".css", ".scss"],
symlinks: false,
// allow src-relative imports
alias: {
"~": path.resolve(__dirname, "src"),
},
modules: ["/node_modules", "node_modules"],
},
module: {
rules: [
{
// use the first matching rule and don't evaluate the rest
oneOf: [
{
test: /\.svg$/,
issuer: /\.ts$/,
exclude: /node_modules/,
use: ["@svgr/webpack", "url-loader"],
},
// load images by URL
{
test: /\.(gif|jpg|jpeg|png)$/,
type: "asset",
},
// compile Typescript with ts-loader
{
test: /\.ts$/,
loader: "ts-loader",
options: {
// type checking forked in plugins
transpileOnly: true,
},
},
// use file-loader for everything else
{
loader: require.resolve("file-loader"),
exclude: /\.(js|mjs|ts|html|json)$/,
options: {
name: "static/media/[contenthash].[ext]",
},
},
],
},
],
},
plugins: [
new CircularDependencyPlugin({
exclude: /node_modules/,
failOnError: true,
}) as any,
// lint source with eslint config
new ESLintPlugin({
extensions: ["js", "ts", "json"],
}),
new DefinePlugin({
__VERSION__: JSON.stringify(manifest.version),
}),
// performant type checking
new ForkTSCheckerPlugin({}),
new UserscriptPlugin({
headers(original) {
const overrides: HeadersProps = {
grant: [
"GM.deleteValue",
"GM.getValue",
"GM.listValues",
"GM.setClipboard",
"GM.setValue",
"GM.xmlHttpRequest",
],
icon: "https://www.google.com/s2/favicons?sz=64&domain=farmrpg.com",
license: "MIT",
match: ["https://farmrpg.com/*", "https://alpha.farmrpg.com/*"],
name: "Farm RPG Farmhand",
namespace: "https://github.com/anstosa/farmrpg-farmhand",
connect: ["greasyfork.org", "github.com"],
// from package.json
// description
// version
// author
// homepage
// bugs
};
const version = isDevelopment
? {
version: `${original.version}-[buildTime]`,
}
: {};
return {
...original,
...overrides,
...version,
};
},
proxyScript: {
baseURL: `http://localhost:${PORT}`,
filename: `[basename].proxy.user.js`,
},
}),
],
};
export default config;