@rsc-kit/core 0.24.0 → 0.24.2

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.
package/dist/vite.js CHANGED
@@ -421,12 +421,12 @@ function loadHostManifest(options) {
421
421
  hostDefs = manifest.defs;
422
422
  }
423
423
  /** Run the backend's manifest command, once per process; see the option. */
424
- function runHostManifest(options, isBuild) {
424
+ function runHostManifest(options, isBuild, again = false) {
425
425
  const spec = options.hostManifest;
426
426
  if (!spec)
427
427
  return;
428
428
  const ran = projectRoot + "\0" + spec.command.join("\0");
429
- if (hostManifestRan.has(ran))
429
+ if (hostManifestRan.has(ran) && !again)
430
430
  return;
431
431
  hostManifestRan.add(ran);
432
432
  const [command, ...args] = spec.command;
@@ -2183,6 +2183,21 @@ function renderHostSignatures() {
2183
2183
  out.push("declare function " + hostGlobal + "<T = " + (sig.result ? tsOf(sig.result) : "void") + ">(name: " +
2184
2184
  JSON.stringify(name) + (params ? ", " + params : "") + "): Promise<T>;");
2185
2185
  }
2186
+ // Every function by name, its arguments and its result: what a test's fake
2187
+ // backend is typed against (createTestApp({ host })), so a fake cannot
2188
+ // answer for a function the backend does not have, or with a value it
2189
+ // would never send.
2190
+ if (hostFunctions?.length) {
2191
+ out.push("interface RscHostFunctions {");
2192
+ for (const name of hostFunctions) {
2193
+ const sig = hostTypes[name];
2194
+ out.push(" " + JSON.stringify(name) + ": " +
2195
+ (sig
2196
+ ? "{ args: [" + tsParams(sig) + "]; result: " + (sig.result ? tsOf(sig.result) : "void") + " };"
2197
+ : "{ args: unknown[]; result: unknown };"));
2198
+ }
2199
+ out.push("}", "");
2200
+ }
2186
2201
  const untyped = hostFunctions?.filter((name) => !hostTypes[name]) ?? null;
2187
2202
  if (untyped === null) {
2188
2203
  out.push("declare function " + hostGlobal + "<T = unknown>(name: string, ...args: unknown[]): Promise<T>;");
@@ -6415,6 +6430,17 @@ export function rscKit(options = {}) {
6415
6430
  }
6416
6431
  const routesPlugin = {
6417
6432
  name: "rsc-kit",
6433
+ // For rsc-kit-typegen --check: every file the config step writes that an
6434
+ // app might commit, so a stale one can be named.
6435
+ api: {
6436
+ generatedFiles() {
6437
+ return [
6438
+ join(projectRoot, HOST_FILE),
6439
+ join(sourceDir, HOST_ACTIONS_MODULE),
6440
+ ...(existsSync(typesDir) ? readdirSync(typesDir).map((name) => join(typesDir, name)) : []),
6441
+ ];
6442
+ },
6443
+ },
6418
6444
  // A Nitro module, which Nitro's Vite plugin collects from any plugin
6419
6445
  // that carries one. Only for a built server: the dev server evaluates
6420
6446
  // the entry through Vite's runner when it is first asked for, and there
@@ -6989,6 +7015,25 @@ export function rscKit(options = {}) {
6989
7015
  void server.restart();
6990
7016
  };
6991
7017
  server.watcher.add(hostActionsPath);
7018
+ // The backend's own source, when the app says where it is: a function
7019
+ // added in Go or PHP is typed in the editor a moment after it is saved,
7020
+ // rather than at the next start. Its rewritten manifest restarts the
7021
+ // server through the handler above.
7022
+ const backendDirs = (options.hostManifest?.watch ?? []).map((dir) => resolve(projectRoot, dir));
7023
+ if (backendDirs.length > 0) {
7024
+ let timer;
7025
+ const backendChanged = (file) => {
7026
+ if (!backendDirs.some((dir) => file === dir || file.startsWith(dir + "/")))
7027
+ return;
7028
+ clearTimeout(timer);
7029
+ timer = setTimeout(() => runHostManifest(options, false, true), 300);
7030
+ };
7031
+ for (const dir of backendDirs)
7032
+ server.watcher.add(dir);
7033
+ server.watcher.on("add", backendChanged);
7034
+ server.watcher.on("change", backendChanged);
7035
+ server.watcher.on("unlink", backendChanged);
7036
+ }
6992
7037
  server.watcher.on("add", hostActionsChanged);
6993
7038
  server.watcher.on("change", hostActionsChanged);
6994
7039
  server.watcher.on("unlink", hostActionsChanged);