@nx/rsbuild 24.0.0-beta.3 → 24.0.0-beta.4

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 @@
1
+ export declare function waitForDevServer(configPath: string, command: 'dev' | 'preview'): Promise<void>;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.waitForDevServer = waitForDevServer;
4
+ const dev_server_ready_check_1 = require("./src/utils/dev-server-ready-check");
5
+ async function waitForDevServer(configPath, command) {
6
+ await (0, dev_server_ready_check_1.waitForRsbuildServer)(configPath, command);
7
+ // The config or its plugins can leave handles open
8
+ process.exit(0);
9
+ }
@@ -0,0 +1 @@
1
+ export { pluginNxReadySignal } from './src/utils/ready-signal-plugin';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pluginNxReadySignal = void 0;
4
+ var ready_signal_plugin_1 = require("./src/utils/ready-signal-plugin");
5
+ Object.defineProperty(exports, "pluginNxReadySignal", { enumerable: true, get: function () { return ready_signal_plugin_1.pluginNxReadySignal; } });
@@ -1,4 +1,5 @@
1
1
  import { defineConfig } from '@rsbuild/core';
2
+ import { pluginNxReadySignal } from '@nx/rsbuild/ready-signal';
2
3
 
3
4
  export default defineConfig({
4
5
  source: {
@@ -20,5 +21,6 @@ export default defineConfig({
20
21
  distPath: {
21
22
  root: 'dist',
22
23
  },
23
- }
24
+ },
25
+ plugins: [pluginNxReadySignal()],
24
26
  });
@@ -111,6 +111,7 @@ async function createRsbuildTargets(configFilePath, projectRoot, options, contex
111
111
  },
112
112
  },
113
113
  };
114
+ const signals = await loadsReadySignalPlugin(rsbuildConfig.content.plugins);
114
115
  targets[options.devTargetName] = {
115
116
  continuous: true,
116
117
  inputs: [...buildInputs],
@@ -119,6 +120,7 @@ async function createRsbuildTargets(configFilePath, projectRoot, options, contex
119
120
  cwd: projectRoot,
120
121
  args: ['--mode=development'],
121
122
  },
123
+ readyWhen: getServerReadyWhen(configFilePath, 'dev', signals),
122
124
  };
123
125
  targets[options.previewTargetName] = {
124
126
  continuous: true,
@@ -129,6 +131,7 @@ async function createRsbuildTargets(configFilePath, projectRoot, options, contex
129
131
  cwd: projectRoot,
130
132
  args: ['--mode=production'],
131
133
  },
134
+ readyWhen: getServerReadyWhen(configFilePath, 'preview', signals),
132
135
  };
133
136
  targets[options.inspectTargetName] = {
134
137
  command: `rsbuild inspect`,
@@ -203,3 +206,29 @@ function normalizeOptions(options) {
203
206
  options.inspectTargetName ??= 'inspect';
204
207
  return options;
205
208
  }
209
+ // A config that loads pluginNxReadySignal() reports when the server listens.
210
+ // Otherwise the check loads the config when the task runs, with the task's
211
+ // env and options, and waits for the server's port.
212
+ function getServerReadyWhen(configFilePath, command, signals) {
213
+ return signals
214
+ ? { signal: true }
215
+ : {
216
+ // Unquoted: the plugin's glob fixes the file name. Quote it if it ever
217
+ // becomes user-chosen, since the shell expands $, backticks and %.
218
+ command: `node -e "require('@nx/rsbuild/ready-check').waitForDevServer(process.argv[1], '${command}')" ${(0, path_1.basename)(configFilePath)}`,
219
+ // Each attempt loads the config
220
+ interval: 1000,
221
+ };
222
+ }
223
+ // Rsbuild's plugins option nests arrays and promises, and allows falsy entries
224
+ async function loadsReadySignalPlugin(plugins) {
225
+ const plugin = await plugins;
226
+ if (Array.isArray(plugin)) {
227
+ for (const entry of plugin) {
228
+ if (await loadsReadySignalPlugin(entry))
229
+ return true;
230
+ }
231
+ return false;
232
+ }
233
+ return plugin?.name === 'nx:ready-signal';
234
+ }
@@ -0,0 +1,31 @@
1
+ export interface RsbuildFlags {
2
+ port?: string;
3
+ host?: string | true;
4
+ envMode?: string;
5
+ envDir?: string;
6
+ config?: string;
7
+ configLoader?: string;
8
+ root?: string;
9
+ noEnv?: true;
10
+ }
11
+ /**
12
+ * The flags `rsbuild dev` and `rsbuild preview` take from the target's
13
+ * options: named options are forwarded as flags, unless `args`, which holds
14
+ * the rest, sets the same flag.
15
+ */
16
+ export declare function parseRsbuildFlags(options: Record<string, unknown>): RsbuildFlags;
17
+ export type ServerAddress = {
18
+ port: number;
19
+ host?: string;
20
+ };
21
+ export declare function getRsbuildServerAddress(server: {
22
+ port?: unknown;
23
+ host?: unknown;
24
+ }): ServerAddress | null;
25
+ /**
26
+ * The readyWhen of the inferred dev and preview targets when the config doesn't
27
+ * load the signal plugin. Runs in the env and with the options of the task,
28
+ * resolves the config the way the Rsbuild CLI does, plugins included, and
29
+ * waits for the server's port.
30
+ */
31
+ export declare function waitForRsbuildServer(configPath: string, command: 'dev' | 'preview'): Promise<void>;
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseRsbuildFlags = parseRsbuildFlags;
4
+ exports.getRsbuildServerAddress = getRsbuildServerAddress;
5
+ exports.waitForRsbuildServer = waitForRsbuildServer;
6
+ const internal_1 = require("@nx/devkit/internal");
7
+ const net_1 = require("net");
8
+ const path_1 = require("path");
9
+ const FLAGS = [
10
+ 'port',
11
+ 'host',
12
+ 'envMode',
13
+ 'envDir',
14
+ 'config',
15
+ 'configLoader',
16
+ 'root',
17
+ ];
18
+ const SHORT_FLAGS = { c: 'config', r: 'root' };
19
+ /**
20
+ * The flags `rsbuild dev` and `rsbuild preview` take from the target's
21
+ * options: named options are forwarded as flags, unless `args`, which holds
22
+ * the rest, sets the same flag.
23
+ */
24
+ function parseRsbuildFlags(options) {
25
+ const flags = {};
26
+ for (const key of FLAGS) {
27
+ const value = options[key];
28
+ if (typeof value === 'string' || typeof value === 'number') {
29
+ flags[key] = String(value);
30
+ }
31
+ else if (key === 'host' && value === true) {
32
+ flags.host = true;
33
+ }
34
+ }
35
+ const args = (0, internal_1.splitShellArgs)(Array.isArray(options.args)
36
+ ? options.args.join(' ')
37
+ : typeof options.args === 'string'
38
+ ? options.args
39
+ : '');
40
+ for (let i = 0; i < args.length; i++) {
41
+ const [, long, short, inline] = args[i].match(/^(?:--([^=]+)|-([a-z]))(?:=(.*))?$/) ?? [];
42
+ const key = (long
43
+ ? long.replace(/([a-z])-([a-z])/g, (_, a, b) => a + b.toUpperCase())
44
+ : SHORT_FLAGS[short]);
45
+ if (!key)
46
+ continue;
47
+ if (key === 'noEnv') {
48
+ flags.noEnv = true;
49
+ continue;
50
+ }
51
+ let value = inline;
52
+ if (value === undefined && args[i + 1] && !args[i + 1].startsWith('-')) {
53
+ value = args[++i];
54
+ }
55
+ if (key === 'host')
56
+ flags.host = value ?? true;
57
+ else if (FLAGS.includes(key) && value !== undefined)
58
+ flags[key] = value;
59
+ }
60
+ return flags;
61
+ }
62
+ function getRsbuildServerAddress(server) {
63
+ const port = Number(server.port);
64
+ if (!Number.isInteger(port) || port <= 0)
65
+ return null;
66
+ return typeof server.host === 'string' &&
67
+ !['localhost', '0.0.0.0', '::'].includes(server.host)
68
+ ? { port, host: server.host }
69
+ : { port };
70
+ }
71
+ /**
72
+ * The readyWhen of the inferred dev and preview targets when the config doesn't
73
+ * load the signal plugin. Runs in the env and with the options of the task,
74
+ * resolves the config the way the Rsbuild CLI does, plugins included, and
75
+ * waits for the server's port.
76
+ */
77
+ async function waitForRsbuildServer(configPath, command) {
78
+ let options = {};
79
+ try {
80
+ options = JSON.parse(process.env.NX_TASK_OPTIONS ?? '{}');
81
+ }
82
+ catch { }
83
+ const flags = parseRsbuildFlags(options);
84
+ const cwd = (0, path_1.resolve)(options.cwd ?? '.');
85
+ const root = (0, path_1.resolve)(cwd, flags.root ?? '.');
86
+ process.chdir(cwd);
87
+ const { createRsbuild, loadConfig, loadEnv } = require('@rsbuild/core');
88
+ // The CLI sets NODE_ENV first, then loads the env files for that mode and
89
+ // the config, whose function form reads the command from argv
90
+ process.env.NODE_ENV ||= command === 'dev' ? 'development' : 'production';
91
+ const envs = flags.noEnv
92
+ ? null
93
+ : loadEnv({
94
+ cwd: flags.envDir ? (0, path_1.resolve)(root, flags.envDir) : root,
95
+ mode: flags.envMode,
96
+ });
97
+ process.argv[2] = command;
98
+ const { content: config } = await loadConfig({
99
+ cwd: root,
100
+ // configPath is the config the CLI finds in the task's cwd, not in --root
101
+ path: flags.config ?? (flags.root ? undefined : configPath),
102
+ envMode: flags.envMode,
103
+ loader: flags.configLoader,
104
+ });
105
+ if (envs) {
106
+ config.source ??= {};
107
+ config.source.define = { ...envs.publicVars, ...config.source.define };
108
+ }
109
+ config.server ??= {};
110
+ if (flags.host !== undefined)
111
+ config.server.host = flags.host;
112
+ if (flags.port)
113
+ config.server.port = Number(flags.port);
114
+ // Rsbuild 1.0 takes the config only as rsbuildConfig
115
+ const rsbuild = await createRsbuild({ cwd: root, rsbuildConfig: config });
116
+ let server = config.server;
117
+ try {
118
+ await rsbuild.initConfigs({ action: command });
119
+ server = rsbuild.getNormalizedConfig().server;
120
+ }
121
+ catch (e) {
122
+ // The preview CLI normalizes the config without generating bundler configs,
123
+ // so a plugin hook that throws there doesn't fail `rsbuild preview`
124
+ if (command === 'dev')
125
+ throw e;
126
+ }
127
+ const address = getRsbuildServerAddress(server);
128
+ if (address)
129
+ await waitForAddress(address);
130
+ }
131
+ async function waitForAddress({ port, host }) {
132
+ const hosts = host ? [host] : ['127.0.0.1', '::1'];
133
+ while (!(await Promise.any(hosts.map((h) => connects(port, h))).catch(() => false))) {
134
+ await new Promise((r) => setTimeout(r, 250));
135
+ }
136
+ }
137
+ function connects(port, host) {
138
+ return new Promise((resolve, reject) => {
139
+ const socket = (0, net_1.createConnection)({ port, host });
140
+ socket.once('connect', () => {
141
+ socket.destroy();
142
+ resolve(true);
143
+ });
144
+ socket.once('error', (e) => {
145
+ socket.destroy();
146
+ reject(e);
147
+ });
148
+ });
149
+ }
@@ -0,0 +1,6 @@
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ /**
3
+ * Reports the dev or preview server's address to an Nx task whose readyWhen
4
+ * is `signal`, once the server listens. Does nothing outside such a task.
5
+ */
6
+ export declare function pluginNxReadySignal(): RsbuildPlugin;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pluginNxReadySignal = pluginNxReadySignal;
4
+ const net_1 = require("net");
5
+ /**
6
+ * Reports the dev or preview server's address to an Nx task whose readyWhen
7
+ * is `signal`, once the server listens. Does nothing outside such a task.
8
+ */
9
+ function pluginNxReadySignal() {
10
+ return {
11
+ name: 'nx:ready-signal',
12
+ setup(api) {
13
+ if (!process.env.NX_READY_SIGNAL_SOCKET) {
14
+ return;
15
+ }
16
+ const { signalTaskReady } = require('@nx/devkit');
17
+ const report = ({ port }) => {
18
+ const { https, host } = api.getNormalizedConfig().server;
19
+ signalTaskReady({
20
+ url: `${https ? 'https' : 'http'}://${getUrlHost(host)}:${port}/`,
21
+ });
22
+ };
23
+ api.onAfterStartDevServer(report);
24
+ // Rsbuild 1 names the preview hook onAfterStartProdServer
25
+ const onAfterStartPreviewServer = api.onAfterStartPreviewServer ??
26
+ api.onAfterStartProdServer;
27
+ onAfterStartPreviewServer(report);
28
+ },
29
+ };
30
+ }
31
+ function getUrlHost(host) {
32
+ if (!host || host === '0.0.0.0' || host === '::') {
33
+ return 'localhost';
34
+ }
35
+ return (0, net_1.isIPv6)(host) ? `[${host}]` : host;
36
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nx/rsbuild",
3
3
  "description": "The Nx Plugin for Rsbuild contains an Nx plugin, executors and utilities that support building applications using Rsbuild.",
4
- "version": "24.0.0-beta.3",
4
+ "version": "24.0.0-beta.4",
5
5
  "type": "commonjs",
6
6
  "files": [
7
7
  "dist",
@@ -47,6 +47,12 @@
47
47
  ],
48
48
  "src/*.js": [
49
49
  "dist/src/*.d.ts"
50
+ ],
51
+ "ready-check": [
52
+ "dist/ready-check.d.ts"
53
+ ],
54
+ "ready-signal": [
55
+ "dist/ready-signal.d.ts"
50
56
  ]
51
57
  }
52
58
  },
@@ -54,14 +60,14 @@
54
60
  "executors": "./executors.json",
55
61
  "dependencies": {
56
62
  "tslib": "^2.3.0",
57
- "@nx/devkit": "24.0.0-beta.3",
58
- "@nx/js": "24.0.0-beta.3",
63
+ "@nx/devkit": "24.0.0-beta.4",
64
+ "@nx/js": "24.0.0-beta.4",
59
65
  "minimatch": "10.2.5",
60
66
  "semver": "^7.6.3",
61
67
  "@phenomnomnominal/tsquery": "~6.2.0"
62
68
  },
63
69
  "devDependencies": {
64
- "nx": "24.0.0-beta.3"
70
+ "nx": "24.0.0-beta.4"
65
71
  },
66
72
  "peerDependencies": {
67
73
  "@rsbuild/core": "^1.0.0 || ^2.0.0"
@@ -97,6 +103,16 @@
97
103
  "types": "./dist/config-utils.d.ts",
98
104
  "default": "./dist/config-utils.js"
99
105
  },
106
+ "./ready-check": {
107
+ "@nx/nx-source": "./ready-check.ts",
108
+ "types": "./dist/ready-check.d.ts",
109
+ "default": "./dist/ready-check.js"
110
+ },
111
+ "./ready-signal": {
112
+ "@nx/nx-source": "./ready-signal.ts",
113
+ "types": "./dist/ready-signal.d.ts",
114
+ "default": "./dist/ready-signal.js"
115
+ },
100
116
  "./src/*": {
101
117
  "@nx/nx-source": "./src/*.ts",
102
118
  "types": "./dist/src/*.d.ts",