@rspack/dev-server 1.0.5 → 1.0.6

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.
@@ -0,0 +1,2 @@
1
+ export declare const addResolveAlias: (name: string, aliasMap: Record<string, string>) => void;
2
+ export declare const removeResolveAlias: (name: string) => void;
package/dist/alias.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeResolveAlias = exports.addResolveAlias = void 0;
4
+ const RESOLVER_MAP = {};
5
+ const addResolveAlias = (name, aliasMap) => {
6
+ const modulePath = require.resolve(name);
7
+ if (RESOLVER_MAP[modulePath]) {
8
+ throw new Error(`Should not add resolve alias to ${name} again.`);
9
+ }
10
+ const m = require.cache[modulePath];
11
+ if (!m) {
12
+ throw new Error("Failed to resolve webpack-dev-server.");
13
+ }
14
+ RESOLVER_MAP[modulePath] = m.require.resolve;
15
+ m.require.resolve = ((id, options) => aliasMap[id] ||
16
+ RESOLVER_MAP[modulePath].apply(m.require, [
17
+ id,
18
+ options
19
+ ]));
20
+ };
21
+ exports.addResolveAlias = addResolveAlias;
22
+ const removeResolveAlias = (name) => {
23
+ const modulePath = require.resolve(name);
24
+ if (!RESOLVER_MAP[modulePath]) {
25
+ return;
26
+ }
27
+ const m = require.cache[modulePath];
28
+ if (!m) {
29
+ throw new Error("Failed to resolve webpack-dev-server");
30
+ }
31
+ if (RESOLVER_MAP[modulePath]) {
32
+ m.require.resolve = RESOLVER_MAP[modulePath];
33
+ delete RESOLVER_MAP[modulePath];
34
+ }
35
+ };
36
+ exports.removeResolveAlias = removeResolveAlias;
package/dist/server.d.ts CHANGED
@@ -20,9 +20,6 @@ export declare class RspackDevServer extends WebpackDevServer {
20
20
  webSocketServer: WebpackDevServer.WebSocketServerImplementation | undefined;
21
21
  static version: string;
22
22
  constructor(options: DevServer, compiler: Compiler | MultiCompiler);
23
- private getClientTransport;
24
23
  initialize(): Promise<void>;
25
- private setupDevMiddleware;
26
- private setupMiddlewares;
27
24
  private addAdditionalEntries;
28
25
  }
package/dist/server.js CHANGED
@@ -15,15 +15,12 @@ exports.RspackDevServer = void 0;
15
15
  */
16
16
  const node_path_1 = __importDefault(require("node:path"));
17
17
  const core_1 = require("@rspack/core");
18
- const webpack_dev_middleware_1 = __importDefault(require("webpack-dev-middleware"));
19
18
  const webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
20
19
  // @ts-ignore 'package.json' is not under 'rootDir'
21
20
  const package_json_1 = require("../package.json");
21
+ const alias_1 = require("./alias");
22
22
  const patch_1 = require("./patch");
23
23
  (0, patch_1.applyDevServerPatch)();
24
- const encodeOverlaySettings = (setting) => typeof setting === "function"
25
- ? encodeURIComponent(setting.toString())
26
- : setting;
27
24
  const getFreePort = async function getFreePort(port, host) {
28
25
  if (typeof port !== "undefined" && port !== null && port !== "auto") {
29
26
  return port;
@@ -46,60 +43,7 @@ webpack_dev_server_1.default.getFreePort = getFreePort;
46
43
  class RspackDevServer extends webpack_dev_server_1.default {
47
44
  constructor(options, compiler) {
48
45
  super(options, compiler);
49
- }
50
- getClientTransport() {
51
- // WARNING: we can't use `super.getClientTransport`,
52
- // because we doesn't had same directory structure.
53
- let clientImplementation;
54
- let clientImplementationFound = true;
55
- const isKnownWebSocketServerImplementation = this.options.webSocketServer &&
56
- typeof this.options.webSocketServer.type === "string" &&
57
- (this.options.webSocketServer.type === "ws" ||
58
- this.options.webSocketServer.type === "sockjs");
59
- let clientTransport;
60
- if (this.options.client) {
61
- if (typeof this.options.client.webSocketTransport !== "undefined") {
62
- clientTransport = this.options.client.webSocketTransport;
63
- }
64
- else if (isKnownWebSocketServerImplementation) {
65
- // @ts-expect-error: TS cannot infer webSocketServer is narrowed
66
- clientTransport = this.options.webSocketServer.type;
67
- }
68
- else {
69
- clientTransport = "ws";
70
- }
71
- }
72
- else {
73
- clientTransport = "ws";
74
- }
75
- switch (typeof clientTransport) {
76
- case "string":
77
- // could be 'sockjs', 'ws', or a path that should be required
78
- if (clientTransport === "sockjs") {
79
- clientImplementation = require.resolve("webpack-dev-server/client/clients/SockJSClient");
80
- }
81
- else if (clientTransport === "ws") {
82
- clientImplementation = require.resolve("webpack-dev-server/client/clients/WebSocketClient");
83
- }
84
- else {
85
- try {
86
- clientImplementation = require.resolve(clientTransport);
87
- }
88
- catch (e) {
89
- clientImplementationFound = false;
90
- }
91
- }
92
- break;
93
- default:
94
- clientImplementationFound = false;
95
- }
96
- if (!clientImplementationFound) {
97
- throw new Error(`${!isKnownWebSocketServerImplementation
98
- ? "When you use custom web socket implementation you must explicitly specify client.webSocketTransport. "
99
- : ""}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 `);
100
- }
101
- // @ts-expect-error
102
- return clientImplementation;
46
+ // override
103
47
  }
104
48
  async initialize() {
105
49
  const compilers = this.compiler instanceof core_1.MultiCompiler
@@ -112,231 +56,28 @@ class RspackDevServer extends webpack_dev_server_1.default {
112
56
  this.logger.warn("Hot Module Replacement (HMR) is enabled for the production build. \n" +
113
57
  "Make sure to disable HMR for production by setting `devServer.hot` to `false` in the configuration.");
114
58
  }
115
- const HMRPluginExists = compiler.options.plugins.find(p => p?.constructor === compiler.webpack.HotModuleReplacementPlugin);
116
- if (HMRPluginExists) {
117
- this.logger.warn(`"hot: true" automatically applies HMR plugin, you don't have to add it manually to your webpack configuration.`);
118
- }
119
- else {
120
- // Apply the HMR plugin
121
- const plugin = new compiler.webpack.HotModuleReplacementPlugin();
122
- plugin.apply(compiler);
123
- }
124
- // Apply modified version of `ansi-html-community`
125
59
  compiler.options.resolve.alias = {
126
60
  "ansi-html-community": node_path_1.default.resolve(__dirname, "./ansiHTML"),
127
61
  ...compiler.options.resolve.alias
128
62
  };
129
63
  }
130
64
  }
131
- if (this.options.webSocketServer) {
132
- for (const compiler of compilers) {
133
- this.addAdditionalEntries(compiler);
134
- new compiler.webpack.ProvidePlugin({
135
- __webpack_dev_server_client__: this.getClientTransport()
136
- }).apply(compiler);
137
- }
138
- }
139
- // @ts-expect-error: `setupHooks` is private function in base class.
140
- this.setupHooks();
141
- // @ts-expect-error: `setupApp` is private function in base class.
142
- this.setupApp();
143
- // @ts-expect-error: `setupHostHeaderCheck` is private function in base class.
144
- this.setupHostHeaderCheck();
145
- this.setupDevMiddleware();
146
- // @ts-expect-error: `setupBuiltInRoutes` is private function in base class.
147
- this.setupBuiltInRoutes();
148
- // @ts-expect-error: `setupWatchFiles` is private function in base class.
149
- this.setupWatchFiles();
150
- // @ts-expect-error: `setupWatchStaticFiles` is private function in base class.
151
- this.setupWatchStaticFiles();
152
- this.setupMiddlewares();
153
- // @ts-expect-error: `createServer` is private function in base class.
154
- this.createServer();
155
- if (this.options.setupExitSignals) {
156
- const signals = ["SIGINT", "SIGTERM"];
157
- let needForceShutdown = false;
158
- for (const signal of signals) {
159
- const listener = () => {
160
- if (needForceShutdown) {
161
- process.exit();
162
- }
163
- this.logger.info("Gracefully shutting down. To force exit, press ^C again. Please wait...");
164
- needForceShutdown = true;
165
- this.stopCallback(() => {
166
- if (typeof this.compiler.close === "function") {
167
- this.compiler.close(() => {
168
- process.exit();
169
- });
170
- }
171
- else {
172
- process.exit();
173
- }
174
- });
175
- };
176
- // @ts-expect-error: `listeners` is private function in base class.
177
- this.listeners.push({ name: signal, listener });
178
- process.on(signal, listener);
179
- }
180
- }
181
- // Proxy WebSocket without the initial http request
182
- // https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
183
- // @ts-expect-error: `webSocketProxies` is private function in base class.
184
- for (const webSocketProxy of this.webSocketProxies) {
185
- this.server.on("upgrade", webSocketProxy.upgrade);
186
- }
187
- }
188
- setupDevMiddleware() {
189
- // @ts-expect-error
190
- this.middleware = (0, webpack_dev_middleware_1.default)(this.compiler, this.options.devMiddleware);
191
- }
192
- setupMiddlewares() {
193
- const middlewares = [];
194
- for (const middleware of middlewares) {
195
- if (typeof middleware === "function") {
196
- // @ts-expect-error
197
- this.app.use(middleware);
198
- }
199
- else if (typeof middleware.path !== "undefined") {
200
- // @ts-expect-error
201
- this.app.use(middleware.path, middleware.middleware);
202
- }
203
- else {
204
- // @ts-expect-error
205
- this.app.use(middleware.middleware);
206
- }
207
- }
208
65
  // @ts-expect-error
209
- super.setupMiddlewares();
66
+ await super.initialize();
210
67
  }
68
+ // @ts-ignore
211
69
  addAdditionalEntries(compiler) {
212
- const additionalEntries = [];
213
- // @ts-expect-error
214
- const isWebTarget = webpack_dev_server_1.default.isWebTarget(compiler);
215
- // TODO maybe empty client
216
- if (this.options.client && isWebTarget) {
217
- let webSocketURLStr = "";
218
- if (this.options.webSocketServer) {
219
- const webSocketURL = this.options.client
220
- .webSocketURL;
221
- const webSocketServer = this.options.webSocketServer;
222
- const searchParams = new URLSearchParams();
223
- let protocol;
224
- // We are proxying dev server and need to specify custom `hostname`
225
- if (typeof webSocketURL.protocol !== "undefined") {
226
- protocol = webSocketURL.protocol;
227
- }
228
- else {
229
- protocol = this.options.server.type === "http" ? "ws:" : "wss:";
230
- }
231
- searchParams.set("protocol", protocol);
232
- if (typeof webSocketURL.username !== "undefined") {
233
- searchParams.set("username", webSocketURL.username);
234
- }
235
- if (typeof webSocketURL.password !== "undefined") {
236
- searchParams.set("password", webSocketURL.password);
237
- }
238
- let hostname;
239
- // SockJS is not supported server mode, so `hostname` and `port` can't specified, let's ignore them
240
- // TODO show warning about this
241
- const isSockJSType = webSocketServer.type === "sockjs";
242
- // We are proxying dev server and need to specify custom `hostname`
243
- if (typeof webSocketURL.hostname !== "undefined") {
244
- hostname = webSocketURL.hostname;
245
- }
246
- // Web socket server works on custom `hostname`, only for `ws` because `sock-js` is not support custom `hostname`
247
- else if (typeof webSocketServer.options?.host !== "undefined" &&
248
- !isSockJSType) {
249
- hostname = webSocketServer.options.host;
250
- }
251
- // The `host` option is specified
252
- else if (typeof this.options.host !== "undefined") {
253
- hostname = this.options.host;
254
- }
255
- // The `port` option is not specified
256
- else {
257
- hostname = "0.0.0.0";
258
- }
259
- searchParams.set("hostname", hostname);
260
- let port;
261
- // We are proxying dev server and need to specify custom `port`
262
- if (typeof webSocketURL.port !== "undefined") {
263
- port = webSocketURL.port;
264
- }
265
- // Web socket server works on custom `port`, only for `ws` because `sock-js` is not support custom `port`
266
- else if (typeof webSocketServer.options?.port !== "undefined" &&
267
- !isSockJSType) {
268
- port = webSocketServer.options.port;
269
- }
270
- // The `port` option is specified
271
- else if (typeof this.options.port === "number") {
272
- port = this.options.port;
273
- }
274
- // The `port` option is specified using `string`
275
- else if (typeof this.options.port === "string" &&
276
- this.options.port !== "auto") {
277
- port = Number(this.options.port);
278
- }
279
- // The `port` option is not specified or set to `auto`
280
- else {
281
- port = "0";
282
- }
283
- searchParams.set("port", String(port));
284
- let pathname = "";
285
- // We are proxying dev server and need to specify custom `pathname`
286
- if (typeof webSocketURL.pathname !== "undefined") {
287
- pathname = webSocketURL.pathname;
288
- }
289
- // Web socket server works on custom `path`
290
- else if (typeof webSocketServer.options?.prefix !== "undefined" ||
291
- typeof webSocketServer.options?.path !== "undefined") {
292
- pathname =
293
- webSocketServer.options.prefix || webSocketServer.options.path;
294
- }
295
- searchParams.set("pathname", pathname);
296
- const client = /** @type {ClientConfiguration} */ this.options.client;
297
- if (typeof client.logging !== "undefined") {
298
- searchParams.set("logging", client.logging);
299
- }
300
- if (typeof client.progress !== "undefined") {
301
- searchParams.set("progress", String(client.progress));
302
- }
303
- if (typeof client.overlay !== "undefined") {
304
- const overlayString = typeof client.overlay === "boolean"
305
- ? String(client.overlay)
306
- : JSON.stringify({
307
- ...client.overlay,
308
- errors: encodeOverlaySettings(client.overlay.errors),
309
- warnings: encodeOverlaySettings(client.overlay.warnings),
310
- runtimeErrors: encodeOverlaySettings(client.overlay.runtimeErrors)
311
- });
312
- searchParams.set("overlay", overlayString);
313
- }
314
- if (typeof client.reconnect !== "undefined") {
315
- searchParams.set("reconnect", typeof client.reconnect === "number"
316
- ? String(client.reconnect)
317
- : "10");
318
- }
319
- if (typeof this.options.hot !== "undefined") {
320
- searchParams.set("hot", String(this.options.hot));
321
- }
322
- if (typeof this.options.liveReload !== "undefined") {
323
- searchParams.set("live-reload", String(this.options.liveReload));
324
- }
325
- webSocketURLStr = searchParams.toString();
326
- }
327
- additionalEntries.push(`${require.resolve("@rspack/dev-server/client/index")}?${webSocketURLStr}`);
328
- }
329
- if (this.options.hot === "only") {
330
- additionalEntries.push(require.resolve("@rspack/core/hot/only-dev-server"));
331
- }
332
- else if (this.options.hot) {
333
- additionalEntries.push(require.resolve("@rspack/core/hot/dev-server"));
70
+ (0, alias_1.addResolveAlias)("webpack-dev-server", {
71
+ "../client/index.js": require.resolve("@rspack/dev-server/client/index"),
72
+ "webpack/hot/only-dev-server": require.resolve("@rspack/core/hot/only-dev-server"),
73
+ "webpack/hot/dev-server": require.resolve("@rspack/core/hot/dev-server")
74
+ });
75
+ try {
76
+ // @ts-expect-error
77
+ super.addAdditionalEntries(compiler);
334
78
  }
335
- const webpack = compiler.webpack;
336
- for (const additionalEntry of additionalEntries) {
337
- new webpack.EntryPlugin(compiler.context, additionalEntry, {
338
- name: undefined
339
- }).apply(compiler);
79
+ finally {
80
+ (0, alias_1.removeResolveAlias)("webpack-dev-server");
340
81
  }
341
82
  }
342
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/dev-server",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "license": "MIT",
5
5
  "description": "Development server for rspack",
6
6
  "main": "./dist/index.js",
@@ -48,8 +48,8 @@
48
48
  "tcp-port-used": "^1.0.2",
49
49
  "typescript": "5.0.2",
50
50
  "wait-for-expect": "^3.0.2",
51
- "@rspack/dev-server": "1.0.5",
52
- "@rspack/core": "1.0.5"
51
+ "@rspack/core": "1.0.6",
52
+ "@rspack/dev-server": "1.0.6"
53
53
  },
54
54
  "dependencies": {
55
55
  "chokidar": "^3.6.0",