@ecopages/react 0.2.0-beta.12 → 0.2.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/README.md +12 -0
  2. package/package.json +20 -7
  3. package/src/layout-compose.d.ts +58 -0
  4. package/src/layout-compose.js +98 -0
  5. package/src/react-hmr-strategy.js +1 -1
  6. package/src/react-renderer.d.ts +31 -4
  7. package/src/react-renderer.js +157 -45
  8. package/src/react.plugin.js +1 -1
  9. package/src/serialize-page-data-script.d.ts +8 -0
  10. package/src/serialize-page-data-script.js +10 -0
  11. package/src/services/react-mdx-config-dependency.service.js +5 -7
  12. package/src/services/react-page-payload.service.js +2 -2
  13. package/src/utils/client-graph-ast-transform.d.ts +18 -0
  14. package/src/utils/client-graph-ast-transform.js +335 -0
  15. package/src/utils/client-graph-boundary-plugin.d.ts +1 -12
  16. package/src/utils/client-graph-boundary-plugin.js +8 -454
  17. package/src/utils/client-graph-reachability.d.ts +75 -0
  18. package/src/utils/client-graph-reachability.js +148 -0
  19. package/src/utils/component-config-traversal.js +4 -2
  20. package/src/utils/hydration-scripts.d.ts +2 -0
  21. package/src/utils/hydration-scripts.js +27 -14
  22. package/src/utils/react-mdx-loader-plugin.js +1 -1
  23. package/node_modules/@ecopages/mdx-core/package.json +0 -22
  24. package/node_modules/@ecopages/mdx-core/src/index.d.ts +0 -2
  25. package/node_modules/@ecopages/mdx-core/src/index.js +0 -18
  26. package/node_modules/@ecopages/mdx-core/src/mdx-loader-plugin.d.ts +0 -11
  27. package/node_modules/@ecopages/mdx-core/src/mdx-loader-plugin.js +0 -44
  28. package/node_modules/@ecopages/mdx-core/src/mdx-utils.d.ts +0 -29
  29. package/node_modules/@ecopages/mdx-core/src/mdx-utils.js +0 -55
@@ -4,8 +4,10 @@ function walkConfigTree(config, visitor, visited = /* @__PURE__ */ new Set()) {
4
4
  }
5
5
  visited.add(config);
6
6
  visitor(config);
7
- if (config.layout?.config) {
8
- walkConfigTree(config.layout.config, visitor, visited);
7
+ for (const layout of config.layouts ?? []) {
8
+ if (layout?.config) {
9
+ walkConfigTree(layout.config, visitor, visited);
10
+ }
9
11
  }
10
12
  for (const component of config.dependencies?.components ?? []) {
11
13
  walkConfigTree(component.config, visitor, visited);
@@ -26,6 +26,8 @@ export type HydrationScriptOptions = {
26
26
  isMdx: boolean;
27
27
  /** Optional router adapter for SPA navigation */
28
28
  router?: ReactRouterAdapter;
29
+ /** Import path for shared layout composition helper */
30
+ layoutComposeImportPath?: string;
29
31
  };
30
32
  export type IslandHydrationScriptOptions = {
31
33
  /** Bundled browser module path for the island component. */
@@ -1,10 +1,23 @@
1
+ const DEFAULT_LAYOUT_COMPOSE_IMPORT_PATH = "@ecopages/react/layout-compose";
2
+ const PAGE_LAYOUT_NORMALIZATION_IMPORT = "@ecopages/core/eco/page-layout-normalization";
3
+ function resolveLayoutComposeImportPath(options) {
4
+ return options.layoutComposeImportPath ?? DEFAULT_LAYOUT_COMPOSE_IMPORT_PATH;
5
+ }
1
6
  function getImportStatement(importPath, isMdx) {
2
7
  return isMdx ? `import * as MDXModule from "${importPath}";
8
+ import { ensurePageConfigLayouts } from "${PAGE_LAYOUT_NORMALIZATION_IMPORT}";
3
9
  const Page = MDXModule.default;
4
- if (MDXModule.config) Page.config = MDXModule.config;` : `import Page from "${importPath}";`;
10
+ if (MDXModule.config) {
11
+ Page.config = MDXModule.config;
12
+ ensurePageConfigLayouts(Page.config);
13
+ }` : `import Page from "${importPath}";`;
5
14
  }
6
15
  function getHmrImportStatement(isMdx) {
7
- return isMdx ? "const NewPage = newModule.default; if (newModule.config) NewPage.config = newModule.config;" : "const NewPage = newModule.default;";
16
+ return isMdx ? `const NewPage = newModule.default;
17
+ if (newModule.config) {
18
+ NewPage.config = newModule.config;
19
+ ensurePageConfigLayouts(NewPage.config);
20
+ }` : "const NewPage = newModule.default;";
8
21
  }
9
22
  function getComponentType(isMdx) {
10
23
  return isMdx ? "MDX" : "React";
@@ -140,12 +153,14 @@ const mount = () => {
140
153
  const newModule = await import(newUrl);
141
154
  const nextProps = getPageData();
142
155
  ${getHmrImportStatement(isMdx)}
143
- const currentPageLayout = Page.config?.layout;
144
- const nextPageLayout = NewPage.config?.layout;
156
+ const currentPageLayoutStack = (Component) =>
157
+ (Component.config?.layouts ?? []).map((layout) => layout?.config?.__eco?.file ?? '').join('|');
158
+ const currentPageLayoutStackKey = currentPageLayoutStack(Page);
159
+ const nextPageLayoutStackKey = currentPageLayoutStack(NewPage);
145
160
 
146
161
  if (window.__ECO_PAGES__?.navigation?.getOwnerState().owner === "react-router") {
147
162
  await window.__ECO_PAGES__?.navigation?.reloadCurrentPage?.({
148
- clearCache: currentPageLayout !== nextPageLayout,
163
+ clearCache: currentPageLayoutStackKey !== nextPageLayoutStackKey,
149
164
  moduleUrl: "${importPath}",
150
165
  source: "react-router"
151
166
  });
@@ -178,9 +193,11 @@ if (document.readyState === "loading") {
178
193
  function createDevScriptWithoutRouter(options) {
179
194
  const { importPath, isMdx, reactImportPath, reactDomClientImportPath, scriptId } = options;
180
195
  const pageModuleUrlExpression = options.pageModuleUrlExpression ?? "import.meta.url";
196
+ const layoutComposeImportPath = resolveLayoutComposeImportPath(options);
181
197
  return `
182
198
  import { hydrateRoot } from "${reactDomClientImportPath}";
183
199
  import { createElement } from "${reactImportPath}";
200
+ import { composeLayoutPageTree } from "${layoutComposeImportPath}";
184
201
  ${getImportStatement(importPath, isMdx)}
185
202
  const pageModuleUrl = ${pageModuleUrlExpression};
186
203
  export default Page;
@@ -211,12 +228,7 @@ window.__ECO_PAGES__.page = {
211
228
  props
212
229
  };
213
230
 
214
- const createTree = (Component, props) => {
215
- const Layout = Component.config?.layout;
216
- const pageElement = createElement(Component, props);
217
- const layoutProps = props?.locals ? { locals: props.locals } : null;
218
- return Layout ? createElement(Layout, layoutProps, pageElement) : pageElement;
219
- };
231
+ const createTree = (Component, props) => composeLayoutPageTree(Component, props);
220
232
 
221
233
  const mount = () => {
222
234
  const props = getPageData();
@@ -264,17 +276,18 @@ function createProdScriptWithRouter(options) {
264
276
  throw new Error("routerImportPath is required when router adapter is configured");
265
277
  }
266
278
  if (isMdx) {
267
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import*as M from"${importPath}";const P=M.default;if(M.config)P.config=M.config;const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}${getProdReuseExistingRouterRootScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(sr()){root=window.__ECO_PAGES__.react.pageRoot;return}if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
279
+ return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import{ensurePageConfigLayouts as epl}from"${PAGE_LAYOUT_NORMALIZATION_IMPORT}";import*as M from"${importPath}";const P=M.default;if(M.config){P.config=M.config;epl(P.config);}const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}${getProdReuseExistingRouterRootScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(sr()){root=window.__ECO_PAGES__.react.pageRoot;return}if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
268
280
  }
269
281
  return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{${components.router} as R,${components.pageContent} as PC}from"${routerImportPath}";import P from"${importPath}";const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}${getProdRouterBootstrapRegistrationScript()}${getProdReuseExistingRouterRootScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>ce(R,${getRouterProps("C", "p")},ce(PC));const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(sr()){root=window.__ECO_PAGES__.react.pageRoot;return}if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
270
282
  }
271
283
  function createProdScriptWithoutRouter(options) {
272
284
  const { importPath, isMdx, reactImportPath, reactDomClientImportPath, scriptId } = options;
273
285
  const pageModuleUrlExpression = options.pageModuleUrlExpression ?? "import.meta.url";
286
+ const layoutComposeImportPath = resolveLayoutComposeImportPath(options);
274
287
  if (isMdx) {
275
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import*as M from"${importPath}";const P=M.default;if(M.config)P.config=M.config;const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>{const L=C.config?.layout;const pe=ce(C,p);const lp=p?.locals?{locals:p.locals}:null;return L?ce(L,lp,pe):pe};const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
288
+ return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{composeLayoutPageTree as clp}from"${layoutComposeImportPath}";import{ensurePageConfigLayouts as epl}from"${PAGE_LAYOUT_NORMALIZATION_IMPORT}";import*as M from"${importPath}";const P=M.default;if(M.config){P.config=M.config;epl(P.config);}const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>clp(C,p);const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
276
289
  }
277
- return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import P from"${importPath}";const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>{const L=C.config?.layout;const pe=ce(C,p);const lp=p?.locals?{locals:p.locals}:null;return L?ce(L,lp,pe):pe};const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
290
+ return `import{hydrateRoot as hr}from"${reactDomClientImportPath}";import{createElement as ce}from"${reactImportPath}";import{composeLayoutPageTree as clp}from"${layoutComposeImportPath}";import P from"${importPath}";const u=${pageModuleUrlExpression};export default P;export const config=P.config;const a=!!document.querySelector('script[data-eco-script-id="${scriptId}"]');if(a){window.__ECO_PAGES__=window.__ECO_PAGES__||{};window.__ECO_PAGES__.react=window.__ECO_PAGES__.react||{};window.__ECO_PAGES__.react.pageRoot=window.__ECO_PAGES__.react.pageRoot||null;let root=window.__ECO_PAGES__.react.pageRoot;${getProdPageRootCleanupScript()}const gd=()=>{const e=document.getElementById("__ECO_PAGE_DATA__");if(e?.textContent){try{return JSON.parse(e.textContent)}catch{}}return{}};const ct=(C,p)=>clp(C,p);const m=()=>{const pr=gd();window.__ECO_PAGES__.page={module:u,props:pr};if(window.__ECO_PAGES__.react?.pageRoot){root=window.__ECO_PAGES__.react.pageRoot;root.render(ct(P,pr));return}root=hr(document.body,ct(P,pr),{onRecoverableError:(e)=>console.warn("[ecopages] Hydration error:",e)});window.__ECO_PAGES__.react.pageRoot=root};${getProdRerunRegistrationScript(scriptId)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}`;
278
291
  }
279
292
  function createHydrationScript(options) {
280
293
  const { isDevelopment, router } = options;
@@ -1,4 +1,4 @@
1
- import { createMdxLoaderPlugin as createMdxLoaderPluginCore } from "@ecopages/mdx-core";
1
+ import { createMdxLoaderPlugin as createMdxLoaderPluginCore } from "@ecopages/mdx/core";
2
2
  function createReactMdxLoaderPlugin(compilerOptions) {
3
3
  return createMdxLoaderPluginCore({
4
4
  name: "react-mdx-loader",
@@ -1,22 +0,0 @@
1
- {
2
- "name": "@ecopages/mdx-core",
3
- "version": "0.2.0-beta.12",
4
- "description": "Shared MDX loader utilities for Ecopages integrations",
5
- "license": "MIT",
6
- "type": "module",
7
- "exports": {
8
- ".": {
9
- "default": "./src/index.js",
10
- "types": "./src/index.d.ts"
11
- }
12
- },
13
- "peerDependencies": {
14
- "@ecopages/core": "0.2.0-beta.12",
15
- "@mdx-js/mdx": "^3.1.0"
16
- },
17
- "dependencies": {
18
- "source-map": "^0.7.6",
19
- "vfile": "^6.0.3"
20
- },
21
- "types": "./src/index.d.ts"
22
- }
@@ -1,2 +0,0 @@
1
- export { createMdxLoaderPlugin, type CreateMdxLoaderPluginOptions } from './mdx-loader-plugin.js';
2
- export { appendMdxExtensions, createMdxExtensionFilter, mergePluginLists, resolveCompileFormat, resolveLoaderExtensions, resolveMdxCompilerOptions, type MdxCompilerOptionsInput, } from './mdx-utils.js';
@@ -1,18 +0,0 @@
1
- import { createMdxLoaderPlugin } from "./mdx-loader-plugin.js";
2
- import {
3
- appendMdxExtensions,
4
- createMdxExtensionFilter,
5
- mergePluginLists,
6
- resolveCompileFormat,
7
- resolveLoaderExtensions,
8
- resolveMdxCompilerOptions
9
- } from "./mdx-utils.js";
10
- export {
11
- appendMdxExtensions,
12
- createMdxExtensionFilter,
13
- createMdxLoaderPlugin,
14
- mergePluginLists,
15
- resolveCompileFormat,
16
- resolveLoaderExtensions,
17
- resolveMdxCompilerOptions
18
- };
@@ -1,11 +0,0 @@
1
- import type { EcoBuildPlugin } from '@ecopages/core/plugins/integration-plugin';
2
- import { type CompileOptions } from '@mdx-js/mdx';
3
- export interface CreateMdxLoaderPluginOptions {
4
- name: string;
5
- compilerOptions?: CompileOptions;
6
- extensions?: string[];
7
- defaultMdExtensions?: string[];
8
- includeSourceMap?: boolean;
9
- loader?: 'js' | 'jsx';
10
- }
11
- export declare function createMdxLoaderPlugin(options: CreateMdxLoaderPluginOptions): EcoBuildPlugin;
@@ -1,44 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import path from "node:path";
3
- import { compile } from "@mdx-js/mdx";
4
- import sourceMap from "source-map";
5
- import { VFile } from "vfile";
6
- import { createMdxExtensionFilter, resolveCompileFormat, resolveLoaderExtensions } from "./mdx-utils.js";
7
- function createMdxLoaderPlugin(options) {
8
- const {
9
- name,
10
- compilerOptions,
11
- extensions = resolveLoaderExtensions(compilerOptions, {
12
- defaultMdExtensions: options.defaultMdExtensions
13
- }),
14
- includeSourceMap = true,
15
- loader = compilerOptions?.jsx ? "jsx" : "js"
16
- } = options;
17
- const filter = createMdxExtensionFilter(extensions, { allowQueryString: true });
18
- return {
19
- name,
20
- setup(build) {
21
- build.onLoad({ filter }, async (args) => {
22
- const filePath = args.path.includes("?") ? args.path.split("?")[0] : args.path;
23
- const source = await readFile(filePath, "utf-8");
24
- const file = new VFile({ path: filePath, value: source });
25
- const compiled = await compile(file, {
26
- ...compilerOptions,
27
- format: resolveCompileFormat(filePath, compilerOptions),
28
- SourceMapGenerator: sourceMap.SourceMapGenerator
29
- });
30
- const inlineSourceMap = includeSourceMap && compiled.map ? `
31
- //# sourceMappingURL=data:application/json;base64,${Buffer.from(JSON.stringify(compiled.map)).toString("base64")}
32
- ` : "";
33
- return {
34
- contents: `${String(compiled.value)}${inlineSourceMap}`,
35
- loader,
36
- resolveDir: path.dirname(args.path)
37
- };
38
- });
39
- }
40
- };
41
- }
42
- export {
43
- createMdxLoaderPlugin
44
- };
@@ -1,29 +0,0 @@
1
- import type { CompileOptions } from '@mdx-js/mdx';
2
- export interface MdxCompilerOptionsInput {
3
- compilerOptions?: CompileOptions;
4
- remarkPlugins?: CompileOptions['remarkPlugins'];
5
- rehypePlugins?: CompileOptions['rehypePlugins'];
6
- recmaPlugins?: CompileOptions['recmaPlugins'];
7
- }
8
- export declare const mergePluginLists: <T>(...lists: Array<readonly T[] | null | undefined>) => T[] | undefined;
9
- export declare const appendMdxExtensions: (target: string[], mdxExtensions: string[]) => void;
10
- export declare const createMdxExtensionFilter: (extensions: string[], options?: {
11
- allowQueryString?: boolean;
12
- }) => RegExp;
13
- export declare function resolveLoaderExtensions(compilerOptions?: CompileOptions, options?: {
14
- defaultMdExtensions?: string[];
15
- }): string[];
16
- /**
17
- * Resolves the MDX parser mode for a source file.
18
- *
19
- * Files with a `.md` extension must be forced into `mdx` mode when the caller
20
- * explicitly opts them into the MDX pipeline. Leaving the compiler in `detect`
21
- * mode would treat `.md` files as plain markdown, causing top-level ESM such as
22
- * `import` and `export` to render as text instead of being compiled.
23
- */
24
- export declare function resolveCompileFormat(filePath: string, compilerOptions?: CompileOptions): CompileOptions['format'];
25
- export declare function resolveMdxCompilerOptions(mdxOptions: MdxCompilerOptionsInput, options: {
26
- jsxImportSource: string;
27
- jsxRuntime?: CompileOptions['jsxRuntime'];
28
- defaults?: CompileOptions;
29
- }): CompileOptions;
@@ -1,55 +0,0 @@
1
- import path from "node:path";
2
- const mergePluginLists = (...lists) => {
3
- const merged = lists.flatMap((list) => list ? [...list] : []);
4
- return merged.length > 0 ? merged : void 0;
5
- };
6
- const appendMdxExtensions = (target, mdxExtensions) => {
7
- for (const extension of mdxExtensions) {
8
- if (!target.includes(extension)) {
9
- target.push(extension);
10
- }
11
- }
12
- };
13
- const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
14
- const createMdxExtensionFilter = (extensions, options) => {
15
- const escaped = extensions.map(escapeRegex);
16
- const suffix = options?.allowQueryString ? "(\\?.*)?$" : "$";
17
- return new RegExp(`(${escaped.join("|")})${suffix}`);
18
- };
19
- function resolveLoaderExtensions(compilerOptions, options) {
20
- const mdxExtensions = compilerOptions?.mdxExtensions ?? [".mdx"];
21
- const mdExtensions = compilerOptions?.mdExtensions ?? options?.defaultMdExtensions ?? [];
22
- return [...mdxExtensions, ...mdExtensions];
23
- }
24
- function resolveCompileFormat(filePath, compilerOptions) {
25
- const configuredFormat = compilerOptions?.format;
26
- if (configuredFormat && configuredFormat !== "detect") {
27
- return configuredFormat;
28
- }
29
- return path.extname(filePath).toLowerCase() === ".md" ? "mdx" : configuredFormat;
30
- }
31
- function resolveMdxCompilerOptions(mdxOptions, options) {
32
- const { compilerOptions, remarkPlugins, rehypePlugins, recmaPlugins } = mdxOptions;
33
- const resolved = {
34
- ...options.defaults,
35
- ...compilerOptions,
36
- jsxImportSource: options.jsxImportSource,
37
- jsxRuntime: options.jsxRuntime ?? "automatic",
38
- development: process.env.NODE_ENV === "development"
39
- };
40
- const mergedRemark = mergePluginLists(compilerOptions?.remarkPlugins, remarkPlugins);
41
- const mergedRehype = mergePluginLists(compilerOptions?.rehypePlugins, rehypePlugins);
42
- const mergedRecma = mergePluginLists(compilerOptions?.recmaPlugins, recmaPlugins);
43
- if (mergedRemark) resolved.remarkPlugins = mergedRemark;
44
- if (mergedRehype) resolved.rehypePlugins = mergedRehype;
45
- if (mergedRecma) resolved.recmaPlugins = mergedRecma;
46
- return resolved;
47
- }
48
- export {
49
- appendMdxExtensions,
50
- createMdxExtensionFilter,
51
- mergePluginLists,
52
- resolveCompileFormat,
53
- resolveLoaderExtensions,
54
- resolveMdxCompilerOptions
55
- };