@ecopages/content-processor 0.3.2 → 0.5.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.
- package/README.md +34 -33
- package/package.json +3 -3
- package/src/codegen.d.ts +9 -5
- package/src/codegen.js +34 -24
- package/src/constants.d.ts +1 -1
- package/src/constants.js +1 -1
- package/src/content-plugins.d.ts +16 -8
- package/src/content-plugins.js +37 -38
- package/src/content-server-boundary-plugin.d.ts +1 -1
- package/src/content-server-boundary-plugin.js +1 -1
- package/src/content-types.d.ts +2 -1
- package/src/content-virtual-modules.d.ts +5 -4
- package/src/plugin.js +1 -1
- package/src/types.d.ts +18 -6
package/README.md
CHANGED
|
@@ -132,7 +132,7 @@ Add one import to the app `modules.d.ts`:
|
|
|
132
132
|
import '@ecopages/content-processor/types';
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
That makes `ecopages:content/*`, `/server`, and `/
|
|
135
|
+
That makes `ecopages:content/*`, `/server`, and `/component` imports resolvable immediately.
|
|
136
136
|
Collection-specific declarations are generated at dev/build time into
|
|
137
137
|
`node_modules/@types/ecopages-content-processor`, which TypeScript loads automatically.
|
|
138
138
|
|
|
@@ -140,24 +140,26 @@ Run `ecopages dev` or `ecopages build` before expecting precise collection entry
|
|
|
140
140
|
|
|
141
141
|
## Virtual module API
|
|
142
142
|
|
|
143
|
-
Each collection exposes `ecopages:content/<collection>` for metadata, `ecopages:content/<collection>/server` for server-only dependency discovery, and `ecopages:content/<collection>/
|
|
143
|
+
Each collection exposes `ecopages:content/<collection>` for metadata, `ecopages:content/<collection>/server` for server-only dependency discovery, and `ecopages:content/<collection>/component` for a lazy MDX component loader that works on the server and in the browser:
|
|
144
144
|
|
|
145
145
|
```typescript
|
|
146
146
|
import { entries, getEntry, getEntryBySegments } from 'ecopages:content/docs';
|
|
147
147
|
import { getComponent, getEntryDependencies } from 'ecopages:content/docs/server';
|
|
148
|
-
import { loadComponent } from 'ecopages:content/docs/
|
|
148
|
+
import { loadComponent } from 'ecopages:content/docs/component';
|
|
149
149
|
import type { Entry } from 'ecopages:content/docs';
|
|
150
150
|
```
|
|
151
151
|
|
|
152
|
-
| Export | Module
|
|
153
|
-
| :----------------------------- |
|
|
154
|
-
| `entries` | entries
|
|
155
|
-
| `getEntry(slug)` | entries
|
|
156
|
-
| `getEntryBySegments(segments)` | entries
|
|
157
|
-
| `getComponent(slug)` | server
|
|
158
|
-
| `getEntryDependencies(slug)` | server
|
|
159
|
-
| `loadComponent(slug)` |
|
|
160
|
-
| `Entry` | entries
|
|
152
|
+
| Export | Module | Description |
|
|
153
|
+
| :----------------------------- | :-------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
154
|
+
| `entries` | entries | Readonly manifest of all entries, sorted by `orderBy`. |
|
|
155
|
+
| `getEntry(slug)` | entries | Lookup by joined slug, e.g. `'getting-started/intro'`. Throws `HttpError.NotFound` when missing. |
|
|
156
|
+
| `getEntryBySegments(segments)` | entries | Lookup by segment array, e.g. `['getting-started', 'intro']`. Throws `HttpError.NotFound` when missing. |
|
|
157
|
+
| `getComponent(slug)` | server | `Promise` of the MDX component for the entry (lazy-loaded per slug). Rejects with `HttpError.NotFound` when missing. |
|
|
158
|
+
| `getEntryDependencies(slug)` | server | Returns `{ components: [entry] }` so collection walks MDX identity. Does not copy the dependency bag. Throws `HttpError.NotFound` when missing. |
|
|
159
|
+
| `loadComponent(slug)` | component | `Promise` of the MDX component for the entry. Server graph: the entry wrapped for cross-Integration rendering. Page Browser Graph: the browser-compiled entry. Rejects with `HttpError.NotFound` when missing. `loadComponent.collection` is the collection name, which stays the same when HMR evaluates the Page again. Pair with `createLazyCollectionComponents()` from `@ecopages/react/lazy-collection-components` when a hydratable React Page renders the component. |
|
|
160
|
+
| `Entry` | entries | **Type only.** `ContentEntry<YourFrontmatter>` — frontmatter fields plus `slug` and `segments`. |
|
|
161
|
+
|
|
162
|
+
`/component` is one specifier with two resolutions. The server graph backs it with the same server module as `/server`, so a Page that imports it shares one component per slug with `getComponent()`. Use it in Pages that also load the entry in the browser, such as a hydratable React Page. `/component` does no asset discovery: a Page that needs the entry's own assets still returns `getEntryDependencies()` from `/server`. The Page Browser Graph backs it with the browser loader and still rejects `/server`, so importing `/component` never makes server code browser-reachable.
|
|
161
163
|
|
|
162
164
|
**Important:** `Entry` exists only in generated `.d.ts` files, not in the runtime cache module. Keep type imports on a separate `import type` line in page files that get bundled. Mixed imports like `import { entries, type Entry }` can cause the bundler to treat `Entry` as a runtime export and fail with `MISSING_EXPORT`.
|
|
163
165
|
|
|
@@ -202,34 +204,33 @@ Return `getEntryDependencies` alone when the Page has no relative assets of its
|
|
|
202
204
|
|
|
203
205
|
### Hydratable React Pages
|
|
204
206
|
|
|
205
|
-
When a React Page must keep the MDX component in its hydrated tree,
|
|
207
|
+
When a React Page must keep the MDX component in its hydrated tree, create one lazy component per slug from the `/component` loader and render it inside a `<Suspense>` boundary. The server waits for the entry and sends it in the HTML; hydration keeps that HTML until the browser has loaded the entry. `getEntryDependencies()` remains server-only and is stripped from the Page Browser Graph. In development, HMR of the Page module renders the already-loaded entry again without the Suspense fallback, and an edited MDX file re-renders that entry without a document reload.
|
|
206
208
|
|
|
207
209
|
```tsx
|
|
208
|
-
import {
|
|
210
|
+
import { Suspense } from 'react';
|
|
211
|
+
import { createLazyCollectionComponents } from '@ecopages/react/lazy-collection-components';
|
|
209
212
|
import { getEntry } from 'ecopages:content/docs';
|
|
210
|
-
import { loadComponent } from 'ecopages:content/docs/
|
|
211
|
-
import {
|
|
213
|
+
import { loadComponent } from 'ecopages:content/docs/component';
|
|
214
|
+
import { getEntryDependencies } from 'ecopages:content/docs/server';
|
|
212
215
|
|
|
213
|
-
const docs =
|
|
214
|
-
|
|
215
|
-
export async function preload({ entry }) {
|
|
216
|
-
await docs.preload(entry.slug);
|
|
217
|
-
}
|
|
216
|
+
const docs = createLazyCollectionComponents(loadComponent);
|
|
218
217
|
|
|
219
218
|
export default eco.page({
|
|
220
|
-
staticProps: async ({ pathname }) => {
|
|
221
|
-
const entry = getEntry(pathname.params.slug);
|
|
222
|
-
await docs.prime(entry.slug, getComponent(entry.slug));
|
|
223
|
-
return { props: { entry } };
|
|
224
|
-
},
|
|
219
|
+
staticProps: async ({ pathname }) => ({ props: { entry: getEntry(pathname.params.slug) } }),
|
|
225
220
|
dependencies: ({ props }) => getEntryDependencies(props.entry.slug),
|
|
226
221
|
render: ({ entry }) => {
|
|
227
222
|
const Content = docs.get(entry.slug);
|
|
228
|
-
return
|
|
223
|
+
return (
|
|
224
|
+
<Suspense fallback={null}>
|
|
225
|
+
<Content />
|
|
226
|
+
</Suspense>
|
|
227
|
+
);
|
|
229
228
|
},
|
|
230
229
|
});
|
|
231
230
|
```
|
|
232
231
|
|
|
232
|
+
Keep the boundary at a fixed place in the Page. During client navigation between entries, React keeps the current entry on screen until the next one has loaded, as long as the boundary is not remounted for each slug.
|
|
233
|
+
|
|
233
234
|
Unknown slugs throw `HttpError.NotFound`, so a catch-all that matches `/docs/[...slug]` still serves the app 404 page instead of a 500.
|
|
234
235
|
|
|
235
236
|
Content MDX entries may import interactive demos and styles without a handwritten `config.dependencies` bag. The MDX loader discovers them:
|
|
@@ -308,12 +309,12 @@ const raw = await scanner.getRawContent('getting-started/intro');
|
|
|
308
309
|
|
|
309
310
|
## Generated artifacts
|
|
310
311
|
|
|
311
|
-
| Path | Purpose
|
|
312
|
-
| :------------------------------------------------------------------- |
|
|
313
|
-
| `.eco/cache/ecopages-content-processor/<collection>.ts` | Generated collection module (manifest + MDX imports).
|
|
314
|
-
| `.eco/cache/ecopages-content-processor/<collection>.browser.ts` | Browser-safe MDX loaders. Relative imports resolve from this file on disk. |
|
|
315
|
-
| `.eco/.server-collections/<collection>/<collection>-<identity>.mjs` | Lazy-built server artifact for one collection.
|
|
316
|
-
| `node_modules/@types/ecopages-content-processor/virtual-module.d.ts` | Generated TypeScript declarations for virtual modules.
|
|
312
|
+
| Path | Purpose |
|
|
313
|
+
| :------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- |
|
|
314
|
+
| `.eco/cache/ecopages-content-processor/<collection>.ts` | Generated collection module (manifest + MDX imports). |
|
|
315
|
+
| `.eco/cache/ecopages-content-processor/<collection>.browser.ts` | Browser-safe MDX loaders behind `/component` in the Page Browser Graph. Relative imports resolve from this file on disk. |
|
|
316
|
+
| `.eco/.server-collections/<collection>/<collection>-<identity>.mjs` | Lazy-built server artifact for one collection. |
|
|
317
|
+
| `node_modules/@types/ecopages-content-processor/virtual-module.d.ts` | Generated TypeScript declarations for virtual modules. |
|
|
317
318
|
|
|
318
319
|
Do not edit generated files manually.
|
|
319
320
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/content-processor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Scan MDX collections at build time and expose typed ecopages:content/* virtual modules for pages and scripts",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"content",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"directory": "packages/processors/content-processor"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@ecopages/core": "0.
|
|
23
|
+
"@ecopages/core": "0.5.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@ecopages/file-system": "0.
|
|
26
|
+
"@ecopages/file-system": "0.5.0",
|
|
27
27
|
"@ecopages/logger": "^0.2.3",
|
|
28
28
|
"remark-frontmatter": "^5.0.0",
|
|
29
29
|
"unified": "^11.0.5",
|
package/src/codegen.d.ts
CHANGED
|
@@ -15,24 +15,28 @@ export declare function renderCollectionEntriesModule(collectionName: string, ma
|
|
|
15
15
|
* loads for the same slug. `getEntryDependencies` returns the entry Component
|
|
16
16
|
* so collection can resolve assets from identity. Keeping MDX imports here
|
|
17
17
|
* leaves the metadata virtual module cheap to load during route preparation.
|
|
18
|
+
* It also exports `getComponent` as `loadComponent`, because the server graph
|
|
19
|
+
* resolves `ecopages:content/<collection>/component` to this module so both
|
|
20
|
+
* specifiers share one cache and one wrapped component per slug.
|
|
18
21
|
*/
|
|
19
|
-
export declare function renderCollectionComponentsModule(
|
|
22
|
+
export declare function renderCollectionComponentsModule(collectionName: string, outputDir: string, entrySources: Array<{
|
|
20
23
|
entry: ContentEntry;
|
|
21
24
|
filePath: string;
|
|
22
25
|
}>): string;
|
|
23
26
|
/**
|
|
24
|
-
* Generates the browser-only virtual module
|
|
27
|
+
* Generates the browser-only virtual module behind `ecopages:content/<collection>/component`
|
|
28
|
+
* in the Page Browser Graph.
|
|
25
29
|
*
|
|
26
30
|
* @remarks
|
|
27
|
-
* Consumers own any
|
|
31
|
+
* Consumers own any per-slug memoization, such as a `React.lazy` lookup. This module only
|
|
28
32
|
* loads one browser-compiled MDX entry on demand and coalesces concurrent
|
|
29
33
|
* requests; it is not a server renderer.
|
|
30
34
|
*/
|
|
31
|
-
export declare function renderCollectionBrowserModule(
|
|
35
|
+
export declare function renderCollectionBrowserModule(collectionName: string, outputDir: string, entrySources: Array<{
|
|
32
36
|
entry: ContentEntry;
|
|
33
37
|
filePath: string;
|
|
34
38
|
}>): string;
|
|
35
|
-
/** Generates
|
|
39
|
+
/** Generates the ambient virtual-module declarations for one collection. */
|
|
36
40
|
export declare function renderCollectionTypesModule(collectionName: string, entryType?: string, typeResolution?: {
|
|
37
41
|
rootDir: string;
|
|
38
42
|
typesOutputFile: string;
|
package/src/codegen.js
CHANGED
|
@@ -9,9 +9,9 @@ function parseEntryTypeImport(entryType) {
|
|
|
9
9
|
}
|
|
10
10
|
return { module, name };
|
|
11
11
|
}
|
|
12
|
-
/** Emits the shared missing-entry
|
|
13
|
-
function
|
|
14
|
-
return '
|
|
12
|
+
/** Emits the shared missing-entry error expression used by both generated runtime modules. */
|
|
13
|
+
function renderUnknownContentEntryError() {
|
|
14
|
+
return 'HttpError.NotFound(`Unknown content entry: ${slug}`)';
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* Generates the metadata-only virtual module for one content collection.
|
|
@@ -39,7 +39,7 @@ ${entriesBySlugLines.join('\n')}
|
|
|
39
39
|
|
|
40
40
|
export function getEntry(slug: string) {
|
|
41
41
|
const entry = entriesBySlug[slug];
|
|
42
|
-
if (!entry) ${
|
|
42
|
+
if (!entry) throw ${renderUnknownContentEntryError()};
|
|
43
43
|
return entry;
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -55,6 +55,10 @@ export function getEntryBySegments(segments: string[]) {
|
|
|
55
55
|
* The loader already attributes identity and live dependency getters. This
|
|
56
56
|
* adapter copies that config by reference onto the callable returned to the
|
|
57
57
|
* route renderer. It does not re-bind or snapshot `config`.
|
|
58
|
+
*
|
|
59
|
+
* The browser variant returns the module's own default export, so loading an
|
|
60
|
+
* unchanged module again returns the same component and an edited module
|
|
61
|
+
* returns a new one.
|
|
58
62
|
*/
|
|
59
63
|
function renderAttachMdxExportsHelper(options) {
|
|
60
64
|
const ownershipImport = options?.ownershipCheck
|
|
@@ -77,10 +81,7 @@ import { invokeContentEntry } from '@ecopages/content-processor/ownership';
|
|
|
77
81
|
) as EcoComponent<Record<string, unknown>>;
|
|
78
82
|
`
|
|
79
83
|
: `
|
|
80
|
-
const component =
|
|
81
|
-
(props: Record<string, unknown> = {}) => Page(props),
|
|
82
|
-
Page,
|
|
83
|
-
) as EcoComponent<Record<string, unknown>>;
|
|
84
|
+
const component = Page;
|
|
84
85
|
`;
|
|
85
86
|
return `${ownershipImport}type ContentMdxModule = {
|
|
86
87
|
default: EcoComponent<Record<string, unknown>>;
|
|
@@ -134,8 +135,11 @@ ${entrySourceFilesBySlugLines.join('\n')}
|
|
|
134
135
|
* loads for the same slug. `getEntryDependencies` returns the entry Component
|
|
135
136
|
* so collection can resolve assets from identity. Keeping MDX imports here
|
|
136
137
|
* leaves the metadata virtual module cheap to load during route preparation.
|
|
138
|
+
* It also exports `getComponent` as `loadComponent`, because the server graph
|
|
139
|
+
* resolves `ecopages:content/<collection>/component` to this module so both
|
|
140
|
+
* specifiers share one cache and one wrapped component per slug.
|
|
137
141
|
*/
|
|
138
|
-
export function renderCollectionComponentsModule(
|
|
142
|
+
export function renderCollectionComponentsModule(collectionName, outputDir, entrySources) {
|
|
139
143
|
return `// This file is auto-generated by @ecopages/content-processor. Do not edit.
|
|
140
144
|
|
|
141
145
|
import type { EcoComponent, PageDependenciesResult } from '@ecopages/core';
|
|
@@ -161,7 +165,7 @@ export async function getComponent(slug: string): Promise<EcoComponent<Record<st
|
|
|
161
165
|
|
|
162
166
|
const load = loadersBySlug[slug];
|
|
163
167
|
if (!load) {
|
|
164
|
-
${
|
|
168
|
+
throw ${renderUnknownContentEntryError()};
|
|
165
169
|
}
|
|
166
170
|
|
|
167
171
|
const loadPromise = (async () => {
|
|
@@ -183,17 +187,22 @@ export async function getEntryDependencies(slug: string): Promise<PageDependenci
|
|
|
183
187
|
components: [component],
|
|
184
188
|
};
|
|
185
189
|
}
|
|
190
|
+
|
|
191
|
+
export const loadComponent = Object.assign((slug: string) => getComponent(slug), {
|
|
192
|
+
collection: ${JSON.stringify(collectionName)},
|
|
193
|
+
});
|
|
186
194
|
`;
|
|
187
195
|
}
|
|
188
196
|
/**
|
|
189
|
-
* Generates the browser-only virtual module
|
|
197
|
+
* Generates the browser-only virtual module behind `ecopages:content/<collection>/component`
|
|
198
|
+
* in the Page Browser Graph.
|
|
190
199
|
*
|
|
191
200
|
* @remarks
|
|
192
|
-
* Consumers own any
|
|
201
|
+
* Consumers own any per-slug memoization, such as a `React.lazy` lookup. This module only
|
|
193
202
|
* loads one browser-compiled MDX entry on demand and coalesces concurrent
|
|
194
203
|
* requests; it is not a server renderer.
|
|
195
204
|
*/
|
|
196
|
-
export function renderCollectionBrowserModule(
|
|
205
|
+
export function renderCollectionBrowserModule(collectionName, outputDir, entrySources) {
|
|
197
206
|
return `// This file is auto-generated by @ecopages/content-processor. Do not edit.
|
|
198
207
|
|
|
199
208
|
import type { EcoComponent } from '@ecopages/core';
|
|
@@ -205,7 +214,7 @@ ${renderMdxLoaderMaps(outputDir, entrySources)}
|
|
|
205
214
|
|
|
206
215
|
const componentLoadPromises = new Map<string, Promise<EcoComponent<Record<string, unknown>>>>();
|
|
207
216
|
|
|
208
|
-
|
|
217
|
+
function loadEntry(slug: string): Promise<EcoComponent<Record<string, unknown>>> {
|
|
209
218
|
const pending = componentLoadPromises.get(slug);
|
|
210
219
|
if (pending) {
|
|
211
220
|
return pending;
|
|
@@ -213,7 +222,7 @@ export function loadComponent(slug: string): Promise<EcoComponent<Record<string,
|
|
|
213
222
|
|
|
214
223
|
const load = loadersBySlug[slug];
|
|
215
224
|
if (!load) {
|
|
216
|
-
${
|
|
225
|
+
return Promise.reject(${renderUnknownContentEntryError()});
|
|
217
226
|
}
|
|
218
227
|
|
|
219
228
|
const loadPromise = load().then((module) => attachMdxExports(module, entrySourceFilesBySlug[slug]!)).finally(() => {
|
|
@@ -222,6 +231,8 @@ export function loadComponent(slug: string): Promise<EcoComponent<Record<string,
|
|
|
222
231
|
componentLoadPromises.set(slug, loadPromise);
|
|
223
232
|
return loadPromise;
|
|
224
233
|
}
|
|
234
|
+
|
|
235
|
+
export const loadComponent = Object.assign(loadEntry, { collection: ${JSON.stringify(collectionName)} });
|
|
225
236
|
`;
|
|
226
237
|
}
|
|
227
238
|
/**
|
|
@@ -282,22 +293,21 @@ function renderCollectionComponentsTypesModule(collectionName) {
|
|
|
282
293
|
export function getEntryDependencies(slug: string): Promise<PageDependenciesResult>;
|
|
283
294
|
}`;
|
|
284
295
|
}
|
|
285
|
-
function
|
|
286
|
-
return `declare module "ecopages:content/${collectionName}/
|
|
287
|
-
import type {
|
|
296
|
+
function renderCollectionLoaderTypesModule(collectionName) {
|
|
297
|
+
return `declare module "ecopages:content/${collectionName}/component" {
|
|
298
|
+
import type { ContentCollectionLoader } from '@ecopages/content-processor/types';
|
|
288
299
|
|
|
289
300
|
/**
|
|
290
|
-
* Loads the
|
|
301
|
+
* Loads the MDX component for one slug on the server and in the browser.
|
|
291
302
|
*
|
|
292
|
-
* @remarks
|
|
293
|
-
* @throws HttpError 404 when the slug is not in the collection.
|
|
303
|
+
* @remarks Rejects with HttpError 404 when the slug is not in the collection.
|
|
294
304
|
*/
|
|
295
|
-
export
|
|
305
|
+
export const loadComponent: ContentCollectionLoader;
|
|
296
306
|
}`;
|
|
297
307
|
}
|
|
298
|
-
/** Generates
|
|
308
|
+
/** Generates the ambient virtual-module declarations for one collection. */
|
|
299
309
|
export function renderCollectionTypesModule(collectionName, entryType, typeResolution) {
|
|
300
|
-
return `${renderCollectionEntriesTypesModule(collectionName, entryType, typeResolution)}\n\n${renderCollectionComponentsTypesModule(collectionName)}\n\n${
|
|
310
|
+
return `${renderCollectionEntriesTypesModule(collectionName, entryType, typeResolution)}\n\n${renderCollectionComponentsTypesModule(collectionName)}\n\n${renderCollectionLoaderTypesModule(collectionName)}`;
|
|
301
311
|
}
|
|
302
312
|
/**
|
|
303
313
|
* Generates the declaration file that gives TypeScript types for every
|
package/src/constants.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Processor registry name and generated artifact namespace. */
|
|
2
2
|
export declare const CONTENT_PROCESSOR_NAME = "ecopages-content-processor";
|
|
3
|
-
export {
|
|
3
|
+
export { CONTENT_COMPONENT_VIRTUAL_MODULE_PATTERN, CONTENT_SERVER_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PREFIX, parseCollectionSpecifier, type ParsedCollectionSpecifier, } from '@ecopages/core/build';
|
|
4
4
|
/** Validates collection keys used in processor options and virtual module paths. */
|
|
5
5
|
export declare const COLLECTION_NAME_PATTERN: RegExp;
|
package/src/constants.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Processor registry name and generated artifact namespace. */
|
|
2
2
|
export const CONTENT_PROCESSOR_NAME = 'ecopages-content-processor';
|
|
3
|
-
export {
|
|
3
|
+
export { CONTENT_COMPONENT_VIRTUAL_MODULE_PATTERN, CONTENT_SERVER_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PREFIX, parseCollectionSpecifier, } from '@ecopages/core/build';
|
|
4
4
|
/** Validates collection keys used in processor options and virtual module paths. */
|
|
5
5
|
export const COLLECTION_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
|
package/src/content-plugins.d.ts
CHANGED
|
@@ -6,23 +6,31 @@ export type EnsureCollectionServerArtifact = (collectionName: string) => Promise
|
|
|
6
6
|
/** Resolves once the generated collection modules exist. */
|
|
7
7
|
export type EnsureCollectionsGenerated = () => Promise<void>;
|
|
8
8
|
export declare function parseCollectionName(specifier: string): string | null;
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Resolves a collection specifier to its generated module path.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* `/component` is the one variant that depends on the graph: the server graph backs it with the
|
|
14
|
+
* server modules, and the Page Browser Graph backs it with the browser modules.
|
|
15
|
+
*/
|
|
16
|
+
export declare function resolveCollectionPath(specifier: string, collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>, collectionBrowserModules?: Record<string, string>, graph?: 'server' | 'browser'): string | null;
|
|
10
17
|
/**
|
|
11
18
|
* Creates the server graph resolver for collection virtual modules.
|
|
12
19
|
*
|
|
13
20
|
* @remarks
|
|
14
|
-
* Compiled `/server` modules are externalized so Node can load the collection
|
|
15
|
-
* artifact directly
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
21
|
+
* Compiled `/server` and `/component` modules are externalized so Node can load the collection
|
|
22
|
+
* artifact directly; both specifiers share one module instance and one component cache. Every
|
|
23
|
+
* resolve waits for `ensureCollectionsGenerated`, so a build that runs before the Processor's
|
|
24
|
+
* `setup()` still finds the generated modules. The Page Browser Graph uses
|
|
25
|
+
* `createContentPluginBundler`, where the content boundary rejects `/server` and `/component`
|
|
26
|
+
* resolves to the browser-safe loader.
|
|
19
27
|
*/
|
|
20
|
-
export declare function createContentPlugin(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>,
|
|
28
|
+
export declare function createContentPlugin(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>, ensureCollectionServerArtifact?: EnsureCollectionServerArtifact, ensureCollectionsGenerated?: EnsureCollectionsGenerated): EcoBuildPlugin;
|
|
21
29
|
/**
|
|
22
30
|
* Creates the Page Browser Graph resolver for collection virtual modules.
|
|
23
31
|
*
|
|
24
32
|
* @remarks
|
|
25
|
-
* It resolves entries and `/
|
|
33
|
+
* It resolves entries and `/component` modules to generated files on disk so
|
|
26
34
|
* relative MDX imports inside those files resolve as filesystem paths. A
|
|
27
35
|
* virtual namespace would keep those imports inside `ecopages-content:` and
|
|
28
36
|
* Rolldown would report them as missing. `/server` resolution is omitted so
|
package/src/content-plugins.js
CHANGED
|
@@ -1,31 +1,38 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import {
|
|
2
|
+
import { CONTENT_COMPONENT_VIRTUAL_MODULE_PATTERN, CONTENT_SERVER_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PATTERN, parseCollectionSpecifier, } from './constants.js';
|
|
3
3
|
export { parseCollectionSpecifier } from './constants.js';
|
|
4
4
|
export function parseCollectionName(specifier) {
|
|
5
5
|
const parsed = parseCollectionSpecifier(specifier);
|
|
6
6
|
return parsed?.collectionName ?? null;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Resolves a collection specifier to its generated module path.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* `/component` is the one variant that depends on the graph: the server graph backs it with the
|
|
13
|
+
* server modules, and the Page Browser Graph backs it with the browser modules.
|
|
14
|
+
*/
|
|
15
|
+
export function resolveCollectionPath(specifier, collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules = {}, graph = 'server') {
|
|
9
16
|
const parsed = parseCollectionSpecifier(specifier);
|
|
10
17
|
if (!parsed) {
|
|
11
18
|
return null;
|
|
12
19
|
}
|
|
13
|
-
if (parsed.variant === '
|
|
20
|
+
if (parsed.variant === 'component' && graph === 'browser') {
|
|
21
|
+
return collectionBrowserModules[parsed.collectionName] ?? null;
|
|
22
|
+
}
|
|
23
|
+
if (parsed.variant === 'server' || parsed.variant === 'component') {
|
|
14
24
|
return (collectionServerCompiledModules?.[parsed.collectionName] ??
|
|
15
25
|
collectionServerModules[parsed.collectionName] ??
|
|
16
26
|
null);
|
|
17
27
|
}
|
|
18
|
-
if (parsed.variant === 'browser') {
|
|
19
|
-
return collectionBrowserModules[parsed.collectionName] ?? null;
|
|
20
|
-
}
|
|
21
28
|
return collectionModules[parsed.collectionName] ?? null;
|
|
22
29
|
}
|
|
23
30
|
function createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, ensureCollectionServerArtifact, ensureCollectionsGenerated, options) {
|
|
24
31
|
return {
|
|
25
32
|
name: 'ecopages:content',
|
|
26
33
|
setup(build) {
|
|
27
|
-
const resolveCollection = (specifier) => resolveCollectionPath(specifier, collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules);
|
|
28
|
-
|
|
34
|
+
const resolveCollection = (specifier) => resolveCollectionPath(specifier, collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, options.graph);
|
|
35
|
+
const resolveGeneratedModule = async (args) => {
|
|
29
36
|
await ensureCollectionsGenerated?.();
|
|
30
37
|
const modulePath = resolveCollection(args.path);
|
|
31
38
|
if (!modulePath) {
|
|
@@ -38,17 +45,16 @@ function createContentResolvePlugin(collectionModules, collectionServerModules,
|
|
|
38
45
|
};
|
|
39
46
|
}
|
|
40
47
|
return { path: modulePath };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
};
|
|
49
|
+
build.onResolve({ filter: CONTENT_VIRTUAL_MODULE_PATTERN }, resolveGeneratedModule);
|
|
50
|
+
if (options.graph === 'server') {
|
|
51
|
+
const resolveServerBackedModule = async (args) => {
|
|
44
52
|
await ensureCollectionsGenerated?.();
|
|
45
53
|
const parsed = parseCollectionSpecifier(args.path);
|
|
46
|
-
if (parsed
|
|
54
|
+
if (parsed) {
|
|
47
55
|
await ensureCollectionServerArtifact?.(parsed.collectionName);
|
|
48
56
|
}
|
|
49
|
-
const compiledPath = parsed?.
|
|
50
|
-
? collectionServerCompiledModules?.[parsed.collectionName]
|
|
51
|
-
: undefined;
|
|
57
|
+
const compiledPath = parsed ? collectionServerCompiledModules?.[parsed.collectionName] : undefined;
|
|
52
58
|
if (compiledPath && options.externalizeCompiledServerModules) {
|
|
53
59
|
return { path: compiledPath, external: true };
|
|
54
60
|
}
|
|
@@ -63,19 +69,13 @@ function createContentResolvePlugin(collectionModules, collectionServerModules,
|
|
|
63
69
|
};
|
|
64
70
|
}
|
|
65
71
|
return { path: modulePath };
|
|
66
|
-
}
|
|
72
|
+
};
|
|
73
|
+
build.onResolve({ filter: CONTENT_SERVER_VIRTUAL_MODULE_PATTERN }, resolveServerBackedModule);
|
|
74
|
+
build.onResolve({ filter: CONTENT_COMPONENT_VIRTUAL_MODULE_PATTERN }, resolveServerBackedModule);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
build.onResolve({ filter: CONTENT_COMPONENT_VIRTUAL_MODULE_PATTERN }, resolveGeneratedModule);
|
|
67
78
|
}
|
|
68
|
-
build.onResolve({ filter: CONTENT_BROWSER_VIRTUAL_MODULE_PATTERN }, async (args) => {
|
|
69
|
-
await ensureCollectionsGenerated?.();
|
|
70
|
-
const modulePath = resolveCollection(args.path);
|
|
71
|
-
if (!modulePath) {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
if (options.namespace) {
|
|
75
|
-
return { namespace: options.namespace, path: modulePath };
|
|
76
|
-
}
|
|
77
|
-
return { path: modulePath };
|
|
78
|
-
});
|
|
79
79
|
},
|
|
80
80
|
};
|
|
81
81
|
}
|
|
@@ -83,20 +83,21 @@ function createContentResolvePlugin(collectionModules, collectionServerModules,
|
|
|
83
83
|
* Creates the server graph resolver for collection virtual modules.
|
|
84
84
|
*
|
|
85
85
|
* @remarks
|
|
86
|
-
* Compiled `/server` modules are externalized so Node can load the collection
|
|
87
|
-
* artifact directly
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
86
|
+
* Compiled `/server` and `/component` modules are externalized so Node can load the collection
|
|
87
|
+
* artifact directly; both specifiers share one module instance and one component cache. Every
|
|
88
|
+
* resolve waits for `ensureCollectionsGenerated`, so a build that runs before the Processor's
|
|
89
|
+
* `setup()` still finds the generated modules. The Page Browser Graph uses
|
|
90
|
+
* `createContentPluginBundler`, where the content boundary rejects `/server` and `/component`
|
|
91
|
+
* resolves to the browser-safe loader.
|
|
91
92
|
*/
|
|
92
|
-
export function createContentPlugin(collectionModules, collectionServerModules, collectionServerCompiledModules,
|
|
93
|
-
return createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules,
|
|
93
|
+
export function createContentPlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, ensureCollectionServerArtifact, ensureCollectionsGenerated) {
|
|
94
|
+
return createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, {}, ensureCollectionServerArtifact, ensureCollectionsGenerated, { graph: 'server', externalizeCompiledServerModules: true });
|
|
94
95
|
}
|
|
95
96
|
/**
|
|
96
97
|
* Creates the Page Browser Graph resolver for collection virtual modules.
|
|
97
98
|
*
|
|
98
99
|
* @remarks
|
|
99
|
-
* It resolves entries and `/
|
|
100
|
+
* It resolves entries and `/component` modules to generated files on disk so
|
|
100
101
|
* relative MDX imports inside those files resolve as filesystem paths. A
|
|
101
102
|
* virtual namespace would keep those imports inside `ecopages-content:` and
|
|
102
103
|
* Rolldown would report them as missing. `/server` resolution is omitted so
|
|
@@ -104,9 +105,7 @@ export function createContentPlugin(collectionModules, collectionServerModules,
|
|
|
104
105
|
* remains browser-reachable.
|
|
105
106
|
*/
|
|
106
107
|
export function createContentPluginBundler(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules = {}, ensureCollectionsGenerated) {
|
|
107
|
-
return createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, undefined, ensureCollectionsGenerated, {
|
|
108
|
-
includeServerModules: false,
|
|
109
|
-
});
|
|
108
|
+
return createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, undefined, ensureCollectionsGenerated, { graph: 'browser' });
|
|
110
109
|
}
|
|
111
110
|
export function getCollectionCachePath(cacheDir, collectionName) {
|
|
112
111
|
return path.join(cacheDir, `${collectionName}.ts`);
|
|
@@ -4,6 +4,6 @@ import type { EcoBuildPlugin } from '@ecopages/core/processor';
|
|
|
4
4
|
*
|
|
5
5
|
* @remarks
|
|
6
6
|
* Server modules lazy-load MDX entries via dynamic `import()` for `getComponent`. Browser builds
|
|
7
|
-
* must never follow that graph; browser-safe MDX is available through `/
|
|
7
|
+
* must never follow that graph; browser-safe MDX is available through `/component` instead.
|
|
8
8
|
*/
|
|
9
9
|
export declare function createContentServerBoundaryPlugin(): EcoBuildPlugin;
|
|
@@ -4,7 +4,7 @@ import { CONTENT_SERVER_VIRTUAL_MODULE_PATTERN } from './constants.js';
|
|
|
4
4
|
*
|
|
5
5
|
* @remarks
|
|
6
6
|
* Server modules lazy-load MDX entries via dynamic `import()` for `getComponent`. Browser builds
|
|
7
|
-
* must never follow that graph; browser-safe MDX is available through `/
|
|
7
|
+
* must never follow that graph; browser-safe MDX is available through `/component` instead.
|
|
8
8
|
*/
|
|
9
9
|
export function createContentServerBoundaryPlugin() {
|
|
10
10
|
return {
|
package/src/content-types.d.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
/// <reference path="./content-virtual-modules.d.js" />
|
|
3
3
|
|
|
4
4
|
export type {
|
|
5
|
-
ContentCollectionBrowserModule,
|
|
6
5
|
ContentCollectionComponentsModule,
|
|
7
6
|
ContentCollectionEntriesModule,
|
|
7
|
+
ContentCollectionLoader,
|
|
8
|
+
ContentCollectionLoaderModule,
|
|
8
9
|
ContentCollectionModule,
|
|
9
10
|
ContentEntry,
|
|
10
11
|
EntryComparator,
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* external module, these declarations become module augmentations and cannot
|
|
7
7
|
* introduce unresolved `ecopages:content/*` specifiers.
|
|
8
8
|
*
|
|
9
|
-
* Variant patterns (`/server`, `/
|
|
9
|
+
* Variant patterns (`/server`, `/component`) must be declared before the catch-all
|
|
10
10
|
* `ecopages:content/*` pattern so TypeScript pattern matching selects them first.
|
|
11
11
|
*/
|
|
12
12
|
|
|
@@ -24,10 +24,11 @@ declare module 'ecopages:content/*/server' {
|
|
|
24
24
|
export function getEntryDependencies(slug: string): Promise<PageDependenciesResult>;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
declare module 'ecopages:content/*/
|
|
28
|
-
import type {
|
|
27
|
+
declare module 'ecopages:content/*/component' {
|
|
28
|
+
import type { ContentCollectionLoader } from '@ecopages/content-processor/types';
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
/** Rejects with HttpError 404 when the slug is not in the collection. */
|
|
31
|
+
export const loadComponent: ContentCollectionLoader;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
declare module 'ecopages:content/*' {
|
package/src/plugin.js
CHANGED
|
@@ -80,7 +80,7 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
80
80
|
plugins() {
|
|
81
81
|
return [
|
|
82
82
|
{
|
|
83
|
-
...createContentPlugin(this.collectionModules, this.collectionServerModules, this.collectionServerCompiledModules,
|
|
83
|
+
...createContentPlugin(this.collectionModules, this.collectionServerModules, this.collectionServerCompiledModules, (collectionName) => this.ensureCollectionServerArtifact(collectionName), () => this.ensureCollectionsGenerated()),
|
|
84
84
|
environments: ['server'],
|
|
85
85
|
},
|
|
86
86
|
...this.browserPlugins,
|
package/src/types.d.ts
CHANGED
|
@@ -44,16 +44,28 @@ export type ContentCollectionComponentsModule = {
|
|
|
44
44
|
*/
|
|
45
45
|
getEntryDependencies(slug: string): Promise<PageDependenciesResult>;
|
|
46
46
|
};
|
|
47
|
-
/**
|
|
48
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Lazy-loads the MDX component for one collection entry.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* The server graph returns the entry wrapped for cross-Integration rendering and the
|
|
52
|
+
* Page Browser Graph returns the browser-compiled entry.
|
|
53
|
+
* Rejects with HttpError 404 when the slug is not in the collection.
|
|
54
|
+
*/
|
|
55
|
+
export type ContentCollectionLoader = {
|
|
56
|
+
(slug: string): Promise<EcoComponent<Record<string, unknown>>>;
|
|
49
57
|
/**
|
|
50
|
-
*
|
|
58
|
+
* The collection name.
|
|
51
59
|
*
|
|
52
60
|
* @remarks
|
|
53
|
-
*
|
|
54
|
-
*
|
|
61
|
+
* The Page Browser Graph inlines the loader into each Page module, so HMR of a Page
|
|
62
|
+
* creates a new loader function. The name stays the same and identifies the collection.
|
|
55
63
|
*/
|
|
56
|
-
|
|
64
|
+
readonly collection: string;
|
|
65
|
+
};
|
|
66
|
+
/** Runtime shape of a generated `ecopages:content/<collection>/component` module. */
|
|
67
|
+
export type ContentCollectionLoaderModule = {
|
|
68
|
+
loadComponent: ContentCollectionLoader;
|
|
57
69
|
};
|
|
58
70
|
/** @deprecated Import entries and server modules separately. */
|
|
59
71
|
export type ContentCollectionModule<TEntry extends Record<string, unknown> = Record<string, unknown>> = ContentCollectionEntriesModule<TEntry> & ContentCollectionComponentsModule;
|