@sanity/workbench-cli 2.1.0 → 2.2.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.
Files changed (80) hide show
  1. package/README.md +1 -1
  2. package/dist/_exports/build.d.ts +4 -4
  3. package/dist/_exports/build.js +2 -1
  4. package/dist/_exports/build.js.map +1 -1
  5. package/dist/_exports/defineApp-DldjQwq6.d.ts +375 -0
  6. package/dist/_exports/deploy.d.ts +10 -9
  7. package/dist/_exports/deploy.js +1 -0
  8. package/dist/_exports/deploy.js.map +1 -1
  9. package/dist/_exports/dev.d.ts +1 -1
  10. package/dist/_exports/index.d.ts +23 -16
  11. package/dist/_exports/index.js +2 -1
  12. package/dist/_exports/index.js.map +1 -1
  13. package/dist/_exports/preview.d.ts +1 -1
  14. package/dist/_exports/{registry-DI7hnTof.d.ts → registry-HE_dEHFa.d.ts} +38 -15
  15. package/dist/_exports/resolveWorkbenchConfig-6iznHbED.d.ts +60 -0
  16. package/dist/_exports/{summarizeInterfaces-DLsq6P43.d.ts → summarizeInterfaces-CjkeWwkY.d.ts} +20 -17
  17. package/dist/_exports/undeploy.d.ts +12 -9
  18. package/dist/actions/build/artifact.js +1 -1
  19. package/dist/actions/build/artifact.js.map +1 -1
  20. package/dist/actions/build/views/artifact.js +1 -1
  21. package/dist/actions/build/views/artifact.js.map +1 -1
  22. package/dist/actions/build/vite/optimize-deps.js +3 -3
  23. package/dist/actions/build/vite/optimize-deps.js.map +1 -1
  24. package/dist/actions/build/vite/plugins/plugin-module-federation.js +1 -2
  25. package/dist/actions/build/vite/plugins/plugin-module-federation.js.map +1 -1
  26. package/dist/actions/build/vite/plugins/plugin-sanity-federation-runtime.js +1 -1
  27. package/dist/actions/build/vite/plugins/plugin-sanity-federation-runtime.js.map +1 -1
  28. package/dist/actions/build/vite/workbench-vite-plugins.js +1 -1
  29. package/dist/actions/build/vite/workbench-vite-plugins.js.map +1 -1
  30. package/dist/actions/deploy/deployPayload.js +0 -3
  31. package/dist/actions/deploy/deployPayload.js.map +1 -1
  32. package/dist/actions/deploy/deployWorkbenchApp.js +46 -4
  33. package/dist/actions/deploy/deployWorkbenchApp.js.map +1 -1
  34. package/dist/actions/deploy/getWorkbench.js +4 -5
  35. package/dist/actions/deploy/getWorkbench.js.map +1 -1
  36. package/dist/actions/deploy/summarizeInterfaces.js +14 -10
  37. package/dist/actions/deploy/summarizeInterfaces.js.map +1 -1
  38. package/dist/actions/deploy/viewDeployment.js +17 -5
  39. package/dist/actions/deploy/viewDeployment.js.map +1 -1
  40. package/dist/actions/dev/deriveConfigs.js +9 -6
  41. package/dist/actions/dev/deriveConfigs.js.map +1 -1
  42. package/dist/actions/dev/exposesSetId.js +2 -2
  43. package/dist/actions/dev/exposesSetId.js.map +1 -1
  44. package/dist/actions/dev/registry.js +31 -25
  45. package/dist/actions/dev/registry.js.map +1 -1
  46. package/dist/actions/dev/startDevManifestWatcher.js.map +1 -1
  47. package/dist/actions/dev/startDevServerRegistration.js +35 -60
  48. package/dist/actions/dev/startDevServerRegistration.js.map +1 -1
  49. package/dist/actions/dev/startWorkbenchDevServer.js +11 -7
  50. package/dist/actions/dev/startWorkbenchDevServer.js.map +1 -1
  51. package/dist/actions/dev/toWireInterface.js +17 -0
  52. package/dist/actions/dev/toWireInterface.js.map +1 -0
  53. package/dist/actions/preview/serveBuiltApplication.js +1 -1
  54. package/dist/actions/preview/serveBuiltApplication.js.map +1 -1
  55. package/dist/actions/preview/startWorkbenchPreview.js +1 -1
  56. package/dist/actions/preview/startWorkbenchPreview.js.map +1 -1
  57. package/dist/actions/undeploy/workbenchUndeployAdapter.js +24 -33
  58. package/dist/actions/undeploy/workbenchUndeployAdapter.js.map +1 -1
  59. package/dist/appId.js +13 -9
  60. package/dist/appId.js.map +1 -1
  61. package/dist/applicationReference.js +12 -0
  62. package/dist/applicationReference.js.map +1 -0
  63. package/dist/contract.js +69 -31
  64. package/dist/contract.js.map +1 -1
  65. package/dist/defineApp.js +52 -59
  66. package/dist/defineApp.js.map +1 -1
  67. package/dist/defineView.js +3 -3
  68. package/dist/defineView.js.map +1 -1
  69. package/dist/deriveInterfaces.js +45 -19
  70. package/dist/deriveInterfaces.js.map +1 -1
  71. package/dist/resolveWorkbenchApp.js +13 -9
  72. package/dist/resolveWorkbenchApp.js.map +1 -1
  73. package/dist/resolveWorkbenchConfig.js +30 -0
  74. package/dist/resolveWorkbenchConfig.js.map +1 -0
  75. package/dist/services/applications.js +7 -1
  76. package/dist/services/applications.js.map +1 -1
  77. package/package.json +3 -3
  78. package/dist/_exports/contract-DyG11fQ7.d.ts +0 -45
  79. package/dist/_exports/defineApp-DSvQx8rf.d.ts +0 -151
  80. package/dist/_exports/resolveWorkbenchApp-Be9eU8mu.d.ts +0 -41
@@ -1,37 +1,24 @@
1
1
  import { getCliConfigUncached } from '@sanity/cli-core';
2
- import { isWorkbenchApp } from '../../defineApp.js';
2
+ import { applicationReference } from '../../applicationReference.js';
3
+ import { isWorkbenchApp, isWorkbenchConfig } from '../../defineApp.js';
3
4
  import { deriveInterfaces } from '../../deriveInterfaces.js';
5
+ import { resolveWorkbenchConfig } from '../../resolveWorkbenchConfig.js';
4
6
  import { formatWorkbenchAppErrors, validateWorkbenchApp } from '../../validateWorkbenchApp.js';
5
7
  import { deriveConfigs } from './deriveConfigs.js';
6
8
  import { trackExposesSet } from './exposesSetId.js';
7
- import { getRegisteredServers, isConfigOnlyServer, registerDevServer } from './registry.js';
9
+ import { getRegisteredServers, registerDevServer } from './registry.js';
8
10
  import { startDevManifestWatcher } from './startDevManifestWatcher.js';
9
11
  /**
10
- * Log any config validation errors without aborting. Unlike build and deploy,
11
- * dev stays up on an invalid config so the author sees the errors and fixes them
12
- * live on the next save.
12
+ * Log any app validation errors without aborting. Unlike build and deploy, dev
13
+ * stays up on an invalid app so the author sees the errors and fixes them live on
14
+ * the next save. A config is not an app — it isn't validated by the app schema
15
+ * (which would spuriously demand a slug/title), so skip it here.
13
16
  */ function reportConfigErrors(app, output) {
17
+ if (isWorkbenchConfig(app)) return;
14
18
  const errors = validateWorkbenchApp(app);
15
19
  if (errors.length === 0) return;
16
20
  output.warn(formatWorkbenchAppErrors(errors));
17
21
  }
18
- /**
19
- * A live server — other than this process — already playing the given role for
20
- * the slug. Only a *same-role* duplicate is a conflict: a config-only server
21
- * (configs, no interfaces — e.g. a media-library config app) is never routed
22
- * as an app, so it may share a slug with the app server it configures. The
23
- * workbench renders the app and publishes both servers' configs, and can
24
- * always tell them apart.
25
- */ function findSameRoleConflict(id, configOnly) {
26
- return getRegisteredServers().find((server)=>server.pid !== process.pid && server.id === id && isConfigOnlyServer(server) === configOnly);
27
- }
28
- /**
29
- * Remedy line for a same-role slug conflict, phrased for the role. Changing the
30
- * slug is only real advice for an app — a config app's slug is fixed by the
31
- * app it configures (e.g. `unstable_defineMediaLibrary` hard-codes it).
32
- */ function conflictRemedy(configOnly) {
33
- return configOnly ? 'Stop that server first.' : 'Stop that server, or give this app its own `slug` in sanity.cli.ts.';
34
- }
35
22
  /** The address the server actually bound — the live socket, which can differ from the configured port under non-strict ports. */ function serverAddress(server) {
36
23
  const resolvedHost = server.config.server.host;
37
24
  const addr = server.httpServer?.address();
@@ -53,33 +40,42 @@ import { startDevManifestWatcher } from './startDevManifestWatcher.js';
53
40
  const interfaces = deriveInterfaces(cliConfig.app, {
54
41
  isApp
55
42
  });
56
- const configs = await deriveConfigs(cliConfig.app);
57
- const id = isWorkbenchApp(cliConfig.app) ? cliConfig.app.slug : undefined;
58
- const configOnly = isConfigOnlyServer({
59
- configs,
60
- interfaces
61
- });
62
- const devServer = id ? findSameRoleConflict(id, configOnly) : undefined;
43
+ const configs = await deriveConfigs(cliConfig);
44
+ const workbenchApp = isWorkbenchApp(cliConfig.app) ? cliConfig.app : undefined;
45
+ const config = resolveWorkbenchConfig(cliConfig);
46
+ // A config runs alongside the app it configures, so give it its own id
47
+ // namespace — sharing the app's slug would make the two indistinguishable here.
48
+ const id = config ? `config:${config.appType}` : workbenchApp?.slug;
49
+ // Identity is resolved and the reference composed here, so the workbench reads
50
+ // both off the registry entry instead of recomposing them. Name defaults to the
51
+ // slug, mirroring brett.
52
+ const name = workbenchApp?.name ?? workbenchApp?.slug;
53
+ const reference = workbenchApp && name ? // is a config, not an app — so its reference is always `<org>/<name>`.
54
+ applicationReference({
55
+ isSingleton: false,
56
+ name,
57
+ organizationId: workbenchApp.organizationId
58
+ }) : undefined;
59
+ // Separate namespaces mean a shared id is a genuine duplicate, so the gate is
60
+ // a plain id match — no config-vs-app role left to reconcile.
61
+ const devServer = id ? getRegisteredServers().find((server)=>server.id === id) : undefined;
63
62
  if (id && devServer) {
64
- const subject = configOnly ? `A config for "${id}"` : `The app "${id}"`;
65
- output.error(`${subject} is already served by another dev server running on port ${devServer.port}, ` + "so the workbench can't tell them apart and this one stays out of it. " + conflictRemedy(configOnly), {
63
+ output.error(`"${id}" is already served by another dev server running on port ${devServer.port}, ` + "so the workbench can't tell them apart and this one stays out of it. Stop that server first.", {
66
64
  exit: false
67
65
  });
68
66
  return {
69
67
  close: async ()=>{}
70
68
  };
71
69
  }
72
- // The role the registry currently advertises for this server; a config edit
73
- // can flip it (see the re-check in `update`). Committed only after a
74
- // successful registry patch, so a failed pass re-checks on the next save.
75
- let registeredConfigOnly = configOnly;
76
70
  const registration = registerDevServer({
77
71
  configs,
78
72
  host: appHost,
79
73
  id,
80
74
  interfaces,
75
+ name,
81
76
  port: appPort,
82
77
  projectId: cliConfig?.api?.projectId,
78
+ reference,
83
79
  type: isApp ? 'coreApp' : 'studio',
84
80
  workDir
85
81
  });
@@ -91,11 +87,11 @@ import { startDevManifestWatcher } from './startDevManifestWatcher.js';
91
87
  // Re-derive every pass (don't omit): the registry patch is a shallow merge,
92
88
  // so omitting would wipe the registered set.
93
89
  extract: async (params)=>{
94
- const app = (await getCliConfigUncached(params.workDir)).app;
95
- reportConfigErrors(app, output);
90
+ const nextConfig = await getCliConfigUncached(params.workDir);
91
+ reportConfigErrors(nextConfig.app, output);
96
92
  return {
97
- configs: await deriveConfigs(app),
98
- interfaces: deriveInterfaces(app, {
93
+ configs: await deriveConfigs(nextConfig),
94
+ interfaces: deriveInterfaces(nextConfig.app, {
99
95
  isApp
100
96
  }),
101
97
  manifest: await extractManifest(params)
@@ -109,31 +105,11 @@ import { startDevManifestWatcher } from './startDevManifestWatcher.js';
109
105
  ],
110
106
  output,
111
107
  update: async (patch)=>{
112
- // A save can flip the server's role — e.g. a config-only app gaining an
113
- // `entry` becomes app-role — so re-run the same-role collision check the
114
- // registration gate applied, or the flip would quietly reintroduce the
115
- // ambiguity (two app-role servers on one slug). The patch is skipped, not
116
- // fatal: the registry keeps the previous shape and the next save retries.
117
- const nextConfigOnly = isConfigOnlyServer({
118
- configs: patch.configs,
119
- interfaces: patch.interfaces
120
- });
121
- if (id && nextConfigOnly !== registeredConfigOnly) {
122
- const conflict = findSameRoleConflict(id, nextConfigOnly);
123
- if (conflict) {
124
- const subject = nextConfigOnly ? `a config for "${id}"` : `the app "${id}"`;
125
- output.error(`This change makes this dev server serve ${subject} like the dev server running on ` + `port ${conflict.port} already does, so the workbench couldn't tell them apart — ` + `keeping the previous registration. ${conflictRemedy(nextConfigOnly)}`, {
126
- exit: false
127
- });
128
- return;
129
- }
130
- }
131
108
  if (!exposesSet.changed({
132
109
  configs: patch.configs,
133
110
  interfaces: patch.interfaces
134
111
  })) {
135
112
  registration.update(patch);
136
- registeredConfigOnly = nextConfigOnly;
137
113
  return;
138
114
  }
139
115
  // Rebuild the remote *before* patching the registry — the patch reloads the
@@ -149,7 +125,6 @@ import { startDevManifestWatcher } from './startDevManifestWatcher.js';
149
125
  ...patch,
150
126
  ...serverAddress(rebuiltServer)
151
127
  } : patch);
152
- registeredConfigOnly = nextConfigOnly;
153
128
  },
154
129
  workDir
155
130
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/actions/dev/startDevServerRegistration.ts"],"sourcesContent":["import {type CliConfig, getCliConfigUncached, type Output} from '@sanity/cli-core'\nimport {type ViteDevServer} from 'vite'\n\nimport {isWorkbenchApp} from '../../defineApp.js'\nimport {deriveInterfaces} from '../../deriveInterfaces.js'\nimport {formatWorkbenchAppErrors, validateWorkbenchApp} from '../../validateWorkbenchApp.js'\nimport {deriveConfigs} from './deriveConfigs.js'\nimport {trackExposesSet} from './exposesSetId.js'\nimport {\n type DevServerManifest,\n getRegisteredServers,\n isConfigOnlyServer,\n registerDevServer,\n} from './registry.js'\nimport {startDevManifestWatcher} from './startDevManifestWatcher.js'\n\ninterface DevServerRegistrationOptions {\n cliConfig: CliConfig\n /**\n * Extract the project manifest to inline into the registry. The caller owns the\n * studio-vs-app split (manifest formats are CLI-domain); registration re-derives\n * the interface set alongside it.\n */\n extractManifest: (params: {\n configPath: string\n workDir: string\n }) => Promise<DevServerManifest['manifest']>\n isApp: boolean\n output: Output\n server: ViteDevServer\n workDir: string\n\n /**\n * Rebuild the app's federation remote when its interface set changes, awaited\n * *before* the registry patch — the patch reloads the workbench page, which must\n * re-fetch a remote that already exposes the new interface. Resolves with the\n * recreated server so the entry gets its actual address (non-strict ports may\n * shift it); must reject if the restart produces no server, so the set stays\n * uncommitted and the next save retries instead of advertising a dead port.\n */\n onInterfaceSetChange?: () => Promise<ViteDevServer>\n}\n\ninterface DevServerRegistrationHandle {\n close: () => Promise<void>\n}\n\n/**\n * Log any config validation errors without aborting. Unlike build and deploy,\n * dev stays up on an invalid config so the author sees the errors and fixes them\n * live on the next save.\n */\nfunction reportConfigErrors(app: CliConfig['app'], output: Output): void {\n const errors = validateWorkbenchApp(app)\n if (errors.length === 0) return\n output.warn(formatWorkbenchAppErrors(errors))\n}\n\n/**\n * A live server — other than this process — already playing the given role for\n * the slug. Only a *same-role* duplicate is a conflict: a config-only server\n * (configs, no interfaces — e.g. a media-library config app) is never routed\n * as an app, so it may share a slug with the app server it configures. The\n * workbench renders the app and publishes both servers' configs, and can\n * always tell them apart.\n */\nfunction findSameRoleConflict(id: string, configOnly: boolean): DevServerManifest | undefined {\n return getRegisteredServers().find(\n (server) =>\n server.pid !== process.pid && server.id === id && isConfigOnlyServer(server) === configOnly,\n )\n}\n\n/**\n * Remedy line for a same-role slug conflict, phrased for the role. Changing the\n * slug is only real advice for an app — a config app's slug is fixed by the\n * app it configures (e.g. `unstable_defineMediaLibrary` hard-codes it).\n */\nfunction conflictRemedy(configOnly: boolean): string {\n return configOnly\n ? 'Stop that server first.'\n : 'Stop that server, or give this app its own `slug` in sanity.cli.ts.'\n}\n\n/** The address the server actually bound — the live socket, which can differ from the configured port under non-strict ports. */\nfunction serverAddress(server: ViteDevServer) {\n const resolvedHost = server.config.server.host\n const addr = server.httpServer?.address()\n return {\n host: typeof resolvedHost === 'string' ? resolvedHost : 'localhost',\n port: typeof addr === 'object' && addr ? addr.port : server.config.server.port,\n }\n}\n\n/**\n * Register the dev server in the registry and watch its config for manifest +\n * interface changes. The workbench reads the entry to locate and render the\n * server; the watcher keeps it current as `sanity.cli.ts` is edited.\n */\nexport async function startDevServerRegistration(\n options: DevServerRegistrationOptions,\n): Promise<DevServerRegistrationHandle> {\n const {cliConfig, extractManifest, isApp, onInterfaceSetChange, output, server, workDir} = options\n\n const {host: appHost, port: appPort} = serverAddress(server)\n\n reportConfigErrors(cliConfig.app, output)\n\n // Forwarded alongside (not inside) the manifest so the workbench renders local\n // panels/workers and reads the configs without a deploy.\n const interfaces = deriveInterfaces(cliConfig.app, {isApp})\n const configs = await deriveConfigs(cliConfig.app)\n\n const id = isWorkbenchApp(cliConfig.app) ? cliConfig.app.slug : undefined\n\n const configOnly = isConfigOnlyServer({configs, interfaces})\n const devServer = id ? findSameRoleConflict(id, configOnly) : undefined\n\n if (id && devServer) {\n const subject = configOnly ? `A config for \"${id}\"` : `The app \"${id}\"`\n output.error(\n `${subject} is already served by another dev server running on port ${devServer.port}, ` +\n \"so the workbench can't tell them apart and this one stays out of it. \" +\n conflictRemedy(configOnly),\n {exit: false},\n )\n return {close: async () => {}}\n }\n\n // The role the registry currently advertises for this server; a config edit\n // can flip it (see the re-check in `update`). Committed only after a\n // successful registry patch, so a failed pass re-checks on the next save.\n let registeredConfigOnly = configOnly\n\n const registration = registerDevServer({\n configs,\n host: appHost,\n id,\n interfaces,\n port: appPort,\n projectId: cliConfig?.api?.projectId,\n type: isApp ? 'coreApp' : 'studio',\n workDir,\n })\n\n const exposesSet = trackExposesSet({configs, interfaces})\n\n const watcher = await startDevManifestWatcher({\n // Re-derive every pass (don't omit): the registry patch is a shallow merge,\n // so omitting would wipe the registered set.\n extract: async (params) => {\n const app = (await getCliConfigUncached(params.workDir)).app\n reportConfigErrors(app, output)\n return {\n configs: await deriveConfigs(app),\n interfaces: deriveInterfaces(app, {isApp}),\n manifest: await extractManifest(params),\n }\n },\n // A studio's root resolves to `sanity.config.*` but its interfaces live in\n // `sanity.cli.*` — watch that too. Apps already root at `sanity.cli.*`.\n extraWatchFilenames: isApp ? undefined : ['sanity.cli.js', 'sanity.cli.ts'],\n output,\n update: async (patch) => {\n // A save can flip the server's role — e.g. a config-only app gaining an\n // `entry` becomes app-role — so re-run the same-role collision check the\n // registration gate applied, or the flip would quietly reintroduce the\n // ambiguity (two app-role servers on one slug). The patch is skipped, not\n // fatal: the registry keeps the previous shape and the next save retries.\n const nextConfigOnly = isConfigOnlyServer({\n configs: patch.configs,\n interfaces: patch.interfaces,\n })\n if (id && nextConfigOnly !== registeredConfigOnly) {\n const conflict = findSameRoleConflict(id, nextConfigOnly)\n if (conflict) {\n const subject = nextConfigOnly ? `a config for \"${id}\"` : `the app \"${id}\"`\n output.error(\n `This change makes this dev server serve ${subject} like the dev server running on ` +\n `port ${conflict.port} already does, so the workbench couldn't tell them apart — ` +\n `keeping the previous registration. ${conflictRemedy(nextConfigOnly)}`,\n {exit: false},\n )\n return\n }\n }\n\n if (\n !exposesSet.changed({\n configs: patch.configs,\n interfaces: patch.interfaces,\n })\n ) {\n registration.update(patch)\n registeredConfigOnly = nextConfigOnly\n return\n }\n // Rebuild the remote *before* patching the registry — the patch reloads the\n // page, which must re-fetch a remote that already exposes the new interface.\n const rebuiltServer = await onInterfaceSetChange?.()\n // Commit only after a successful rebuild, so a thrown one retries next pass.\n exposesSet.commit({\n configs: patch.configs,\n interfaces: patch.interfaces,\n })\n // The recreated server can bind a different port (non-strict ports).\n registration.update(rebuiltServer ? {...patch, ...serverAddress(rebuiltServer)} : patch)\n registeredConfigOnly = nextConfigOnly\n },\n workDir,\n })\n\n return {\n close: async () => {\n registration.release()\n await watcher.close()\n },\n }\n}\n"],"names":["getCliConfigUncached","isWorkbenchApp","deriveInterfaces","formatWorkbenchAppErrors","validateWorkbenchApp","deriveConfigs","trackExposesSet","getRegisteredServers","isConfigOnlyServer","registerDevServer","startDevManifestWatcher","reportConfigErrors","app","output","errors","length","warn","findSameRoleConflict","id","configOnly","find","server","pid","process","conflictRemedy","serverAddress","resolvedHost","config","host","addr","httpServer","address","port","startDevServerRegistration","options","cliConfig","extractManifest","isApp","onInterfaceSetChange","workDir","appHost","appPort","interfaces","configs","slug","undefined","devServer","subject","error","exit","close","registeredConfigOnly","registration","projectId","api","type","exposesSet","watcher","extract","params","manifest","extraWatchFilenames","update","patch","nextConfigOnly","conflict","changed","rebuiltServer","commit","release"],"mappings":"AAAA,SAAwBA,oBAAoB,QAAoB,mBAAkB;AAGlF,SAAQC,cAAc,QAAO,qBAAoB;AACjD,SAAQC,gBAAgB,QAAO,4BAA2B;AAC1D,SAAQC,wBAAwB,EAAEC,oBAAoB,QAAO,gCAA+B;AAC5F,SAAQC,aAAa,QAAO,qBAAoB;AAChD,SAAQC,eAAe,QAAO,oBAAmB;AACjD,SAEEC,oBAAoB,EACpBC,kBAAkB,EAClBC,iBAAiB,QACZ,gBAAe;AACtB,SAAQC,uBAAuB,QAAO,+BAA8B;AAiCpE;;;;CAIC,GACD,SAASC,mBAAmBC,GAAqB,EAAEC,MAAc;IAC/D,MAAMC,SAASV,qBAAqBQ;IACpC,IAAIE,OAAOC,MAAM,KAAK,GAAG;IACzBF,OAAOG,IAAI,CAACb,yBAAy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sBAAsB;gBACjD,MAAMc,WAAWhD,qBAAqBC,IAAI8C;gBAC1C,IAAIC,UAAU;oBACZ,MAAMlB,UAAUiB,iBAAiB,CAAC,cAAc,EAAE9C,GAAG,CAAC,CAAC,GAAG,CAAC,SAAS,EAAEA,GAAG,CAAC,CAAC;oBAC3EL,OAAOmC,KAAK,CACV,CAAC,wCAAwC,EAAED,QAAQ,gCAAgC,CAAC,GAClF,CAAC,KAAK,EAAEkB,SAASjC,IAAI,CAAC,2DAA2D,CAAC,GAClF,CAAC,mCAAmC,EAAER,eAAewC,iBAAiB,EACxE;wBAACf,MAAM;oBAAK;oBAEd;gBACF;YACF;YAEA,IACE,CAACO,WAAWU,OAAO,CAAC;gBAClBvB,SAASoB,MAAMpB,OAAO;gBACtBD,YAAYqB,MAAMrB,UAAU;YAC9B,IACA;gBACAU,aAAaU,MAAM,CAACC;gBACpBZ,uBAAuBa;gBACvB;YACF;YACA,4EAA4E;YAC5E,6EAA6E;YAC7E,MAAMG,gBAAgB,MAAM7B;YAC5B,6EAA6E;YAC7EkB,WAAWY,MAAM,CAAC;gBAChBzB,SAASoB,MAAMpB,OAAO;gBACtBD,YAAYqB,MAAMrB,UAAU;YAC9B;YACA,qEAAqE;YACrEU,aAAaU,MAAM,CAACK,gBAAgB;gBAAC,GAAGJ,KAAK;gBAAE,GAAGtC,cAAc0C,cAAc;YAAA,IAAIJ;YAClFZ,uBAAuBa;QACzB;QACAzB;IACF;IAEA,OAAO;QACLW,OAAO;YACLE,aAAaiB,OAAO;YACpB,MAAMZ,QAAQP,KAAK;QACrB;IACF;AACF"}
1
+ {"version":3,"sources":["../../../src/actions/dev/startDevServerRegistration.ts"],"sourcesContent":["import {type CliConfig, getCliConfigUncached, type Output} from '@sanity/cli-core'\nimport {type ViteDevServer} from 'vite'\n\nimport {applicationReference} from '../../applicationReference.js'\nimport {isWorkbenchApp, isWorkbenchConfig} from '../../defineApp.js'\nimport {deriveInterfaces} from '../../deriveInterfaces.js'\nimport {resolveWorkbenchConfig} from '../../resolveWorkbenchConfig.js'\nimport {formatWorkbenchAppErrors, validateWorkbenchApp} from '../../validateWorkbenchApp.js'\nimport {deriveConfigs} from './deriveConfigs.js'\nimport {trackExposesSet} from './exposesSetId.js'\nimport {type DevServerManifest, getRegisteredServers, registerDevServer} from './registry.js'\nimport {startDevManifestWatcher} from './startDevManifestWatcher.js'\n\ninterface DevServerRegistrationOptions {\n cliConfig: CliConfig\n /**\n * Extract the project manifest to inline into the registry. The caller owns the\n * studio-vs-app split (manifest formats are CLI-domain); registration re-derives\n * the interface set alongside it.\n */\n extractManifest: (params: {\n configPath: string\n workDir: string\n }) => Promise<DevServerManifest['manifest']>\n isApp: boolean\n output: Output\n server: ViteDevServer\n workDir: string\n\n /**\n * Rebuild the app's federation remote when its interface set changes, awaited\n * *before* the registry patch — the patch reloads the workbench page, which must\n * re-fetch a remote that already exposes the new interface. Resolves with the\n * recreated server so the entry gets its actual address (non-strict ports may\n * shift it); must reject if the restart produces no server, so the set stays\n * uncommitted and the next save retries instead of advertising a dead port.\n */\n onInterfaceSetChange?: () => Promise<ViteDevServer>\n}\n\ninterface DevServerRegistrationHandle {\n close: () => Promise<void>\n}\n\n/**\n * Log any app validation errors without aborting. Unlike build and deploy, dev\n * stays up on an invalid app so the author sees the errors and fixes them live on\n * the next save. A config is not an app — it isn't validated by the app schema\n * (which would spuriously demand a slug/title), so skip it here.\n */\nfunction reportConfigErrors(app: CliConfig['app'], output: Output): void {\n if (isWorkbenchConfig(app)) return\n const errors = validateWorkbenchApp(app)\n if (errors.length === 0) return\n output.warn(formatWorkbenchAppErrors(errors))\n}\n\n/** The address the server actually bound — the live socket, which can differ from the configured port under non-strict ports. */\nfunction serverAddress(server: ViteDevServer) {\n const resolvedHost = server.config.server.host\n const addr = server.httpServer?.address()\n return {\n host: typeof resolvedHost === 'string' ? resolvedHost : 'localhost',\n port: typeof addr === 'object' && addr ? addr.port : server.config.server.port,\n }\n}\n\n/**\n * Register the dev server in the registry and watch its config for manifest +\n * interface changes. The workbench reads the entry to locate and render the\n * server; the watcher keeps it current as `sanity.cli.ts` is edited.\n */\nexport async function startDevServerRegistration(\n options: DevServerRegistrationOptions,\n): Promise<DevServerRegistrationHandle> {\n const {cliConfig, extractManifest, isApp, onInterfaceSetChange, output, server, workDir} = options\n\n const {host: appHost, port: appPort} = serverAddress(server)\n\n reportConfigErrors(cliConfig.app, output)\n\n // Forwarded alongside (not inside) the manifest so the workbench renders local\n // panels/workers and reads the configs without a deploy.\n const interfaces = deriveInterfaces(cliConfig.app, {isApp})\n const configs = await deriveConfigs(cliConfig)\n\n const workbenchApp = isWorkbenchApp(cliConfig.app) ? cliConfig.app : undefined\n const config = resolveWorkbenchConfig(cliConfig)\n // A config runs alongside the app it configures, so give it its own id\n // namespace — sharing the app's slug would make the two indistinguishable here.\n const id = config ? `config:${config.appType}` : workbenchApp?.slug\n // Identity is resolved and the reference composed here, so the workbench reads\n // both off the registry entry instead of recomposing them. Name defaults to the\n // slug, mirroring brett.\n const name = workbenchApp?.name ?? workbenchApp?.slug\n const reference =\n workbenchApp && name\n ? // No workbench app is a singleton now that the only one (the Media Library)\n // is a config, not an app — so its reference is always `<org>/<name>`.\n applicationReference({\n isSingleton: false,\n name,\n organizationId: workbenchApp.organizationId,\n })\n : undefined\n\n // Separate namespaces mean a shared id is a genuine duplicate, so the gate is\n // a plain id match — no config-vs-app role left to reconcile.\n const devServer = id ? getRegisteredServers().find((server) => server.id === id) : undefined\n\n if (id && devServer) {\n output.error(\n `\"${id}\" is already served by another dev server running on port ${devServer.port}, ` +\n \"so the workbench can't tell them apart and this one stays out of it. Stop that server first.\",\n {exit: false},\n )\n return {close: async () => {}}\n }\n\n const registration = registerDevServer({\n configs,\n host: appHost,\n id,\n interfaces,\n name,\n port: appPort,\n projectId: cliConfig?.api?.projectId,\n reference,\n type: isApp ? 'coreApp' : 'studio',\n workDir,\n })\n\n const exposesSet = trackExposesSet({configs, interfaces})\n\n const watcher = await startDevManifestWatcher({\n // Re-derive every pass (don't omit): the registry patch is a shallow merge,\n // so omitting would wipe the registered set.\n extract: async (params) => {\n const nextConfig = await getCliConfigUncached(params.workDir)\n reportConfigErrors(nextConfig.app, output)\n return {\n configs: await deriveConfigs(nextConfig),\n interfaces: deriveInterfaces(nextConfig.app, {isApp}),\n manifest: await extractManifest(params),\n }\n },\n // A studio's root resolves to `sanity.config.*` but its interfaces live in\n // `sanity.cli.*` — watch that too. Apps already root at `sanity.cli.*`.\n extraWatchFilenames: isApp ? undefined : ['sanity.cli.js', 'sanity.cli.ts'],\n output,\n update: async (patch) => {\n if (\n !exposesSet.changed({\n configs: patch.configs,\n interfaces: patch.interfaces,\n })\n ) {\n registration.update(patch)\n return\n }\n // Rebuild the remote *before* patching the registry — the patch reloads the\n // page, which must re-fetch a remote that already exposes the new interface.\n const rebuiltServer = await onInterfaceSetChange?.()\n // Commit only after a successful rebuild, so a thrown one retries next pass.\n exposesSet.commit({\n configs: patch.configs,\n interfaces: patch.interfaces,\n })\n // The recreated server can bind a different port (non-strict ports).\n registration.update(rebuiltServer ? {...patch, ...serverAddress(rebuiltServer)} : patch)\n },\n workDir,\n })\n\n return {\n close: async () => {\n registration.release()\n await watcher.close()\n },\n }\n}\n"],"names":["getCliConfigUncached","applicationReference","isWorkbenchApp","isWorkbenchConfig","deriveInterfaces","resolveWorkbenchConfig","formatWorkbenchAppErrors","validateWorkbenchApp","deriveConfigs","trackExposesSet","getRegisteredServers","registerDevServer","startDevManifestWatcher","reportConfigErrors","app","output","errors","length","warn","serverAddress","server","resolvedHost","config","host","addr","httpServer","address","port","startDevServerRegistration","options","cliConfig","extractManifest","isApp","onInterfaceSetChange","workDir","appHost","appPort","interfaces","configs","workbenchApp","undefined","id","appType","slug","name","reference","isSingleton","organizationId","devServer","find","error","exit","close","registration","projectId","api","type","exposesSet","watcher","extract","params","nextConfig","manifest","extraWatchFilenames","update","patch","changed","rebuiltServer","commit","release"],"mappings":"AAAA,SAAwBA,oBAAoB,QAAoB,mBAA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,OAAO;gBACLuB,SAAS,MAAM9B,cAAcqD;gBAC7BxB,YAAYjC,iBAAiByD,WAAW/C,GAAG,EAAE;oBAACkB;gBAAK;gBACnD8B,UAAU,MAAM/B,gBAAgB6B;YAClC;QACF;QACA,2EAA2E;QAC3E,wEAAwE;QACxEG,qBAAqB/B,QAAQQ,YAAY;YAAC;YAAiB;SAAgB;QAC3EzB;QACAiD,QAAQ,OAAOC;YACb,IACE,CAACR,WAAWS,OAAO,CAAC;gBAClB5B,SAAS2B,MAAM3B,OAAO;gBACtBD,YAAY4B,MAAM5B,UAAU;YAC9B,IACA;gBACAgB,aAAaW,MAAM,CAACC;gBACpB;YACF;YACA,4EAA4E;YAC5E,6EAA6E;YAC7E,MAAME,gBAAgB,MAAMlC;YAC5B,6EAA6E;YAC7EwB,WAAWW,MAAM,CAAC;gBAChB9B,SAAS2B,MAAM3B,OAAO;gBACtBD,YAAY4B,MAAM5B,UAAU;YAC9B;YACA,qEAAqE;YACrEgB,aAAaW,MAAM,CAACG,gBAAgB;gBAAC,GAAGF,KAAK;gBAAE,GAAG9C,cAAcgD,cAAc;YAAA,IAAIF;QACpF;QACA/B;IACF;IAEA,OAAO;QACLkB,OAAO;YACLC,aAAagB,OAAO;YACpB,MAAMX,QAAQN,KAAK;QACrB;IACF;AACF"}
@@ -4,9 +4,10 @@ import { isStaging } from '@sanity/cli-core/util';
4
4
  import viteReact from '@vitejs/plugin-react';
5
5
  import { createServer } from 'vite';
6
6
  import { z } from 'zod/mini';
7
- import { isWorkbenchApp } from '../../defineApp.js';
7
+ import { isWorkbenchApp, isWorkbenchConfig } from '../../defineApp.js';
8
8
  import { createExposesTracker } from './exposesSetId.js';
9
9
  import { acquireWorkbenchLock, getRegisteredServers, isConfigOnlyServer, readWorkbenchLock, watchRegistry } from './registry.js';
10
+ import { toWireInterface } from './toWireInterface.js';
10
11
  import { writeWorkbenchRuntime } from './writeWorkbenchRuntime.js';
11
12
  const devDebug = subdebug('dev');
12
13
  const renderDashboardEntry = '@sanity/workbench-cli/_internal_render';
@@ -19,7 +20,8 @@ const toApplicationsPayload = (servers)=>({
19
20
  applications: servers.filter((server)=>isLocalApp(server)).map(({ host, id, interfaces, manifest, port, projectId, type })=>({
20
21
  host,
21
22
  id,
22
- interfaces,
23
+ // Views cross to the remote keyed on `type`, never the internal `surface`.
24
+ interfaces: interfaces?.map((iface)=>toWireInterface(iface)),
23
25
  manifest,
24
26
  port,
25
27
  projectId,
@@ -83,9 +85,11 @@ const toApplicationsPayload = (servers)=>({
83
85
  }
84
86
  export async function startWorkbenchDevServer(options) {
85
87
  const { cacheDir, cliConfig, httpHost, httpPort: workbenchPort, mode, output, reactStrictMode, workDir } = options;
86
- // Workbench is opted into solely by calling `unstable_defineApp`.
87
- if (!isWorkbenchApp(cliConfig?.app)) {
88
- devDebug('Not a workbench app, skipping workbench dev server');
88
+ // Workbench is opted into by a `defineApplication` app or a config-only
89
+ // `unstable_defineMediaLibrary` config — the latter still needs the shell to
90
+ // render it.
91
+ if (!isWorkbenchApp(cliConfig?.app) && !isWorkbenchConfig(cliConfig?.app)) {
92
+ devDebug('Not a workbench app or config, skipping workbench dev server');
89
93
  return {
90
94
  close: noop,
91
95
  httpHost,
@@ -242,7 +246,7 @@ async function createWorkbenchViteServer(options) {
242
246
  }
243
247
  };
244
248
  }
245
- // Workbench is opted into via `unstable_defineApp`, which carries the
249
+ // Workbench is opted into via `defineApplication`, which carries the
246
250
  // organization ID. Deliberately no fallback (e.g. resolving it from the
247
251
  // configured project): the lookup would need an authenticated user and an
248
252
  // API round-trip on every startup for something the opt-in already declares.
@@ -250,7 +254,7 @@ const resolveOrganizationId = (cliConfig)=>{
250
254
  if (cliConfig.app?.organizationId) {
251
255
  return cliConfig.app.organizationId;
252
256
  }
253
- throw new Error('Workbench requires an organization ID. Pass "organizationId" to unstable_defineApp() in sanity.cli.ts.');
257
+ throw new Error('Workbench requires an organization ID. Pass "organizationId" to defineApplication() in sanity.cli.ts.');
254
258
  };
255
259
  // Restricts protocol to http(s) so the URL is safe to interpolate into HTML
256
260
  // attributes and Link headers downstream.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/actions/dev/startWorkbenchDevServer.ts"],"sourcesContent":["import {fileURLToPath} from 'node:url'\n\nimport {type CliConfig, type Output, subdebug} from '@sanity/cli-core'\nimport {isStaging} from '@sanity/cli-core/util'\nimport viteReact from '@vitejs/plugin-react'\nimport {createServer, type InlineConfig, type Plugin, type ViteDevServer} from 'vite'\nimport {z} from 'zod/mini'\n\nimport {isWorkbenchApp} from '../../defineApp.js'\nimport {createExposesTracker} from './exposesSetId.js'\nimport {\n acquireWorkbenchLock,\n type DevServerManifest,\n getRegisteredServers,\n isConfigOnlyServer,\n readWorkbenchLock,\n watchRegistry,\n} from './registry.js'\nimport {writeWorkbenchRuntime} from './writeWorkbenchRuntime.js'\n\nconst devDebug = subdebug('dev')\nconst renderDashboardEntry = '@sanity/workbench-cli/_internal_render'\nconst renderDashboardPath = fileURLToPath(\n new URL('../../_exports/_internal_render.js', import.meta.url),\n)\n\nconst noop = async () => {}\n\n// Every server is a local app except a config-only one — a config\n// with no interfaces (the media library). A server with both lands in both channels.\nconst isLocalApp = (server: DevServerManifest): boolean => !isConfigOnlyServer(server)\n\nconst toApplicationsPayload = (servers: DevServerManifest[]) => ({\n applications: servers\n .filter((server) => isLocalApp(server))\n .map(({host, id, interfaces, manifest, port, projectId, type}) => ({\n host,\n id,\n interfaces,\n manifest,\n port,\n projectId,\n type,\n })),\n configs: servers.flatMap(({configs, host, port}) =>\n // The registry stores the config flat; the workbench wire shape nests the\n // type-specific payload (`fields` for a media library) under `config`, keyed\n // by the `appType` discriminator.\n (configs ?? []).map(({appType, id, moduleName, version, ...config}) => ({\n appType,\n config,\n id,\n moduleName,\n remoteURL: `http://${host}:${port}`,\n version,\n })),\n ),\n})\n\n/**\n * Bridge the dev-server registry into a workbench Vite server's HMR channel so\n * the page tracks apps as they come and go. A changed interface set means a\n * rebuilt remote — full-reload to drop the stale remote-entry; otherwise\n * rebroadcast for a soft reconcile. Returns a detach fn.\n */\nfunction attachViteDevServerBridge(server: ViteDevServer): () => void {\n server.ws.on('sanity:workbench:get-local-applications', (_, client) => {\n client.send(\n 'sanity:workbench:local-applications',\n toApplicationsPayload(getRegisteredServers()),\n )\n })\n\n const setTracker = createExposesTracker()\n const registryWatcher = watchRegistry((servers) => {\n if (setTracker.hasChanged(servers)) {\n server.ws.send({type: 'full-reload'})\n return\n }\n server.ws.send('sanity:workbench:local-applications', toApplicationsPayload(servers))\n })\n\n return () => registryWatcher.close()\n}\n\n/**\n * Make the workbench remote act as the machine's workbench: claim the singleton\n * lock so app `sanity dev`s register into it instead of each starting their own,\n * and bridge the registry so the remote shows the local apps. No-op lock if one\n * is already held.\n */\nexport function startWorkbenchRemoteCoordinator(options: {\n httpHost: string | undefined\n port: number\n server: ViteDevServer\n}): {close: () => Promise<void>} {\n const {httpHost, port, server} = options\n\n const lock = acquireWorkbenchLock({host: httpHost || 'localhost', port})\n if (!lock) {\n const existing = readWorkbenchLock()\n devDebug(\n 'Workbench lock already held by pid %d on port %d; bridging the registry without claiming it',\n existing?.pid,\n existing?.port,\n )\n }\n\n const detachBridge = attachViteDevServerBridge(server)\n\n return {\n close: async () => {\n detachBridge()\n lock?.release()\n },\n }\n}\n\ninterface WorkbenchDevServerResult {\n close: () => Promise<void>\n httpHost: string | undefined\n workbenchAvailable: boolean\n workbenchPort: number\n}\n\nexport interface StartWorkbenchOptions {\n /** Dependency-cache dir for the workbench Vite server, kept apart from the user's own. */\n cacheDir: string\n cliConfig: CliConfig\n httpHost: string | undefined\n httpPort: number\n /** `dev` renders a live app and honors a local workbench-UI override; `preview`\n * (`sanity start`) previews a build and loads the deployed workbench UI. */\n mode: 'development' | 'preview'\n output: Output\n /** Wrap the workbench in React StrictMode; the CLI resolves it (unset collapses to `false`). */\n reactStrictMode: boolean\n workDir: string\n}\n\nexport async function startWorkbenchDevServer(\n options: StartWorkbenchOptions,\n): Promise<WorkbenchDevServerResult> {\n const {\n cacheDir,\n cliConfig,\n httpHost,\n httpPort: workbenchPort,\n mode,\n output,\n reactStrictMode,\n workDir,\n } = options\n\n // Workbench is opted into solely by calling `unstable_defineApp`.\n if (!isWorkbenchApp(cliConfig?.app)) {\n devDebug('Not a workbench app, skipping workbench dev server')\n return {close: noop, httpHost, workbenchAvailable: false, workbenchPort}\n }\n\n // Acquire an exclusive lock — only one workbench per machine.\n // Uses O_EXCL which is atomic at the OS level, preventing races when\n // multiple `sanity dev` processes start simultaneously (e.g. via turbo).\n const workbenchLock = acquireWorkbenchLock({host: httpHost || 'localhost', port: workbenchPort})\n if (!workbenchLock) {\n const existing = readWorkbenchLock()\n devDebug(\n 'Workbench already running at pid %d on port %d, skipping',\n existing?.pid,\n existing?.port,\n )\n return {\n close: noop,\n httpHost: existing?.host ?? httpHost,\n workbenchAvailable: true,\n workbenchPort: existing?.port ?? workbenchPort,\n }\n }\n\n // The lock is already held; an exception here (runtime-file write failure,\n // invalid remote URL) would otherwise leak it until the next acquire prunes\n // the stale PID.\n let result: Awaited<ReturnType<typeof createWorkbenchViteServer>>\n try {\n result = await createWorkbenchViteServer({\n cacheDir,\n cliConfig,\n httpHost,\n mode,\n output,\n reactStrictMode,\n workbenchPort,\n workDir,\n })\n } catch (err) {\n workbenchLock.release()\n throw err\n }\n\n if (!result) {\n workbenchLock.release()\n return {close: noop, httpHost, workbenchAvailable: false, workbenchPort}\n }\n\n const {actualPort, close} = result\n workbenchLock.updatePort(actualPort)\n\n return {\n close: async () => {\n workbenchLock.release()\n await close()\n },\n httpHost,\n workbenchAvailable: true,\n workbenchPort: actualPort,\n }\n}\n\ninterface CreateWorkbenchViteServerOptions {\n cacheDir: string\n cliConfig: CliConfig\n httpHost: string | undefined\n mode: 'development' | 'preview'\n output: Output\n reactStrictMode: boolean\n workbenchPort: number\n workDir: string\n}\n\ninterface CreateWorkbenchViteServerResult {\n actualPort: number\n close: () => Promise<void>\n}\n\nasync function createWorkbenchViteServer(\n options: CreateWorkbenchViteServerOptions,\n): Promise<CreateWorkbenchViteServerResult | undefined> {\n const {cacheDir, cliConfig, httpHost, mode, output, reactStrictMode, workbenchPort, workDir} =\n options\n\n // `preview` loads `.env.development` (the env hook treats only `build`/`deploy`\n // as production), which points the workbench UI at a local dev server that\n // isn't running here. Ignore the override and load the deployed UI instead.\n const remoteUrl =\n mode === 'preview'\n ? undefined\n : parseRemoteUrl(process.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL)\n\n const organizationId = resolveOrganizationId(cliConfig)\n\n devDebug('Writing workbench runtime files')\n const root = await writeWorkbenchRuntime({\n cwd: workDir,\n organizationId,\n reactStrictMode,\n remoteUrl,\n })\n\n const viteConfig: InlineConfig = {\n // Custom cache directory so sanity's vite cache doesn't conflict with local vite projects\n cacheDir,\n configFile: false,\n define: {\n __SANITY_STAGING__: isStaging(),\n 'import.meta.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL': JSON.stringify(remoteUrl),\n },\n logLevel: 'warn',\n mode: 'development',\n optimizeDeps: {\n // Keep this entry out of pre-bundling so Vite injects its HMR context.\n exclude: [renderDashboardEntry],\n },\n // viteReact looks inert here — it transforms none of the host's own modules —\n // but it's load-bearing for the remotes. It serves the Fast Refresh runtime at\n // /@react-refresh and injects the preamble that defines window.$RefreshReg$. The\n // federated remotes loaded into this page are react-refresh transformed, so\n // without the preamble they throw \"can't detect preamble\", and without the\n // runtime their /@react-refresh import (wired by @module-federation/vite's\n // remoteHmr) fails. Dropping it as dead code broke every panel; see #1262.\n plugins: [viteReact(), ...(remoteUrl ? [remoteManifestPreloadHeaderPlugin(remoteUrl)] : [])],\n resolve: {\n // The generated Vite root cannot reliably resolve this package.\n alias: {[renderDashboardEntry]: renderDashboardPath},\n dedupe: ['react', 'react-dom'],\n },\n root,\n server: {\n host: httpHost,\n port: workbenchPort,\n strictPort: false,\n warmup: {\n clientFiles: ['./workbench.js'],\n },\n },\n }\n\n devDebug('Creating workbench vite server')\n const server = await createServer(viteConfig)\n try {\n await server.listen()\n } catch (err) {\n await server.close()\n output.warn(\n `Workbench dev server failed to start: ${err instanceof Error ? err.message : String(err)}`,\n )\n return undefined\n }\n\n // Vite may have picked a different port if the desired one was occupied\n const addr = server.httpServer?.address()\n const actualPort = typeof addr === 'object' && addr ? addr.port : workbenchPort\n\n // Fire-and-forget: warm the workbench remote's Vite transform pipeline so\n // the first browser request hits a pre-populated module graph.\n if (remoteUrl) {\n fetch(remoteUrl)\n .then((r) => r.body?.cancel())\n .catch(() => {})\n devDebug('Warming workbench remote at %s', remoteUrl)\n }\n\n const detachBridge = attachViteDevServerBridge(server)\n\n return {\n actualPort,\n close: async () => {\n detachBridge()\n await server.close()\n },\n }\n}\n\n// Workbench is opted into via `unstable_defineApp`, which carries the\n// organization ID. Deliberately no fallback (e.g. resolving it from the\n// configured project): the lookup would need an authenticated user and an\n// API round-trip on every startup for something the opt-in already declares.\nconst resolveOrganizationId = (cliConfig: CliConfig): string => {\n if (cliConfig.app?.organizationId) {\n return cliConfig.app.organizationId\n }\n\n throw new Error(\n 'Workbench requires an organization ID. Pass \"organizationId\" to unstable_defineApp() in sanity.cli.ts.',\n )\n}\n\n// Restricts protocol to http(s) so the URL is safe to interpolate into HTML\n// attributes and Link headers downstream.\nconst remoteUrlSchema = z.url({normalize: true, protocol: /^https?$/})\n\nfunction parseRemoteUrl(value: string | undefined): string | undefined {\n if (!value) return undefined\n\n const result = remoteUrlSchema.safeParse(value)\n\n if (!result.success) {\n throw new Error(\n `Invalid SANITY_INTERNAL_WORKBENCH_REMOTE_URL: ${value} (must be an http(s) URL)`,\n )\n }\n\n return result.data\n}\n\n/**\n * Sets a `Link: <remoteUrl>; rel=preload; as=fetch; crossorigin` response header\n * on the index document so the browser can start fetching the Module Federation\n * manifest as soon as response headers arrive — before HTML parsing reaches the\n * in-head preconnect hint. `as=fetch` matches how the federation runtime later\n * retrieves the JSON manifest, allowing the preload entry to satisfy that fetch.\n */\nfunction remoteManifestPreloadHeaderPlugin(remoteUrl: string): Plugin {\n return {\n apply: 'serve',\n configureServer(server) {\n server.middlewares.use((req, res, next) => {\n const pathname = (req.url || '/').split('?')[0]\n if (pathname === '/' || pathname === '/index.html') {\n res.setHeader('Link', `<${remoteUrl}>; rel=preload; as=fetch; crossorigin`)\n }\n next()\n })\n },\n name: 'sanity:workbench-remote-preload-header',\n }\n}\n"],"names":["fileURLToPath","subdebug","isStaging","viteReact","createServer","z","isWorkbenchApp","createExposesTracker","acquireWorkbenchLock","getRegisteredServers","isConfigOnlyServer","readWorkbenchLock","watchRegistry","writeWorkbenchRuntime","devDebug","renderDashboardEntry","renderDashboardPath","URL","url","noop","isLocalApp","server","toApplicationsPayload","servers","applications","filter","map","host","id","interfaces","manifest","port","projectId","type","configs","flatMap","appType","moduleName","version","config","remoteURL","attachViteDevServerBridge","ws","on","_","client","send","setTracker","registryWatcher","hasChanged","close","startWorkbenchRemoteCoordinator","options","httpHost","lock","existing","pid","detachBridge","release","startWorkbenchDevServer","cacheDir","cliConfig","httpPort","workbenchPort","mode","output","reactStrictMode","workDir","app","workbenchAvailable","workbenchLock","result","createWorkbenchViteServer","err","actualPort","updatePort","remoteUrl","undefined","parseRemoteUrl","process","env","SANITY_INTERNAL_WORKBENCH_REMOTE_URL","organizationId","resolveOrganizationId","root","cwd","viteConfig","configFile","define","__SANITY_STAGING__","JSON","stringify","logLevel","optimizeDeps","exclude","plugins","remoteManifestPreloadHeaderPlugin","resolve","alias","dedupe","strictPort","warmup","clientFiles","listen","warn","Error","message","String","addr","httpServer","address","fetch","then","r","body","cancel","catch","remoteUrlSchema","normalize","protocol","value","safeParse","success","data","apply","configureServer","middlewares","use","req","res","next","pathname","split","setHeader","name"],"mappings":"AAAA,SAAQA,aAAa,QAAO,WAAU;AAEtC,SAAqCC,QAAQ,QAAO,mBAAkB;AACtE,SAAQC,SAAS,QAAO,wBAAuB;AAC/C,OAAOC,eAAe,uBAAsB;AAC5C,SAAQC,YAAY,QAA2D,OAAM;AACrF,SAAQC,CAA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AMiC,wBAAwB,CAACtB;IAC7B,IAAIA,UAAUO,GAAG,EAAEc,gBAAgB;QACjC,OAAOrB,UAAUO,GAAG,CAACc,cAAc;IACrC;IAEA,MAAM,IAAIuB,MACR;AAEJ;AAEA,4EAA4E;AAC5E,0CAA0C;AAC1C,MAAMY,kBAAkBhH,EAAEa,GAAG,CAAC;IAACoG,WAAW;IAAMC,UAAU;AAAU;AAEpE,SAASzC,eAAe0C,KAAyB;IAC/C,IAAI,CAACA,OAAO,OAAO3C;IAEnB,MAAMN,SAAS8C,gBAAgBI,SAAS,CAACD;IAEzC,IAAI,CAACjD,OAAOmD,OAAO,EAAE;QACnB,MAAM,IAAIjB,MACR,CAAC,8CAA8C,EAAEe,MAAM,yBAAyB,CAAC;IAErF;IAEA,OAAOjD,OAAOoD,IAAI;AACpB;AAEA;;;;;;CAMC,GACD,SAAS3B,kCAAkCpB,SAAiB;IAC1D,OAAO;QACLgD,OAAO;QACPC,iBAAgBxG,MAAM;YACpBA,OAAOyG,WAAW,CAACC,GAAG,CAAC,CAACC,KAAKC,KAAKC;gBAChC,MAAMC,WAAW,AAACH,CAAAA,IAAI9G,GAAG,IAAI,GAAE,EAAGkH,KAAK,CAAC,IAAI,CAAC,EAAE;gBAC/C,IAAID,aAAa,OAAOA,aAAa,eAAe;oBAClDF,IAAII,SAAS,CAAC,QAAQ,CAAC,CAAC,EAAEzD,UAAU,qCAAqC,CAAC;gBAC5E;gBACAsD;YACF;QACF;QACAI,MAAM;IACR;AACF"}
1
+ {"version":3,"sources":["../../../src/actions/dev/startWorkbenchDevServer.ts"],"sourcesContent":["import {fileURLToPath} from 'node:url'\n\nimport {type CliConfig, type Output, subdebug} from '@sanity/cli-core'\nimport {isStaging} from '@sanity/cli-core/util'\nimport viteReact from '@vitejs/plugin-react'\nimport {createServer, type InlineConfig, type Plugin, type ViteDevServer} from 'vite'\nimport {z} from 'zod/mini'\n\nimport {isWorkbenchApp, isWorkbenchConfig} from '../../defineApp.js'\nimport {createExposesTracker} from './exposesSetId.js'\nimport {\n acquireWorkbenchLock,\n type DevServerManifest,\n getRegisteredServers,\n isConfigOnlyServer,\n readWorkbenchLock,\n watchRegistry,\n} from './registry.js'\nimport {toWireInterface} from './toWireInterface.js'\nimport {writeWorkbenchRuntime} from './writeWorkbenchRuntime.js'\n\nconst devDebug = subdebug('dev')\nconst renderDashboardEntry = '@sanity/workbench-cli/_internal_render'\nconst renderDashboardPath = fileURLToPath(\n new URL('../../_exports/_internal_render.js', import.meta.url),\n)\n\nconst noop = async () => {}\n\n// Every server is a local app except a config-only one — a config\n// with no interfaces (the media library). A server with both lands in both channels.\nconst isLocalApp = (server: DevServerManifest): boolean => !isConfigOnlyServer(server)\n\nconst toApplicationsPayload = (servers: DevServerManifest[]) => ({\n applications: servers\n .filter((server) => isLocalApp(server))\n .map(({host, id, interfaces, manifest, port, projectId, type}) => ({\n host,\n id,\n // Views cross to the remote keyed on `type`, never the internal `surface`.\n interfaces: interfaces?.map((iface) => toWireInterface(iface)),\n manifest,\n port,\n projectId,\n type,\n })),\n configs: servers.flatMap(({configs, host, port}) =>\n // The registry stores the config flat; the workbench wire shape nests the\n // type-specific payload (`fields` for a media library) under `config`, keyed\n // by the `appType` discriminator.\n (configs ?? []).map(({appType, id, moduleName, version, ...config}) => ({\n appType,\n config,\n id,\n moduleName,\n remoteURL: `http://${host}:${port}`,\n version,\n })),\n ),\n})\n\n/**\n * Bridge the dev-server registry into a workbench Vite server's HMR channel so\n * the page tracks apps as they come and go. A changed interface set means a\n * rebuilt remote — full-reload to drop the stale remote-entry; otherwise\n * rebroadcast for a soft reconcile. Returns a detach fn.\n */\nfunction attachViteDevServerBridge(server: ViteDevServer): () => void {\n server.ws.on('sanity:workbench:get-local-applications', (_, client) => {\n client.send(\n 'sanity:workbench:local-applications',\n toApplicationsPayload(getRegisteredServers()),\n )\n })\n\n const setTracker = createExposesTracker()\n const registryWatcher = watchRegistry((servers) => {\n if (setTracker.hasChanged(servers)) {\n server.ws.send({type: 'full-reload'})\n return\n }\n server.ws.send('sanity:workbench:local-applications', toApplicationsPayload(servers))\n })\n\n return () => registryWatcher.close()\n}\n\n/**\n * Make the workbench remote act as the machine's workbench: claim the singleton\n * lock so app `sanity dev`s register into it instead of each starting their own,\n * and bridge the registry so the remote shows the local apps. No-op lock if one\n * is already held.\n */\nexport function startWorkbenchRemoteCoordinator(options: {\n httpHost: string | undefined\n port: number\n server: ViteDevServer\n}): {close: () => Promise<void>} {\n const {httpHost, port, server} = options\n\n const lock = acquireWorkbenchLock({host: httpHost || 'localhost', port})\n if (!lock) {\n const existing = readWorkbenchLock()\n devDebug(\n 'Workbench lock already held by pid %d on port %d; bridging the registry without claiming it',\n existing?.pid,\n existing?.port,\n )\n }\n\n const detachBridge = attachViteDevServerBridge(server)\n\n return {\n close: async () => {\n detachBridge()\n lock?.release()\n },\n }\n}\n\ninterface WorkbenchDevServerResult {\n close: () => Promise<void>\n httpHost: string | undefined\n workbenchAvailable: boolean\n workbenchPort: number\n}\n\nexport interface StartWorkbenchOptions {\n /** Dependency-cache dir for the workbench Vite server, kept apart from the user's own. */\n cacheDir: string\n cliConfig: CliConfig\n httpHost: string | undefined\n httpPort: number\n /** `dev` renders a live app and honors a local workbench-UI override; `preview`\n * (`sanity start`) previews a build and loads the deployed workbench UI. */\n mode: 'development' | 'preview'\n output: Output\n /** Wrap the workbench in React StrictMode; the CLI resolves it (unset collapses to `false`). */\n reactStrictMode: boolean\n workDir: string\n}\n\nexport async function startWorkbenchDevServer(\n options: StartWorkbenchOptions,\n): Promise<WorkbenchDevServerResult> {\n const {\n cacheDir,\n cliConfig,\n httpHost,\n httpPort: workbenchPort,\n mode,\n output,\n reactStrictMode,\n workDir,\n } = options\n\n // Workbench is opted into by a `defineApplication` app or a config-only\n // `unstable_defineMediaLibrary` config — the latter still needs the shell to\n // render it.\n if (!isWorkbenchApp(cliConfig?.app) && !isWorkbenchConfig(cliConfig?.app)) {\n devDebug('Not a workbench app or config, skipping workbench dev server')\n return {close: noop, httpHost, workbenchAvailable: false, workbenchPort}\n }\n\n // Acquire an exclusive lock — only one workbench per machine.\n // Uses O_EXCL which is atomic at the OS level, preventing races when\n // multiple `sanity dev` processes start simultaneously (e.g. via turbo).\n const workbenchLock = acquireWorkbenchLock({host: httpHost || 'localhost', port: workbenchPort})\n if (!workbenchLock) {\n const existing = readWorkbenchLock()\n devDebug(\n 'Workbench already running at pid %d on port %d, skipping',\n existing?.pid,\n existing?.port,\n )\n return {\n close: noop,\n httpHost: existing?.host ?? httpHost,\n workbenchAvailable: true,\n workbenchPort: existing?.port ?? workbenchPort,\n }\n }\n\n // The lock is already held; an exception here (runtime-file write failure,\n // invalid remote URL) would otherwise leak it until the next acquire prunes\n // the stale PID.\n let result: Awaited<ReturnType<typeof createWorkbenchViteServer>>\n try {\n result = await createWorkbenchViteServer({\n cacheDir,\n cliConfig,\n httpHost,\n mode,\n output,\n reactStrictMode,\n workbenchPort,\n workDir,\n })\n } catch (err) {\n workbenchLock.release()\n throw err\n }\n\n if (!result) {\n workbenchLock.release()\n return {close: noop, httpHost, workbenchAvailable: false, workbenchPort}\n }\n\n const {actualPort, close} = result\n workbenchLock.updatePort(actualPort)\n\n return {\n close: async () => {\n workbenchLock.release()\n await close()\n },\n httpHost,\n workbenchAvailable: true,\n workbenchPort: actualPort,\n }\n}\n\ninterface CreateWorkbenchViteServerOptions {\n cacheDir: string\n cliConfig: CliConfig\n httpHost: string | undefined\n mode: 'development' | 'preview'\n output: Output\n reactStrictMode: boolean\n workbenchPort: number\n workDir: string\n}\n\ninterface CreateWorkbenchViteServerResult {\n actualPort: number\n close: () => Promise<void>\n}\n\nasync function createWorkbenchViteServer(\n options: CreateWorkbenchViteServerOptions,\n): Promise<CreateWorkbenchViteServerResult | undefined> {\n const {cacheDir, cliConfig, httpHost, mode, output, reactStrictMode, workbenchPort, workDir} =\n options\n\n // `preview` loads `.env.development` (the env hook treats only `build`/`deploy`\n // as production), which points the workbench UI at a local dev server that\n // isn't running here. Ignore the override and load the deployed UI instead.\n const remoteUrl =\n mode === 'preview'\n ? undefined\n : parseRemoteUrl(process.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL)\n\n const organizationId = resolveOrganizationId(cliConfig)\n\n devDebug('Writing workbench runtime files')\n const root = await writeWorkbenchRuntime({\n cwd: workDir,\n organizationId,\n reactStrictMode,\n remoteUrl,\n })\n\n const viteConfig: InlineConfig = {\n // Custom cache directory so sanity's vite cache doesn't conflict with local vite projects\n cacheDir,\n configFile: false,\n define: {\n __SANITY_STAGING__: isStaging(),\n 'import.meta.env.SANITY_INTERNAL_WORKBENCH_REMOTE_URL': JSON.stringify(remoteUrl),\n },\n logLevel: 'warn',\n mode: 'development',\n optimizeDeps: {\n // Keep this entry out of pre-bundling so Vite injects its HMR context.\n exclude: [renderDashboardEntry],\n },\n // viteReact looks inert here — it transforms none of the host's own modules —\n // but it's load-bearing for the remotes. It serves the Fast Refresh runtime at\n // /@react-refresh and injects the preamble that defines window.$RefreshReg$. The\n // federated remotes loaded into this page are react-refresh transformed, so\n // without the preamble they throw \"can't detect preamble\", and without the\n // runtime their /@react-refresh import (wired by @module-federation/vite's\n // remoteHmr) fails. Dropping it as dead code broke every panel; see #1262.\n plugins: [viteReact(), ...(remoteUrl ? [remoteManifestPreloadHeaderPlugin(remoteUrl)] : [])],\n resolve: {\n // The generated Vite root cannot reliably resolve this package.\n alias: {[renderDashboardEntry]: renderDashboardPath},\n dedupe: ['react', 'react-dom'],\n },\n root,\n server: {\n host: httpHost,\n port: workbenchPort,\n strictPort: false,\n warmup: {\n clientFiles: ['./workbench.js'],\n },\n },\n }\n\n devDebug('Creating workbench vite server')\n const server = await createServer(viteConfig)\n try {\n await server.listen()\n } catch (err) {\n await server.close()\n output.warn(\n `Workbench dev server failed to start: ${err instanceof Error ? err.message : String(err)}`,\n )\n return undefined\n }\n\n // Vite may have picked a different port if the desired one was occupied\n const addr = server.httpServer?.address()\n const actualPort = typeof addr === 'object' && addr ? addr.port : workbenchPort\n\n // Fire-and-forget: warm the workbench remote's Vite transform pipeline so\n // the first browser request hits a pre-populated module graph.\n if (remoteUrl) {\n fetch(remoteUrl)\n .then((r) => r.body?.cancel())\n .catch(() => {})\n devDebug('Warming workbench remote at %s', remoteUrl)\n }\n\n const detachBridge = attachViteDevServerBridge(server)\n\n return {\n actualPort,\n close: async () => {\n detachBridge()\n await server.close()\n },\n }\n}\n\n// Workbench is opted into via `defineApplication`, which carries the\n// organization ID. Deliberately no fallback (e.g. resolving it from the\n// configured project): the lookup would need an authenticated user and an\n// API round-trip on every startup for something the opt-in already declares.\nconst resolveOrganizationId = (cliConfig: CliConfig): string => {\n if (cliConfig.app?.organizationId) {\n return cliConfig.app.organizationId\n }\n\n throw new Error(\n 'Workbench requires an organization ID. Pass \"organizationId\" to defineApplication() in sanity.cli.ts.',\n )\n}\n\n// Restricts protocol to http(s) so the URL is safe to interpolate into HTML\n// attributes and Link headers downstream.\nconst remoteUrlSchema = z.url({normalize: true, protocol: /^https?$/})\n\nfunction parseRemoteUrl(value: string | undefined): string | undefined {\n if (!value) return undefined\n\n const result = remoteUrlSchema.safeParse(value)\n\n if (!result.success) {\n throw new Error(\n `Invalid SANITY_INTERNAL_WORKBENCH_REMOTE_URL: ${value} (must be an http(s) URL)`,\n )\n }\n\n return result.data\n}\n\n/**\n * Sets a `Link: <remoteUrl>; rel=preload; as=fetch; crossorigin` response header\n * on the index document so the browser can start fetching the Module Federation\n * manifest as soon as response headers arrive — before HTML parsing reaches the\n * in-head preconnect hint. `as=fetch` matches how the federation runtime later\n * retrieves the JSON manifest, allowing the preload entry to satisfy that fetch.\n */\nfunction remoteManifestPreloadHeaderPlugin(remoteUrl: string): Plugin {\n return {\n apply: 'serve',\n configureServer(server) {\n server.middlewares.use((req, res, next) => {\n const pathname = (req.url || '/').split('?')[0]\n if (pathname === '/' || pathname === '/index.html') {\n res.setHeader('Link', `<${remoteUrl}>; rel=preload; as=fetch; crossorigin`)\n }\n next()\n })\n },\n name: 'sanity:workbench-remote-preload-header',\n }\n}\n"],"names":["fileURLToPath","subdebug","isStaging","viteReact","createServer","z","isWorkbenchApp","isWorkbenchConfig","createExposesTracker","acquireWorkbenchLock","getRegisteredServers","isConfigOnlyServer","readWorkbenchLock","watchRegistry","toWireInterface","writeWorkbenchRuntime","devDebug","renderDashboardEntry","renderDashboardPath","URL","url","noop","isLocalApp","server","toApplicationsPayload","servers","applications","filter","map","host","id","interfaces","manifest","port","projectId","type","iface","configs","flatMap","appType","moduleName","version","config","remoteURL","attachViteDevServerBridge","ws","on","_","client","send","setTracker","registryWatcher","hasChanged","close","startWorkbenchRemoteCoordinator","options","httpHost","lock","existing","pid","detachBridge","release","startWorkbenchDevServer","cacheDir","cliConfig","httpPort","workbenchPort","mode","output","reactStrictMode","workDir","app","workbenchAvailable","workbenchLock","result","createWorkbenchViteServer","err","actualPort","updatePort","remoteUrl","undefined","parseRemoteUrl","process","env","SANITY_INTERNAL_WORKBENCH_REMOTE_URL","organizationId","resolveOrganizationId","root","cwd","viteConfig","configFile","define","__SANITY_STAGING__","JSON","stringify","logLevel","optimizeDeps","exclude","plugins","remoteManifestPreloadHeaderPlugin","resolve","alias","dedupe","strictPort","warmup","clientFiles","listen","warn","Error","message","String","addr","httpServer","address","fetch","then","r","body","cancel","catch","remoteUrlSchema","normalize","protocol","value","safeParse","success","data","apply","configureServer","middlewares","use","req","res","next","pathname","split","setHeader","name"],"mappings":"AAAA,SAAQA,aAAa,QAAO,WAAU;AAEtC,SAAqCC,QAAQ,QAAO,mBAAkB;AACtE,SAAQC,SAAS,QAAO,wBAAuB;AAC/C,OAAOC,eAAe,uBAAsB;AAC5C,SAAQC,YAAY,QAA2D,OAAM;AACrF,SAAQC,CAAC,QAAO,WAAU;AAE1B,SAAQC,cAAc,EAAEC,iBAAiB,QAAO,qBAAoB;AACpE,SAAQC,oBAAoB,QAAO,oBAAmB;AACtD,SACEC,oBAAoB,EAEpBC,oBAAoB,EACpBC,kBAAkB,EAClBC,iBAAiB,EACjBC,aA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MiC,wBAAwB,CAACtB;IAC7B,IAAIA,UAAUO,GAAG,EAAEc,gBAAgB;QACjC,OAAOrB,UAAUO,GAAG,CAACc,cAAc;IACrC;IAEA,MAAM,IAAIuB,MACR;AAEJ;AAEA,4EAA4E;AAC5E,0CAA0C;AAC1C,MAAMY,kBAAkBnH,EAAEe,GAAG,CAAC;IAACqG,WAAW;IAAMC,UAAU;AAAU;AAEpE,SAASzC,eAAe0C,KAAyB;IAC/C,IAAI,CAACA,OAAO,OAAO3C;IAEnB,MAAMN,SAAS8C,gBAAgBI,SAAS,CAACD;IAEzC,IAAI,CAACjD,OAAOmD,OAAO,EAAE;QACnB,MAAM,IAAIjB,MACR,CAAC,8CAA8C,EAAEe,MAAM,yBAAyB,CAAC;IAErF;IAEA,OAAOjD,OAAOoD,IAAI;AACpB;AAEA;;;;;;CAMC,GACD,SAAS3B,kCAAkCpB,SAAiB;IAC1D,OAAO;QACLgD,OAAO;QACPC,iBAAgBzG,MAAM;YACpBA,OAAO0G,WAAW,CAACC,GAAG,CAAC,CAACC,KAAKC,KAAKC;gBAChC,MAAMC,WAAW,AAACH,CAAAA,IAAI/G,GAAG,IAAI,GAAE,EAAGmH,KAAK,CAAC,IAAI,CAAC,EAAE;gBAC/C,IAAID,aAAa,OAAOA,aAAa,eAAe;oBAClDF,IAAII,SAAS,CAAC,QAAQ,CAAC,CAAC,EAAEzD,UAAU,qCAAqC,CAAC;gBAC5E;gBACAsD;YACF;QACF;QACAI,MAAM;IACR;AACF"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Convert a registry interface to the remote's wire shape. Views rename
3
+ * `surface` → `type`; workers (already `type`) pass through untouched. The only
4
+ * surface→type conversion for local dev — mirrors the deploy boundary.
5
+ * @internal
6
+ */ export function toWireInterface(iface) {
7
+ if ('surface' in iface) {
8
+ const { surface, ...rest } = iface;
9
+ return {
10
+ ...rest,
11
+ type: surface
12
+ };
13
+ }
14
+ return iface;
15
+ }
16
+
17
+ //# sourceMappingURL=toWireInterface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/actions/dev/toWireInterface.ts"],"sourcesContent":["import {type DevServerInterface} from './deriveConfigs.js'\n\n// The registry stores view interfaces discriminated on `surface` (the author\n// API's word), but the workbench remote — like the deployed application record\n// (see viewDeployment.ts) — keys every interface on `type`. This module is the\n// single, strictly-typed boundary that converts the internal `surface` shape to\n// the wire `type` shape for local dev, so the two can never drift again: the\n// return type carries no `surface`, so a raw registry interface cannot be sent\n// to the remote without passing through here.\n\n/** A registry view interface — the members discriminated on `surface`. */\ntype ViewInterface = Extract<DevServerInterface, {surface: unknown}>\n/** A registry worker interface — already keyed on `type`. */\ntype WorkerInterface = Extract<DevServerInterface, {type: unknown}>\n\n/**\n * An interface as the workbench remote consumes it: every member keyed on\n * `type`. Views have their `surface` renamed to `type`; workers pass through.\n * @internal\n */\nexport type WorkbenchWireInterface =\n | (Omit<ViewInterface, 'surface'> & {type: ViewInterface['surface']})\n | WorkerInterface\n\n/**\n * Convert a registry interface to the remote's wire shape. Views rename\n * `surface` → `type`; workers (already `type`) pass through untouched. The only\n * surface→type conversion for local dev — mirrors the deploy boundary.\n * @internal\n */\nexport function toWireInterface(iface: DevServerInterface): WorkbenchWireInterface {\n if ('surface' in iface) {\n const {surface, ...rest} = iface\n return {...rest, type: surface}\n }\n return iface\n}\n"],"names":["toWireInterface","iface","surface","rest","type"],"mappings":"AAwBA;;;;;CAKC,GACD,OAAO,SAASA,gBAAgBC,KAAyB;IACvD,IAAI,aAAaA,OAAO;QACtB,MAAM,EAACC,OAAO,EAAE,GAAGC,MAAK,GAAGF;QAC3B,OAAO;YAAC,GAAGE,IAAI;YAAEC,MAAMF;QAAO;IAChC;IACA,OAAOD;AACT"}
@@ -4,7 +4,7 @@ import { checkBuiltOutput } from '../deploy/checkBuiltOutput.js';
4
4
  const previewDebug = subdebug('preview');
5
5
  /**
6
6
  * Serve a built workbench app's `dist` as static files over a Vite `preview`
7
- * server, so its `mf-manifest.json` + `remote-entry.js` are reachable exactly as
7
+ * server, so its manifest and referenced assets are reachable exactly as
8
8
  * a live `sanity dev` remote would be — the workbench then federates it in.
9
9
  *
10
10
  * A missing federation build surfaces as `BUILD_NOT_FOUND` (from
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/actions/preview/serveBuiltApplication.ts"],"sourcesContent":["import {subdebug} from '@sanity/cli-core'\nimport {type InlineConfig, preview} from 'vite'\n\nimport {checkBuiltOutput} from '../deploy/checkBuiltOutput.js'\n\nconst previewDebug = subdebug('preview')\n\nexport interface BuiltApplicationServer {\n close: () => Promise<void>\n /** Routable host the remote is reachable at, for the registry's remote URL. */\n host: string\n /** Port Vite bound — may differ from the requested one under non-strict ports. */\n port: number\n}\n\n/**\n * Serve a built workbench app's `dist` as static files over a Vite `preview`\n * server, so its `mf-manifest.json` + `remote-entry.js` are reachable exactly as\n * a live `sanity dev` remote would be — the workbench then federates it in.\n *\n * A missing federation build surfaces as `BUILD_NOT_FOUND` (from\n * `checkBuiltOutput`) so the command shows the same \"run sanity build\" hint the\n * studio preview path does; real I/O errors surface as themselves.\n */\nexport async function serveBuiltApplication(options: {\n cacheDir: string\n httpHost: string\n httpPort: number\n outDir: string\n workDir: string\n}): Promise<BuiltApplicationServer> {\n const {cacheDir, httpHost, httpPort, outDir, workDir} = options\n\n await checkBuiltOutput(outDir)\n\n const config: InlineConfig = {\n // Serve at the root so `mf-manifest.json` and its `publicPath: 'auto'` chunks\n // resolve under the registered remote URL.\n base: '/',\n // Needed for vite to serve `outDir` itself rather than `outDir/dist`.\n build: {outDir},\n cacheDir,\n configFile: false,\n logLevel: 'warn',\n mode: 'production',\n preview: {host: httpHost, port: httpPort, strictPort: false},\n root: workDir,\n }\n\n previewDebug('Serving built remote from %s', outDir)\n const server = await preview(config)\n\n const addr = server.httpServer.address()\n const port = typeof addr === 'object' && addr ? addr.port : httpPort\n\n return {\n // Drop idle keep-alive sockets so shutdown doesn't stall on a browser that\n // kept the connection open — otherwise `close` waits out the teardown grace.\n // `closeAllConnections` isn't on Vite's `HttpServer` type, but is on the\n // underlying node server (18.2+); the optional call tolerates its absence.\n close: () =>\n new Promise((resolve, reject) => {\n server.httpServer.close((err) => (err ? reject(err) : resolve()))\n ;(server.httpServer as {closeAllConnections?: () => void}).closeAllConnections?.()\n }),\n host: httpHost,\n port,\n }\n}\n"],"names":["subdebug","preview","checkBuiltOutput","previewDebug","serveBuiltApplication","options","cacheDir","httpHost","httpPort","outDir","workDir","config","base","build","configFile","logLevel","mode","host","port","strictPort","root","server","addr","httpServer","address","close","Promise","resolve","reject","err","closeAllConnections"],"mappings":"AAAA,SAAQA,QAAQ,QAAO,mBAAkB;AACzC,SAA2BC,OAAO,QAAO,OAAM;AAE/C,SAAQC,gBAAgB,QAAO,gCAA+B;AAE9D,MAAMC,eAAeH,SAAS;AAU9B;;;;;;;;CAQC,GACD,OAAO,eAAeI,sBAAsBC,OAM3C;IACC,MAAM,EAACC,QAAQ,EAAEC,QAAQ,EAAEC,QAAQ,EAAEC,MAAM,EAAEC,OAAO,EAAC,GAAGL;IAExD,MAAMH,iBAAiBO;IAEvB,MAAME,SAAuB;QAC3B,8EAA8E;QAC9E,2CAA2C;QAC3CC,MAAM;QACN,sEAAsE;QACtEC,OAAO;YAACJ;QAAM;QACdH;QACAQ,YAAY;QACZC,UAAU;QACVC,MAAM;QACNf,SAAS;YAACgB,MAAMV;YAAUW,MAAMV;YAAUW,YAAY;QAAK;QAC3DC,MAAMV;IACR;IAEAP,aAAa,gCAAgCM;IAC7C,MAAMY,SAAS,MAAMpB,QAAQU;IAE7B,MAAMW,OAAOD,OAAOE,UAAU,CAACC,OAAO;IACtC,MAAMN,OAAO,OAAOI,SAAS,YAAYA,OAAOA,KAAKJ,IAAI,GAAGV;IAE5D,OAAO;QACL,2EAA2E;QAC3E,6EAA6E;QAC7E,yEAAyE;QACzE,2EAA2E;QAC3EiB,OAAO,IACL,IAAIC,QAAQ,CAACC,SAASC;gBACpBP,OAAOE,UAAU,CAACE,KAAK,CAAC,CAACI,MAASA,MAAMD,OAAOC,OAAOF;gBACpDN,OAAOE,UAAU,CAAwCO,mBAAmB;YAChF;QACFb,MAAMV;QACNW;IACF;AACF"}
1
+ {"version":3,"sources":["../../../src/actions/preview/serveBuiltApplication.ts"],"sourcesContent":["import {subdebug} from '@sanity/cli-core'\nimport {type InlineConfig, preview} from 'vite'\n\nimport {checkBuiltOutput} from '../deploy/checkBuiltOutput.js'\n\nconst previewDebug = subdebug('preview')\n\nexport interface BuiltApplicationServer {\n close: () => Promise<void>\n /** Routable host the remote is reachable at, for the registry's remote URL. */\n host: string\n /** Port Vite bound — may differ from the requested one under non-strict ports. */\n port: number\n}\n\n/**\n * Serve a built workbench app's `dist` as static files over a Vite `preview`\n * server, so its manifest and referenced assets are reachable exactly as\n * a live `sanity dev` remote would be — the workbench then federates it in.\n *\n * A missing federation build surfaces as `BUILD_NOT_FOUND` (from\n * `checkBuiltOutput`) so the command shows the same \"run sanity build\" hint the\n * studio preview path does; real I/O errors surface as themselves.\n */\nexport async function serveBuiltApplication(options: {\n cacheDir: string\n httpHost: string\n httpPort: number\n outDir: string\n workDir: string\n}): Promise<BuiltApplicationServer> {\n const {cacheDir, httpHost, httpPort, outDir, workDir} = options\n\n await checkBuiltOutput(outDir)\n\n const config: InlineConfig = {\n // Serve at the root so `mf-manifest.json` and its `publicPath: 'auto'` chunks\n // resolve under the registered remote URL.\n base: '/',\n // Needed for vite to serve `outDir` itself rather than `outDir/dist`.\n build: {outDir},\n cacheDir,\n configFile: false,\n logLevel: 'warn',\n mode: 'production',\n preview: {host: httpHost, port: httpPort, strictPort: false},\n root: workDir,\n }\n\n previewDebug('Serving built remote from %s', outDir)\n const server = await preview(config)\n\n const addr = server.httpServer.address()\n const port = typeof addr === 'object' && addr ? addr.port : httpPort\n\n return {\n // Drop idle keep-alive sockets so shutdown doesn't stall on a browser that\n // kept the connection open — otherwise `close` waits out the teardown grace.\n // `closeAllConnections` isn't on Vite's `HttpServer` type, but is on the\n // underlying node server (18.2+); the optional call tolerates its absence.\n close: () =>\n new Promise((resolve, reject) => {\n server.httpServer.close((err) => (err ? reject(err) : resolve()))\n ;(server.httpServer as {closeAllConnections?: () => void}).closeAllConnections?.()\n }),\n host: httpHost,\n port,\n }\n}\n"],"names":["subdebug","preview","checkBuiltOutput","previewDebug","serveBuiltApplication","options","cacheDir","httpHost","httpPort","outDir","workDir","config","base","build","configFile","logLevel","mode","host","port","strictPort","root","server","addr","httpServer","address","close","Promise","resolve","reject","err","closeAllConnections"],"mappings":"AAAA,SAAQA,QAAQ,QAAO,mBAAkB;AACzC,SAA2BC,OAAO,QAAO,OAAM;AAE/C,SAAQC,gBAAgB,QAAO,gCAA+B;AAE9D,MAAMC,eAAeH,SAAS;AAU9B;;;;;;;;CAQC,GACD,OAAO,eAAeI,sBAAsBC,OAM3C;IACC,MAAM,EAACC,QAAQ,EAAEC,QAAQ,EAAEC,QAAQ,EAAEC,MAAM,EAAEC,OAAO,EAAC,GAAGL;IAExD,MAAMH,iBAAiBO;IAEvB,MAAME,SAAuB;QAC3B,8EAA8E;QAC9E,2CAA2C;QAC3CC,MAAM;QACN,sEAAsE;QACtEC,OAAO;YAACJ;QAAM;QACdH;QACAQ,YAAY;QACZC,UAAU;QACVC,MAAM;QACNf,SAAS;YAACgB,MAAMV;YAAUW,MAAMV;YAAUW,YAAY;QAAK;QAC3DC,MAAMV;IACR;IAEAP,aAAa,gCAAgCM;IAC7C,MAAMY,SAAS,MAAMpB,QAAQU;IAE7B,MAAMW,OAAOD,OAAOE,UAAU,CAACC,OAAO;IACtC,MAAMN,OAAO,OAAOI,SAAS,YAAYA,OAAOA,KAAKJ,IAAI,GAAGV;IAE5D,OAAO;QACL,2EAA2E;QAC3E,6EAA6E;QAC7E,yEAAyE;QACzE,2EAA2E;QAC3EiB,OAAO,IACL,IAAIC,QAAQ,CAACC,SAASC;gBACpBP,OAAOE,UAAU,CAACE,KAAK,CAAC,CAACI,MAASA,MAAMD,OAAOC,OAAOF;gBACpDN,OAAOE,UAAU,CAAwCO,mBAAmB;YAChF;QACFb,MAAMV;QACNW;IACF;AACF"}
@@ -54,7 +54,7 @@ import { serveBuiltApplication } from './serveBuiltApplication.js';
54
54
  const workbench = resolveWorkbenchApp(cliConfig);
55
55
  if (!workbench) throw new Error('`sanity start` was invoked in a non-workbench application');
56
56
  const inlinedId = await readInlinedAppId(outDir);
57
- const configs = await deriveConfigs(cliConfig.app);
57
+ const configs = await deriveConfigs(cliConfig);
58
58
  const id = inlinedId ?? await buildAppId(workbench);
59
59
  const registration = registerDevServer({
60
60
  configs,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/actions/preview/startWorkbenchPreview.ts"],"sourcesContent":["import {readFile} from 'node:fs/promises'\nimport path from 'node:path'\nimport {styleText} from 'node:util'\n\nimport {type CliConfig, findProjectRoot, type Output} from '@sanity/cli-core'\n\nimport {buildAppId, SANITY_APP_ID_FILE} from '../../appId.js'\nimport {deriveInterfaces} from '../../deriveInterfaces.js'\nimport {resolveWorkbenchApp} from '../../resolveWorkbenchApp.js'\nimport {createServerLifecycle, toDisplayHost} from '../../util/serverOrchestration.js'\nimport {deriveConfigs} from '../dev/deriveConfigs.js'\nimport {type DevServerManifest, registerDevServer} from '../dev/registry.js'\nimport {startWorkbenchDevServer} from '../dev/startWorkbenchDevServer.js'\nimport {serveBuiltApplication} from './serveBuiltApplication.js'\n\nexport interface StartWorkbenchPreviewOptions {\n /** Directory for the workbench Vite server's dependency cache. */\n cacheDir: string\n /** CLI-domain `app.id`/`deployment.appId` deprecation check, run before registering. */\n checkForDeprecatedAppId: () => void\n cliConfig: CliConfig\n /** Extract the project manifest to inline into the registry (studio-vs-app handled by the CLI). */\n extractManifest: (params: {\n configPath: string\n workDir: string\n }) => Promise<DevServerManifest['manifest']>\n httpHost: string\n httpPort: number\n isApp: boolean\n /** The built `dist` directory to serve as the federation remote. */\n outDir: string\n output: Output\n reactStrictMode: boolean\n workDir: string\n}\n\n/**\n * `sanity start` for a workbench app: serve a production build the way dev serves\n * a live one. The same singleton workbench shell renders it and the same registry\n * advertises it — only the remote differs, static files from the build output\n * instead of a live Vite dev server. There's no config watcher or rebuild: a\n * build is fixed, so nothing re-syncs.\n *\n * A running workbench claims the configured port, so the built remote binds the\n * next one. Without one the remote takes the configured port and announces its\n * own URL.\n */\nexport async function startWorkbenchPreview(\n options: StartWorkbenchPreviewOptions,\n): Promise<{close: () => Promise<void>}> {\n const {\n cacheDir,\n checkForDeprecatedAppId,\n cliConfig,\n extractManifest,\n httpHost,\n httpPort,\n isApp,\n outDir,\n output,\n reactStrictMode,\n workDir,\n } = options\n\n const {close, closers, installSignalHandlers} = createServerLifecycle()\n\n const workbench = await startWorkbenchDevServer({\n cacheDir,\n cliConfig,\n httpHost,\n httpPort,\n mode: 'preview',\n output,\n reactStrictMode,\n workDir,\n })\n closers.push(workbench.close)\n\n const remotePort = workbench.workbenchAvailable ? workbench.workbenchPort + 1 : httpPort\n\n const remote = await serveBuiltApplication({\n cacheDir,\n httpHost,\n httpPort: remotePort,\n outDir,\n workDir,\n }).catch(async (err) => {\n await close()\n throw err\n })\n closers.push(remote.close)\n\n try {\n // Callers provide CLI-only validation and manifest extraction to keep them\n // out of workbench-cli.\n checkForDeprecatedAppId()\n const configPath = (await findProjectRoot(workDir)).path\n const workbench = resolveWorkbenchApp(cliConfig)\n\n if (!workbench) throw new Error('`sanity start` was invoked in a non-workbench application')\n const inlinedId = await readInlinedAppId(outDir)\n const configs = await deriveConfigs(cliConfig.app)\n const id = inlinedId ?? (await buildAppId(workbench))\n const registration = registerDevServer({\n configs,\n host: remote.host,\n id,\n interfaces: deriveInterfaces(cliConfig.app, {isApp}),\n manifest: await extractManifest({configPath, workDir}),\n manifestUpdatedAt: new Date().toISOString(),\n port: remote.port,\n projectId: cliConfig?.api?.projectId,\n type: isApp ? 'coreApp' : 'studio',\n workDir,\n })\n closers.push(async () => registration.release())\n } catch (err) {\n await close()\n throw err\n }\n\n if (workbench.workbenchAvailable) {\n const workbenchUrl = `http://${toDisplayHost(workbench.httpHost)}:${workbench.workbenchPort}`\n output.log(\n `Workbench preview server started at ${styleText(['blue', 'underline'], workbenchUrl)} (serving build on port ${remote.port})`,\n )\n } else {\n const remoteUrl = `http://${toDisplayHost(remote.host)}:${remote.port}`\n output.log(`Serving build at ${styleText(['blue', 'underline'], remoteUrl)}`)\n }\n\n installSignalHandlers()\n\n return {close}\n}\n\n/** The id the build inlined into its bundle, or undefined when absent. */\nasync function readInlinedAppId(outDir: string): Promise<string | undefined> {\n try {\n return (await readFile(path.join(outDir, SANITY_APP_ID_FILE), 'utf8')).trim() || undefined\n } catch {\n return undefined\n }\n}\n"],"names":["readFile","path","styleText","findProjectRoot","buildAppId","SANITY_APP_ID_FILE","deriveInterfaces","resolveWorkbenchApp","createServerLifecycle","toDisplayHost","deriveConfigs","registerDevServer","startWorkbenchDevServer","serveBuiltApplication","startWorkbenchPreview","options","cacheDir","checkForDeprecatedAppId","cliConfig","extractManifest","httpHost","httpPort","isApp","outDir","output","reactStrictMode","workDir","close","closers","installSignalHandlers","workbench","mode","push","remotePort","workbenchAvailable","workbenchPort","remote","catch","err","configPath","Error","inlinedId","readInlinedAppId","configs","app","id","registration","host","interfaces","manifest","manifestUpdatedAt","Date","toISOString","port","projectId","api","type","release","workbenchUrl","log","remoteUrl","join","trim","undefined"],"mappings":"AAAA,SAAQA,QAAQ,QAAO,mBAAkB;AACzC,OAAOC,UAAU,YAAW;AAC5B,SAAQC,SAAS,QAAO,YAAW;AAEnC,SAAwBC,eAAe,QAAoB,mBAAkB;AAE7E,SAAQC,UAAU,EAAEC,kBAAkB,QAAO,iBAAgB;AAC7D,SAAQC,gBAAgB,QAAO,4BAA2B;AAC1D,SAAQC,mBAAmB,QAAO,+BAA8B;AAChE,SAAQC,qBAAqB,EAAEC,aAAa,QAAO,oCAAmC;AACtF,SAAQC,aAAa,QAAO,0BAAyB;AACrD,SAAgCC,iBAAiB,QAAO,qBAAoB;AAC5E,SAAQC,uBA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;gBAAYb;YAAO;YACpDwB,mBAAmB,IAAIC,OAAOC,WAAW;YACzCC,MAAMjB,OAAOiB,IAAI;YACjBC,WAAWpC,WAAWqC,KAAKD;YAC3BE,MAAMlC,QAAQ,YAAY;YAC1BI;QACF;QACAE,QAAQI,IAAI,CAAC,UAAYc,aAAaW,OAAO;IAC/C,EAAE,OAAOnB,KAAK;QACZ,MAAMX;QACN,MAAMW;IACR;IAEA,IAAIR,UAAUI,kBAAkB,EAAE;QAChC,MAAMwB,eAAe,CAAC,OAAO,EAAEjD,cAAcqB,UAAUV,QAAQ,EAAE,CAAC,EAAEU,UAAUK,aAAa,EAAE;QAC7FX,OAAOmC,GAAG,CACR,CAAC,oCAAoC,EAAEzD,UAAU;YAAC;YAAQ;SAAY,EAAEwD,cAAc,wBAAwB,EAAEtB,OAAOiB,IAAI,CAAC,CAAC,CAAC;IAElI,OAAO;QACL,MAAMO,YAAY,CAAC,OAAO,EAAEnD,cAAc2B,OAAOW,IAAI,EAAE,CAAC,EAAEX,OAAOiB,IAAI,EAAE;QACvE7B,OAAOmC,GAAG,CAAC,CAAC,iBAAiB,EAAEzD,UAAU;YAAC;YAAQ;SAAY,EAAE0D,YAAY;IAC9E;IAEA/B;IAEA,OAAO;QAACF;IAAK;AACf;AAEA,wEAAwE,GACxE,eAAee,iBAAiBnB,MAAc;IAC5C,IAAI;QACF,OAAO,AAAC,CAAA,MAAMvB,SAASC,KAAK4D,IAAI,CAACtC,QAAQlB,qBAAqB,OAAM,EAAGyD,IAAI,MAAMC;IACnF,EAAE,OAAM;QACN,OAAOA;IACT;AACF"}
1
+ {"version":3,"sources":["../../../src/actions/preview/startWorkbenchPreview.ts"],"sourcesContent":["import {readFile} from 'node:fs/promises'\nimport path from 'node:path'\nimport {styleText} from 'node:util'\n\nimport {type CliConfig, findProjectRoot, type Output} from '@sanity/cli-core'\n\nimport {buildAppId, SANITY_APP_ID_FILE} from '../../appId.js'\nimport {deriveInterfaces} from '../../deriveInterfaces.js'\nimport {resolveWorkbenchApp} from '../../resolveWorkbenchApp.js'\nimport {createServerLifecycle, toDisplayHost} from '../../util/serverOrchestration.js'\nimport {deriveConfigs} from '../dev/deriveConfigs.js'\nimport {type DevServerManifest, registerDevServer} from '../dev/registry.js'\nimport {startWorkbenchDevServer} from '../dev/startWorkbenchDevServer.js'\nimport {serveBuiltApplication} from './serveBuiltApplication.js'\n\nexport interface StartWorkbenchPreviewOptions {\n /** Directory for the workbench Vite server's dependency cache. */\n cacheDir: string\n /** CLI-domain `app.id`/`deployment.appId` deprecation check, run before registering. */\n checkForDeprecatedAppId: () => void\n cliConfig: CliConfig\n /** Extract the project manifest to inline into the registry (studio-vs-app handled by the CLI). */\n extractManifest: (params: {\n configPath: string\n workDir: string\n }) => Promise<DevServerManifest['manifest']>\n httpHost: string\n httpPort: number\n isApp: boolean\n /** The built `dist` directory to serve as the federation remote. */\n outDir: string\n output: Output\n reactStrictMode: boolean\n workDir: string\n}\n\n/**\n * `sanity start` for a workbench app: serve a production build the way dev serves\n * a live one. The same singleton workbench shell renders it and the same registry\n * advertises it — only the remote differs, static files from the build output\n * instead of a live Vite dev server. There's no config watcher or rebuild: a\n * build is fixed, so nothing re-syncs.\n *\n * A running workbench claims the configured port, so the built remote binds the\n * next one. Without one the remote takes the configured port and announces its\n * own URL.\n */\nexport async function startWorkbenchPreview(\n options: StartWorkbenchPreviewOptions,\n): Promise<{close: () => Promise<void>}> {\n const {\n cacheDir,\n checkForDeprecatedAppId,\n cliConfig,\n extractManifest,\n httpHost,\n httpPort,\n isApp,\n outDir,\n output,\n reactStrictMode,\n workDir,\n } = options\n\n const {close, closers, installSignalHandlers} = createServerLifecycle()\n\n const workbench = await startWorkbenchDevServer({\n cacheDir,\n cliConfig,\n httpHost,\n httpPort,\n mode: 'preview',\n output,\n reactStrictMode,\n workDir,\n })\n closers.push(workbench.close)\n\n const remotePort = workbench.workbenchAvailable ? workbench.workbenchPort + 1 : httpPort\n\n const remote = await serveBuiltApplication({\n cacheDir,\n httpHost,\n httpPort: remotePort,\n outDir,\n workDir,\n }).catch(async (err) => {\n await close()\n throw err\n })\n closers.push(remote.close)\n\n try {\n // Callers provide CLI-only validation and manifest extraction to keep them\n // out of workbench-cli.\n checkForDeprecatedAppId()\n const configPath = (await findProjectRoot(workDir)).path\n const workbench = resolveWorkbenchApp(cliConfig)\n\n if (!workbench) throw new Error('`sanity start` was invoked in a non-workbench application')\n const inlinedId = await readInlinedAppId(outDir)\n const configs = await deriveConfigs(cliConfig)\n const id = inlinedId ?? (await buildAppId(workbench))\n const registration = registerDevServer({\n configs,\n host: remote.host,\n id,\n interfaces: deriveInterfaces(cliConfig.app, {isApp}),\n manifest: await extractManifest({configPath, workDir}),\n manifestUpdatedAt: new Date().toISOString(),\n port: remote.port,\n projectId: cliConfig?.api?.projectId,\n type: isApp ? 'coreApp' : 'studio',\n workDir,\n })\n closers.push(async () => registration.release())\n } catch (err) {\n await close()\n throw err\n }\n\n if (workbench.workbenchAvailable) {\n const workbenchUrl = `http://${toDisplayHost(workbench.httpHost)}:${workbench.workbenchPort}`\n output.log(\n `Workbench preview server started at ${styleText(['blue', 'underline'], workbenchUrl)} (serving build on port ${remote.port})`,\n )\n } else {\n const remoteUrl = `http://${toDisplayHost(remote.host)}:${remote.port}`\n output.log(`Serving build at ${styleText(['blue', 'underline'], remoteUrl)}`)\n }\n\n installSignalHandlers()\n\n return {close}\n}\n\n/** The id the build inlined into its bundle, or undefined when absent. */\nasync function readInlinedAppId(outDir: string): Promise<string | undefined> {\n try {\n return (await readFile(path.join(outDir, SANITY_APP_ID_FILE), 'utf8')).trim() || undefined\n } catch {\n return undefined\n }\n}\n"],"names":["readFile","path","styleText","findProjectRoot","buildAppId","SANITY_APP_ID_FILE","deriveInterfaces","resolveWorkbenchApp","createServerLifecycle","toDisplayHost","deriveConfigs","registerDevServer","startWorkbenchDevServer","serveBuiltApplication","startWorkbenchPreview","options","cacheDir","checkForDeprecatedAppId","cliConfig","extractManifest","httpHost","httpPort","isApp","outDir","output","reactStrictMode","workDir","close","closers","installSignalHandlers","workbench","mode","push","remotePort","workbenchAvailable","workbenchPort","remote","catch","err","configPath","Error","inlinedId","readInlinedAppId","configs","id","registration","host","interfaces","app","manifest","manifestUpdatedAt","Date","toISOString","port","projectId","api","type","release","workbenchUrl","log","remoteUrl","join","trim","undefined"],"mappings":"AAAA,SAAQA,QAAQ,QAAO,mBAAkB;AACzC,OAAOC,UAAU,YAAW;AAC5B,SAAQC,SAAS,QAAO,YAAW;AAEnC,SAAwBC,eAAe,QAAoB,mBAAkB;AAE7E,SAAQC,UAAU,EAAEC,kBAAkB,QAAO,iBAAgB;AAC7D,SAAQC,gBAAgB,QAAO,4BAA2B;AAC1D,SAAQC,mBAAmB,QAAO,+BAA8B;AAChE,SAAQC,qBAAqB,EAAEC,aAAa,QAAO,oCAAmC;AACtF,SAAQC,aAAa,QAAO,0BAAyB;AACrD,SAAgCC,iBAAiB,QAAO,qBAAoB;AAC5E,SAAQC,uBAAuB,QAAO,oCAAmC;AACzE,SAAQC,qBAAqB,QAAO,6BAA4B;AAuBhE;;;;;;;;;;CAUC,GACD,OAAO,eAAeC,sBACpBC,OAAqC;IAErC,MAAM,EACJC,QAAQ,EACRC,uBAAuB,EACvBC,SAAS,EACTC,eAAe,EACfC,QAAQ,EACRC,QAAQ,EACRC,KAAK,EACLC,MAAM,EACNC,MAAM,EACNC,eAAe,EACfC,OAAO,EACR,GAAGX;IAEJ,MAAM,EAACY,KAAK,EAAEC,OAAO,EAAEC,qBAAqB,EAAC,GAAGrB;IAEhD,MAAMsB,YAAY,MAAMlB,wBAAwB;QAC9CI;QACAE;QACAE;QACAC;QACAU,MAAM;QACNP;QACAC;QACAC;IACF;IACAE,QAAQI,IAAI,CAACF,UAAUH,KAAK;IAE5B,MAAMM,aAAaH,UAAUI,kBAAkB,GAAGJ,UAAUK,aAAa,GAAG,IAAId;IAEhF,MAAMe,SAAS,MAAMvB,sBAAsB;QACzCG;QACAI;QACAC,UAAUY;QACVV;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AI,EAAE;QACvE7B,OAAOmC,GAAG,CAAC,CAAC,iBAAiB,EAAEzD,UAAU;YAAC;YAAQ;SAAY,EAAE0D,YAAY;IAC9E;IAEA/B;IAEA,OAAO;QAACF;IAAK;AACf;AAEA,wEAAwE,GACxE,eAAee,iBAAiBnB,MAAc;IAC5C,IAAI;QACF,OAAO,AAAC,CAAA,MAAMvB,SAASC,KAAK4D,IAAI,CAACtC,QAAQlB,qBAAqB,OAAM,EAAGyD,IAAI,MAAMC;IACnF,EAAE,OAAM;QACN,OAAOA;IACT;AACF"}
@@ -3,29 +3,29 @@ import { deleteConfig, listConfigs } from '../../services/installations.js';
3
3
  import { resolveInstallationId, summarizeConfig } from '../deploy/deployConfig.js';
4
4
  import { summarizeInterfaces } from '../deploy/summarizeInterfaces.js';
5
5
  /**
6
- * The undeploy adapter for workbench apps, mirroring what a workbench deploy
7
- * creates: apps that expose interfaces delete their Brett application (the
8
- * server soft-deletes its deployments and refuses singletons with active
9
- * installations); a singleton without interfaces — the media library — deletes
10
- * its installation's config snapshots instead.
6
+ * The undeploy adapter for workbench apps and configs, mirroring what a
7
+ * workbench deploy creates: an app that exposes interfaces deletes its Brett
8
+ * application (the server soft-deletes its deployments and refuses singletons
9
+ * with active installations); a config — the media library — deletes its
10
+ * installation's config snapshots instead. The two are severed at the source,
11
+ * so exactly one of `workbench`/`config` is supplied and the brand decides the path.
11
12
  */ export function createWorkbenchUndeployAdapter(options) {
12
- const { appId, organizationId, type, workbench } = options;
13
- // Keyed on singleton-ness, not on a locally declared config, so an undeploy
14
- // still reaches the server's config snapshots after the fields are removed
15
- // from sanity.cli.ts.
16
- const configOnly = !!workbench.isSingleton && !workbench.hasInterfaces;
13
+ const { appId, config, organizationId, type, workbench } = options;
17
14
  // Workbench-internal, so kept off the reported target; resolveTarget stashes it for the delete.
18
15
  let installationId;
19
16
  return {
20
17
  resolveTarget: async ()=>{
21
- if (!configOnly) return resolveApplicationTarget({
22
- appId,
23
- type,
24
- workbench
25
- });
18
+ if (!config) {
19
+ if (!workbench) throw new Error('No workbench app or config to undeploy');
20
+ return resolveApplicationTarget({
21
+ appId,
22
+ type,
23
+ workbench
24
+ });
25
+ }
26
26
  const resolved = await resolveConfigTarget({
27
- organizationId,
28
- workbench
27
+ config,
28
+ organizationId
29
29
  });
30
30
  installationId = resolved.installationId;
31
31
  return resolved.resolution;
@@ -71,12 +71,7 @@ async function resolveApplicationTarget({ appId, type, workbench }) {
71
71
  views
72
72
  },
73
73
  services,
74
- summary: [
75
- ...lines,
76
- ...workbench.isSingleton === undefined ? [] : [
77
- `Singleton: ${workbench.isSingleton}`
78
- ]
79
- ],
74
+ summary: lines,
80
75
  title: application.title,
81
76
  type,
82
77
  url: getApplicationUrl({
@@ -88,10 +83,8 @@ async function resolveApplicationTarget({ appId, type, workbench }) {
88
83
  type: 'found'
89
84
  };
90
85
  }
91
- async function resolveConfigTarget({ organizationId, workbench }) {
92
- const config = workbench.config;
93
- const appType = config?.appType ?? workbench.applicationType;
94
- if (!appType) throw new Error('The app declares no app type to resolve its installation');
86
+ async function resolveConfigTarget({ config, organizationId }) {
87
+ const { appType } = config;
95
88
  if (!organizationId) {
96
89
  throw new Error('sanity.cli.ts does not contain an organization identifier ("app.organizationId"), which is required to resolve the installation');
97
90
  }
@@ -126,15 +119,13 @@ async function resolveConfigTarget({ organizationId, workbench }) {
126
119
  id: null,
127
120
  payload: {
128
121
  appId: null,
129
- ...config ? {
130
- config: summarizeConfig(config)
131
- } : {},
122
+ config: summarizeConfig(config),
132
123
  type: 'coreApp'
133
124
  },
134
- summary: config ? [
125
+ summary: [
135
126
  summarizeConfig(config)
136
- ] : [],
137
- title: workbench.slug,
127
+ ],
128
+ title: appType,
138
129
  type: 'coreApp',
139
130
  url: getWorkbenchUrl(organizationId)
140
131
  },