@ecopages/content-processor 0.2.0-rc.9 → 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.
- package/README.md +25 -21
- package/package.json +3 -3
- package/src/collection-server-module-build.service.d.ts +19 -0
- package/src/collection-server-module-build.service.js +42 -0
- package/src/collection-types.d.ts +11 -7
- package/src/constants.d.ts +1 -1
- package/src/constants.js +1 -1
- package/src/content-plugins.d.ts +13 -7
- package/src/content-plugins.js +19 -24
- package/src/content-server-boundary-plugin.d.ts +1 -1
- package/src/content-virtual-modules.d.ts +1 -1
- package/src/ownership.js +1 -1
- package/src/plugin.d.ts +33 -16
- package/src/plugin.js +140 -78
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ Build-time content collections for Ecopages. Scans MDX (or other configured exte
|
|
|
4
4
|
|
|
5
5
|
## Mental model
|
|
6
6
|
|
|
7
|
+
Build contributions use one `plugins(app)` method. Collection modules and types are generated in `setup()`, and a build that resolves a collection first waits for the same generation. The server resolver and browser resolver declare separate environments, so browser compilation cannot select the server implementation by registration order.
|
|
8
|
+
|
|
7
9
|
1. **Declare collections** in `eco.config.ts` — content directory, frontmatter schema, and sort order.
|
|
8
10
|
2. **Scan at build time** — the processor validates frontmatter and writes generated modules under `.eco/cache/`.
|
|
9
11
|
3. **Import the virtual module** — pages and layouts use `ecopages:content/<collection>` for `entries`, metadata, and MDX components.
|
|
@@ -31,6 +33,8 @@ The processor gives you a typed manifest and MDX components at build time. Wire
|
|
|
31
33
|
|
|
32
34
|
During development, edits to an entry's MDX body invalidate its compiled server collection bundle even when its frontmatter and generated manifest are unchanged. The next request rebuilds that bundle and renders the new body.
|
|
33
35
|
|
|
36
|
+
The content processor owns the compiled collection artifacts and their invalidation generation. Core provides the shared server-module build path; collection identity, source tracking, and cache lifetime stay with the content processor.
|
|
37
|
+
|
|
34
38
|
Co-located non-entry files inside a collection directory (for example `survey-form.config.ts`, helper modules, or JSON data) also invalidate the compiled server collection without rewriting the generated entries manifest.
|
|
35
39
|
|
|
36
40
|
## Installation
|
|
@@ -46,14 +50,14 @@ Peer dependency: `@ecopages/core`.
|
|
|
46
50
|
Register the processor in `eco.config.ts`:
|
|
47
51
|
|
|
48
52
|
```typescript
|
|
49
|
-
import {
|
|
53
|
+
import { defineConfig } from '@ecopages/core/config';
|
|
50
54
|
import { contentProcessorPlugin } from '@ecopages/content-processor/plugin';
|
|
51
55
|
import { compareEntriesByField } from '@ecopages/content-processor';
|
|
56
|
+
import { blogFrontmatterSchema } from './src/content/blog-schema';
|
|
52
57
|
import { docsFrontmatterSchema } from './src/content/docs-schema';
|
|
53
58
|
|
|
54
|
-
export default
|
|
55
|
-
|
|
56
|
-
.setProcessors([
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
processors: [
|
|
57
61
|
contentProcessorPlugin({
|
|
58
62
|
options: {
|
|
59
63
|
collections: {
|
|
@@ -73,22 +77,21 @@ export default await new ConfigBuilder()
|
|
|
73
77
|
},
|
|
74
78
|
},
|
|
75
79
|
}),
|
|
76
|
-
]
|
|
77
|
-
|
|
80
|
+
],
|
|
81
|
+
});
|
|
78
82
|
```
|
|
79
83
|
|
|
80
84
|
### Collection options
|
|
81
85
|
|
|
82
|
-
| Option
|
|
83
|
-
|
|
|
84
|
-
| `contentDir`
|
|
85
|
-
| `schema`
|
|
86
|
-
| `entryType`
|
|
87
|
-
| `orderBy`
|
|
88
|
-
| `extensions`
|
|
89
|
-
| `routePrefix`
|
|
90
|
-
| `devPrewarm`
|
|
91
|
-
| `devPrewarmReadiness` | no | `'background'` (default) or `'beforeReady'` to block the framework ready signal until prewarm finishes (listen port may already be open). |
|
|
86
|
+
| Option | Required | Description |
|
|
87
|
+
| :------------ | :------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
88
|
+
| `contentDir` | yes | Directory relative to app `srcDir`, e.g. `content/docs`. |
|
|
89
|
+
| `schema` | yes | Standard Schema validator for frontmatter. |
|
|
90
|
+
| `entryType` | no | Frontmatter type for generated virtual-module types. Format: `./path/to/schema#TypeName`. The processor wraps it as `ContentEntry<YourFrontmatter>`. |
|
|
91
|
+
| `orderBy` | no | Comparator function for manifest sort. Default: {@link compareEntriesBySlug}. Use {@link compareEntriesByField} for frontmatter fields. |
|
|
92
|
+
| `extensions` | no | File extensions to scan. Default: `['.mdx']`. Multi-dot extensions (`.radiant.mdx`, `.react.mdx`) are matched longest-first; declaration order does not matter. |
|
|
93
|
+
| `routePrefix` | no | Public URL prefix for entries, e.g. `/docs`. Used with `devPrewarm`. |
|
|
94
|
+
| `devPrewarm` | no | Dev prewarm: `'first'`, `'all'`, `{ slugs }`, or `{ limit }`. Core SSR-prewarms after the HMR-ready pipeline exists; prewarm schedules renders only and does not control HTML cache admission. |
|
|
92
95
|
|
|
93
96
|
Collection keys must be kebab-case (`docs`, `api-reference`). Each key becomes `ecopages:content/<key>`.
|
|
94
97
|
|
|
@@ -305,11 +308,12 @@ const raw = await scanner.getRawContent('getting-started/intro');
|
|
|
305
308
|
|
|
306
309
|
## Generated artifacts
|
|
307
310
|
|
|
308
|
-
| Path | Purpose
|
|
309
|
-
| :------------------------------------------------------------------- |
|
|
310
|
-
| `.eco/cache/ecopages-content-processor/<collection>.ts` | Generated collection module (manifest + MDX imports).
|
|
311
|
-
| `.eco
|
|
312
|
-
|
|
|
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. |
|
|
313
317
|
|
|
314
318
|
Do not edit generated files manually.
|
|
315
319
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/content-processor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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.3.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@ecopages/file-system": "0.
|
|
26
|
+
"@ecopages/file-system": "0.3.0",
|
|
27
27
|
"@ecopages/logger": "^0.2.3",
|
|
28
28
|
"remark-frontmatter": "^5.0.0",
|
|
29
29
|
"unified": "^11.0.5",
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ProcessorContext } from '@ecopages/core/processor';
|
|
2
|
+
export type CollectionServerBuildArtifact = {
|
|
3
|
+
collectionName: string;
|
|
4
|
+
outputPath: string;
|
|
5
|
+
outputUrl: string;
|
|
6
|
+
sourcePaths: string[];
|
|
7
|
+
buildIdentity: string;
|
|
8
|
+
invalidationVersion: number;
|
|
9
|
+
};
|
|
10
|
+
type CollectionServerArtifacts = Map<string, CollectionServerBuildArtifact>;
|
|
11
|
+
/** Builds one generated collection server module as an independent server unit. */
|
|
12
|
+
export declare function buildCollectionServerModule(input: {
|
|
13
|
+
appConfig: ProcessorContext['config'];
|
|
14
|
+
artifacts: CollectionServerArtifacts;
|
|
15
|
+
collectionName: string;
|
|
16
|
+
sourceFilePath: string;
|
|
17
|
+
ownedSourcePaths: readonly string[];
|
|
18
|
+
}): Promise<CollectionServerBuildArtifact>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import { fileSystem } from '@ecopages/file-system';
|
|
4
|
+
import { buildServerModule, contentHash, recordCollectionBuild } from '@ecopages/core/build';
|
|
5
|
+
import { requireAppRuntime } from '@ecopages/core/integration';
|
|
6
|
+
function createCollectionBuildIdentity(sourceFilePath, ownedSourcePaths) {
|
|
7
|
+
const sourcePaths = [
|
|
8
|
+
...new Set([sourceFilePath, ...ownedSourcePaths].map((sourcePath) => path.resolve(sourcePath))),
|
|
9
|
+
].sort();
|
|
10
|
+
const sourceIdentity = sourcePaths
|
|
11
|
+
.map((sourcePath) => `${sourcePath}\0${fileSystem.exists(sourcePath) ? fileSystem.hash(sourcePath) : 'missing'}`)
|
|
12
|
+
.join('\0');
|
|
13
|
+
return contentHash(`collection-server-build-v3\0${sourceIdentity}`, 16);
|
|
14
|
+
}
|
|
15
|
+
/** Builds one generated collection server module as an independent server unit. */
|
|
16
|
+
export async function buildCollectionServerModule(input) {
|
|
17
|
+
const { appConfig, artifacts, collectionName, sourceFilePath, ownedSourcePaths } = input;
|
|
18
|
+
const runtime = requireAppRuntime(appConfig);
|
|
19
|
+
const invalidationVersion = runtime.moduleLoader.getDevelopmentGeneration();
|
|
20
|
+
const buildIdentity = createCollectionBuildIdentity(sourceFilePath, ownedSourcePaths);
|
|
21
|
+
const existing = artifacts.get(collectionName);
|
|
22
|
+
if (existing?.buildIdentity === buildIdentity && existing.invalidationVersion === invalidationVersion) {
|
|
23
|
+
return existing;
|
|
24
|
+
}
|
|
25
|
+
recordCollectionBuild(runtime.requestPipelineMetrics);
|
|
26
|
+
const outputPath = await buildServerModule(appConfig, {
|
|
27
|
+
entrypoint: sourceFilePath,
|
|
28
|
+
outputDirectory: path.join('.server-collections', collectionName),
|
|
29
|
+
outputFileName: `${collectionName}-${buildIdentity}-${invalidationVersion}.mjs`,
|
|
30
|
+
failureMessage: `Error building collection server module ${collectionName}`,
|
|
31
|
+
});
|
|
32
|
+
const artifact = {
|
|
33
|
+
collectionName,
|
|
34
|
+
outputPath,
|
|
35
|
+
outputUrl: pathToFileURL(outputPath).href,
|
|
36
|
+
sourcePaths: [...ownedSourcePaths, sourceFilePath],
|
|
37
|
+
buildIdentity,
|
|
38
|
+
invalidationVersion,
|
|
39
|
+
};
|
|
40
|
+
artifacts.set(collectionName, artifact);
|
|
41
|
+
return artifact;
|
|
42
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { StandardSchema } from '@ecopages/core';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ContentEntry } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Declares one content collection scanned at build time.
|
|
5
5
|
* Collection keys become virtual module paths: `ecopages:content/<key>`.
|
|
@@ -9,7 +9,16 @@ export type ContentCollectionDefinition<TFrontmatter extends Record<string, unkn
|
|
|
9
9
|
contentDir: string;
|
|
10
10
|
/** Standard Schema validator for frontmatter (Zod, Valibot, ArkType, etc.). */
|
|
11
11
|
schema: StandardSchema<unknown, TFrontmatter>;
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Sorts the collection manifest. Defaults to slug order.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* Declared as a method so a comparator typed for this collection's frontmatter, such as
|
|
17
|
+
* `(a: ContentEntry<DocsFrontmatter>, b: ContentEntry<DocsFrontmatter>) => number`, is
|
|
18
|
+
* accepted in the `collections` record, whose entries are not typed per collection. That
|
|
19
|
+
* is sound because `schema` validates every entry before it reaches the comparator.
|
|
20
|
+
*/
|
|
21
|
+
orderBy?(a: ContentEntry<TFrontmatter>, b: ContentEntry<TFrontmatter>): number;
|
|
13
22
|
/** File extensions treated as content. Defaults to `['.mdx']`. */
|
|
14
23
|
extensions?: string[];
|
|
15
24
|
/**
|
|
@@ -28,11 +37,6 @@ export type ContentCollectionDefinition<TFrontmatter extends Record<string, unkn
|
|
|
28
37
|
* Rendering is performed by core; this only declares pathnames from the manifest.
|
|
29
38
|
*/
|
|
30
39
|
devPrewarm?: ContentDevPrewarmConfig;
|
|
31
|
-
/**
|
|
32
|
-
* When `beforeReady`, core awaits SSR prewarm for declared paths before the dev server reports ready.
|
|
33
|
-
* @default 'background'
|
|
34
|
-
*/
|
|
35
|
-
devPrewarmReadiness?: 'background' | 'beforeReady';
|
|
36
40
|
};
|
|
37
41
|
/** Dev prewarm selection for a content collection manifest. */
|
|
38
42
|
export type ContentDevPrewarmConfig = 'first' | 'all' | {
|
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 { CONTENT_BROWSER_VIRTUAL_MODULE_PATTERN, CONTENT_SERVER_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PREFIX, parseCollectionSpecifier, type ParsedCollectionSpecifier, } from '@ecopages/core/build
|
|
3
|
+
export { CONTENT_BROWSER_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 { CONTENT_BROWSER_VIRTUAL_MODULE_PATTERN, CONTENT_SERVER_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PREFIX, parseCollectionSpecifier, } from '@ecopages/core/build
|
|
3
|
+
export { CONTENT_BROWSER_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
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import type { EcoBuildPlugin } from '@ecopages/core/
|
|
1
|
+
import type { EcoBuildPlugin } from '@ecopages/core/processor';
|
|
2
2
|
import { type ParsedCollectionSpecifier } from './constants.js';
|
|
3
3
|
export type { ParsedCollectionSpecifier };
|
|
4
4
|
export { parseCollectionSpecifier } from './constants.js';
|
|
5
5
|
export type EnsureCollectionServerArtifact = (collectionName: string) => Promise<void>;
|
|
6
|
+
/** Resolves once the generated collection modules exist. */
|
|
7
|
+
export type EnsureCollectionsGenerated = () => Promise<void>;
|
|
6
8
|
export declare function parseCollectionName(specifier: string): string | null;
|
|
7
9
|
export declare function resolveCollectionPath(specifier: string, collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>, collectionBrowserModules?: Record<string, string>): string | null;
|
|
8
10
|
/**
|
|
@@ -10,20 +12,24 @@ export declare function resolveCollectionPath(specifier: string, collectionModul
|
|
|
10
12
|
*
|
|
11
13
|
* @remarks
|
|
12
14
|
* Compiled `/server` modules are externalized so Node can load the collection
|
|
13
|
-
* artifact directly.
|
|
15
|
+
* artifact directly. Every resolve waits for `ensureCollectionsGenerated`, so a build that runs before the
|
|
16
|
+
* Processor's `setup()` still finds the generated modules. The Page Browser Graph uses `createContentPluginBundler`,
|
|
14
17
|
* where the content boundary rejects `/server` and resolves `/browser` to
|
|
15
18
|
* browser-safe source.
|
|
16
19
|
*/
|
|
17
|
-
export declare function createContentPlugin(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>, collectionBrowserModules?: Record<string, string>, ensureCollectionServerArtifact?: EnsureCollectionServerArtifact): EcoBuildPlugin;
|
|
20
|
+
export declare function createContentPlugin(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>, collectionBrowserModules?: Record<string, string>, ensureCollectionServerArtifact?: EnsureCollectionServerArtifact, ensureCollectionsGenerated?: EnsureCollectionsGenerated): EcoBuildPlugin;
|
|
18
21
|
/**
|
|
19
22
|
* Creates the Page Browser Graph resolver for collection virtual modules.
|
|
20
23
|
*
|
|
21
24
|
* @remarks
|
|
22
|
-
* It resolves entries and `/browser` modules
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
+
* It resolves entries and `/browser` modules to generated files on disk so
|
|
26
|
+
* relative MDX imports inside those files resolve as filesystem paths. A
|
|
27
|
+
* virtual namespace would keep those imports inside `ecopages-content:` and
|
|
28
|
+
* Rolldown would report them as missing. `/server` resolution is omitted so
|
|
29
|
+
* the content server-boundary plugin can reject any server artifact that
|
|
30
|
+
* remains browser-reachable.
|
|
25
31
|
*/
|
|
26
|
-
export declare function createContentPluginBundler(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>, collectionBrowserModules?: Record<string, string
|
|
32
|
+
export declare function createContentPluginBundler(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>, collectionServerCompiledModules?: Record<string, string>, collectionBrowserModules?: Record<string, string>, ensureCollectionsGenerated?: EnsureCollectionsGenerated): EcoBuildPlugin;
|
|
27
33
|
export declare function getCollectionCachePath(cacheDir: string, collectionName: string): string;
|
|
28
34
|
export declare function getCollectionServerCachePath(cacheDir: string, collectionName: string): string;
|
|
29
35
|
/** Returns the generated browser module path for one collection. */
|
package/src/content-plugins.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import { fileSystem } from '@ecopages/file-system';
|
|
3
2
|
import { CONTENT_BROWSER_VIRTUAL_MODULE_PATTERN, CONTENT_SERVER_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PATTERN, parseCollectionSpecifier, } from './constants.js';
|
|
4
3
|
export { parseCollectionSpecifier } from './constants.js';
|
|
5
4
|
export function parseCollectionName(specifier) {
|
|
@@ -21,12 +20,13 @@ export function resolveCollectionPath(specifier, collectionModules, collectionSe
|
|
|
21
20
|
}
|
|
22
21
|
return collectionModules[parsed.collectionName] ?? null;
|
|
23
22
|
}
|
|
24
|
-
function createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, ensureCollectionServerArtifact, options) {
|
|
23
|
+
function createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, ensureCollectionServerArtifact, ensureCollectionsGenerated, options) {
|
|
25
24
|
return {
|
|
26
25
|
name: 'ecopages:content',
|
|
27
26
|
setup(build) {
|
|
28
27
|
const resolveCollection = (specifier) => resolveCollectionPath(specifier, collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules);
|
|
29
|
-
build.onResolve({ filter: CONTENT_VIRTUAL_MODULE_PATTERN }, (args) => {
|
|
28
|
+
build.onResolve({ filter: CONTENT_VIRTUAL_MODULE_PATTERN }, async (args) => {
|
|
29
|
+
await ensureCollectionsGenerated?.();
|
|
30
30
|
const modulePath = resolveCollection(args.path);
|
|
31
31
|
if (!modulePath) {
|
|
32
32
|
return undefined;
|
|
@@ -41,8 +41,9 @@ function createContentResolvePlugin(collectionModules, collectionServerModules,
|
|
|
41
41
|
});
|
|
42
42
|
if (options.includeServerModules !== false) {
|
|
43
43
|
build.onResolve({ filter: CONTENT_SERVER_VIRTUAL_MODULE_PATTERN }, async (args) => {
|
|
44
|
+
await ensureCollectionsGenerated?.();
|
|
44
45
|
const parsed = parseCollectionSpecifier(args.path);
|
|
45
|
-
if (parsed?.variant === 'server'
|
|
46
|
+
if (parsed?.variant === 'server') {
|
|
46
47
|
await ensureCollectionServerArtifact?.(parsed.collectionName);
|
|
47
48
|
}
|
|
48
49
|
const compiledPath = parsed?.variant === 'server'
|
|
@@ -64,7 +65,8 @@ function createContentResolvePlugin(collectionModules, collectionServerModules,
|
|
|
64
65
|
return { path: modulePath };
|
|
65
66
|
});
|
|
66
67
|
}
|
|
67
|
-
build.onResolve({ filter: CONTENT_BROWSER_VIRTUAL_MODULE_PATTERN }, (args) => {
|
|
68
|
+
build.onResolve({ filter: CONTENT_BROWSER_VIRTUAL_MODULE_PATTERN }, async (args) => {
|
|
69
|
+
await ensureCollectionsGenerated?.();
|
|
68
70
|
const modulePath = resolveCollection(args.path);
|
|
69
71
|
if (!modulePath) {
|
|
70
72
|
return undefined;
|
|
@@ -82,36 +84,29 @@ function createContentResolvePlugin(collectionModules, collectionServerModules,
|
|
|
82
84
|
*
|
|
83
85
|
* @remarks
|
|
84
86
|
* Compiled `/server` modules are externalized so Node can load the collection
|
|
85
|
-
* artifact directly.
|
|
87
|
+
* artifact directly. Every resolve waits for `ensureCollectionsGenerated`, so a build that runs before the
|
|
88
|
+
* Processor's `setup()` still finds the generated modules. The Page Browser Graph uses `createContentPluginBundler`,
|
|
86
89
|
* where the content boundary rejects `/server` and resolves `/browser` to
|
|
87
90
|
* browser-safe source.
|
|
88
91
|
*/
|
|
89
|
-
export function createContentPlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules = {}, ensureCollectionServerArtifact) {
|
|
90
|
-
return createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, ensureCollectionServerArtifact, { includeServerModules: true, externalizeCompiledServerModules: true });
|
|
92
|
+
export function createContentPlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules = {}, ensureCollectionServerArtifact, ensureCollectionsGenerated) {
|
|
93
|
+
return createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, ensureCollectionServerArtifact, ensureCollectionsGenerated, { includeServerModules: true, externalizeCompiledServerModules: true });
|
|
91
94
|
}
|
|
92
95
|
/**
|
|
93
96
|
* Creates the Page Browser Graph resolver for collection virtual modules.
|
|
94
97
|
*
|
|
95
98
|
* @remarks
|
|
96
|
-
* It resolves entries and `/browser` modules
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
+
* It resolves entries and `/browser` modules to generated files on disk so
|
|
100
|
+
* relative MDX imports inside those files resolve as filesystem paths. A
|
|
101
|
+
* virtual namespace would keep those imports inside `ecopages-content:` and
|
|
102
|
+
* Rolldown would report them as missing. `/server` resolution is omitted so
|
|
103
|
+
* the content server-boundary plugin can reject any server artifact that
|
|
104
|
+
* remains browser-reachable.
|
|
99
105
|
*/
|
|
100
|
-
export function createContentPluginBundler(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules = {}) {
|
|
101
|
-
|
|
102
|
-
namespace: 'ecopages-content',
|
|
106
|
+
export function createContentPluginBundler(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules = {}, ensureCollectionsGenerated) {
|
|
107
|
+
return createContentResolvePlugin(collectionModules, collectionServerModules, collectionServerCompiledModules, collectionBrowserModules, undefined, ensureCollectionsGenerated, {
|
|
103
108
|
includeServerModules: false,
|
|
104
109
|
});
|
|
105
|
-
return {
|
|
106
|
-
name: plugin.name,
|
|
107
|
-
setup(build) {
|
|
108
|
-
plugin.setup?.(build);
|
|
109
|
-
build.onLoad({ filter: /.*/, namespace: 'ecopages-content' }, async (args) => {
|
|
110
|
-
const contents = await fileSystem.readFile(args.path);
|
|
111
|
-
return { contents, loader: 'ts' };
|
|
112
|
-
});
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
110
|
}
|
|
116
111
|
export function getCollectionCachePath(cacheDir, collectionName) {
|
|
117
112
|
return path.join(cacheDir, `${collectionName}.ts`);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Fallback declarations for content virtual modules before the configured
|
|
3
|
-
* collection declarations are generated by `ecopages
|
|
3
|
+
* collection declarations are generated by `ecopages types`, `ecopages dev`, or `ecopages build`.
|
|
4
4
|
*
|
|
5
5
|
* @remarks This must remain a script (without imports or exports). In an
|
|
6
6
|
* external module, these declarations become module augmentations and cannot
|
package/src/ownership.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getComponentRenderContext
|
|
1
|
+
import { getComponentRenderContext } from '@ecopages/core/integration';
|
|
2
2
|
/**
|
|
3
3
|
* Throws when an MDX content entry is invoked under a different integration
|
|
4
4
|
* than the one that compiled it.
|
package/src/plugin.d.ts
CHANGED
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
* ContentProcessorPlugin
|
|
3
3
|
* @module @ecopages/content-processor
|
|
4
4
|
*/
|
|
5
|
-
import { Processor, type EcoBuildPlugin, type ProcessorConfig } from '@ecopages/core/
|
|
5
|
+
import { Processor, type EcoBuildPlugin, type ProcessorConfig } from '@ecopages/core/processor';
|
|
6
6
|
import type { ContentProcessorConfig } from './collection-types.js';
|
|
7
7
|
/**
|
|
8
8
|
* Build-time processor that scans configured content collections and exposes
|
|
9
9
|
* each one as `ecopages:content/<collection>` virtual modules.
|
|
10
10
|
*/
|
|
11
11
|
export declare class ContentProcessorPlugin extends Processor<ContentProcessorConfig> {
|
|
12
|
-
private
|
|
12
|
+
private collectionsGeneration?;
|
|
13
13
|
private readonly scanners;
|
|
14
14
|
private readonly collectionBuildPromises;
|
|
15
15
|
private readonly collectionGenerations;
|
|
16
|
+
private readonly collectionServerBuiltAt;
|
|
17
|
+
private readonly collectionServerArtifacts;
|
|
16
18
|
/**
|
|
17
19
|
* Absolute paths to generated collection entry modules, keyed by collection name.
|
|
18
20
|
* Virtual-module plugins close over this object so mutations stay live.
|
|
@@ -28,10 +30,9 @@ export declare class ContentProcessorPlugin extends Processor<ContentProcessorCo
|
|
|
28
30
|
* Absolute paths to pre-built collection server artifacts for route-module externalization.
|
|
29
31
|
*/
|
|
30
32
|
readonly collectionServerCompiledModules: Record<string, string>;
|
|
31
|
-
constructor(config: Omit<ProcessorConfig<ContentProcessorConfig>, 'name'
|
|
32
|
-
get
|
|
33
|
-
|
|
34
|
-
invalidateServerArtifacts(): void;
|
|
33
|
+
constructor(config: Omit<ProcessorConfig<ContentProcessorConfig>, 'name'>);
|
|
34
|
+
private get browserPlugins();
|
|
35
|
+
plugins(): EcoBuildPlugin[];
|
|
35
36
|
private getCollectionsConfig;
|
|
36
37
|
private getCollectionContentRoot;
|
|
37
38
|
private writeGeneratedFile;
|
|
@@ -50,7 +51,11 @@ export declare class ContentProcessorPlugin extends Processor<ContentProcessorCo
|
|
|
50
51
|
private regenerateCollections;
|
|
51
52
|
private regenerateAllCollections;
|
|
52
53
|
private isCollectionEntryPath;
|
|
53
|
-
private
|
|
54
|
+
private handleContentFileChanges;
|
|
55
|
+
private collectCollectionWatchChanges;
|
|
56
|
+
private recordCollectionWatchChange;
|
|
57
|
+
private applyCollectionWatchChanges;
|
|
58
|
+
private updateChangedCollectionEntries;
|
|
54
59
|
private initializeWatchPaths;
|
|
55
60
|
private generateTypes;
|
|
56
61
|
private buildCollectionServerArtifact;
|
|
@@ -63,19 +68,31 @@ export declare class ContentProcessorPlugin extends Processor<ContentProcessorCo
|
|
|
63
68
|
* builds share the same promise.
|
|
64
69
|
*/
|
|
65
70
|
ensureCollectionServerArtifact(collectionName: string): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* The app module loader's development generation, which advances whenever server source changes.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* A compiled collection embeds the modules its entries import, such as a component, so it is stale once the
|
|
76
|
+
* generation differs from the one it was built at. Core announces no invalidation to Processors.
|
|
77
|
+
*/
|
|
78
|
+
private getServerSourceGeneration;
|
|
79
|
+
/**
|
|
80
|
+
* Generates the collection modules and types once, and shares that work between callers.
|
|
81
|
+
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* {@link setup} and every collection resolve call it, so a build that starts before `setup()` waits for the
|
|
84
|
+
* generated modules instead of finding none. A failed generation is dropped so the next caller retries.
|
|
85
|
+
*/
|
|
66
86
|
private ensureCollectionsGenerated;
|
|
67
|
-
prepareBuildContributions(): Promise<void>;
|
|
68
87
|
setup(): Promise<void>;
|
|
69
|
-
process(_input: unknown): Promise<void>;
|
|
70
88
|
/**
|
|
71
|
-
*
|
|
89
|
+
* Adds the pathnames of collections with `devPrewarm` and a `routePrefix` to the app's `devPrewarmPaths`.
|
|
72
90
|
*
|
|
73
91
|
* @remarks
|
|
74
|
-
* Core
|
|
92
|
+
* Core renders the list in the background after the server is listening. The entries are read from the
|
|
93
|
+
* manifests at startup, so an entry created later is not prewarmed until the next start. Existing paths stay,
|
|
94
|
+
* and a repeated `setup()` adds nothing twice.
|
|
75
95
|
*/
|
|
76
|
-
|
|
77
|
-
pathnames: readonly string[];
|
|
78
|
-
readiness: 'background' | 'beforeReady';
|
|
79
|
-
}>;
|
|
96
|
+
private addDevPrewarmPaths;
|
|
80
97
|
}
|
|
81
|
-
export declare const contentProcessorPlugin: (config: Omit<ProcessorConfig<ContentProcessorConfig>, "name"
|
|
98
|
+
export declare const contentProcessorPlugin: (config: Omit<ProcessorConfig<ContentProcessorConfig>, "name">) => ContentProcessorPlugin;
|
package/src/plugin.js
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { fileSystem } from '@ecopages/file-system';
|
|
7
|
-
import {
|
|
7
|
+
import { isEcopagesEnvEnabled } from '@ecopages/core/processor';
|
|
8
|
+
import { requireAppRuntime } from '@ecopages/core/integration';
|
|
9
|
+
import { mergeProcessorOptions, Processor, resolveGeneratedPath, writeGeneratedTypesPackage, } from '@ecopages/core/processor';
|
|
8
10
|
import { Logger } from '@ecopages/logger';
|
|
9
11
|
import { renderCollectionComponentsModule, renderCollectionBrowserModule, renderCollectionEntriesModule, renderVirtualModuleTypes, } from './codegen.js';
|
|
10
12
|
import { compareEntriesBySlug } from './sort.js';
|
|
@@ -13,9 +15,9 @@ import { createContentPlugin, createContentPluginBundler, getCollectionCachePath
|
|
|
13
15
|
import { createContentServerBoundaryPlugin } from './content-server-boundary-plugin.js';
|
|
14
16
|
import { COLLECTION_NAME_PATTERN, CONTENT_PROCESSOR_NAME } from './constants.js';
|
|
15
17
|
import { buildContentDevPrewarmPathnames } from './dev-prewarm-paths.js';
|
|
16
|
-
import { buildCollectionServerModule } from '
|
|
18
|
+
import { buildCollectionServerModule, } from './collection-server-module-build.service.js';
|
|
17
19
|
const logger = new Logger('[@ecopages/content-processor]', {
|
|
18
|
-
debug:
|
|
20
|
+
debug: isEcopagesEnvEnabled('ECOPAGES_LOGGER_DEBUG'),
|
|
19
21
|
});
|
|
20
22
|
function assertValidCollectionName(collectionName) {
|
|
21
23
|
if (!COLLECTION_NAME_PATTERN.test(collectionName)) {
|
|
@@ -27,10 +29,12 @@ function assertValidCollectionName(collectionName) {
|
|
|
27
29
|
* each one as `ecopages:content/<collection>` virtual modules.
|
|
28
30
|
*/
|
|
29
31
|
export class ContentProcessorPlugin extends Processor {
|
|
30
|
-
|
|
32
|
+
collectionsGeneration;
|
|
31
33
|
scanners = new Map();
|
|
32
34
|
collectionBuildPromises = new Map();
|
|
33
35
|
collectionGenerations = new Map();
|
|
36
|
+
collectionServerBuiltAt = new Map();
|
|
37
|
+
collectionServerArtifacts = new Map();
|
|
34
38
|
/**
|
|
35
39
|
* Absolute paths to generated collection entry modules, keyed by collection name.
|
|
36
40
|
* Virtual-module plugins close over this object so mutations stay live.
|
|
@@ -50,33 +54,29 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
50
54
|
const defaultWatchConfig = {
|
|
51
55
|
paths: [],
|
|
52
56
|
extensions: ['.mdx', '.md', '.ts', '.tsx', '.js', '.jsx', '.json'],
|
|
53
|
-
|
|
54
|
-
onChange: async (ctx) => this.handleContentFileEvent(ctx.path, 'change'),
|
|
55
|
-
onDelete: async (ctx) => this.handleContentFileEvent(ctx.path, 'delete'),
|
|
57
|
+
onChanges: async (changes) => this.handleContentFileChanges(changes),
|
|
56
58
|
};
|
|
57
59
|
super({
|
|
58
60
|
...config,
|
|
59
61
|
name: CONTENT_PROCESSOR_NAME,
|
|
60
|
-
description: 'Build-time content collections exposed as ecopages:content/* virtual modules.',
|
|
61
62
|
watch: config.watch ? mergeProcessorOptions(defaultWatchConfig, config.watch) : defaultWatchConfig,
|
|
62
63
|
});
|
|
63
64
|
}
|
|
64
|
-
get
|
|
65
|
+
get browserPlugins() {
|
|
65
66
|
return [
|
|
66
67
|
createContentServerBoundaryPlugin(),
|
|
67
|
-
createContentPluginBundler(this.collectionModules, this.collectionServerModules, this.collectionServerCompiledModules, this.collectionBrowserModules),
|
|
68
|
-
];
|
|
68
|
+
createContentPluginBundler(this.collectionModules, this.collectionServerModules, this.collectionServerCompiledModules, this.collectionBrowserModules, () => this.ensureCollectionsGenerated()),
|
|
69
|
+
].map((plugin) => ({ ...plugin, environments: ['browser'] }));
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
+
plugins() {
|
|
71
72
|
return [
|
|
72
|
-
|
|
73
|
+
{
|
|
74
|
+
...createContentPlugin(this.collectionModules, this.collectionServerModules, this.collectionServerCompiledModules, this.collectionBrowserModules, (collectionName) => this.ensureCollectionServerArtifact(collectionName), () => this.ensureCollectionsGenerated()),
|
|
75
|
+
environments: ['server'],
|
|
76
|
+
},
|
|
77
|
+
...this.browserPlugins,
|
|
73
78
|
];
|
|
74
79
|
}
|
|
75
|
-
invalidateServerArtifacts() {
|
|
76
|
-
for (const collectionName of Object.keys(this.getCollectionsConfig())) {
|
|
77
|
-
this.invalidateCollectionServerArtifact(collectionName);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
80
|
getCollectionsConfig() {
|
|
81
81
|
if (!this.options?.collections) {
|
|
82
82
|
throw new Error('Content processor requires `options.collections`');
|
|
@@ -189,48 +189,89 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
189
189
|
return normalizedPath.endsWith(dotted);
|
|
190
190
|
});
|
|
191
191
|
}
|
|
192
|
-
async
|
|
192
|
+
async handleContentFileChanges({ created, changed, deleted }) {
|
|
193
193
|
if (!this.context) {
|
|
194
194
|
return;
|
|
195
195
|
}
|
|
196
|
-
const
|
|
197
|
-
const affectedCollections = [];
|
|
198
|
-
for (const [collectionName, definition] of Object.entries(this.getCollectionsConfig())) {
|
|
199
|
-
const contentRoot = this.getCollectionContentRoot(definition.contentDir);
|
|
200
|
-
if (normalizedPath.startsWith(contentRoot + path.sep) || normalizedPath === contentRoot) {
|
|
201
|
-
affectedCollections.push(collectionName);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
196
|
+
const collectionChanges = this.collectCollectionWatchChanges({ created, changed, deleted });
|
|
204
197
|
let shouldRegenerateTypes = false;
|
|
205
|
-
for (const collectionName of
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
198
|
+
for (const [collectionName, changes] of collectionChanges) {
|
|
199
|
+
const regeneratedTypes = await this.applyCollectionWatchChanges(collectionName, changes);
|
|
200
|
+
shouldRegenerateTypes ||= regeneratedTypes;
|
|
201
|
+
}
|
|
202
|
+
if (shouldRegenerateTypes) {
|
|
203
|
+
this.generateTypes();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
collectCollectionWatchChanges({ created, changed, deleted, }) {
|
|
207
|
+
const collectionChanges = new Map();
|
|
208
|
+
const events = [
|
|
209
|
+
{ paths: created, event: 'create' },
|
|
210
|
+
{ paths: changed, event: 'change' },
|
|
211
|
+
{ paths: deleted, event: 'delete' },
|
|
212
|
+
];
|
|
213
|
+
for (const { paths, event } of events) {
|
|
214
|
+
for (const filePath of paths) {
|
|
215
|
+
const normalizedPath = path.normalize(filePath);
|
|
216
|
+
for (const [collectionName, definition] of Object.entries(this.getCollectionsConfig())) {
|
|
217
|
+
const contentRoot = this.getCollectionContentRoot(definition.contentDir);
|
|
218
|
+
if (normalizedPath.startsWith(contentRoot + path.sep) || normalizedPath === contentRoot) {
|
|
219
|
+
const changes = collectionChanges.get(collectionName) ?? {
|
|
220
|
+
regenerate: false,
|
|
221
|
+
changedEntries: [],
|
|
222
|
+
invalidateServerArtifact: false,
|
|
223
|
+
};
|
|
224
|
+
collectionChanges.set(collectionName, changes);
|
|
225
|
+
this.recordCollectionWatchChange(changes, normalizedPath, event, definition.extensions);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
216
228
|
}
|
|
217
|
-
|
|
218
|
-
|
|
229
|
+
}
|
|
230
|
+
return collectionChanges;
|
|
231
|
+
}
|
|
232
|
+
recordCollectionWatchChange(changes, filePath, event, configuredExtensions) {
|
|
233
|
+
if (!this.isCollectionEntryPath(filePath, configuredExtensions)) {
|
|
234
|
+
changes.invalidateServerArtifact = true;
|
|
235
|
+
}
|
|
236
|
+
else if (event === 'change') {
|
|
237
|
+
changes.changedEntries.push(filePath);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
changes.regenerate = true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
async applyCollectionWatchChanges(collectionName, changes) {
|
|
244
|
+
if (changes.regenerate) {
|
|
245
|
+
await this.regenerateCollectionModule(collectionName);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
const updateResult = await this.updateChangedCollectionEntries(collectionName, changes);
|
|
249
|
+
if (updateResult === 'collection') {
|
|
250
|
+
await this.regenerateCollectionModule(collectionName);
|
|
251
|
+
}
|
|
252
|
+
else if (updateResult === 'entries') {
|
|
253
|
+
await this.regenerateCollectionEntriesModule(collectionName);
|
|
254
|
+
}
|
|
255
|
+
if (changes.invalidateServerArtifact && updateResult !== 'collection') {
|
|
256
|
+
this.invalidateCollectionServerArtifact(collectionName);
|
|
257
|
+
}
|
|
258
|
+
return updateResult !== 'none';
|
|
259
|
+
}
|
|
260
|
+
async updateChangedCollectionEntries(collectionName, changes) {
|
|
261
|
+
let result = 'none';
|
|
262
|
+
for (const filePath of changes.changedEntries) {
|
|
263
|
+
const updateResult = await this.getOrCreateScanner(collectionName).updateEntryForPath(filePath, 'change');
|
|
219
264
|
if (updateResult === 'no-op') {
|
|
220
|
-
|
|
221
|
-
continue;
|
|
265
|
+
changes.invalidateServerArtifact = true;
|
|
222
266
|
}
|
|
223
|
-
if (updateResult === 'manifest') {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
267
|
+
else if (updateResult === 'manifest' && result === 'none') {
|
|
268
|
+
result = 'entries';
|
|
269
|
+
}
|
|
270
|
+
else if (updateResult !== 'manifest') {
|
|
271
|
+
result = 'collection';
|
|
227
272
|
}
|
|
228
|
-
await this.regenerateCollectionModule(collectionName);
|
|
229
|
-
shouldRegenerateTypes = true;
|
|
230
|
-
}
|
|
231
|
-
if (shouldRegenerateTypes) {
|
|
232
|
-
this.generateTypes();
|
|
233
273
|
}
|
|
274
|
+
return result;
|
|
234
275
|
}
|
|
235
276
|
initializeWatchPaths() {
|
|
236
277
|
if (!this.watchConfig || !this.context) {
|
|
@@ -270,11 +311,12 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
270
311
|
});
|
|
271
312
|
}
|
|
272
313
|
async buildCollectionServerArtifact(collectionName, serverOutputFile, ownedSourcePaths) {
|
|
273
|
-
if (!this.context
|
|
314
|
+
if (!this.context) {
|
|
274
315
|
return;
|
|
275
316
|
}
|
|
276
317
|
const artifact = await buildCollectionServerModule({
|
|
277
318
|
appConfig: this.context.config,
|
|
319
|
+
artifacts: this.collectionServerArtifacts,
|
|
278
320
|
collectionName,
|
|
279
321
|
sourceFilePath: serverOutputFile,
|
|
280
322
|
ownedSourcePaths,
|
|
@@ -290,9 +332,12 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
290
332
|
* builds share the same promise.
|
|
291
333
|
*/
|
|
292
334
|
async ensureCollectionServerArtifact(collectionName) {
|
|
293
|
-
if (!this.context
|
|
335
|
+
if (!this.context) {
|
|
294
336
|
return;
|
|
295
337
|
}
|
|
338
|
+
if (this.collectionServerBuiltAt.get(collectionName) !== this.getServerSourceGeneration()) {
|
|
339
|
+
delete this.collectionServerCompiledModules[collectionName];
|
|
340
|
+
}
|
|
296
341
|
if (this.collectionServerCompiledModules[collectionName]) {
|
|
297
342
|
return;
|
|
298
343
|
}
|
|
@@ -305,6 +350,7 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
305
350
|
return;
|
|
306
351
|
}
|
|
307
352
|
const generation = this.collectionGenerations.get(collectionName) ?? 0;
|
|
353
|
+
const serverSourceGeneration = this.getServerSourceGeneration();
|
|
308
354
|
const serverOutputFile = this.collectionServerModules[collectionName];
|
|
309
355
|
if (!serverOutputFile) {
|
|
310
356
|
throw new Error(`Unknown content collection: ${collectionName}`);
|
|
@@ -313,9 +359,13 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
313
359
|
const ownedSourcePaths = scanner ? (await scanner.getEntrySources()).map(({ filePath }) => filePath) : [];
|
|
314
360
|
const buildPromise = this.buildCollectionServerArtifact(collectionName, serverOutputFile, ownedSourcePaths)
|
|
315
361
|
.then(() => {
|
|
316
|
-
|
|
362
|
+
const changedDuringBuild = generation !== (this.collectionGenerations.get(collectionName) ?? 0) ||
|
|
363
|
+
serverSourceGeneration !== this.getServerSourceGeneration();
|
|
364
|
+
if (changedDuringBuild) {
|
|
317
365
|
delete this.collectionServerCompiledModules[collectionName];
|
|
366
|
+
return;
|
|
318
367
|
}
|
|
368
|
+
this.collectionServerBuiltAt.set(collectionName, serverSourceGeneration);
|
|
319
369
|
})
|
|
320
370
|
.finally(() => this.collectionBuildPromises.delete(collectionName));
|
|
321
371
|
this.collectionBuildPromises.set(collectionName, buildPromise);
|
|
@@ -324,37 +374,51 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
324
374
|
await this.ensureCollectionServerArtifact(collectionName);
|
|
325
375
|
}
|
|
326
376
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
377
|
+
/**
|
|
378
|
+
* The app module loader's development generation, which advances whenever server source changes.
|
|
379
|
+
*
|
|
380
|
+
* @remarks
|
|
381
|
+
* A compiled collection embeds the modules its entries import, such as a component, so it is stale once the
|
|
382
|
+
* generation differs from the one it was built at. Core announces no invalidation to Processors.
|
|
383
|
+
*/
|
|
384
|
+
getServerSourceGeneration() {
|
|
385
|
+
return this.context ? requireAppRuntime(this.context.config).moduleLoader.getDevelopmentGeneration() : 0;
|
|
334
386
|
}
|
|
335
|
-
|
|
336
|
-
|
|
387
|
+
/**
|
|
388
|
+
* Generates the collection modules and types once, and shares that work between callers.
|
|
389
|
+
*
|
|
390
|
+
* @remarks
|
|
391
|
+
* {@link setup} and every collection resolve call it, so a build that starts before `setup()` waits for the
|
|
392
|
+
* generated modules instead of finding none. A failed generation is dropped so the next caller retries.
|
|
393
|
+
*/
|
|
394
|
+
ensureCollectionsGenerated() {
|
|
395
|
+
this.collectionsGeneration ??= (async () => {
|
|
396
|
+
this.initializeWatchPaths();
|
|
397
|
+
await this.regenerateAllCollections();
|
|
398
|
+
})().catch((error) => {
|
|
399
|
+
this.collectionsGeneration = undefined;
|
|
400
|
+
throw error;
|
|
401
|
+
});
|
|
402
|
+
return this.collectionsGeneration;
|
|
337
403
|
}
|
|
338
404
|
async setup() {
|
|
339
405
|
await this.ensureCollectionsGenerated();
|
|
340
|
-
|
|
341
|
-
async process(_input) {
|
|
342
|
-
await this.regenerateAllCollections();
|
|
406
|
+
await this.addDevPrewarmPaths();
|
|
343
407
|
}
|
|
344
408
|
/**
|
|
345
|
-
*
|
|
409
|
+
* Adds the pathnames of collections with `devPrewarm` and a `routePrefix` to the app's `devPrewarmPaths`.
|
|
346
410
|
*
|
|
347
411
|
* @remarks
|
|
348
|
-
* Core
|
|
412
|
+
* Core renders the list in the background after the server is listening. The entries are read from the
|
|
413
|
+
* manifests at startup, so an entry created later is not prewarmed until the next start. Existing paths stay,
|
|
414
|
+
* and a repeated `setup()` adds nothing twice.
|
|
349
415
|
*/
|
|
350
|
-
async
|
|
351
|
-
|
|
416
|
+
async addDevPrewarmPaths() {
|
|
417
|
+
if (!this.context) {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
352
420
|
const pathnames = [];
|
|
353
|
-
let readiness = 'background';
|
|
354
421
|
for (const [collectionName, definition] of Object.entries(this.getCollectionsConfig())) {
|
|
355
|
-
if (definition.devPrewarmReadiness === 'beforeReady') {
|
|
356
|
-
readiness = 'beforeReady';
|
|
357
|
-
}
|
|
358
422
|
if (!definition.devPrewarm || !definition.routePrefix) {
|
|
359
423
|
continue;
|
|
360
424
|
}
|
|
@@ -362,10 +426,8 @@ export class ContentProcessorPlugin extends Processor {
|
|
|
362
426
|
const entries = await scanner.getManifest();
|
|
363
427
|
pathnames.push(...buildContentDevPrewarmPathnames(definition, entries));
|
|
364
428
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
readiness,
|
|
368
|
-
};
|
|
429
|
+
const config = this.context.config;
|
|
430
|
+
config.devPrewarmPaths = [...new Set([...(config.devPrewarmPaths ?? []), ...pathnames])];
|
|
369
431
|
}
|
|
370
432
|
}
|
|
371
433
|
export const contentProcessorPlugin = (config) => {
|