-
Notifications
You must be signed in to change notification settings - Fork 304
/
server.webpack.config.ts
141 lines (134 loc) · 3.2 KB
/
server.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
import fs from "fs/promises";
import path from "path";
import webpack from "webpack";
import nodeExternals from "webpack-node-externals";
const SERVERS = [
"anima",
"ask",
"backup",
"bob",
"chat",
["gaia-v2", "server/gaia_v2/main.ts"],
"gizmo",
"logic",
"map",
"newton",
"oob",
"sink",
"spawn",
"sync",
"task",
"trigger",
"web",
] as const;
function sourcePath(...parts: string[]) {
return path.resolve(__dirname, "src", ...parts);
}
function createEntryPoints() {
const entryPoints: Record<string, string> = {};
for (const config of SERVERS) {
const [name, entrypointPath] =
typeof config === "string"
? [config, `server/${config}/main.ts`]
: config;
entryPoints[name] = sourcePath(entrypointPath);
}
return entryPoints;
}
async function attemptBuildFromFile(...relativePath: string[]) {
try {
const buildId = (
await fs.readFile(path.join(__dirname, ...relativePath))
).toString();
if (buildId !== "local") {
return buildId;
}
} catch (error) {
// Pass through
}
}
async function getBuildId() {
return (
(await attemptBuildFromFile(".next", "BUILD_ID")) ??
(await attemptBuildFromFile("BUILD_ID")) ??
"unknown"
);
}
async function createWebpackConfig() {
return <webpack.Configuration>{
mode: "production",
// Configure NodeJS environment settings.
externalsPresets: { node: true },
node: {
global: false,
__filename: true,
__dirname: true,
},
target: "node",
// Don't include node_modules, it'll be part of the dist.
externals: [nodeExternals()],
entry: createEntryPoints(),
devtool: "inline-source-map",
context: __dirname,
cache: {
type: "filesystem",
},
optimization: {
// We don't need minimization on the server, if anything it just makes
// source maps more confusing.
minimize: false,
},
// Configure some expected defines.
plugins: [
new webpack.DefinePlugin({
"process.env.IS_SERVER": JSON.stringify(true),
"process.env.BUILD_ID": JSON.stringify(await getBuildId()),
"process.env.BUILD_TIMESTAMP": JSON.stringify(Date.now()),
}),
],
module: {
rules: [
{
test: /\.ts$/,
use: [
"thread-loader",
{
loader: "ts-loader",
options: {
configFile: "tsconfig.server.json",
transpileOnly: true,
happyPackMode: true,
},
},
],
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [".ts", "..."],
alias: {
"@/galois": sourcePath("galois/js/"),
"@/wasm/cayley": path.resolve(
__dirname,
"src/gen/cayley/impl/wasm_bundler"
),
"@": sourcePath(),
},
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist"),
},
experiments: {
futureDefaults: true,
},
// Skip all Webpack warnings around entrypoint size.
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
};
}
export default createWebpackConfig();