-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.server.ts
100 lines (95 loc) · 2.93 KB
/
config.server.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
import path from "path";
import type { RemixConfig } from "@remix-run/dev/dist/config";
import webpack from "webpack";
import VirtualModulesPlugin from "webpack-virtual-modules";
import nodeExternals from "webpack-node-externals";
const mode =
process.env.NODE_ENV === "development" ? "development" : "production";
export const createServerConfig = (
remixConfig: RemixConfig
): webpack.Configuration => {
const entryPoint = path.resolve(remixConfig.rootDirectory, "server.ts");
const isModule = remixConfig.serverModuleFormat === "esm";
return {
mode,
devtool: mode === "development" ? "inline-cheap-source-map" : undefined,
context: remixConfig.rootDirectory,
target: "node",
resolve: {
alias: {
"~": remixConfig.appDirectory,
},
extensions: [".tsx", ".ts", ".jsx", ".js"],
},
optimization: {
moduleIds: "deterministic",
},
experiments: isModule ? { outputModule: true } : undefined,
externalsType: isModule ? "module" : undefined,
externalsPresets: { node: true },
externals: [
nodeExternals({
allowlist: [
/@remix-run\/dev\/server-build/,
// https://github.com/liady/webpack-node-externals#how-can-i-bundle-required-assets-ie-css-files-from-node_modules
/\.(?!(?:jsx?|json)$).{1,5}$/i,
],
}),
],
entry: entryPoint,
output: {
filename: path.basename(remixConfig.serverBuildPath),
library: { type: isModule ? "module" : "commonjs" },
chunkFormat: isModule ? "module" : "commonjs",
chunkLoading: isModule ? "import" : "require",
module: isModule,
path: path.dirname(remixConfig.serverBuildPath),
publicPath: remixConfig.publicPath,
assetModuleFilename: "_assets/[name]-[contenthash][ext]",
cssChunkFilename: "_assets/[name]-[contenthash][ext]",
},
module: {
rules: [
{
test: /\.[j|t]sx?$/,
loader: "esbuild-loader",
exclude: /node_modules/,
options: {
target: "es2019",
loader: "tsx",
},
},
{
test: /\.module\.css$/i,
use: [
{
loader: require.resolve(
"./scripts/compiler-webpack/loaders/remix-css-loader.ts"
),
options: { emit: false },
},
{
loader: "css-loader",
options: { modules: true },
},
],
},
{
test: /\.css$/i,
type: "asset/resource",
exclude: /\.module\.css$/i,
},
],
},
plugins: [
new VirtualModulesPlugin({
[entryPoint]: remixConfig.serverBuildTargetEntryModule,
}),
new webpack.EnvironmentPlugin({
REMIX_DEV_SERVER_WS_PORT: JSON.stringify(remixConfig.devServerPort),
NODE_ENV: JSON.stringify(mode),
}),
new webpack.ProvidePlugin({ React: ["react"] }),
],
};
};