-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.browser.ts
147 lines (137 loc) · 4.09 KB
/
config.browser.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
import path from "path";
import { builtinModules } from "module";
import { ESBuildMinifyPlugin } from "esbuild-loader";
import type { RemixConfig } from "@remix-run/dev/dist/config";
import webpack from "webpack";
import VirtualModulesPlugin from "webpack-virtual-modules";
import * as obj from "./scripts/utils/object";
const BROWSER_ROUTE_PREFIX = "__remix_browser_route__";
const BROWSER_ROUTE_REGEX = new RegExp("/" + BROWSER_ROUTE_PREFIX);
const getBrowserRoutes = (remixConfig: RemixConfig): [string, string][] =>
obj
.entries(remixConfig.routes)
.map(([id, route]) => [
id,
path.resolve(
remixConfig.appDirectory,
path.dirname(route.file),
BROWSER_ROUTE_PREFIX + path.basename(route.file)
),
]);
const mode =
process.env.NODE_ENV === "development" ? "development" : "production";
export const createBrowserConfig = (
remixConfig: RemixConfig
): webpack.Configuration => {
const browserRoutes = getBrowserRoutes(remixConfig);
return {
mode,
devtool: mode === "development" ? "inline-cheap-source-map" : undefined,
target: "web",
resolve: {
fallback: obj.fromEntries(builtinModules.map((m) => [m, false] as const)),
alias: {
"~": remixConfig.appDirectory,
},
extensions: [".tsx", ".ts", ".jsx", ".js"],
},
entry: {
"entry.client": path.resolve(
remixConfig.appDirectory,
remixConfig.entryClientFile
),
...obj.fromEntries(browserRoutes),
},
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: true },
},
{
loader: "css-loader",
options: { modules: true },
},
],
},
{
test: /\.css$/i,
type: "asset/resource",
exclude: /\.module\.css$/i,
},
{
test: /\.server\./,
loader: require.resolve(
path.join(
__dirname,
"./scripts/compiler-webpack/loaders/empty-module-loader.ts"
)
),
},
{
test: BROWSER_ROUTE_REGEX,
loader: require.resolve(
path.join(
__dirname,
"./scripts/compiler-webpack/loaders/browser-route-loader.ts"
)
),
options: { remixConfig, browserRouteRegex: BROWSER_ROUTE_REGEX },
},
],
},
output: {
path: remixConfig.assetsBuildDirectory,
publicPath: remixConfig.publicPath,
module: true,
library: { type: "module" },
chunkFormat: "module",
chunkLoading: "import",
assetModuleFilename: "_assets/[name]-[contenthash][ext]",
cssChunkFilename: "_assets/[name]-[contenthash][ext]",
filename: "[name]-[contenthash].js",
chunkFilename: "[name]-[contenthash].js",
},
optimization: {
moduleIds: "deterministic",
runtimeChunk: "single",
// treeshake unused code in development
// needed so that browser build does not pull in server code
usedExports: true,
innerGraph: true,
splitChunks: {
chunks: "async", // not all, async as workaround
},
minimize: mode === "production",
minimizer: [new ESBuildMinifyPlugin({ target: "es2019" })],
},
externalsType: "module",
experiments: {
outputModule: true,
},
plugins: [
new VirtualModulesPlugin(
obj.fromEntries(browserRoutes.map(([, route]) => [route, ""] as const))
),
new webpack.EnvironmentPlugin({
REMIX_DEV_SERVER_WS_PORT: JSON.stringify(remixConfig.devServerPort),
}),
// shim react so it can be used without importing
new webpack.ProvidePlugin({ React: ["react"] }),
],
};
};