@ecopages/content-processor 0.2.0-beta.23 → 0.2.0-beta.24

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/content-processor",
3
- "version": "0.2.0-beta.23",
3
+ "version": "0.2.0-beta.24",
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",
@@ -17,10 +17,10 @@
17
17
  "directory": "packages/processors/content-processor"
18
18
  },
19
19
  "peerDependencies": {
20
- "@ecopages/core": "0.2.0-beta.23"
20
+ "@ecopages/core": "0.2.0-beta.24"
21
21
  },
22
22
  "dependencies": {
23
- "@ecopages/file-system": "0.2.0-beta.23",
23
+ "@ecopages/file-system": "0.2.0-beta.24",
24
24
  "@ecopages/logger": "^0.2.3",
25
25
  "remark-frontmatter": "^5.0.0",
26
26
  "unified": "^11.0.5",
package/src/codegen.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ContentEntry } from './types.js';
2
- export declare function renderCollectionModule(collectionName: string, outputDir: string, manifest: ContentEntry[], entrySources: Array<{
2
+ export declare function renderCollectionEntriesModule(collectionName: string, manifest: ContentEntry[]): string;
3
+ export declare function renderCollectionComponentsModule(collectionName: string, outputDir: string, entrySources: Array<{
3
4
  entry: ContentEntry;
4
5
  filePath: string;
5
6
  }>): string;
package/src/codegen.js CHANGED
@@ -12,25 +12,13 @@ function parseEntryTypeImport(entryType) {
12
12
  }
13
13
  return { module, name };
14
14
  }
15
- function renderCollectionModule(collectionName, outputDir, manifest, entrySources) {
16
- const importLines = [];
17
- const componentMapLines = [];
15
+ function renderCollectionEntriesModule(collectionName, manifest) {
18
16
  const entriesBySlugLines = [];
19
- for (const { entry, filePath } of entrySources) {
20
- const identifier = slugToIdentifier(collectionName, entry.slug);
21
- const importPath = relative(outputDir, filePath).split("\\").join("/");
22
- importLines.push(`import ${identifier} from '${importPath}';`);
23
- componentMapLines.push(` '${entry.slug}': ${identifier},`);
24
- }
25
17
  for (const [index, entry] of manifest.entries()) {
26
18
  entriesBySlugLines.push(` '${entry.slug}': entries[${index}],`);
27
19
  }
28
20
  return `// This file is auto-generated by @ecopages/content-processor. Do not edit.
29
21
 
30
- import type { EcoComponent } from '@ecopages/core';
31
-
32
- ${importLines.join("\n")}
33
-
34
22
  export const entries = [
35
23
  ${manifest.map((entry) => ` ${serializeEntry(entry)},`).join("\n")}
36
24
  ] as const;
@@ -39,10 +27,6 @@ const entriesBySlug: Record<string, (typeof entries)[number]> = {
39
27
  ${entriesBySlugLines.join("\n")}
40
28
  };
41
29
 
42
- const componentsBySlug: Record<string, EcoComponent<Record<string, unknown>>> = {
43
- ${componentMapLines.join("\n")}
44
- };
45
-
46
30
  export function getEntry(slug: string) {
47
31
  const entry = entriesBySlug[slug];
48
32
  if (!entry) throw new Error(\`Unknown content entry: \${slug}\`);
@@ -52,6 +36,26 @@ export function getEntry(slug: string) {
52
36
  export function getEntryBySegments(segments: string[]) {
53
37
  return getEntry(segments.join('/'));
54
38
  }
39
+ `;
40
+ }
41
+ function renderCollectionComponentsModule(collectionName, outputDir, entrySources) {
42
+ const importLines = [];
43
+ const componentMapLines = [];
44
+ for (const { entry, filePath } of entrySources) {
45
+ const identifier = slugToIdentifier(collectionName, entry.slug);
46
+ const importPath = relative(outputDir, filePath).split("\\").join("/");
47
+ importLines.push(`import ${identifier} from '${importPath}';`);
48
+ componentMapLines.push(` '${entry.slug}': ${identifier},`);
49
+ }
50
+ return `// This file is auto-generated by @ecopages/content-processor. Do not edit.
51
+
52
+ import type { EcoComponent } from '@ecopages/core';
53
+
54
+ ${importLines.join("\n")}
55
+
56
+ const componentsBySlug: Record<string, EcoComponent<Record<string, unknown>>> = {
57
+ ${componentMapLines.join("\n")}
58
+ };
55
59
 
56
60
  export function getComponent(slug: string): EcoComponent<Record<string, unknown>> {
57
61
  const component = componentsBySlug[slug];
@@ -74,7 +78,7 @@ function resolveEntryTypeImport(entryType, rootDir, typesOutputFile) {
74
78
  }
75
79
  return { module: relativeModule, name };
76
80
  }
77
- function renderCollectionTypesModule(collectionName, entryType, typeResolution) {
81
+ function renderCollectionEntriesTypesModule(collectionName, entryType, typeResolution) {
78
82
  const entryDeclaration = entryType ? (() => {
79
83
  const { module, name } = typeResolution ? resolveEntryTypeImport(entryType, typeResolution.rootDir, typeResolution.typesOutputFile) : parseEntryTypeImport(entryType);
80
84
  return `import type { ContentEntry } from '@ecopages/content-processor/types';
@@ -84,16 +88,25 @@ function renderCollectionTypesModule(collectionName, entryType, typeResolution)
84
88
 
85
89
  export type Entry = ContentEntry<Record<string, unknown>>;`;
86
90
  return `declare module "ecopages:content/${collectionName}" {
87
- import type { EcoComponent } from '@ecopages/core';
88
-
89
91
  ${entryDeclaration}
90
92
 
91
93
  export const entries: readonly Entry[];
92
94
  export function getEntry(slug: string): Entry;
93
95
  export function getEntryBySegments(segments: string[]): Entry;
96
+ }`;
97
+ }
98
+ function renderCollectionComponentsTypesModule(collectionName) {
99
+ return `declare module "ecopages:content/${collectionName}/server" {
100
+ import type { EcoComponent } from '@ecopages/core';
101
+
94
102
  export function getComponent(slug: string): EcoComponent<Record<string, unknown>>;
95
103
  }`;
96
104
  }
105
+ function renderCollectionTypesModule(collectionName, entryType, typeResolution) {
106
+ return `${renderCollectionEntriesTypesModule(collectionName, entryType, typeResolution)}
107
+
108
+ ${renderCollectionComponentsTypesModule(collectionName)}`;
109
+ }
97
110
  function renderVirtualModuleTypes(collections, typeResolution) {
98
111
  return `/**
99
112
  * Do not edit manually. This file is auto-generated.
@@ -104,7 +117,8 @@ ${Object.entries(collections).map(([name, definition]) => renderCollectionTypesM
104
117
  `;
105
118
  }
106
119
  export {
107
- renderCollectionModule,
120
+ renderCollectionComponentsModule,
121
+ renderCollectionEntriesModule,
108
122
  renderCollectionTypesModule,
109
123
  renderVirtualModuleTypes
110
124
  };
@@ -2,7 +2,9 @@
2
2
  export declare const CONTENT_PROCESSOR_NAME = "ecopages-content-processor";
3
3
  /** Virtual module prefix for collection imports, e.g. `ecopages:content/docs`. */
4
4
  export declare const CONTENT_VIRTUAL_MODULE_PREFIX = "ecopages:content";
5
- /** Matches `ecopages:content/<collection>` collection specifiers. */
5
+ /** Matches `ecopages:content/<collection>` metadata specifiers (no MDX imports). */
6
6
  export declare const CONTENT_VIRTUAL_MODULE_PATTERN: RegExp;
7
+ /** Matches `ecopages:content/<collection>/server` component resolver specifiers (MDX imports). */
8
+ export declare const CONTENT_SERVER_VIRTUAL_MODULE_PATTERN: RegExp;
7
9
  /** Validates collection keys used in processor options and virtual module paths. */
8
10
  export declare const COLLECTION_NAME_PATTERN: RegExp;
package/src/constants.js CHANGED
@@ -1,10 +1,12 @@
1
1
  const CONTENT_PROCESSOR_NAME = "ecopages-content-processor";
2
2
  const CONTENT_VIRTUAL_MODULE_PREFIX = "ecopages:content";
3
3
  const CONTENT_VIRTUAL_MODULE_PATTERN = /^ecopages:content\/[a-z][a-z0-9-]*$/;
4
+ const CONTENT_SERVER_VIRTUAL_MODULE_PATTERN = /^ecopages:content\/[a-z][a-z0-9-]+\/server$/;
4
5
  const COLLECTION_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
5
6
  export {
6
7
  COLLECTION_NAME_PATTERN,
7
8
  CONTENT_PROCESSOR_NAME,
9
+ CONTENT_SERVER_VIRTUAL_MODULE_PATTERN,
8
10
  CONTENT_VIRTUAL_MODULE_PATTERN,
9
11
  CONTENT_VIRTUAL_MODULE_PREFIX
10
12
  };
@@ -1,7 +1,12 @@
1
1
  import type { EcoBuildPlugin } from '@ecopages/core/plugins/processor';
2
- declare function parseCollectionName(specifier: string): string | null;
3
- declare function resolveCollectionPath(specifier: string, collectionModules: Record<string, string>): string | null;
4
- export declare function createContentPlugin(collectionModules: Record<string, string>): EcoBuildPlugin;
5
- export declare function createContentPluginBundler(collectionModules: Record<string, string>): EcoBuildPlugin;
2
+ export type ParsedCollectionSpecifier = {
3
+ collectionName: string;
4
+ variant: 'entries' | 'server';
5
+ };
6
+ export declare function parseCollectionName(specifier: string): string | null;
7
+ export declare function parseCollectionSpecifier(specifier: string): ParsedCollectionSpecifier | null;
8
+ export declare function resolveCollectionPath(specifier: string, collectionModules: Record<string, string>, collectionServerModules: Record<string, string>): string | null;
9
+ export declare function createContentPlugin(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>): EcoBuildPlugin;
10
+ export declare function createContentPluginBundler(collectionModules: Record<string, string>, collectionServerModules: Record<string, string>): EcoBuildPlugin;
6
11
  export declare function getCollectionCachePath(cacheDir: string, collectionName: string): string;
7
- export { parseCollectionName, resolveCollectionPath };
12
+ export declare function getCollectionServerCachePath(cacheDir: string, collectionName: string): string;
@@ -1,50 +1,86 @@
1
1
  import path from "node:path";
2
2
  import { fileSystem } from "@ecopages/file-system";
3
- import { CONTENT_VIRTUAL_MODULE_PATTERN } from "./constants.js";
3
+ import { CONTENT_SERVER_VIRTUAL_MODULE_PATTERN, CONTENT_VIRTUAL_MODULE_PATTERN } from "./constants.js";
4
4
  function parseCollectionName(specifier) {
5
- if (specifier.startsWith("ecopages:content/")) {
6
- return specifier.slice("ecopages:content/".length);
5
+ const parsed = parseCollectionSpecifier(specifier);
6
+ return parsed?.collectionName ?? null;
7
+ }
8
+ function parseCollectionSpecifier(specifier) {
9
+ const normalized = specifier.startsWith("ecopages:content/") ? specifier.slice("ecopages:content/".length) : specifier.startsWith("content/") ? specifier.slice("content/".length) : null;
10
+ if (!normalized) {
11
+ return null;
7
12
  }
8
- if (specifier.startsWith("content/")) {
9
- return specifier.slice("content/".length);
13
+ if (normalized.endsWith("/server")) {
14
+ return {
15
+ collectionName: normalized.slice(0, -"/server".length),
16
+ variant: "server"
17
+ };
10
18
  }
11
- return null;
19
+ return {
20
+ collectionName: normalized,
21
+ variant: "entries"
22
+ };
12
23
  }
13
- function resolveCollectionPath(specifier, collectionModules) {
14
- const collectionName = parseCollectionName(specifier);
15
- if (!collectionName) {
24
+ function resolveCollectionPath(specifier, collectionModules, collectionServerModules) {
25
+ const parsed = parseCollectionSpecifier(specifier);
26
+ if (!parsed) {
16
27
  return null;
17
28
  }
18
- return collectionModules[collectionName] ?? null;
29
+ if (parsed.variant === "server") {
30
+ return collectionServerModules[parsed.collectionName] ?? null;
31
+ }
32
+ return collectionModules[parsed.collectionName] ?? null;
19
33
  }
20
- function createContentPlugin(collectionModules) {
34
+ function createContentResolvePlugin(collectionModules, collectionServerModules, options) {
21
35
  return {
22
36
  name: "ecopages:content",
23
37
  setup(build) {
38
+ const resolveCollection = (specifier) => resolveCollectionPath(specifier, collectionModules, collectionServerModules);
24
39
  build.onResolve({ filter: CONTENT_VIRTUAL_MODULE_PATTERN }, (args) => {
25
- const modulePath = resolveCollectionPath(args.path, collectionModules);
40
+ const modulePath = resolveCollection(args.path);
26
41
  if (!modulePath) {
27
42
  return void 0;
28
43
  }
44
+ if (options.namespace) {
45
+ return {
46
+ namespace: options.namespace,
47
+ path: modulePath
48
+ };
49
+ }
29
50
  return { path: modulePath };
30
51
  });
52
+ if (options.includeServerModules !== false) {
53
+ build.onResolve({ filter: CONTENT_SERVER_VIRTUAL_MODULE_PATTERN }, (args) => {
54
+ const modulePath = resolveCollection(args.path);
55
+ if (!modulePath) {
56
+ return void 0;
57
+ }
58
+ if (options.namespace) {
59
+ return {
60
+ namespace: options.namespace,
61
+ path: modulePath
62
+ };
63
+ }
64
+ return { path: modulePath };
65
+ });
66
+ }
31
67
  }
32
68
  };
33
69
  }
34
- function createContentPluginBundler(collectionModules) {
70
+ function createContentPlugin(collectionModules, collectionServerModules) {
71
+ return createContentResolvePlugin(collectionModules, collectionServerModules, {
72
+ includeServerModules: true
73
+ });
74
+ }
75
+ function createContentPluginBundler(collectionModules, collectionServerModules) {
76
+ const plugin = createContentResolvePlugin(collectionModules, collectionServerModules, {
77
+ namespace: "ecopages-content",
78
+ includeServerModules: false
79
+ });
35
80
  return {
36
- name: "ecopages:content",
81
+ name: plugin.name,
37
82
  setup(build) {
38
- build.onResolve({ filter: CONTENT_VIRTUAL_MODULE_PATTERN }, (args) => {
39
- const modulePath = resolveCollectionPath(args.path, collectionModules);
40
- if (!modulePath) {
41
- return void 0;
42
- }
43
- return {
44
- namespace: "ecopages-content",
45
- path: modulePath
46
- };
47
- });
83
+ plugin.setup?.(build);
48
84
  build.onLoad({ filter: /.*/, namespace: "ecopages-content" }, async (args) => {
49
85
  const contents = await fileSystem.readFile(args.path);
50
86
  return { contents, loader: "ts" };
@@ -55,10 +91,15 @@ function createContentPluginBundler(collectionModules) {
55
91
  function getCollectionCachePath(cacheDir, collectionName) {
56
92
  return path.join(cacheDir, `${collectionName}.ts`);
57
93
  }
94
+ function getCollectionServerCachePath(cacheDir, collectionName) {
95
+ return path.join(cacheDir, `${collectionName}.server.ts`);
96
+ }
58
97
  export {
59
98
  createContentPlugin,
60
99
  createContentPluginBundler,
61
100
  getCollectionCachePath,
101
+ getCollectionServerCachePath,
62
102
  parseCollectionName,
103
+ parseCollectionSpecifier,
63
104
  resolveCollectionPath
64
105
  };
@@ -0,0 +1,9 @@
1
+ import type { EcoBuildPlugin } from '@ecopages/core/plugins/processor';
2
+ /**
3
+ * Keeps `ecopages:content/<collection>/server` out of browser bundles.
4
+ *
5
+ * @remarks
6
+ * Server modules static-import every MDX entry for `getComponent`. Browser builds
7
+ * must never follow that graph — highlighted MDX output belongs in SSR HTML only.
8
+ */
9
+ export declare function createContentServerBoundaryPlugin(): EcoBuildPlugin;
@@ -0,0 +1,15 @@
1
+ import { CONTENT_SERVER_VIRTUAL_MODULE_PATTERN } from "./constants.js";
2
+ function createContentServerBoundaryPlugin() {
3
+ return {
4
+ name: "ecopages:content-server-boundary",
5
+ setup(build) {
6
+ build.onResolve({ filter: CONTENT_SERVER_VIRTUAL_MODULE_PATTERN }, (args) => ({
7
+ path: args.path,
8
+ external: true
9
+ }));
10
+ }
11
+ };
12
+ }
13
+ export {
14
+ createContentServerBoundaryPlugin
15
+ };
package/src/plugin.d.ts CHANGED
@@ -12,10 +12,14 @@ export declare class ContentProcessorPlugin extends Processor<ContentProcessorCo
12
12
  private collectionsGenerated;
13
13
  private readonly scanners;
14
14
  /**
15
- * Absolute paths to generated collection modules, keyed by collection name.
15
+ * Absolute paths to generated collection entry modules, keyed by collection name.
16
16
  * Virtual-module plugins close over this object so mutations stay live.
17
17
  */
18
18
  readonly collectionModules: Record<string, string>;
19
+ /**
20
+ * Absolute paths to generated server-only component resolver modules.
21
+ */
22
+ readonly collectionServerModules: Record<string, string>;
19
23
  constructor(config: Omit<ProcessorConfig<ContentProcessorConfig>, 'name' | 'description'>);
20
24
  get buildPlugins(): EcoBuildPlugin[];
21
25
  get plugins(): EcoBuildPlugin[];
package/src/plugin.js CHANGED
@@ -6,10 +6,20 @@ import {
6
6
  resolveGeneratedPath
7
7
  } from "@ecopages/core/plugins/processor";
8
8
  import { Logger } from "@ecopages/logger";
9
- import { renderCollectionModule, renderVirtualModuleTypes } from "./codegen.js";
9
+ import {
10
+ renderCollectionComponentsModule,
11
+ renderCollectionEntriesModule,
12
+ renderVirtualModuleTypes
13
+ } from "./codegen.js";
10
14
  import { compareEntriesBySlug } from "./sort.js";
11
15
  import { ContentScanner } from "./content-scanner.js";
12
- import { createContentPlugin, createContentPluginBundler, getCollectionCachePath } from "./content-plugins.js";
16
+ import {
17
+ createContentPlugin,
18
+ createContentPluginBundler,
19
+ getCollectionCachePath,
20
+ getCollectionServerCachePath
21
+ } from "./content-plugins.js";
22
+ import { createContentServerBoundaryPlugin } from "./content-server-boundary-plugin.js";
13
23
  import { COLLECTION_NAME_PATTERN, CONTENT_PROCESSOR_NAME } from "./constants.js";
14
24
  const logger = new Logger("[@ecopages/content-processor]", {
15
25
  debug: process.env.ECOPAGES_LOGGER_DEBUG === "true"
@@ -25,10 +35,14 @@ class ContentProcessorPlugin extends Processor {
25
35
  collectionsGenerated = false;
26
36
  scanners = /* @__PURE__ */ new Map();
27
37
  /**
28
- * Absolute paths to generated collection modules, keyed by collection name.
38
+ * Absolute paths to generated collection entry modules, keyed by collection name.
29
39
  * Virtual-module plugins close over this object so mutations stay live.
30
40
  */
31
41
  collectionModules = {};
42
+ /**
43
+ * Absolute paths to generated server-only component resolver modules.
44
+ */
45
+ collectionServerModules = {};
32
46
  constructor(config) {
33
47
  const defaultWatchConfig = {
34
48
  paths: [],
@@ -45,10 +59,13 @@ class ContentProcessorPlugin extends Processor {
45
59
  });
46
60
  }
47
61
  get buildPlugins() {
48
- return [createContentPluginBundler(this.collectionModules)];
62
+ return [
63
+ createContentServerBoundaryPlugin(),
64
+ createContentPluginBundler(this.collectionModules, this.collectionServerModules)
65
+ ];
49
66
  }
50
67
  get plugins() {
51
- return [createContentPlugin(this.collectionModules)];
68
+ return [createContentPlugin(this.collectionModules, this.collectionServerModules)];
52
69
  }
53
70
  getCollectionsConfig() {
54
71
  if (!this.options?.collections) {
@@ -101,13 +118,18 @@ class ContentProcessorPlugin extends Processor {
101
118
  const entrySources = await scanner.getEntrySources();
102
119
  const manifest = entrySources.map(({ entry }) => entry);
103
120
  const outputFile = getCollectionCachePath(this.context.cache, collectionName);
121
+ const serverOutputFile = getCollectionServerCachePath(this.context.cache, collectionName);
104
122
  const outputDir = path.dirname(outputFile);
105
- const output = renderCollectionModule(collectionName, outputDir, manifest, entrySources);
106
- this.writeGeneratedFile(outputFile, output);
123
+ const entriesOutput = renderCollectionEntriesModule(collectionName, manifest);
124
+ const componentsOutput = renderCollectionComponentsModule(collectionName, outputDir, entrySources);
125
+ this.writeGeneratedFile(outputFile, entriesOutput);
126
+ this.writeGeneratedFile(serverOutputFile, componentsOutput);
107
127
  this.collectionModules[collectionName] = outputFile;
128
+ this.collectionServerModules[collectionName] = serverOutputFile;
108
129
  logger.debug("Generated content collection module", {
109
130
  collectionName,
110
131
  outputFile,
132
+ serverOutputFile,
111
133
  entryCount: manifest.length
112
134
  });
113
135
  }
package/src/types.d.ts CHANGED
@@ -13,10 +13,15 @@ export type ContentEntry<T extends Record<string, unknown> = Record<string, unkn
13
13
  };
14
14
  /** @deprecated Use {@link EntryComparator} instead. */
15
15
  export type OrderBy<T extends Record<string, unknown> = Record<string, unknown>> = EntryComparator<T>;
16
- /** Runtime shape of a generated `ecopages:content/<collection>` module. */
17
- export type ContentCollectionModule<TEntry extends Record<string, unknown> = Record<string, unknown>> = {
16
+ /** Runtime shape of a generated `ecopages:content/<collection>` entries module. */
17
+ export type ContentCollectionEntriesModule<TEntry extends Record<string, unknown> = Record<string, unknown>> = {
18
18
  readonly entries: readonly ContentEntry<TEntry>[];
19
19
  getEntry(slug: string): ContentEntry<TEntry>;
20
20
  getEntryBySegments(segments: string[]): ContentEntry<TEntry>;
21
+ };
22
+ /** Runtime shape of a generated `ecopages:content/<collection>/server` module. */
23
+ export type ContentCollectionComponentsModule = {
21
24
  getComponent(slug: string): EcoComponent<Record<string, unknown>>;
22
25
  };
26
+ /** @deprecated Import entries and server modules separately. */
27
+ export type ContentCollectionModule<TEntry extends Record<string, unknown> = Record<string, unknown>> = ContentCollectionEntriesModule<TEntry> & ContentCollectionComponentsModule;