@rsbuild/core 0.0.16 → 0.0.18

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 (49) hide show
  1. package/dist/cli/commands.d.ts +3 -0
  2. package/dist/cli/commands.js +4 -2
  3. package/dist/cli/config.d.ts +4 -0
  4. package/dist/cli/index.js +2 -1
  5. package/dist/plugins/define.js +15 -27
  6. package/dist/plugins/html.d.ts +7 -1
  7. package/dist/plugins/html.js +72 -48
  8. package/dist/plugins/index.js +0 -1
  9. package/dist/plugins/nodeAddons.js +10 -17
  10. package/dist/plugins/startUrl.js +12 -16
  11. package/dist/rspack-plugins/HtmlBasicPlugin.d.ts +19 -0
  12. package/dist/rspack-plugins/HtmlBasicPlugin.js +87 -0
  13. package/dist/rspack-provider/core/createCompiler.js +7 -5
  14. package/dist/rspack-provider/core/devMiddleware.d.ts +2 -4
  15. package/dist/rspack-provider/core/startDevServer.d.ts +1 -2
  16. package/dist/rspack-provider/core/startDevServer.js +6 -31
  17. package/dist/rspack-provider/plugins/css.js +2 -2
  18. package/dist/rspack-provider/plugins/less.js +1 -1
  19. package/dist/rspack-provider/plugins/progress.js +5 -1
  20. package/dist/rspack-provider/plugins/resolve.js +1 -2
  21. package/dist/rspack-provider/plugins/sass.js +2 -2
  22. package/dist/rspack-provider/plugins/swc.js +5 -8
  23. package/dist/rspack-provider/provider.js +5 -3
  24. package/dist/rspack-provider/shared/plugin.js +0 -1
  25. package/dist/server/constants.d.ts +6 -0
  26. package/dist/server/constants.js +49 -0
  27. package/dist/server/dev-middleware/hmr-client/createSocketUrl.d.ts +16 -0
  28. package/dist/server/dev-middleware/hmr-client/createSocketUrl.js +68 -0
  29. package/dist/server/dev-middleware/hmr-client/index.d.ts +1 -0
  30. package/dist/server/dev-middleware/hmr-client/index.js +164 -0
  31. package/dist/server/dev-middleware/index.d.ts +22 -0
  32. package/dist/server/dev-middleware/index.js +90 -0
  33. package/dist/server/dev-middleware/socketServer.d.ts +22 -0
  34. package/dist/server/dev-middleware/socketServer.js +167 -0
  35. package/dist/server/devServer.d.ts +23 -0
  36. package/dist/server/devServer.js +172 -0
  37. package/dist/server/https.d.ts +6 -0
  38. package/dist/server/https.js +50 -0
  39. package/dist/server/index.d.ts +2 -0
  40. package/dist/server/index.js +31 -0
  41. package/dist/server/prodServer.d.ts +26 -0
  42. package/dist/server/prodServer.js +133 -0
  43. package/package.json +32 -6
  44. package/static/template.html +7 -0
  45. package/types.d.ts +130 -0
  46. package/dist/plugins/assetsRetry.d.ts +0 -2
  47. package/dist/plugins/assetsRetry.js +0 -66
  48. package/dist/utils/generateMetaTags.d.ts +0 -0
  49. package/dist/utils/generateMetaTags.js +0 -1
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var socketServer_exports = {};
30
+ __export(socketServer_exports, {
31
+ default: () => SocketServer
32
+ });
33
+ module.exports = __toCommonJS(socketServer_exports);
34
+ var import_ws = __toESM(require("ws"));
35
+ var import_shared = require("@rsbuild/shared");
36
+ class SocketServer {
37
+ constructor(options) {
38
+ this.sockets = [];
39
+ this.timer = null;
40
+ this.options = options;
41
+ }
42
+ // create socket, install socket handler, bind socket event
43
+ prepare(app) {
44
+ var _a;
45
+ this.app = app;
46
+ this.wsServer = new import_ws.default.Server({
47
+ noServer: true,
48
+ path: (_a = this.options.client) == null ? void 0 : _a.path
49
+ });
50
+ this.app.on("upgrade", (req, sock, head) => {
51
+ if (!this.wsServer.shouldHandle(req)) {
52
+ return;
53
+ }
54
+ this.wsServer.handleUpgrade(req, sock, head, (connection) => {
55
+ this.wsServer.emit("connection", connection, req);
56
+ });
57
+ });
58
+ this.wsServer.on("error", (err) => {
59
+ import_shared.logger.error(err);
60
+ });
61
+ this.timer = setInterval(() => {
62
+ this.wsServer.clients.forEach((socket) => {
63
+ const extWs = socket;
64
+ if (!extWs.isAlive) {
65
+ extWs.terminate();
66
+ } else {
67
+ extWs.isAlive = false;
68
+ extWs.ping(() => {
69
+ });
70
+ }
71
+ });
72
+ }, 3e4);
73
+ this.wsServer.on("connection", (socket) => {
74
+ this.onConnect(socket);
75
+ });
76
+ }
77
+ updateStats(stats) {
78
+ this.stats = stats;
79
+ this.sendStats();
80
+ }
81
+ // write message to each socket
82
+ sockWrite(type, data) {
83
+ this.sockets.forEach((socket) => {
84
+ this.send(socket, JSON.stringify({ type, data }));
85
+ });
86
+ }
87
+ singleWrite(socket, type, data) {
88
+ this.send(socket, JSON.stringify({ type, data }));
89
+ }
90
+ close() {
91
+ this.sockets.forEach((socket) => {
92
+ socket.close();
93
+ });
94
+ if (this.timer) {
95
+ clearInterval(this.timer);
96
+ this.timer = null;
97
+ }
98
+ }
99
+ onConnect(socket) {
100
+ const connection = socket;
101
+ connection.isAlive = true;
102
+ connection.on("pong", () => {
103
+ connection.isAlive = true;
104
+ });
105
+ if (!connection) {
106
+ return;
107
+ }
108
+ this.sockets.push(connection);
109
+ connection.on("close", () => {
110
+ const idx = this.sockets.indexOf(connection);
111
+ if (idx >= 0) {
112
+ this.sockets.splice(idx, 1);
113
+ }
114
+ });
115
+ if (this.options.hot || this.options.hot === "only") {
116
+ this.singleWrite(connection, "hot");
117
+ }
118
+ if (this.options.liveReload) {
119
+ this.singleWrite(connection, "liveReload");
120
+ }
121
+ if (this.stats) {
122
+ this.sendStats(true);
123
+ }
124
+ }
125
+ // get standard stats
126
+ getStats() {
127
+ const curStats = this.stats;
128
+ if (!curStats) {
129
+ return null;
130
+ }
131
+ const defaultStats = {
132
+ all: false,
133
+ hash: true,
134
+ assets: true,
135
+ warnings: true,
136
+ errors: true,
137
+ errorDetails: false
138
+ };
139
+ return curStats.toJson(defaultStats);
140
+ }
141
+ // determine what message should send by stats
142
+ sendStats(force = false) {
143
+ const stats = this.getStats();
144
+ if (!stats) {
145
+ return null;
146
+ }
147
+ const shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every((asset) => !asset.emitted);
148
+ if (shouldEmit) {
149
+ return this.sockWrite("still-ok");
150
+ }
151
+ this.sockWrite("hash", stats.hash);
152
+ if (stats.errors && stats.errors.length > 0) {
153
+ return this.sockWrite("errors", stats.errors);
154
+ } else if (stats.warnings && stats.warnings.length > 0) {
155
+ return this.sockWrite("warnings", stats.warnings);
156
+ } else {
157
+ return this.sockWrite("ok");
158
+ }
159
+ }
160
+ // send message to connecting socket
161
+ send(connection, message) {
162
+ if (connection.readyState !== 1) {
163
+ return;
164
+ }
165
+ connection.send(message);
166
+ }
167
+ }
@@ -0,0 +1,23 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ import { Server } from 'http';
5
+ import type { ListenOptions } from 'net';
6
+ import { RsbuildDevServerOptions, CreateDevServerOptions, ServerApi } from '@rsbuild/shared';
7
+ import connect from 'connect';
8
+ export declare class RsbuildDevServer {
9
+ private readonly dev;
10
+ private readonly devMiddleware;
11
+ private pwd;
12
+ private app;
13
+ middlewares: connect.Server;
14
+ constructor(options: RsbuildDevServerOptions);
15
+ private getDevOptions;
16
+ private applySetupMiddlewares;
17
+ onInit(app: Server): Promise<void>;
18
+ private applyDefaultMiddlewares;
19
+ createHTTPServer(): Promise<Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse> | import("https").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>>;
20
+ listen(options?: number | ListenOptions | undefined, listener?: () => void): void;
21
+ close(): void;
22
+ }
23
+ export declare function createDevServer(options: CreateDevServerOptions): Promise<ServerApi>;
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var devServer_exports = {};
30
+ __export(devServer_exports, {
31
+ RsbuildDevServer: () => RsbuildDevServer,
32
+ createDevServer: () => createDevServer
33
+ });
34
+ module.exports = __toCommonJS(devServer_exports);
35
+ var import_http = require("http");
36
+ var import_https = require("https");
37
+ var import_url = __toESM(require("url"));
38
+ var import_shared = require("@rsbuild/shared");
39
+ var import_constants = require("./constants");
40
+ var import_dev_middleware = __toESM(require("./dev-middleware"));
41
+ var import_deepmerge = require("@rsbuild/shared/deepmerge");
42
+ var import_connect = __toESM(require("connect"));
43
+ class RsbuildDevServer {
44
+ constructor(options) {
45
+ this.middlewares = (0, import_connect.default)();
46
+ this.pwd = options.pwd;
47
+ this.dev = this.getDevOptions(options);
48
+ this.devMiddleware = new import_dev_middleware.default({
49
+ dev: this.dev,
50
+ devMiddleware: options.devMiddleware
51
+ });
52
+ }
53
+ getDevOptions(options) {
54
+ const devOptions = typeof options.dev === "boolean" ? {} : options.dev;
55
+ const defaultOptions = (0, import_constants.getDefaultDevOptions)();
56
+ return (0, import_deepmerge.deepmerge)(defaultOptions, devOptions);
57
+ }
58
+ applySetupMiddlewares() {
59
+ const setupMiddlewares = this.dev.setupMiddlewares || [];
60
+ const serverOptions = {
61
+ sockWrite: (type, data) => this.devMiddleware.sockWrite(type, data)
62
+ };
63
+ const before = [];
64
+ const after = [];
65
+ setupMiddlewares.forEach((handler) => {
66
+ handler(
67
+ {
68
+ unshift: (...handlers) => before.unshift(...handlers),
69
+ push: (...handlers) => after.push(...handlers)
70
+ },
71
+ serverOptions
72
+ );
73
+ });
74
+ return { before, after };
75
+ }
76
+ // Complete the preparation of services
77
+ async onInit(app) {
78
+ this.app = app;
79
+ const { before, after } = this.applySetupMiddlewares();
80
+ before.forEach((fn) => this.middlewares.use(fn));
81
+ await this.applyDefaultMiddlewares(app);
82
+ after.forEach((fn) => this.middlewares.use(fn));
83
+ }
84
+ async applyDefaultMiddlewares(app) {
85
+ const { dev, devMiddleware } = this;
86
+ if (dev.compress) {
87
+ const { default: compression } = await Promise.resolve().then(() => __toESM(require("http-compression")));
88
+ this.middlewares.use((req, res, next) => {
89
+ compression({
90
+ gzip: true,
91
+ brotli: false
92
+ })(req, res, next);
93
+ });
94
+ }
95
+ this.middlewares.use((req, res, next) => {
96
+ res.setHeader("Access-Control-Allow-Origin", "*");
97
+ const path = req.url ? import_url.default.parse(req.url).pathname : "";
98
+ if (path == null ? void 0 : path.includes("hot-update")) {
99
+ res.setHeader("Access-Control-Allow-Credentials", "false");
100
+ }
101
+ const confHeaders = dev.headers;
102
+ if (confHeaders) {
103
+ for (const [key, value] of Object.entries(confHeaders)) {
104
+ res.setHeader(key, value);
105
+ }
106
+ }
107
+ next();
108
+ });
109
+ devMiddleware.init(app);
110
+ devMiddleware.middleware && this.middlewares.use(devMiddleware.middleware);
111
+ if (dev.historyApiFallback) {
112
+ const { default: connectHistoryApiFallback } = await Promise.resolve().then(() => __toESM(require("connect-history-api-fallback")));
113
+ const historyApiFallbackMiddleware = connectHistoryApiFallback(
114
+ // @ts-expect-error
115
+ typeof dev.historyApiFallback === "boolean" ? {} : dev.historyApiFallback
116
+ );
117
+ this.middlewares.use(historyApiFallbackMiddleware);
118
+ devMiddleware.middleware && this.middlewares.use(devMiddleware.middleware);
119
+ }
120
+ }
121
+ async createHTTPServer() {
122
+ const { dev } = this;
123
+ const devHttpsOption = typeof dev === "object" && dev.https;
124
+ if (devHttpsOption) {
125
+ const { genHttpsOptions } = require("./https");
126
+ const httpsOptions = await genHttpsOptions(devHttpsOption, this.pwd);
127
+ return (0, import_https.createServer)(httpsOptions, this.middlewares);
128
+ } else {
129
+ return (0, import_http.createServer)(this.middlewares);
130
+ }
131
+ }
132
+ listen(options, listener) {
133
+ const callback = () => {
134
+ listener == null ? void 0 : listener();
135
+ };
136
+ if (typeof options === "object") {
137
+ this.app.listen(options, callback);
138
+ } else {
139
+ this.app.listen(options || 8080, callback);
140
+ }
141
+ }
142
+ close() {
143
+ this.app.close();
144
+ }
145
+ }
146
+ async function createDevServer(options) {
147
+ var _a;
148
+ const { server: serverOptions = {}, ...devOptions } = options;
149
+ const logger = (_a = serverOptions.logger) != null ? _a : import_shared.logger;
150
+ const devServer = new RsbuildDevServer(devOptions);
151
+ const httpServer = serverOptions.customApp || await devServer.createHTTPServer();
152
+ const server = {
153
+ middlewares: devServer.middlewares,
154
+ init: async () => {
155
+ await devServer.onInit(httpServer);
156
+ },
157
+ // resolvedConfig,
158
+ listen: (options2, cb) => {
159
+ devServer.listen(options2, cb);
160
+ },
161
+ logger,
162
+ close: () => {
163
+ devServer.close();
164
+ }
165
+ };
166
+ return server;
167
+ }
168
+ // Annotate the CommonJS export names for ESM import in node:
169
+ 0 && (module.exports = {
170
+ RsbuildDevServer,
171
+ createDevServer
172
+ });
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ import { type DevServerHttpsOptions } from '@rsbuild/shared';
3
+ export declare const genHttpsOptions: (userOptions: DevServerHttpsOptions, pwd: string) => Promise<{
4
+ key?: Buffer | string;
5
+ cert?: Buffer | string;
6
+ }>;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var https_exports = {};
20
+ __export(https_exports, {
21
+ genHttpsOptions: () => genHttpsOptions
22
+ });
23
+ module.exports = __toCommonJS(https_exports);
24
+ var import_shared = require("@rsbuild/shared");
25
+ const genHttpsOptions = async (userOptions, pwd) => {
26
+ const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
27
+ if (!httpsOptions.key || !httpsOptions.cert) {
28
+ let devcertPath;
29
+ try {
30
+ devcertPath = require.resolve("devcert", { paths: [pwd, __dirname] });
31
+ } catch (err) {
32
+ const command = import_shared.color.bold(import_shared.color.yellow(`npm add devcert@1.2.2 -D`));
33
+ import_shared.logger.error(
34
+ `You have enabled "dev.https" option, but the "devcert" package is not installed.`
35
+ );
36
+ import_shared.logger.error(
37
+ `Please run ${command} to install manually, otherwise the https can not work.`
38
+ );
39
+ throw new Error('[https] "devcert" is not found.');
40
+ }
41
+ const devcert = require(devcertPath);
42
+ const selfsign = await devcert.certificateFor(["localhost"]);
43
+ return selfsign;
44
+ }
45
+ return httpsOptions;
46
+ };
47
+ // Annotate the CommonJS export names for ESM import in node:
48
+ 0 && (module.exports = {
49
+ genHttpsOptions
50
+ });
@@ -0,0 +1,2 @@
1
+ export { createDevServer } from './devServer';
2
+ export { startProdServer } from './prodServer';
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var server_exports = {};
20
+ __export(server_exports, {
21
+ createDevServer: () => import_devServer.createDevServer,
22
+ startProdServer: () => import_prodServer.startProdServer
23
+ });
24
+ module.exports = __toCommonJS(server_exports);
25
+ var import_devServer = require("./devServer");
26
+ var import_prodServer = require("./prodServer");
27
+ // Annotate the CommonJS export names for ESM import in node:
28
+ 0 && (module.exports = {
29
+ createDevServer,
30
+ startProdServer
31
+ });
@@ -0,0 +1,26 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import type { ListenOptions } from 'net';
4
+ import { Server } from 'http';
5
+ import connect from 'connect';
6
+ import { Context, RsbuildConfig, StartServerResult } from '@rsbuild/shared';
7
+ type RsbuildProdServerOptions = {
8
+ pwd: string;
9
+ output: {
10
+ path: string;
11
+ assetPrefix?: string;
12
+ };
13
+ };
14
+ export declare class RsbuildProdServer {
15
+ private app;
16
+ private options;
17
+ middlewares: connect.Server;
18
+ constructor(options: RsbuildProdServerOptions);
19
+ onInit(app: Server): Promise<void>;
20
+ private applyDefaultMiddlewares;
21
+ createHTTPServer(): Promise<Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>>;
22
+ listen(options?: number | ListenOptions | undefined, listener?: () => void): void;
23
+ close(): void;
24
+ }
25
+ export declare function startProdServer(context: Context, rsbuildConfig: RsbuildConfig): Promise<StartServerResult>;
26
+ export {};
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var prodServer_exports = {};
30
+ __export(prodServer_exports, {
31
+ RsbuildProdServer: () => RsbuildProdServer,
32
+ startProdServer: () => startProdServer
33
+ });
34
+ module.exports = __toCommonJS(prodServer_exports);
35
+ var import_http = require("http");
36
+ var import_connect = __toESM(require("connect"));
37
+ var import_path = require("path");
38
+ var import_sirv = __toESM(require("sirv"));
39
+ var import_shared = require("@rsbuild/shared");
40
+ class RsbuildProdServer {
41
+ constructor(options) {
42
+ this.middlewares = (0, import_connect.default)();
43
+ this.options = options;
44
+ }
45
+ // Complete the preparation of services
46
+ async onInit(app) {
47
+ this.app = app;
48
+ await this.applyDefaultMiddlewares();
49
+ }
50
+ async applyDefaultMiddlewares() {
51
+ const {
52
+ output: { path, assetPrefix },
53
+ pwd
54
+ } = this.options;
55
+ const assetMiddleware = (0, import_sirv.default)((0, import_path.join)(pwd, path), {
56
+ etag: true,
57
+ dev: true,
58
+ ignores: false
59
+ });
60
+ this.middlewares.use((req, res, next) => {
61
+ const url = req.url;
62
+ if (assetPrefix && (url == null ? void 0 : url.startsWith(assetPrefix))) {
63
+ req.url = url.slice(assetPrefix.length);
64
+ assetMiddleware(req, res, (...args) => {
65
+ req.url = url;
66
+ next(...args);
67
+ });
68
+ } else {
69
+ assetMiddleware(req, res, next);
70
+ }
71
+ });
72
+ }
73
+ async createHTTPServer() {
74
+ return (0, import_http.createServer)(this.middlewares);
75
+ }
76
+ listen(options, listener) {
77
+ const callback = () => {
78
+ listener == null ? void 0 : listener();
79
+ };
80
+ if (typeof options === "object") {
81
+ this.app.listen(options, callback);
82
+ } else {
83
+ this.app.listen(options || 8080, callback);
84
+ }
85
+ }
86
+ close() {
87
+ this.app.close();
88
+ }
89
+ }
90
+ async function startProdServer(context, rsbuildConfig) {
91
+ var _a, _b, _c, _d;
92
+ if (!process.env.NODE_ENV) {
93
+ process.env.NODE_ENV = "production";
94
+ }
95
+ const port = await (0, import_shared.getPort)(((_a = rsbuildConfig.dev) == null ? void 0 : _a.port) || import_shared.DEFAULT_PORT);
96
+ const server = new RsbuildProdServer({
97
+ pwd: context.rootPath,
98
+ output: {
99
+ path: ((_c = (_b = rsbuildConfig.output) == null ? void 0 : _b.distPath) == null ? void 0 : _c.root) || import_shared.ROOT_DIST_DIR,
100
+ assetPrefix: (_d = rsbuildConfig.output) == null ? void 0 : _d.assetPrefix
101
+ }
102
+ });
103
+ const httpServer = await server.createHTTPServer();
104
+ await server.onInit(httpServer);
105
+ return new Promise((resolve) => {
106
+ server.listen(
107
+ {
108
+ host: import_shared.DEFAULT_DEV_HOST,
109
+ port
110
+ },
111
+ () => {
112
+ const urls = (0, import_shared.getAddressUrls)("http", port);
113
+ (0, import_shared.printServerURLs)(urls);
114
+ resolve({
115
+ port,
116
+ urls: urls.map((item) => item.url),
117
+ server: {
118
+ middlewares: server.middlewares,
119
+ close: () => {
120
+ server.close();
121
+ }
122
+ // TODO: decouple with Modern.js server
123
+ }
124
+ });
125
+ }
126
+ );
127
+ });
128
+ }
129
+ // Annotate the CommonJS export names for ESM import in node:
130
+ 0 && (module.exports = {
131
+ RsbuildProdServer,
132
+ startProdServer
133
+ });