-
Notifications
You must be signed in to change notification settings - Fork 965
Expand file tree
/
Copy pathrspack.dev.config.ts
More file actions
179 lines (153 loc) · 4.78 KB
/
Copy pathrspack.dev.config.ts
File metadata and controls
179 lines (153 loc) · 4.78 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { rspack, type Configuration } from '@rspack/core';
import type { Configuration as DevServerConfig } from '@rspack/dev-server';
import { ReactRefreshRspackPlugin } from '@rspack/plugin-react-refresh';
import { fallbacksProvidePluginConfig } from '@teambit/webpack';
import errorOverlayMiddleware from 'react-dev-utils/errorOverlayMiddleware';
import evalSourceMapMiddleware from 'react-dev-utils/evalSourceMapMiddleware';
import noopServiceWorkerMiddleware from 'react-dev-utils/noopServiceWorkerMiddleware';
import redirectServedPath from 'react-dev-utils/redirectServedPathMiddleware';
import getPublicUrlOrPath from 'react-dev-utils/getPublicUrlOrPath';
import path, { sep } from 'path';
import { html } from './html';
import {
moduleFileExtensions,
resolveAlias,
resolveFallbackDev,
cssParser,
mjsRule,
swcRule,
sourceMapRule,
fontRule,
styleRules,
} from './rspack.common';
const clientHost = process.env.WDS_SOCKET_HOST;
const clientPath = process.env.WDS_SOCKET_PATH;
const port = process.env.WDS_SOCKET_PORT;
const publicUrlOrPath = getPublicUrlOrPath(true, sep, `${sep}public`);
export interface RspackConfigWithDevServer extends Configuration {
devServer: DevServerConfig;
}
export function devConfig(workspaceDir, entryFiles, title): RspackConfigWithDevServer {
const resolveWorkspacePath = (relativePath) => path.resolve(workspaceDir, relativePath);
const host = process.env.HOST || 'localhost';
return {
mode: 'development',
devtool: 'eval-cheap-module-source-map',
// enable persistent cache
cache: true,
entry: {
main: entryFiles,
},
output: {
filename: 'static/js/[name].bundle.js',
path: resolveWorkspacePath('/'),
publicPath: publicUrlOrPath,
pathinfo: false, // faster compilation
chunkFilename: 'static/js/[name].chunk.js',
},
optimization: {
splitChunks: {
chunks: 'all',
maxSize: 2000000, // 2MB max — smaller chunks for parallel download + caching
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
priority: 10,
},
},
},
},
infrastructureLogging: {
level: 'error',
},
stats: {
errorDetails: true,
},
devServer: {
allowedHosts: 'all',
static: [
{
directory: resolveWorkspacePath(publicUrlOrPath),
staticOptions: {},
publicPath: publicUrlOrPath,
watch: false,
},
],
compress: true,
hot: true,
liveReload: false, // HMR only — liveReload causes full page reloads on HMR failure
host,
historyApiFallback: {
disableDotRule: true,
index: publicUrlOrPath,
},
client: {
overlay: { errors: true, warnings: false },
reconnect: 5,
...(clientHost || clientPath || port
? {
webSocketURL: {
hostname: clientHost,
pathname: clientPath,
port,
},
}
: {}),
},
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error('rspack-dev-server is not defined');
}
// cache JS/CSS assets in the browser so subsequent page loads are instant
middlewares.unshift((req: any, res: any, next: any) => {
if (/\.(js|css)(\?.*)?$/.test(req.url || '')) {
res.setHeader('Cache-Control', 'public, max-age=120');
}
next();
});
middlewares.push(
// @ts-ignore @types/wds mismatch
evalSourceMapMiddleware(devServer),
errorOverlayMiddleware(),
redirectServedPath(publicUrlOrPath),
noopServiceWorkerMiddleware(publicUrlOrPath)
);
return middlewares;
},
devMiddleware: {
publicPath: publicUrlOrPath.slice(0, -1),
},
},
resolve: {
extensions: moduleFileExtensions.map((ext) => `.${ext}`),
alias: resolveAlias(),
fallback: resolveFallbackDev,
},
watchOptions: {
ignored: ['**/.bit/**', '**/.git/**', '**/node_modules/.cache/**'],
poll: false, // native FS watching for the UI server
},
module: {
parser: cssParser,
rules: [
mjsRule(),
swcRule({ dev: true, refresh: true }),
sourceMapRule(),
...styleRules({ sourceMap: true }),
fontRule(),
],
},
plugins: [
new ReactRefreshRspackPlugin(),
new rspack.HtmlRspackPlugin({
inject: true,
templateContent: html(title || 'My component workspace')(),
chunks: ['main'],
filename: 'index.html',
}),
new rspack.ProvidePlugin({ process: fallbacksProvidePluginConfig.process }),
],
};
}