@conloca/cloud-astro 0.2.5 → 0.3.0

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 (226) hide show
  1. package/README.md +75 -24
  2. package/dist/.build-inputs.json +47 -29
  3. package/dist/src/component-manifest.d.ts +11 -0
  4. package/dist/src/component-manifest.d.ts.map +1 -1
  5. package/dist/src/component-manifest.js +20 -2
  6. package/dist/src/component-manifest.js.map +1 -1
  7. package/dist/src/constants.d.ts +4 -4
  8. package/dist/src/constants.js +4 -4
  9. package/dist/src/d3-build.d.ts +6 -2
  10. package/dist/src/d3-build.d.ts.map +1 -1
  11. package/dist/src/d3-build.js +15 -11
  12. package/dist/src/d3-build.js.map +1 -1
  13. package/dist/src/d8-adapters.d.ts +10 -0
  14. package/dist/src/d8-adapters.d.ts.map +1 -0
  15. package/dist/src/d8-adapters.js +19 -0
  16. package/dist/src/d8-adapters.js.map +1 -0
  17. package/dist/src/d8-build.d.ts +99 -0
  18. package/dist/src/d8-build.d.ts.map +1 -0
  19. package/dist/src/d8-build.js +265 -0
  20. package/dist/src/d8-build.js.map +1 -0
  21. package/dist/src/d8-guards.d.ts +29 -0
  22. package/dist/src/d8-guards.d.ts.map +1 -0
  23. package/dist/src/d8-guards.js +145 -0
  24. package/dist/src/d8-guards.js.map +1 -0
  25. package/dist/src/d8-middleware.d.ts +7 -0
  26. package/dist/src/d8-middleware.d.ts.map +1 -0
  27. package/dist/src/d8-middleware.js +15 -0
  28. package/dist/src/d8-middleware.js.map +1 -0
  29. package/dist/src/d8-render.d.ts +9 -0
  30. package/dist/src/d8-render.d.ts.map +1 -0
  31. package/dist/src/d8-render.js +52 -0
  32. package/dist/src/d8-render.js.map +1 -0
  33. package/dist/src/descriptors.d.ts.map +1 -1
  34. package/dist/src/descriptors.js +6 -2
  35. package/dist/src/descriptors.js.map +1 -1
  36. package/dist/src/doctor/ancestor-chain.d.ts +14 -0
  37. package/dist/src/doctor/ancestor-chain.d.ts.map +1 -0
  38. package/dist/src/doctor/ancestor-chain.js +57 -0
  39. package/dist/src/doctor/ancestor-chain.js.map +1 -0
  40. package/dist/src/doctor/checks-d8.d.ts +7 -0
  41. package/dist/src/doctor/checks-d8.d.ts.map +1 -0
  42. package/dist/src/doctor/checks-d8.js +240 -0
  43. package/dist/src/doctor/checks-d8.js.map +1 -0
  44. package/dist/src/doctor/checks-frames.d.ts +5 -0
  45. package/dist/src/doctor/checks-frames.d.ts.map +1 -0
  46. package/dist/src/doctor/checks-frames.js +78 -0
  47. package/dist/src/doctor/checks-frames.js.map +1 -0
  48. package/dist/src/doctor/checks-live.d.ts +5 -14
  49. package/dist/src/doctor/checks-live.d.ts.map +1 -1
  50. package/dist/src/doctor/checks-live.js +9 -21
  51. package/dist/src/doctor/checks-live.js.map +1 -1
  52. package/dist/src/doctor/checks-offline.d.ts +2 -0
  53. package/dist/src/doctor/checks-offline.d.ts.map +1 -1
  54. package/dist/src/doctor/checks-offline.js +17 -8
  55. package/dist/src/doctor/checks-offline.js.map +1 -1
  56. package/dist/src/doctor/checks-preview-live.d.ts +7 -2
  57. package/dist/src/doctor/checks-preview-live.d.ts.map +1 -1
  58. package/dist/src/doctor/checks-preview-live.js +107 -7
  59. package/dist/src/doctor/checks-preview-live.js.map +1 -1
  60. package/dist/src/doctor/checks-preview.d.ts.map +1 -1
  61. package/dist/src/doctor/checks-preview.js +9 -4
  62. package/dist/src/doctor/checks-preview.js.map +1 -1
  63. package/dist/src/doctor/config-scan.d.ts +24 -6
  64. package/dist/src/doctor/config-scan.d.ts.map +1 -1
  65. package/dist/src/doctor/config-scan.js +89 -18
  66. package/dist/src/doctor/config-scan.js.map +1 -1
  67. package/dist/src/doctor/request.d.ts +14 -0
  68. package/dist/src/doctor/request.d.ts.map +1 -0
  69. package/dist/src/doctor/request.js +17 -0
  70. package/dist/src/doctor/request.js.map +1 -0
  71. package/dist/src/doctor/run.d.ts.map +1 -1
  72. package/dist/src/doctor/run.js +2 -0
  73. package/dist/src/doctor/run.js.map +1 -1
  74. package/dist/src/doctor/types.d.ts +8 -0
  75. package/dist/src/doctor/types.d.ts.map +1 -1
  76. package/dist/src/doctor/types.js +8 -0
  77. package/dist/src/doctor/types.js.map +1 -1
  78. package/dist/src/editor-page/bundle.d.ts +4 -0
  79. package/dist/src/editor-page/bundle.d.ts.map +1 -1
  80. package/dist/src/editor-page/bundle.js +55 -7
  81. package/dist/src/editor-page/bundle.js.map +1 -1
  82. package/dist/src/editor-page/data-schemas.d.ts +19 -5
  83. package/dist/src/editor-page/data-schemas.d.ts.map +1 -1
  84. package/dist/src/editor-page/data-schemas.js +17 -13
  85. package/dist/src/editor-page/data-schemas.js.map +1 -1
  86. package/dist/src/editor-page/emit.d.ts +7 -3
  87. package/dist/src/editor-page/emit.d.ts.map +1 -1
  88. package/dist/src/editor-page/emit.js +36 -19
  89. package/dist/src/editor-page/emit.js.map +1 -1
  90. package/dist/src/editor-page/frame-chains.d.ts +51 -0
  91. package/dist/src/editor-page/frame-chains.d.ts.map +1 -0
  92. package/dist/src/editor-page/frame-chains.js +121 -0
  93. package/dist/src/editor-page/frame-chains.js.map +1 -0
  94. package/dist/src/editor-page/page.d.ts +1 -3
  95. package/dist/src/editor-page/page.d.ts.map +1 -1
  96. package/dist/src/editor-page/page.js +3 -5
  97. package/dist/src/editor-page/page.js.map +1 -1
  98. package/dist/src/integration.d.ts.map +1 -1
  99. package/dist/src/integration.js +231 -124
  100. package/dist/src/integration.js.map +1 -1
  101. package/dist/src/node.d.ts +2 -0
  102. package/dist/src/node.d.ts.map +1 -1
  103. package/dist/src/node.js +4 -0
  104. package/dist/src/node.js.map +1 -1
  105. package/dist/src/preview/_resolve-opts.d.ts +26 -1
  106. package/dist/src/preview/_resolve-opts.d.ts.map +1 -1
  107. package/dist/src/preview/_resolve-opts.js.map +1 -1
  108. package/dist/src/preview/banner.d.ts +3 -2
  109. package/dist/src/preview/banner.d.ts.map +1 -1
  110. package/dist/src/preview/banner.js +15 -6
  111. package/dist/src/preview/banner.js.map +1 -1
  112. package/dist/src/preview/content-api.d.ts +23 -1
  113. package/dist/src/preview/content-api.d.ts.map +1 -1
  114. package/dist/src/preview/content-api.js +73 -7
  115. package/dist/src/preview/content-api.js.map +1 -1
  116. package/dist/src/preview/d8-middleware.d.ts +25 -0
  117. package/dist/src/preview/d8-middleware.d.ts.map +1 -0
  118. package/dist/src/preview/d8-middleware.js +94 -0
  119. package/dist/src/preview/d8-middleware.js.map +1 -0
  120. package/dist/src/preview/dispatcher.d.ts +3 -16
  121. package/dist/src/preview/dispatcher.d.ts.map +1 -1
  122. package/dist/src/preview/dispatcher.js +54 -136
  123. package/dist/src/preview/dispatcher.js.map +1 -1
  124. package/dist/src/preview/headers.d.ts +3 -0
  125. package/dist/src/preview/headers.d.ts.map +1 -1
  126. package/dist/src/preview/headers.js +10 -1
  127. package/dist/src/preview/headers.js.map +1 -1
  128. package/dist/src/preview/links.d.ts +23 -0
  129. package/dist/src/preview/links.d.ts.map +1 -0
  130. package/dist/src/preview/links.js +168 -0
  131. package/dist/src/preview/links.js.map +1 -0
  132. package/dist/src/preview/middleware.d.ts +9 -3
  133. package/dist/src/preview/middleware.d.ts.map +1 -1
  134. package/dist/src/preview/middleware.js +36 -16
  135. package/dist/src/preview/middleware.js.map +1 -1
  136. package/dist/src/preview/prefix-router.d.ts +67 -0
  137. package/dist/src/preview/prefix-router.d.ts.map +1 -0
  138. package/dist/src/preview/prefix-router.js +153 -0
  139. package/dist/src/preview/prefix-router.js.map +1 -0
  140. package/dist/src/preview/preview-path.d.ts +8 -3
  141. package/dist/src/preview/preview-path.d.ts.map +1 -1
  142. package/dist/src/preview/preview-path.js +16 -4
  143. package/dist/src/preview/preview-path.js.map +1 -1
  144. package/dist/src/preview/site-middleware.d.ts +8 -2
  145. package/dist/src/preview/site-middleware.d.ts.map +1 -1
  146. package/dist/src/preview/site-middleware.js +95 -31
  147. package/dist/src/preview/site-middleware.js.map +1 -1
  148. package/dist/src/preview/site-page-sources.d.ts +21 -0
  149. package/dist/src/preview/site-page-sources.d.ts.map +1 -0
  150. package/dist/src/preview/site-page-sources.js +129 -0
  151. package/dist/src/preview/site-page-sources.js.map +1 -0
  152. package/dist/src/preview/site-pages.d.ts +18 -0
  153. package/dist/src/preview/site-pages.d.ts.map +1 -0
  154. package/dist/src/preview/site-pages.js +7 -0
  155. package/dist/src/preview/site-pages.js.map +1 -0
  156. package/dist/src/preview/unready-pages.d.ts +21 -0
  157. package/dist/src/preview/unready-pages.d.ts.map +1 -0
  158. package/dist/src/preview/unready-pages.js +54 -0
  159. package/dist/src/preview/unready-pages.js.map +1 -0
  160. package/dist/src/preview-build.d.ts +17 -16
  161. package/dist/src/preview-build.d.ts.map +1 -1
  162. package/dist/src/preview-build.js +37 -40
  163. package/dist/src/preview-build.js.map +1 -1
  164. package/dist/src/preview-image-endpoint.d.ts +8 -0
  165. package/dist/src/preview-image-endpoint.d.ts.map +1 -0
  166. package/dist/src/preview-image-endpoint.js +34 -0
  167. package/dist/src/preview-image-endpoint.js.map +1 -0
  168. package/dist/src/preview-render-seams.d.ts +7 -0
  169. package/dist/src/preview-render-seams.d.ts.map +1 -0
  170. package/dist/src/preview-render-seams.js +27 -0
  171. package/dist/src/preview-render-seams.js.map +1 -0
  172. package/dist/src/site-middleware.d.ts +2 -4
  173. package/dist/src/site-middleware.d.ts.map +1 -1
  174. package/dist/src/site-middleware.js +6 -22
  175. package/dist/src/site-middleware.js.map +1 -1
  176. package/dist/src/types.d.ts +15 -16
  177. package/dist/src/types.d.ts.map +1 -1
  178. package/package.json +138 -6
  179. package/src/component-manifest.ts +30 -4
  180. package/src/constants.ts +4 -4
  181. package/src/d3-build.ts +20 -14
  182. package/src/d8-adapters.ts +19 -0
  183. package/src/d8-build.ts +356 -0
  184. package/src/d8-guards.ts +177 -0
  185. package/src/d8-middleware.ts +17 -0
  186. package/src/d8-render.ts +61 -0
  187. package/src/descriptors.ts +6 -2
  188. package/src/doctor/ancestor-chain.ts +58 -0
  189. package/src/doctor/checks-d8.ts +256 -0
  190. package/src/doctor/checks-frames.ts +84 -0
  191. package/src/doctor/checks-live.ts +10 -39
  192. package/src/doctor/checks-offline.ts +21 -8
  193. package/src/doctor/checks-preview-live.ts +114 -8
  194. package/src/doctor/checks-preview.ts +9 -4
  195. package/src/doctor/config-scan.ts +86 -15
  196. package/src/doctor/request.ts +27 -0
  197. package/src/doctor/run.ts +2 -0
  198. package/src/doctor/types.ts +9 -0
  199. package/src/editor-page/bundle.ts +65 -7
  200. package/src/editor-page/data-schemas.ts +30 -16
  201. package/src/editor-page/emit.ts +52 -24
  202. package/src/editor-page/frame-chains.ts +143 -0
  203. package/src/editor-page/page.ts +3 -4
  204. package/src/integration.ts +277 -135
  205. package/src/node.ts +4 -0
  206. package/src/preview/_resolve-opts.ts +17 -1
  207. package/src/preview/banner.ts +17 -4
  208. package/src/preview/content-api.ts +78 -6
  209. package/src/preview/d8-middleware.ts +124 -0
  210. package/src/preview/dispatcher.ts +63 -153
  211. package/src/preview/headers.ts +10 -1
  212. package/src/preview/links.ts +175 -0
  213. package/src/preview/middleware.ts +41 -19
  214. package/src/preview/prefix-router.ts +197 -0
  215. package/src/preview/preview-path.ts +24 -6
  216. package/src/preview/site-middleware.ts +114 -38
  217. package/src/preview/site-page-sources.ts +146 -0
  218. package/src/preview/site-pages.ts +22 -0
  219. package/src/preview/unready-pages.ts +65 -0
  220. package/src/preview-build.ts +43 -42
  221. package/src/preview-image-endpoint.ts +35 -0
  222. package/src/preview-render-seams.ts +31 -0
  223. package/src/site-middleware.ts +6 -24
  224. package/src/skill/SKILL.md +25 -2
  225. package/src/types.ts +15 -16
  226. package/src/virtual-modules.d.ts +17 -2
@@ -60,6 +60,10 @@ export interface EditorBundleDescriptors {
60
60
  dataBindings?: EditorDataBindings;
61
61
  /** The URL the site serves its media folder at (conlocaCMS `assetRefBase`), which every picked image link starts with. */
62
62
  assetRefBase?: string;
63
+ /** Absolute path of the fragment components render module (conlocaCMS `fragmentComponentsPath`). */
64
+ fragmentComponentsPath?: string;
65
+ /** Route name → absolute path of that route's editor frame module (conlocaCMS routing `editorFrame`). */
66
+ editorFrames?: Record<string, string>;
63
67
  }
64
68
 
65
69
  export interface EditorPageBundleInput {
@@ -164,7 +168,12 @@ export async function bundleEditorPage(input: EditorPageBundleInput): Promise<Ed
164
168
  const entryPath = resolve(input.root, input.puckConfigPath);
165
169
  const descriptors = input.descriptors;
166
170
  const discovered = descriptors?.discoveredMdxComponents ?? [];
167
- const virtualEntry = [
171
+ const renderPath = descriptors?.fragmentComponentsPath && resolve(descriptors.fragmentComponentsPath);
172
+ const frames = Object.entries(descriptors?.editorFrames ?? {}).map(([route, path]) => ({
173
+ route,
174
+ path: resolve(path),
175
+ }));
176
+ const staticEntry = [
168
177
  `export { default } from ${JSON.stringify(entryPath)};`,
169
178
  // Copy the namespace before selecting optional names: a direct named
170
179
  // re-export would reject schemas modules that declare only some of them.
@@ -181,7 +190,22 @@ export async function bundleEditorPage(input: EditorPageBundleInput): Promise<Ed
181
190
  ...(descriptors?.assetRefBase === undefined
182
191
  ? []
183
192
  : [`export const assetRefBase = ${JSON.stringify(descriptors.assetRefBase)};`]),
184
- ].join('\n');
193
+ // Namespace copies, like the schemas module's: a missing export reaches generateBundle's checks.
194
+ ...(renderPath
195
+ ? [
196
+ `import * as __fc from ${JSON.stringify(renderPath)};`,
197
+ 'export const fragmentComponents = { ...__fc }.fragmentComponents;',
198
+ ]
199
+ : []),
200
+ ...(frames.length > 0
201
+ ? [
202
+ ...frames.map(({ path }, index) => `import * as __f${index} from ${JSON.stringify(path)};`),
203
+ `export const editorFrames = { ${frames
204
+ .map(({ route }, index) => `${JSON.stringify(route)}: { ...__f${index} }.default`)
205
+ .join(', ')} };`,
206
+ ]
207
+ : []),
208
+ ];
185
209
 
186
210
  const jsAssets: EditorAsset[] = [];
187
211
  const entryId = '\0conloca-editor-page-entry.js';
@@ -190,14 +214,48 @@ export async function bundleEditorPage(input: EditorPageBundleInput): Promise<Ed
190
214
  resolveId(id) {
191
215
  return id === entryId ? id : null;
192
216
  },
193
- load(id) {
194
- return id === entryId ? virtualEntry : null;
217
+ async load(id) {
218
+ if (id !== entryId) return null;
219
+ if (!descriptors) return staticEntry.join('\n');
220
+ // The renderer publish runs: the @conloca/mdx copy the site's @conloca/astro-cms resolves.
221
+ // A resolve that throws (an exports map without `./render`, as in @conloca/mdx ≤0.2.6) counts as unresolved.
222
+ const tryResolve = (specifier: string, importer: string) =>
223
+ this.resolve(specifier, importer).then(
224
+ (resolved) => resolved?.id,
225
+ () => undefined,
226
+ );
227
+ const astroCms = await tryResolve('@conloca/astro-cms', join(input.root, 'package.json'));
228
+ const renderer = astroCms && (await tryResolve('@conloca/mdx/render', astroCms));
229
+ if (!renderer) {
230
+ input.logger.warn(
231
+ `${PACKAGE_NAME}: @conloca/mdx/render does not resolve from ` +
232
+ `${astroCms ?? `@conloca/astro-cms (looked up from ${input.root})`}; ` +
233
+ "the editor renders fragments with its own renderer instead of the site's.",
234
+ );
235
+ return staticEntry.join('\n');
236
+ }
237
+ return [...staticEntry, `export { evaluateMDXToComponent } from ${JSON.stringify(renderer)};`].join('\n');
195
238
  },
196
239
  async generateBundle() {
240
+ // Vite resolves symlinks (macOS's /var → /private/var), so look modules up by their resolved ids.
241
+ const exportsOf = async (path: string) => {
242
+ const id = (await this.resolve(path))?.id;
243
+ return (id && this.getModuleInfo(id)?.exports) || [];
244
+ };
245
+ if (renderPath && !(await exportsOf(renderPath)).includes('fragmentComponents')) {
246
+ input.logger.warn(
247
+ `${renderPath}: the fragment components module does not export fragmentComponents; ` +
248
+ 'the editor renders fragments with no site components.',
249
+ );
250
+ }
251
+ for (const { route, path } of frames) {
252
+ // oxlint-disable-next-line eslint/no-await-in-loop -- in order, so the first frame without one is reported.
253
+ if (!(await exportsOf(path)).includes('default')) {
254
+ this.error(`${PACKAGE_NAME}: the editor frame for route "${route}" (${path}) has no default export.`);
255
+ }
256
+ }
197
257
  if (!descriptors?.schemasPath) return;
198
- // Vite resolves symlinks (macOS's /var → /private/var), so look the module up by its resolved id.
199
- const schemasId = (await this.resolve(resolve(descriptors.schemasPath)))?.id;
200
- const exports = (schemasId && this.getModuleInfo(schemasId)?.exports) || [];
258
+ const exports = await exportsOf(resolve(descriptors.schemasPath));
201
259
  if (!SCHEMA_MODULE_EXPORTS.some((name) => exports.includes(name))) {
202
260
  input.logger.warn(
203
261
  `${PACKAGE_NAME}: the schemas module at ${descriptors.schemasPath} exports none of ` +
@@ -6,34 +6,48 @@ import { runnerImport, type UserConfig } from 'vite';
6
6
  import { PACKAGE_NAME } from '../constants.js';
7
7
  import { messageOf } from '../message-of.js';
8
8
 
9
+ /** The schemas module's exports the editor page uses, or why it failed to load. */
10
+ export type LoadedSchemasModule = { dataSchemas: unknown; mdxComponents: unknown } | { error: string };
11
+
9
12
  /**
10
- * Writes `<folder>/data-schemas.json`: the JSON schema `metadata` of each `dataSchemas` collection in the schemas
11
- * module, in the module's order. The module runs through the site's own Vite plugins, as the content config does, so
12
- * schema generators such as typia's transform apply. A module that fails to load costs the file, not the build.
13
+ * Loads the schemas module through the site's own Vite plugins, as the content config does, so schema generators such
14
+ * as typia's transform apply. Never throws: a failed load is returned as its message.
13
15
  */
14
- export async function emitDataSchemas(input: {
15
- folder: string;
16
+ export async function loadSchemasModule(input: {
16
17
  root: string;
17
18
  vite: UserConfig;
18
19
  schemasPath: string;
19
- logger: { warn(message: string): void };
20
- }): Promise<void> {
21
- let dataSchemas: unknown;
20
+ }): Promise<LoadedSchemasModule> {
22
21
  try {
23
22
  // shortcut: runnerImport resolves plugins in serve mode, so an apply: 'build' schema transform is skipped (warning, no file); upgrade if a site needs one.
24
- const { module } = await runnerImport<{ dataSchemas?: unknown }>(resolve(input.root, input.schemasPath), {
25
- ...input.vite,
26
- configFile: false,
27
- root: input.root,
28
- logLevel: 'warn',
29
- });
30
- dataSchemas = module.dataSchemas;
23
+ const { module } = await runnerImport<{ dataSchemas?: unknown; mdxComponents?: unknown }>(
24
+ resolve(input.root, input.schemasPath),
25
+ { ...input.vite, configFile: false, root: input.root, logLevel: 'warn' },
26
+ );
27
+ return { dataSchemas: module.dataSchemas, mdxComponents: module.mdxComponents };
31
28
  } catch (error) {
29
+ return { error: messageOf(error) };
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Writes `<folder>/data-schemas.json`: the JSON schema `metadata` of each `dataSchemas` collection in the loaded
35
+ * schemas module, in the module's order. A module that failed to load costs the file, not the build.
36
+ */
37
+ export async function emitDataSchemas(input: {
38
+ folder: string;
39
+ schemasPath: string;
40
+ loaded: LoadedSchemasModule;
41
+ logger: { warn(message: string): void };
42
+ }): Promise<void> {
43
+ const { loaded } = input;
44
+ if ('error' in loaded) {
32
45
  input.logger.warn(
33
- `${PACKAGE_NAME}: the schemas module at ${input.schemasPath} failed to load, so ${DATA_SCHEMAS_FILE} is not written: ${messageOf(error)}`,
46
+ `${PACKAGE_NAME}: the schemas module at ${input.schemasPath} failed to load, so ${DATA_SCHEMAS_FILE} is not written: ${loaded.error}`,
34
47
  );
35
48
  return;
36
49
  }
50
+ const { dataSchemas } = loaded;
37
51
  if (!isRecord(dataSchemas)) return;
38
52
  const collections = Object.fromEntries(
39
53
  Object.entries(dataSchemas).flatMap(([name, entry]) => {
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { existsSync } from 'node:fs';
3
- import { cp, mkdir, writeFile } from 'node:fs/promises';
3
+ import { mkdir, writeFile } from 'node:fs/promises';
4
4
  import { isAbsolute, join, relative, sep } from 'node:path';
5
5
  import {
6
6
  type AstroI18nLocales,
@@ -15,6 +15,8 @@ import {
15
15
  branchDeploysOf,
16
16
  CONLOCA_JSON_MAX_BYTES,
17
17
  contentPathsOf,
18
+ type DeployHost,
19
+ deployHostFromEnv,
18
20
  EDITOR_HOST_PROTOCOL,
19
21
  EDITOR_PAGE_INFO_FORMATS,
20
22
  type EditorMedia,
@@ -40,7 +42,8 @@ import { packageVersion } from '../package-version.js';
40
42
  import type { InheritedOptions } from '../resolve-options.js';
41
43
  import { resolveBuildSiteId } from '../site-id.js';
42
44
  import { bundleEditorPage } from './bundle.js';
43
- import { emitDataSchemas } from './data-schemas.js';
45
+ import { emitDataSchemas, loadSchemasModule } from './data-schemas.js';
46
+ import { emitFrameChains } from './frame-chains.js';
44
47
  import { findFramingBlockers } from './framing.js';
45
48
  import { renderEditorPage } from './page.js';
46
49
  import { baseMismatch } from './site-base.js';
@@ -69,6 +72,9 @@ export interface EmitEditorPageInput {
69
72
  serverOutput: boolean;
70
73
  /** Astro's `i18n`, whose non-default locale folders hold no pages to offer. */
71
74
  i18n: AstroI18nLocales | undefined;
75
+ /** Astro's `trailingSlash` and `build.format`: whether a built page is served with a trailing slash. */
76
+ trailingSlash: 'always' | 'never' | 'ignore';
77
+ buildFormat: 'directory' | 'file' | 'preserve';
72
78
  logger: { info(message: string): void; warn(message: string): void };
73
79
  }
74
80
 
@@ -115,15 +121,22 @@ export async function emitEditorPage(input: EmitEditorPageInput): Promise<void>
115
121
  ...(descriptors === undefined ? {} : { descriptors }),
116
122
  logger,
117
123
  });
118
- if (descriptors?.schemasPath) {
119
- await emitDataSchemas({
120
- folder,
121
- root: input.root,
122
- vite: input.vite,
123
- schemasPath: descriptors.schemasPath,
124
- logger,
125
- });
126
- }
124
+ const schemasPath = descriptors?.schemasPath;
125
+ const schemas = schemasPath
126
+ ? await loadSchemasModule({ root: input.root, vite: input.vite, schemasPath })
127
+ : undefined;
128
+ if (schemasPath && schemas) await emitDataSchemas({ folder, schemasPath, loaded: schemas, logger });
129
+ await emitFrameChains({
130
+ folder,
131
+ root: input.root,
132
+ vite: input.vite,
133
+ editorFrames: descriptors?.editorFrames,
134
+ dataBindings: descriptors?.dataBindings,
135
+ contentRoot,
136
+ trailingSlash: input.trailingSlash,
137
+ buildFormat: input.buildFormat,
138
+ logger,
139
+ });
127
140
 
128
141
  let editorCssFile: string | null = null;
129
142
  if (inherited.editorCss) {
@@ -143,6 +156,14 @@ export async function emitEditorPage(input: EmitEditorPageInput): Promise<void>
143
156
  folder: EDITOR_DIR,
144
157
  puckConfigPath: inherited.puckConfigPath,
145
158
  projectRoot: input.root,
159
+ vite: input.vite,
160
+ ...(descriptors?.fragmentComponentsPath
161
+ ? { fragmentComponentsPath: descriptors.fragmentComponentsPath }
162
+ : {}),
163
+ ...(schemas ? { schemas } : {}),
164
+ ...(descriptors?.discoveredMdxComponents
165
+ ? { discoveredMdxComponents: descriptors.discoveredMdxComponents }
166
+ : {}),
146
167
  logger,
147
168
  });
148
169
 
@@ -160,13 +181,16 @@ export async function emitEditorPage(input: EmitEditorPageInput): Promise<void>
160
181
  }
161
182
  const branchDeploys = branchDeploysFromEnv(process.env);
162
183
  const preview: PreviewSignal = { mode: input.previewMode, protocol: PREVIEW_PROTOCOL };
163
- const written: WrittenConlocaJson = {
164
- ...info,
165
- preview,
166
- ...(branchDeploys === undefined ? {} : { branchDeploys }),
167
- ...(contentPaths === undefined ? {} : { contentPaths }),
168
- ...(media === undefined ? {} : { media }),
169
- };
184
+ const written = withDeployHost(
185
+ {
186
+ ...info,
187
+ preview,
188
+ ...(branchDeploys === undefined ? {} : { branchDeploys }),
189
+ ...(contentPaths === undefined ? {} : { contentPaths }),
190
+ ...(media === undefined ? {} : { media }),
191
+ },
192
+ process.env,
193
+ );
170
194
  const collected = await collectStaticPageRoutes({
171
195
  pagesDir: input.pagesDir,
172
196
  serverOutput: input.serverOutput,
@@ -198,12 +222,6 @@ export async function emitEditorPage(input: EmitEditorPageInput): Promise<void>
198
222
  logger.info(`editor page written: ${base}${EDITOR_DIR}/`);
199
223
  }
200
224
 
201
- /** Copies <from>/_conloca/editor into <to>, for a preview child whose client folder serves the site. */
202
- export async function mirrorEditorPage(fromDir: string, toDir: string): Promise<void> {
203
- await mkdir(join(toDir, '_conloca'), { recursive: true });
204
- await cp(join(fromDir, EDITOR_DIR), join(toDir, EDITOR_DIR), { recursive: true });
205
- }
206
-
207
225
  /**
208
226
  * A separate preview deploy (D4) serves no editor page, but it answers its own conloca.json: its release, protocol
209
227
  * and mode, read next to the site's to tell when the two deploys run different releases.
@@ -242,6 +260,15 @@ export function branchDeploysFromEnv(env: Record<string, string | undefined>): B
242
260
  return project === undefined ? undefined : branchDeploysOf({ branchDeploys: { host: 'cloudflare-pages', project } });
243
261
  }
244
262
 
263
+ /** `written` with the host whose CI runs this build, so a local build writes no `host`. */
264
+ export function withDeployHost(
265
+ written: WrittenConlocaJson,
266
+ env: Record<string, string | undefined>,
267
+ ): WrittenConlocaJson {
268
+ const host = deployHostFromEnv(env);
269
+ return host === undefined ? written : { ...written, host };
270
+ }
271
+
245
272
  /** Each content path under the repository root, written from that root ('' for the root itself); others are dropped. */
246
273
  export function contentPathsFromBuild(paths: readonly string[], repoRoot: string | undefined): string[] | undefined {
247
274
  if (repoRoot === undefined) return undefined;
@@ -258,6 +285,7 @@ export function contentPathsFromBuild(paths: readonly string[], repoRoot: string
258
285
  export interface WrittenConlocaJson extends EditorPageInfo {
259
286
  preview: PreviewSignal;
260
287
  branchDeploys?: BranchDeploys;
288
+ host?: DeployHost;
261
289
  contentPaths?: string[];
262
290
  media?: EditorMedia;
263
291
  }
@@ -0,0 +1,143 @@
1
+ import { writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { type EditorDataBindings, isContentPublished } from '@conloca/content-api';
4
+ import { FileSystemContentAPI, type Site } from '@conloca/content-api/node';
5
+ import { runnerImport, type UserConfig } from 'vite';
6
+ import { PACKAGE_NAME } from '../constants.js';
7
+ import { type AncestorElement, ancestorChain } from '../doctor/ancestor-chain.js';
8
+ import { messageOf } from '../message-of.js';
9
+ import { cleanPagePath } from '../preview/preview-path.js';
10
+
11
+ const VIRTUAL_ID = 'virtual:conloca-frame-chains';
12
+ const RESOLVED_ID = `\0${VIRTUAL_ID}`;
13
+
14
+ /**
15
+ * What `_conloca/editor/frames.json` holds: each framed route's published page, at the path the site serves it at,
16
+ * and the elements its frame wraps it in.
17
+ */
18
+ export interface FramesFile {
19
+ version: 1;
20
+ entries: { route: string; pathname: string; chain: AncestorElement[] }[];
21
+ }
22
+
23
+ /**
24
+ * Renders each route's frame around a page-root marker with the site's React, through the site's Vite plugins, and
25
+ * reads the elements between `<body>` and the marker. Each frame loads in its own runner (the runner closes once the
26
+ * import settles), so a frame that fails costs only its route, under `failures`.
27
+ */
28
+ export async function renderFrameChains(input: {
29
+ root: string;
30
+ vite: UserConfig;
31
+ frames: Record<string, string>;
32
+ pathnames: Record<string, string>;
33
+ }): Promise<{ chains: Record<string, AncestorElement[]>; failures: Record<string, string> }> {
34
+ const chains: Record<string, AncestorElement[]> = {};
35
+ const failures: Record<string, string> = {};
36
+ for (const [route, path] of Object.entries(input.frames)) {
37
+ const code = [
38
+ "import { createElement } from 'react';",
39
+ "import { renderToStaticMarkup } from 'react-dom/server';",
40
+ `import Frame from ${JSON.stringify(path)};`,
41
+ 'export function render(route, pathname) {',
42
+ " return renderToStaticMarkup(createElement(Frame, { pathname }, createElement('div', { 'data-conloca-page-root': '' })));",
43
+ '}',
44
+ '',
45
+ ].join('\n');
46
+ try {
47
+ // One frame at a time: each runner loads the site's Vite plugins, and a failure must cost only its route.
48
+ // oxlint-disable-next-line eslint/no-await-in-loop -- see above.
49
+ const { module } = await runnerImport<{ render: (route: string, pathname: string) => string }>(VIRTUAL_ID, {
50
+ ...input.vite,
51
+ configFile: false,
52
+ root: input.root,
53
+ logLevel: 'warn',
54
+ plugins: [
55
+ ...(input.vite.plugins ?? []),
56
+ {
57
+ name: 'conloca-frame-chains',
58
+ resolveId: (id) => (id === VIRTUAL_ID ? RESOLVED_ID : undefined),
59
+ load: (id) => (id === RESOLVED_ID ? code : undefined),
60
+ },
61
+ ],
62
+ });
63
+ const chain = ancestorChain(`<body>${module.render(route, input.pathnames[route] ?? '/')}</body>`);
64
+ if (chain === undefined) failures[route] = 'the frame did not render its children';
65
+ else chains[route] = chain;
66
+ } catch (error) {
67
+ failures[route] = messageOf(error);
68
+ }
69
+ }
70
+ return { chains, failures };
71
+ }
72
+
73
+ /**
74
+ * Writes `<folder>/frames.json` when a routing entry declares an editor frame: per framed entry, the first page of its
75
+ * collection that is published at build time in the default locale, and its frame's chain. The page's path gets the
76
+ * trailing slash Astro's `trailingSlash` and `build.format` give it, so CC30 fetches it without a redirect. Never
77
+ * fails the build.
78
+ */
79
+ export async function emitFrameChains(input: {
80
+ folder: string;
81
+ root: string;
82
+ vite: UserConfig;
83
+ editorFrames: Record<string, string> | undefined;
84
+ dataBindings: EditorDataBindings | undefined;
85
+ contentRoot: string | undefined;
86
+ buildTime?: Date;
87
+ trailingSlash: 'always' | 'never' | 'ignore';
88
+ buildFormat: 'directory' | 'file' | 'preserve';
89
+ logger: { info(message: string): void; warn(message: string): void };
90
+ }): Promise<void> {
91
+ const { dataBindings, logger } = input;
92
+ const framed = (dataBindings?.entries ?? []).flatMap((entry) => {
93
+ const path = entry.frame === undefined ? undefined : input.editorFrames?.[entry.frame];
94
+ return path === undefined ? [] : [{ route: entry.name, collection: entry.collection, path }];
95
+ });
96
+ if (framed.length === 0 || dataBindings === undefined) return;
97
+ if (input.contentRoot === undefined) {
98
+ logger.info('no content root, so frames.json is not written');
99
+ return;
100
+ }
101
+ const buildTime = input.buildTime ?? new Date();
102
+ let site: Site | null;
103
+ try {
104
+ site = (await FileSystemContentAPI.create({ contentRoot: input.contentRoot })).getSite(dataBindings.siteName);
105
+ } catch (error) {
106
+ logger.warn(`${PACKAGE_NAME}: the content could not be read, so frames.json is not written: ${messageOf(error)}`);
107
+ return;
108
+ }
109
+ const frames: Record<string, string> = {};
110
+ const pathnames: Record<string, string> = {};
111
+ for (const { route, collection, path } of framed) {
112
+ // Scheduled pages are left out: frames.json is public.
113
+ let pathname: string | undefined;
114
+ for (const manifest of site?.listContent({ collection, locales: [dataBindings.locale] }) ?? []) {
115
+ const localized = manifest.locales[dataBindings.locale];
116
+ if (localized && !localized.unpublished && isContentPublished(localized, buildTime)) {
117
+ pathname = localized.pathname;
118
+ break;
119
+ }
120
+ }
121
+ if (pathname === undefined) {
122
+ logger.info(`route ${route} has no published page, so frames.json leaves its editor frame out`);
123
+ continue;
124
+ }
125
+ frames[route] = path;
126
+ pathnames[route] = pathname;
127
+ }
128
+ if (Object.keys(frames).length === 0) return;
129
+ const { chains, failures } = await renderFrameChains({ root: input.root, vite: input.vite, frames, pathnames });
130
+ for (const [route, message] of Object.entries(failures)) {
131
+ logger.warn(
132
+ `${PACKAGE_NAME}: the editor frame of route ${route} (${frames[route]}) failed to render, so frames.json leaves it out: ${message}`,
133
+ );
134
+ }
135
+ const entries = Object.keys(chains).map((route) => ({
136
+ route,
137
+ pathname: cleanPagePath(pathnames[route], input.trailingSlash, input.buildFormat),
138
+ chain: chains[route],
139
+ }));
140
+ if (entries.length === 0) return;
141
+ const file: FramesFile = { version: 1, entries };
142
+ await writeFile(join(input.folder, 'frames.json'), `${JSON.stringify(file, null, 2)}\n`);
143
+ }
@@ -3,13 +3,12 @@ import {
3
3
  EDITOR_HOST_PROTOCOL,
4
4
  EDITOR_UI_MODULE_FILES,
5
5
  type EditorHostPageData,
6
+ editorPageContentSecurityPolicy,
6
7
  } from '@conloca/content-api/editor-host';
7
8
 
8
9
  /**
9
10
  * The editor page's `index.html`: the editor UI from `<editorUiOrigin>/host/v<protocol>/`, its `importmap` script, and
10
- * the page data as a JSON block that never executes. The meta CSP admits scripts only from the page itself, that path
11
- * and the `importmap` script's hash; it leaves styles and fonts alone, so the editor UI's stylesheet may load its web
12
- * font.
11
+ * the page data as a JSON block that never executes, under `editorPageContentSecurityPolicy`.
13
12
  */
14
13
  export function renderEditorPage(data: EditorHostPageData, editorUiOrigin: string): string {
15
14
  const uiPath = `${editorUiOrigin}/host/v${EDITOR_HOST_PROTOCOL}/`;
@@ -24,7 +23,7 @@ export function renderEditorPage(data: EditorHostPageData, editorUiOrigin: strin
24
23
  '<head>',
25
24
  '<meta charset="utf-8">',
26
25
  '<meta name="robots" content="noindex, nofollow">',
27
- `<meta http-equiv="Content-Security-Policy" content="script-src 'self' ${uiPath} 'sha256-${importMapHash}'; object-src 'none'; base-uri 'none'">`,
26
+ `<meta http-equiv="Content-Security-Policy" content="${editorPageContentSecurityPolicy(uiPath, importMapHash)}">`,
28
27
  '<title>Conloca editor</title>',
29
28
  `<script type="importmap">${importMapJson}</script>`,
30
29
  // `<` escaped so no value can close the element; JSON.parse reads `\u003c` back as `<`.