@rspack/dev-server 0.0.21 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/LICENSE +2 -1
  3. package/dist/config.d.ts +20 -14
  4. package/dist/config.d.ts.map +1 -1
  5. package/dist/config.js +0 -52
  6. package/dist/config.js.map +1 -1
  7. package/dist/server.d.ts +14 -41
  8. package/dist/server.d.ts.map +1 -1
  9. package/dist/server.js +330 -257
  10. package/dist/server.js.map +1 -1
  11. package/jest.config.js +3 -1
  12. package/package.json +14 -12
  13. package/src/config.ts +22 -67
  14. package/src/server.ts +374 -311
  15. package/tests/__snapshots__/normalizeOptions.test.ts.snap +164 -26
  16. package/tests/e2e/hot-reaload.test.ts +35 -39
  17. package/tests/e2e-fixtures/react/node_modules/scheduler/LICENSE +21 -0
  18. package/tests/e2e-fixtures/react/node_modules/scheduler/README.md +9 -0
  19. package/tests/e2e-fixtures/react/node_modules/scheduler/cjs/scheduler-unstable_mock.development.js +700 -0
  20. package/tests/e2e-fixtures/react/node_modules/scheduler/cjs/scheduler-unstable_mock.production.min.js +20 -0
  21. package/tests/e2e-fixtures/react/node_modules/scheduler/cjs/scheduler-unstable_post_task.development.js +207 -0
  22. package/tests/e2e-fixtures/react/node_modules/scheduler/cjs/scheduler-unstable_post_task.production.min.js +14 -0
  23. package/tests/e2e-fixtures/react/node_modules/scheduler/cjs/scheduler.development.js +634 -0
  24. package/tests/e2e-fixtures/react/node_modules/scheduler/cjs/scheduler.production.min.js +19 -0
  25. package/tests/e2e-fixtures/react/node_modules/scheduler/index.js +7 -0
  26. package/tests/e2e-fixtures/react/node_modules/scheduler/package.json +36 -0
  27. package/tests/e2e-fixtures/react/node_modules/scheduler/umd/scheduler-unstable_mock.development.js +699 -0
  28. package/tests/e2e-fixtures/react/node_modules/scheduler/umd/scheduler-unstable_mock.production.min.js +19 -0
  29. package/tests/e2e-fixtures/react/node_modules/scheduler/umd/scheduler.development.js +152 -0
  30. package/tests/e2e-fixtures/react/node_modules/scheduler/umd/scheduler.production.min.js +146 -0
  31. package/tests/e2e-fixtures/react/node_modules/scheduler/umd/scheduler.profiling.min.js +146 -0
  32. package/tests/e2e-fixtures/react/node_modules/scheduler/unstable_mock.js +7 -0
  33. package/tests/e2e-fixtures/react/node_modules/scheduler/unstable_post_task.js +7 -0
  34. package/tests/e2e-fixtures/react/package.json +2 -0
  35. package/tests/e2e-fixtures/react/webpack.config.js +6 -2
  36. package/tests/helpers/emitFile.ts +58 -4
  37. package/tests/normalizeOptions.test.ts +50 -16
  38. package/tsconfig.tsbuildinfo +1 -1
  39. package/dist/logger.d.ts +0 -7
  40. package/dist/logger.d.ts.map +0 -1
  41. package/dist/logger.js +0 -28
  42. package/dist/logger.js.map +0 -1
  43. package/dist/ws.d.ts +0 -13
  44. package/dist/ws.d.ts.map +0 -1
  45. package/dist/ws.js +0 -12
  46. package/dist/ws.js.map +0 -1
  47. package/src/logger.ts +0 -30
  48. package/src/ws.ts +0 -18
package/dist/server.js CHANGED
@@ -1,98 +1,169 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
29
  exports.RspackDevServer = void 0;
30
+ const core_1 = require("@rspack/core");
31
+ const dev_middleware_1 = __importStar(require("@rspack/dev-middleware"));
7
32
  const fs_1 = __importDefault(require("fs"));
8
- const chokidar_1 = __importDefault(require("chokidar"));
9
- const http_1 = __importDefault(require("http"));
10
- const logger_1 = require("./logger");
11
33
  const webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
12
- const express_1 = __importDefault(require("express"));
13
- const dev_middleware_1 = __importDefault(require("@rspack/dev-middleware"));
14
- const ws_1 = require("./ws");
15
- const config_1 = require("./config");
16
- // copy from webpack-dev-server
17
- class RspackDevServer {
18
- constructor(compiler) {
19
- this.compiler = compiler;
20
- this.logger = (0, logger_1.createLogger)("rspack-dev-server");
21
- this.staticWatchers = [];
22
- this.listeners = [];
23
- this.sockets = [];
24
- this.currentHash = "";
25
- this.options = this.normalizeOptions(compiler.options.devServer);
26
- this.rewriteCompilerOptions();
27
- this.addAdditionEntires();
28
- }
29
- normalizeOptions(dev = {}) {
30
- return (0, config_1.resolveDevOptions)(dev, this.compiler.options);
31
- }
32
- rewriteCompilerOptions() {
33
- var _a, _b;
34
- this.compiler.options.devServer = this.options;
35
- if (!this.compiler.options.builtins.react) {
36
- this.compiler.options.builtins.react = {};
37
- }
38
- this.compiler.options.builtins.react.development =
39
- (_a = this.compiler.options.builtins.react.development) !== null && _a !== void 0 ? _a : true;
40
- if (this.options.hot) {
41
- this.compiler.options.builtins.react.refresh =
42
- (_b = this.compiler.options.builtins.react.refresh) !== null && _b !== void 0 ? _b : true;
43
- }
34
+ class RspackDevServer extends webpack_dev_server_1.default {
35
+ constructor(options, compiler) {
36
+ // @ts-expect-error
37
+ super(options, compiler);
44
38
  }
45
- addAdditionEntires() {
39
+ addAdditionEntires(compiler) {
46
40
  var _a;
47
- const entries = [];
41
+ const additionalEntries = [];
42
+ const isWebTarget = isWebTarget2(compiler);
43
+ if (this.options.client && isWebTarget) {
44
+ let webSocketURLStr = "";
45
+ if (this.options.webSocketServer) {
46
+ const webSocketURL = this.options.client
47
+ .webSocketURL;
48
+ const webSocketServer = this.options.webSocketServer;
49
+ const searchParams = new URLSearchParams();
50
+ let protocol;
51
+ // We are proxying dev server and need to specify custom `hostname`
52
+ if (typeof webSocketURL.protocol !== "undefined") {
53
+ protocol = webSocketURL.protocol;
54
+ }
55
+ else {
56
+ protocol = this.options.server.type === "http" ? "ws:" : "wss:";
57
+ }
58
+ searchParams.set("protocol", protocol);
59
+ if (typeof webSocketURL.username !== "undefined") {
60
+ searchParams.set("username", webSocketURL.username);
61
+ }
62
+ if (typeof webSocketURL.password !== "undefined") {
63
+ searchParams.set("password", webSocketURL.password);
64
+ }
65
+ let hostname;
66
+ // SockJS is not supported server mode, so `hostname` and `port` can't specified, let's ignore them
67
+ // TODO show warning about this
68
+ const isSockJSType = webSocketServer.type === "sockjs";
69
+ // We are proxying dev server and need to specify custom `hostname`
70
+ if (typeof webSocketURL.hostname !== "undefined") {
71
+ hostname = webSocketURL.hostname;
72
+ }
73
+ // Web socket server works on custom `hostname`, only for `ws` because `sock-js` is not support custom `hostname`
74
+ else if (typeof webSocketServer.options.host !== "undefined" &&
75
+ !isSockJSType) {
76
+ hostname = webSocketServer.options.host;
77
+ }
78
+ // The `host` option is specified
79
+ else if (typeof this.options.host !== "undefined") {
80
+ hostname = this.options.host;
81
+ }
82
+ // The `port` option is not specified
83
+ else {
84
+ hostname = "0.0.0.0";
85
+ }
86
+ searchParams.set("hostname", hostname);
87
+ let port;
88
+ // We are proxying dev server and need to specify custom `port`
89
+ if (typeof webSocketURL.port !== "undefined") {
90
+ port = webSocketURL.port;
91
+ }
92
+ // Web socket server works on custom `port`, only for `ws` because `sock-js` is not support custom `port`
93
+ else if (typeof webSocketServer.options.port !== "undefined" &&
94
+ !isSockJSType) {
95
+ port = webSocketServer.options.port;
96
+ }
97
+ // The `port` option is specified
98
+ else if (typeof this.options.port === "number") {
99
+ port = this.options.port;
100
+ }
101
+ // The `port` option is specified using `string`
102
+ else if (typeof this.options.port === "string" &&
103
+ this.options.port !== "auto") {
104
+ port = Number(this.options.port);
105
+ }
106
+ // The `port` option is not specified or set to `auto`
107
+ else {
108
+ port = "0";
109
+ }
110
+ searchParams.set("port", String(port));
111
+ let pathname = "";
112
+ // We are proxying dev server and need to specify custom `pathname`
113
+ if (typeof webSocketURL.pathname !== "undefined") {
114
+ pathname = webSocketURL.pathname;
115
+ }
116
+ // Web socket server works on custom `path`
117
+ else if (typeof webSocketServer.options.prefix !== "undefined" ||
118
+ typeof webSocketServer.options.path !== "undefined") {
119
+ pathname =
120
+ webSocketServer.options.prefix || webSocketServer.options.path;
121
+ }
122
+ searchParams.set("pathname", pathname);
123
+ const client = /** @type {ClientConfiguration} */ this.options.client;
124
+ if (typeof client.logging !== "undefined") {
125
+ searchParams.set("logging", client.logging);
126
+ }
127
+ if (typeof client.progress !== "undefined") {
128
+ searchParams.set("progress", String(client.progress));
129
+ }
130
+ if (typeof client.overlay !== "undefined") {
131
+ searchParams.set("overlay", typeof client.overlay === "boolean"
132
+ ? String(client.overlay)
133
+ : JSON.stringify(client.overlay));
134
+ }
135
+ if (typeof client.reconnect !== "undefined") {
136
+ searchParams.set("reconnect", typeof client.reconnect === "number"
137
+ ? String(client.reconnect)
138
+ : "10");
139
+ }
140
+ if (typeof this.options.hot !== "undefined") {
141
+ searchParams.set("hot", String(this.options.hot));
142
+ }
143
+ if (typeof this.options.liveReload !== "undefined") {
144
+ searchParams.set("live-reload", String(this.options.liveReload));
145
+ }
146
+ webSocketURLStr = searchParams.toString();
147
+ }
148
+ // TODO: should use providerPlugin
149
+ additionalEntries.push(this.getClientTransport());
150
+ additionalEntries.push(`${require.resolve("@rspack/dev-client")}?${webSocketURLStr}`);
151
+ }
48
152
  if (this.options.hot) {
49
153
  const hotUpdateEntryPath = require.resolve("@rspack/dev-client/devServer");
50
- entries.push(hotUpdateEntryPath);
51
- if ((_a = this.compiler.options.builtins.react) === null || _a === void 0 ? void 0 : _a.refresh) {
154
+ additionalEntries.push(hotUpdateEntryPath);
155
+ if ((_a = compiler.options.builtins.react) === null || _a === void 0 ? void 0 : _a.refresh) {
52
156
  const reactRefreshEntryPath = require.resolve("@rspack/dev-client/react-refresh");
53
- entries.push(reactRefreshEntryPath);
157
+ additionalEntries.push(reactRefreshEntryPath);
54
158
  }
55
159
  }
56
- const devClientEntryPath = require.resolve("@rspack/dev-client");
57
- entries.push(devClientEntryPath);
58
- for (const key in this.compiler.options.entry) {
59
- this.compiler.options.entry[key].import.unshift(...entries);
60
- }
61
- }
62
- static isAbsoluteURL(URL) {
63
- return webpack_dev_server_1.default.isAbsoluteURL(URL);
64
- }
65
- static findIp(gateway) {
66
- return webpack_dev_server_1.default.findIp(gateway);
67
- }
68
- static async internalIP(family) {
69
- return webpack_dev_server_1.default.internalIP(family);
70
- }
71
- static async internalIPSync(family) {
72
- return webpack_dev_server_1.default.internalIPSync(family);
73
- }
74
- static async getHostname(hostname) {
75
- return webpack_dev_server_1.default.getHostname(hostname);
76
- }
77
- static async getFreePort(port, host) {
78
- return webpack_dev_server_1.default.getFreePort(port, host);
79
- }
80
- static findCacheDir() {
81
- // TODO: we need remove the `webpack-dev-server` tag in WebpackDevServer;
82
- return "";
83
- }
84
- getCompilerOptions() {
85
- return this.compiler.options;
86
- }
87
- sendMessage(clients, type, data, params) {
88
- for (const client of clients) {
89
- if (client.readyState === 1) {
90
- client.send(JSON.stringify({ type, data, params }));
91
- }
160
+ for (const key in compiler.options.entry) {
161
+ compiler.options.entry[key].import.unshift(...additionalEntries);
92
162
  }
93
163
  }
94
164
  watchFiles(watchPath, watchOptions) {
95
- const watcher = chokidar_1.default.watch(watchPath, watchOptions);
165
+ const chokidar = require("chokidar");
166
+ const watcher = chokidar.watch(watchPath, watchOptions);
96
167
  // disabling refreshing on changing the content
97
168
  if (this.options.liveReload) {
98
169
  // TODO: remove this after we had memory filesystem
@@ -107,213 +178,191 @@ class RspackDevServer {
107
178
  }
108
179
  this.staticWatchers.push(watcher);
109
180
  }
110
- invalidate(callback = () => { }) {
111
- if (this.middleware) {
112
- this.middleware.invalidate(callback);
181
+ getClientTransport() {
182
+ // WARNING: we can't use `super.getClientTransport`,
183
+ // because we doesn't had same directory structure.
184
+ // and TODO: we need impelement `webpack.providerPlugin`
185
+ let clientImplementation;
186
+ let clientImplementationFound = true;
187
+ const isKnownWebSocketServerImplementation = this.options.webSocketServer &&
188
+ typeof this.options.webSocketServer.type === "string" &&
189
+ (this.options.webSocketServer.type === "ws" ||
190
+ this.options.webSocketServer.type === "sockjs");
191
+ let clientTransport;
192
+ if (this.options.client) {
193
+ if (
194
+ // @ts-ignore
195
+ typeof this.options.client.webSocketTransport !== "undefined") {
196
+ // @ts-ignore
197
+ clientTransport = this.options.client.webSocketTransport;
198
+ }
199
+ else if (isKnownWebSocketServerImplementation) {
200
+ // @ts-ignore
201
+ clientTransport = this.options.webSocketServer.type;
202
+ }
203
+ else {
204
+ clientTransport = "ws";
205
+ }
206
+ }
207
+ else {
208
+ clientTransport = "ws";
113
209
  }
210
+ switch (typeof clientTransport) {
211
+ case "string":
212
+ // could be 'sockjs', 'ws', or a path that should be required
213
+ if (clientTransport === "sockjs") {
214
+ clientImplementation = require.resolve("@rspack/dev-client/clients/SockJSClient");
215
+ }
216
+ else if (clientTransport === "ws") {
217
+ clientImplementation = require.resolve("@rspack/dev-client/clients/WebSocketClient");
218
+ }
219
+ else {
220
+ try {
221
+ clientImplementation = require.resolve(clientTransport);
222
+ throw Error("Do not support custom ws client now");
223
+ }
224
+ catch (e) {
225
+ clientImplementationFound = false;
226
+ }
227
+ }
228
+ break;
229
+ default:
230
+ clientImplementationFound = false;
231
+ }
232
+ if (!clientImplementationFound) {
233
+ throw new Error(`${!isKnownWebSocketServerImplementation
234
+ ? "When you use custom web socket implementation you must explicitly specify client.webSocketTransport. "
235
+ : ""}client.webSocketTransport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class `);
236
+ }
237
+ return clientImplementation;
114
238
  }
115
- async start() {
239
+ async initialize() {
240
+ const compilers = this.compiler instanceof core_1.MultiCompiler
241
+ ? this.compiler.compilers
242
+ : [this.compiler];
243
+ compilers.forEach(compiler => {
244
+ var _a, _b, _c;
245
+ var _d, _e, _f;
246
+ (_a = (_d = compiler.options.builtins).react) !== null && _a !== void 0 ? _a : (_d.react = {});
247
+ if (this.options.hot) {
248
+ (_b = (_e = compiler.options.builtins.react).refresh) !== null && _b !== void 0 ? _b : (_e.refresh = true);
249
+ (_c = (_f = compiler.options.builtins.react).development) !== null && _c !== void 0 ? _c : (_f.development = true);
250
+ }
251
+ else if (compiler.options.builtins.react.refresh) {
252
+ this.logger.warn("builtins.react.refresh needs builtins.react.development and devServer.hot enabled");
253
+ }
254
+ });
255
+ if (this.options.webSocketServer) {
256
+ compilers.forEach(compiler => {
257
+ this.addAdditionEntires(compiler);
258
+ });
259
+ }
116
260
  this.setupHooks();
261
+ // @ts-expect-error: `setupApp` is private function in base class.
117
262
  this.setupApp();
118
- this.createServer();
119
- this.setupWatchStaticFiles();
120
- this.createWebsocketServer();
263
+ // @ts-expect-error: `setupHostHeaderCheck` is private function in base class.
264
+ this.setupHostHeaderCheck();
121
265
  this.setupDevMiddleware();
266
+ // @ts-expect-error: `setupBuiltInRoutes` is private function in base class.
267
+ this.setupBuiltInRoutes();
268
+ // @ts-expect-error: `setupWatchFiles` is private function in base class.
269
+ this.setupWatchFiles();
270
+ // @ts-expect-error: `setupWatchStaticFiles` is private function in base class.
271
+ this.setupWatchStaticFiles();
122
272
  this.setupMiddlewares();
123
- const host = await RspackDevServer.getHostname(this.options.host);
124
- const port = await RspackDevServer.getFreePort(this.options.port, host);
125
- this.options.port = port;
126
- await new Promise(resolve => this.server.listen({
127
- port,
128
- host
129
- }, () => {
130
- this.logger.info(`Loopback: http://localhost:${port}`);
131
- let internalIPv4 = webpack_dev_server_1.default.internalIPSync("v4");
132
- this.logger.info(`Your Network (IPV4) http://${internalIPv4}:${port}`);
133
- resolve({});
134
- }));
135
- }
136
- startCallback(callback) {
137
- throw new Error("Method not implemented.");
138
- }
139
- stopCallback(callback) {
140
- throw new Error("Method not implemented.");
141
- }
142
- listen(port, hostname, fn) {
143
- throw new Error("Method not implemented.");
144
- }
145
- close(callback) {
146
- throw new Error("Method not implemented.");
147
- }
148
- async stop() {
149
- await Promise.all(this.staticWatchers.map(watcher => watcher.close()));
150
- this.middleware = null;
151
- this.staticWatchers = [];
152
- if (this.server) {
153
- this.server.close();
154
- }
155
- if (this.webSocketServer) {
156
- await new Promise(resolve => {
157
- this.webSocketServer.implementation.close(() => {
158
- resolve(void 0);
159
- });
160
- for (const client of this.webSocketServer.clients)
161
- client.terminate();
273
+ // @ts-expect-error: `createServer` is private function in base class.
274
+ this.createServer();
275
+ if (this.options.setupExitSignals) {
276
+ const signals = ["SIGINT", "SIGTERM"];
277
+ let needForceShutdown = false;
278
+ signals.forEach(signal => {
279
+ const listener = () => {
280
+ if (needForceShutdown) {
281
+ process.exit();
282
+ }
283
+ this.logger.info("Gracefully shutting down. To force exit, press ^C again. Please wait...");
284
+ needForceShutdown = true;
285
+ this.stopCallback(() => {
286
+ if (typeof this.compiler.close === "function") {
287
+ this.compiler.close(() => {
288
+ process.exit();
289
+ });
290
+ }
291
+ else {
292
+ process.exit();
293
+ }
294
+ });
295
+ };
296
+ // @ts-expect-error: `listeners` is private function in base class.
297
+ this.listeners.push({ name: signal, listener });
298
+ process.on(signal, listener);
162
299
  });
163
300
  }
164
- }
165
- setupApp() {
166
- this.app = (0, express_1.default)();
167
- }
168
- setupWatchStaticFiles() {
169
- if (this.options.static.watch === false) {
170
- return;
171
- }
172
- this.watchFiles(this.options.static.directory, this.options.static.watch);
301
+ // Proxy WebSocket without the initial http request
302
+ // https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
303
+ // @ts-expect-error: `webSocketProxies` is private function in base class.
304
+ this.webSocketProxies.forEach(webSocketProxy => {
305
+ this.server.on("upgrade", webSocketProxy.upgrade);
306
+ }, this);
173
307
  }
174
308
  setupDevMiddleware() {
175
309
  // @ts-ignored
176
310
  this.middleware = (0, dev_middleware_1.default)(this.compiler, this.options.devMiddleware);
177
311
  }
178
- createWebsocketServer() {
179
- if (this.options.webSocketServer !== false) {
180
- this.webSocketServer = (0, ws_1.createWebsocketServer)(this);
181
- }
182
- }
183
312
  setupMiddlewares() {
184
- const options = this.options;
185
313
  const middlewares = [];
186
- middlewares.push({
187
- name: "rdm",
188
- middleware: this.middleware
189
- });
190
- if (this.compiler.options.experiments.lazyCompilation) {
191
- middlewares.push({
192
- middleware: (req, res, next) => {
193
- if (req.url.indexOf("/lazy-compilation-web/") > -1) {
194
- const path = req.url.replace("/lazy-compilation-web/", "");
195
- if (fs_1.default.existsSync(path)) {
196
- this.compiler.rebuild([path], (error, stats) => {
197
- if (error) {
198
- throw error;
199
- }
200
- res.write("");
201
- res.end();
202
- console.log("lazy compiler success");
314
+ const compilers = this.compiler instanceof core_1.MultiCompiler
315
+ ? this.compiler.compilers
316
+ : [this.compiler];
317
+ if (Array.isArray(this.options.static)) {
318
+ this.options.static.forEach(staticOptions => {
319
+ staticOptions.publicPath.forEach(publicPath => {
320
+ compilers.forEach(compiler => {
321
+ if (compiler.options.builtins.noEmitAssets) {
322
+ middlewares.push({
323
+ name: "rspack-memory-assets",
324
+ path: publicPath,
325
+ middleware: (0, dev_middleware_1.getRspackMemoryAssets)(compiler, this.middleware)
203
326
  });
204
327
  }
205
- }
206
- }
328
+ });
329
+ });
207
330
  });
208
331
  }
209
- // Todo Add options
210
- const connectHistoryApiFallback = require("connect-history-api-fallback");
211
- middlewares.push({
212
- name: "[connect-history-api-fallback]",
213
- middleware: connectHistoryApiFallback({
214
- verbose: true,
215
- logger: console.log.bind(console)
216
- })
217
- });
218
- /**
219
- * supports three kinds of proxy configuration
220
- * {context: 'xxxx', target: 'yyy}
221
- * {['xxx']: { target: 'yyy}}
222
- * [{context: 'xxx',target:'yyy'}, {context: 'aaa', target: 'zzzz'}]
223
- */
224
- if (typeof options.proxy !== "undefined") {
225
- const { createProxyMiddleware } = require("http-proxy-middleware");
226
- function getProxyMiddleware(proxyConfig) {
227
- if (proxyConfig.target) {
228
- const context = proxyConfig.context || proxyConfig.path;
229
- return createProxyMiddleware(context, proxyConfig);
230
- }
231
- if (proxyConfig.router) {
232
- return createProxyMiddleware(proxyConfig);
233
- }
234
- }
235
- if (!Array.isArray(options.proxy)) {
236
- if (Object.prototype.hasOwnProperty.call(options.proxy, "target") ||
237
- Object.prototype.hasOwnProperty.call(options.proxy, "router")) {
238
- options.proxy = [options.proxy];
239
- }
240
- else {
241
- options.proxy = Object.keys(options.proxy).map(context => {
242
- let proxyOptions;
243
- // For backwards compatibility reasons.
244
- const correctedContext = context
245
- .replace(/^\*$/, "**")
246
- .replace(/\/\*$/, "");
247
- if (typeof ( /** @type {ProxyConfigMap} */options.proxy[context]) ===
248
- "string") {
249
- proxyOptions = {
250
- context: correctedContext,
251
- target:
252
- /** @type {ProxyConfigMap} */
253
- options.proxy[context]
254
- };
255
- }
256
- else {
257
- proxyOptions = {
258
- // @ts-ignore
259
- ... /** @type {ProxyConfigMap} */options.proxy[context]
260
- };
261
- proxyOptions.context = correctedContext;
332
+ compilers.forEach(compiler => {
333
+ if (compiler.options.experiments.lazyCompilation) {
334
+ middlewares.push({
335
+ middleware: (req, res, next) => {
336
+ if (req.url.indexOf("/lazy-compilation-web/") > -1) {
337
+ const path = req.url.replace("/lazy-compilation-web/", "");
338
+ if (fs_1.default.existsSync(path)) {
339
+ compiler.rebuild(new Set([path]), new Set(), error => {
340
+ if (error) {
341
+ throw error;
342
+ }
343
+ res.write("");
344
+ res.end();
345
+ console.log("lazy compiler success");
346
+ });
347
+ }
262
348
  }
263
- return proxyOptions;
264
- });
265
- }
266
- }
267
- options.proxy.forEach(proxyConfig => {
268
- const handler = async (req, res, next) => {
269
- let proxyMiddleware = getProxyMiddleware(proxyConfig);
270
- const isByPassFuncDefined = typeof proxyConfig.bypass === "function";
271
- const bypassUrl = isByPassFuncDefined
272
- ? await proxyConfig.bypass(req, res, proxyConfig)
273
- : null;
274
- if (typeof bypassUrl === "boolean") {
275
- req.url = null;
276
- next();
277
- }
278
- else if (typeof bypassUrl === "string") {
279
- req.url = bypassUrl;
280
- }
281
- else if (proxyMiddleware) {
282
- return proxyMiddleware(req, res, next);
283
349
  }
284
- else {
285
- next();
286
- }
287
- };
288
- middlewares.push({
289
- name: "http-proxy-middleware",
290
- middleware: handler
291
350
  });
292
- middlewares.push({
293
- name: "http-proxy-middleware-error-handler",
294
- middleware: (error, req, res, next) => handler(req, res, next)
295
- });
296
- });
297
- }
298
- const publicPath = this.compiler.options.output.publicPath === "auto"
299
- ? ""
300
- : this.compiler.options.output.publicPath;
301
- middlewares.push({
302
- name: "express-static",
303
- path: publicPath,
304
- middleware: express_1.default.static(this.options.static.directory)
351
+ }
305
352
  });
306
- middlewares.forEach(m => {
307
- if (m.path) {
308
- this.app.use(m.path, m.middleware);
353
+ middlewares.forEach(middleware => {
354
+ if (typeof middleware === "function") {
355
+ this.app.use(middleware);
356
+ }
357
+ else if (typeof middleware.path !== "undefined") {
358
+ this.app.use(middleware.path, middleware.middleware);
309
359
  }
310
360
  else {
311
- this.app.use(m.middleware);
361
+ this.app.use(middleware.middleware);
312
362
  }
313
363
  });
314
- }
315
- createServer() {
316
- this.server = http_1.default.createServer(this.app);
364
+ // @ts-expect-error
365
+ super.setupMiddlewares();
317
366
  }
318
367
  setupHooks() {
319
368
  this.compiler.hooks.done.tap("dev-server", stats => {
@@ -325,4 +374,28 @@ class RspackDevServer {
325
374
  }
326
375
  }
327
376
  exports.RspackDevServer = RspackDevServer;
377
+ // TODO: use WebpackDevServer.isWebTarget instead of this once we have a new webpack-dev-server version
378
+ function isWebTarget2(compiler) {
379
+ if (compiler.options.resolve.conditionNames &&
380
+ compiler.options.resolve.conditionNames.includes("browser")) {
381
+ return true;
382
+ }
383
+ const target = compiler.options.target;
384
+ const webTargets = [
385
+ "web",
386
+ "webworker",
387
+ "electron-preload",
388
+ "electron-renderer",
389
+ "node-webkit",
390
+ undefined,
391
+ null
392
+ ];
393
+ if (Array.isArray(target)) {
394
+ return target.some(r => webTargets.includes(r));
395
+ }
396
+ if (typeof target === "string") {
397
+ return webTargets.includes(target);
398
+ }
399
+ return false;
400
+ }
328
401
  //# sourceMappingURL=server.js.map