@ecopages/react 0.2.0-beta.31 → 0.2.0-beta.33

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 (57) hide show
  1. package/README.md +5 -1
  2. package/package.json +4 -4
  3. package/src/app/client-only.js +13 -17
  4. package/src/app/dynamic.js +7 -10
  5. package/src/app/dynamic.test.browser.js +27 -29
  6. package/src/bundling/bootstrap-package-paths.js +5 -6
  7. package/src/bundling/bundle.js +115 -114
  8. package/src/bundling/discover-layout-runtime-modules.js +166 -155
  9. package/src/bundling/runtime-alias-map.d.ts +4 -1
  10. package/src/bundling/runtime-alias-map.js +107 -95
  11. package/src/bundling/runtime-bundle.js +250 -274
  12. package/src/bundling/runtime-interop-plugin.js +31 -35
  13. package/src/bundling/runtime-mode.js +10 -5
  14. package/src/bundling/runtime-modules.js +46 -35
  15. package/src/client-graph/ast-transform.js +415 -305
  16. package/src/client-graph/boundary-cache.js +146 -104
  17. package/src/client-graph/boundary-plugin.js +122 -104
  18. package/src/client-graph/component-config-traversal.js +70 -49
  19. package/src/client-graph/declared-modules.js +63 -41
  20. package/src/client-graph/reachability-analyzer.js +506 -313
  21. package/src/client-graph/specifier-classification.js +209 -141
  22. package/src/contracts/router-adapter.js +6 -0
  23. package/src/dev-transform/react-dev-transform-contributor.d.ts +17 -0
  24. package/src/dev-transform/react-dev-transform-contributor.js +22 -0
  25. package/src/hmr/hmr-scripts.js +22 -9
  26. package/src/hmr/hmr-strategy.d.ts +12 -181
  27. package/src/hmr/hmr-strategy.js +321 -650
  28. package/src/hmr/page-metadata-cache.js +43 -40
  29. package/src/hmr/react-hmr-dev-transform-plugins.d.ts +23 -0
  30. package/src/hmr/react-hmr-dev-transform-plugins.js +20 -0
  31. package/src/hydration/assert-no-bare-ecopages-imports.js +6 -6
  32. package/src/hydration/bootstrap-imports.js +30 -20
  33. package/src/hydration/hydration-asset.js +216 -213
  34. package/src/hydration/hydration-scripts.d.ts +4 -0
  35. package/src/hydration/hydration-scripts.js +199 -66
  36. package/src/hydration/hydration-scripts.test.browser.js +163 -168
  37. package/src/hydration/page-data-manifest.js +89 -45
  38. package/src/hydration/page-data-reader.js +45 -32
  39. package/src/hydration/page-payload.js +101 -99
  40. package/src/hydration/serialize-page-data-script.js +27 -28
  41. package/src/mdx/mdx-config-dependency.js +108 -112
  42. package/src/mdx/mdx-loader-plugin.js +7 -10
  43. package/src/page/page-module.js +72 -66
  44. package/src/plugin/react.constants.js +1 -4
  45. package/src/plugin/react.plugin.d.ts +1 -0
  46. package/src/plugin/react.plugin.js +227 -216
  47. package/src/plugin/react.types.js +1 -0
  48. package/src/render/component-ownership.js +42 -18
  49. package/src/render/component-ssr.js +168 -190
  50. package/src/render/eco-embed.js +8 -10
  51. package/src/render/errors.js +21 -16
  52. package/src/render/html-boundary.js +55 -51
  53. package/src/render/layout-compose.js +122 -94
  54. package/src/render/react-renderer.js +433 -468
  55. package/src/render/react-runtime.js +25 -20
  56. package/src/render/unified-layout-composition.js +74 -85
  57. package/src/runtime/use-sync-external-store-with-selector.js +52 -54
@@ -1,23 +1,47 @@
1
- function getComponentIntegration(component) {
2
- return component?.config?.integration ?? component?.config?.__eco?.integration;
1
+ /**
2
+ * Ownership helpers for deciding whether a component stays in the React render lane.
3
+ */
4
+ /**
5
+ * Reads the declared integration name for a component or layout.
6
+ *
7
+ * @remarks
8
+ * Honors both the explicit `config.integration` override and injected
9
+ * `config.__eco.integration` metadata because pages can arrive through authored
10
+ * config as well as build-time component metadata.
11
+ */
12
+ export function getComponentIntegration(component) {
13
+ return component?.config?.integration ?? component?.config?.__eco?.integration;
3
14
  }
4
- function isReactManagedComponent(component, reactIntegrationName) {
5
- const integration = getComponentIntegration(component);
6
- return integration === void 0 || integration === reactIntegrationName;
15
+ /**
16
+ * Returns whether a component should stay inside the React render lane.
17
+ *
18
+ * @remarks
19
+ * Components without explicit integration metadata are treated as React-owned
20
+ * because this renderer only receives them after the route pipeline has already
21
+ * selected the React integration.
22
+ */
23
+ export function isReactManagedComponent(component, reactIntegrationName) {
24
+ const integration = getComponentIntegration(component);
25
+ return integration === undefined || integration === reactIntegrationName;
7
26
  }
8
- function getComponentRequires(component) {
9
- return component?.requires;
27
+ export function getComponentRequires(component) {
28
+ return component?.requires;
10
29
  }
11
- function asReactComponent(component) {
12
- return component;
30
+ /**
31
+ * Commits a framework-agnostic component to React semantics.
32
+ *
33
+ * @remarks
34
+ * Core keeps `EcoComponent` broad so integrations can share the same public surface;
35
+ * once the React renderer is executing, `createElement()` needs a concrete React
36
+ * component signature.
37
+ */
38
+ export function asReactComponent(component) {
39
+ return component;
13
40
  }
14
- function asNonReactShellComponent(component) {
15
- return component;
41
+ /**
42
+ * Commits a mixed-shell component to the string-returning contract required by
43
+ * non-React layouts and HTML templates.
44
+ */
45
+ export function asNonReactShellComponent(component) {
46
+ return component;
16
47
  }
17
- export {
18
- asNonReactShellComponent,
19
- asReactComponent,
20
- getComponentIntegration,
21
- getComponentRequires,
22
- isReactManagedComponent
23
- };
@@ -1,202 +1,180 @@
1
- import { isValidElement } from "react";
2
- import { hasSingleRootElement } from "./html-boundary.js";
3
- import { getIslandComponentKey } from "../hydration/hydration-asset.js";
4
- import { asNonReactShellComponent, asReactComponent } from "./component-ownership.js";
5
- import { ReactRenderError } from "./errors.js";
6
- function buildHydrationProps(props) {
7
- if (!props || !Object.prototype.hasOwnProperty.call(props, "locals")) {
8
- return props ?? {};
9
- }
10
- const { locals: _locals, ...hydrationProps } = props;
11
- return hydrationProps;
1
+ /**
2
+ * React component SSR helpers: renderToString, foreign-subtree HTML, island attributes.
3
+ */
4
+ import { buildIslandHostAttributes } from '@ecopages/core';
5
+ import { isValidElement } from 'react';
6
+ import { hasSingleRootElement } from './html-boundary.js';
7
+ import { getIslandComponentKey } from '../hydration/hydration-asset.js';
8
+ import { asNonReactShellComponent, asReactComponent } from './component-ownership.js';
9
+ import { ReactRenderError } from './errors.js';
10
+ export function buildHydrationProps(props) {
11
+ if (!props || !Object.prototype.hasOwnProperty.call(props, 'locals')) {
12
+ return props ?? {};
13
+ }
14
+ const { locals: _locals, ...hydrationProps } = props;
15
+ return hydrationProps;
12
16
  }
13
- async function renderNonReactShellComponent(Component, props, label) {
14
- const output = await Component(props);
15
- if (typeof output === "string") {
16
- return output;
17
- }
18
- throw new ReactRenderError(`${label} must return a string when used as a mixed shell for React pages.`);
17
+ /**
18
+ * Renders a non-React layout or HTML template and enforces that mixed shells
19
+ * return serialized HTML.
20
+ */
21
+ export async function renderNonReactShellComponent(Component, props, label) {
22
+ const output = await Component(props);
23
+ if (typeof output === 'string') {
24
+ return output;
25
+ }
26
+ throw new ReactRenderError(`${label} must return a string when used as a mixed shell for React pages.`);
19
27
  }
20
- function renderComponentHtml(options) {
21
- const { input, context, runtimeContext, runtime, normalizeUnresolvedMarkerArtifactHtml } = options;
22
- const { react, reactDomServer } = runtime;
23
- if (input.children === void 0) {
24
- return normalizeUnresolvedMarkerArtifactHtml(
25
- reactDomServer.renderToString(react.createElement(asReactComponent(input.component), input.props))
26
- );
27
- }
28
- if (isValidElement(input.children)) {
29
- return normalizeUnresolvedMarkerArtifactHtml(
30
- reactDomServer.renderToString(
31
- react.createElement(asReactComponent(input.component), input.props, input.children)
32
- )
33
- );
34
- }
35
- const resolvedChildHtml = typeof input.children === "string" ? input.children : String(input.children ?? "");
36
- const rawChildrenToken = `__ECO_RAW_HTML_CHILD_${context.componentInstanceId ?? "component"}__`;
37
- if (runtimeContext) {
38
- runtimeContext.rawChildrenToken = rawChildrenToken;
39
- runtimeContext.rawChildrenHtml = resolvedChildHtml;
40
- }
41
- const html = reactDomServer.renderToString(
42
- react.createElement(asReactComponent(input.component), input.props, rawChildrenToken)
43
- );
44
- return normalizeUnresolvedMarkerArtifactHtml(html.split(rawChildrenToken).join(resolvedChildHtml));
28
+ /**
29
+ * Renders one React component while preserving already-resolved child HTML.
30
+ *
31
+ * @remarks
32
+ * When nested foreign-subtree resolution has already produced child HTML, the child
33
+ * payload must remain raw SSR output rather than a React string child, otherwise
34
+ * React would escape it. This helper renders a unique token through React and swaps
35
+ * that token back to the resolved HTML afterward.
36
+ */
37
+ export function renderComponentHtml(options) {
38
+ const { input, context, runtimeContext, runtime, normalizeUnresolvedMarkerArtifactHtml } = options;
39
+ const { react, reactDomServer } = runtime;
40
+ if (input.children === undefined) {
41
+ return normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(react.createElement(asReactComponent(input.component), input.props)));
42
+ }
43
+ if (isValidElement(input.children)) {
44
+ return normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(react.createElement(asReactComponent(input.component), input.props, input.children)));
45
+ }
46
+ const resolvedChildHtml = typeof input.children === 'string' ? input.children : String(input.children ?? '');
47
+ const rawChildrenToken = `__ECO_RAW_HTML_CHILD_${context.componentInstanceId ?? 'component'}__`;
48
+ if (runtimeContext) {
49
+ runtimeContext.rawChildrenToken = rawChildrenToken;
50
+ runtimeContext.rawChildrenHtml = resolvedChildHtml;
51
+ }
52
+ const html = reactDomServer.renderToString(react.createElement(asReactComponent(input.component), input.props, rawChildrenToken));
53
+ return normalizeUnresolvedMarkerArtifactHtml(html.split(rawChildrenToken).join(resolvedChildHtml));
45
54
  }
46
- function toReactNode(children, integrationName) {
47
- if (children === null || typeof children === "string" || typeof children === "number" || typeof children === "boolean" || isValidElement(children) || Array.isArray(children)) {
48
- return children;
49
- }
50
- throw new TypeError(`[ecopages] ${integrationName} renderer expected a React node child.`);
55
+ export function toReactNode(children, integrationName) {
56
+ if (children === null ||
57
+ typeof children === 'string' ||
58
+ typeof children === 'number' ||
59
+ typeof children === 'boolean' ||
60
+ isValidElement(children) ||
61
+ Array.isArray(children)) {
62
+ return children;
63
+ }
64
+ throw new TypeError(`[ecopages] ${integrationName} renderer expected a React node child.`);
51
65
  }
52
- function restoreRuntimeChildHtml(html, runtimeContext) {
53
- if (!runtimeContext?.rawChildrenToken || runtimeContext.rawChildrenHtml === void 0) {
54
- return html;
55
- }
56
- return html.split(runtimeContext.rawChildrenToken).join(runtimeContext.rawChildrenHtml);
66
+ /**
67
+ * Restores raw child HTML placeholders stored on the foreign-subtree runtime context.
68
+ *
69
+ * @remarks
70
+ * Queued foreign-subtree resolution may render children through a fragment path before
71
+ * nested integration tokens are resolved. React must never see resolved child HTML as a
72
+ * normal string child or it would escape it.
73
+ */
74
+ export function restoreRuntimeChildHtml(html, runtimeContext) {
75
+ if (!runtimeContext?.rawChildrenToken || runtimeContext.rawChildrenHtml === undefined) {
76
+ return html;
77
+ }
78
+ return html.split(runtimeContext.rawChildrenToken).join(runtimeContext.rawChildrenHtml);
57
79
  }
58
- async function renderQueuedChildrenToHtml(options) {
59
- const {
60
- children,
61
- runtimeContext,
62
- queuedResolutionsByToken,
63
- resolveToken,
64
- runtime,
65
- integrationName,
66
- normalizeUnresolvedMarkerArtifactHtml,
67
- resolveQueuedTokens
68
- } = options;
69
- if (children === void 0) {
70
- return void 0;
71
- }
72
- const { react, reactDomServer } = runtime;
73
- let html = normalizeUnresolvedMarkerArtifactHtml(
74
- reactDomServer.renderToString(
75
- react.createElement(react.Fragment, null, toReactNode(children, integrationName))
76
- )
77
- );
78
- html = restoreRuntimeChildHtml(html, runtimeContext);
79
- return await resolveQueuedTokens(html, queuedResolutionsByToken, resolveToken);
80
+ export async function renderQueuedChildrenToHtml(options) {
81
+ const { children, runtimeContext, queuedResolutionsByToken, resolveToken, runtime, integrationName, normalizeUnresolvedMarkerArtifactHtml, resolveQueuedTokens, } = options;
82
+ if (children === undefined) {
83
+ return undefined;
84
+ }
85
+ const { react, reactDomServer } = runtime;
86
+ let html = normalizeUnresolvedMarkerArtifactHtml(reactDomServer.renderToString(react.createElement(react.Fragment, null, toReactNode(children, integrationName))));
87
+ html = restoreRuntimeChildHtml(html, runtimeContext);
88
+ return await resolveQueuedTokens(html, queuedResolutionsByToken, resolveToken);
80
89
  }
81
- async function renderReactQueuedForeignSubtreeChildren(options) {
82
- const { currentRuntimeContext, ...rest } = options;
83
- const renderedHtml = await renderQueuedChildrenToHtml({
84
- ...rest,
85
- runtimeContext: currentRuntimeContext
86
- });
87
- if (renderedHtml === void 0) {
88
- return { assets: [] };
89
- }
90
- return {
91
- assets: [],
92
- html: renderedHtml
93
- };
90
+ export async function renderReactQueuedForeignSubtreeChildren(options) {
91
+ const { currentRuntimeContext, ...rest } = options;
92
+ const renderedHtml = await renderQueuedChildrenToHtml({
93
+ ...rest,
94
+ runtimeContext: currentRuntimeContext,
95
+ });
96
+ if (renderedHtml === undefined) {
97
+ return { assets: [] };
98
+ }
99
+ return {
100
+ assets: [],
101
+ html: renderedHtml,
102
+ };
94
103
  }
95
- async function renderForeignComponentWithSerializedHtml(options) {
96
- const {
97
- input,
98
- runtimeContext,
99
- integrationName,
100
- canResolveAssets,
101
- processComponentDependencies,
102
- dedupeProcessedAssets,
103
- getRootTagName,
104
- resolveQueuedForeignSubtreeHtml
105
- } = options;
106
- let props = input.props;
107
- if (input.children !== void 0) {
108
- props = {
109
- ...input.props,
110
- children: typeof input.children === "string" ? input.children : String(input.children ?? "")
104
+ export async function renderForeignComponentWithSerializedHtml(options) {
105
+ const { input, runtimeContext, integrationName, canResolveAssets, processComponentDependencies, dedupeProcessedAssets, getRootTagName, resolveQueuedForeignSubtreeHtml, } = options;
106
+ let props = input.props;
107
+ if (input.children !== undefined) {
108
+ props = {
109
+ ...input.props,
110
+ children: typeof input.children === 'string' ? input.children : String(input.children ?? ''),
111
+ };
112
+ }
113
+ const html = await renderNonReactShellComponent(asNonReactShellComponent(input.component), props, 'Component');
114
+ const hasDependencies = Boolean(input.component.config?.dependencies);
115
+ const assets = hasDependencies && canResolveAssets ? await processComponentDependencies([input.component]) : undefined;
116
+ const queuedForeignSubtreeResolution = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
117
+ const mergedAssets = dedupeProcessedAssets([...(assets ?? []), ...queuedForeignSubtreeResolution.assets]);
118
+ return {
119
+ html: queuedForeignSubtreeResolution.html,
120
+ canAttachAttributes: true,
121
+ rootTag: getRootTagName(queuedForeignSubtreeResolution.html),
122
+ integrationName,
123
+ assets: mergedAssets.length > 0 ? mergedAssets : undefined,
111
124
  };
112
- }
113
- const html = await renderNonReactShellComponent(
114
- asNonReactShellComponent(input.component),
115
- props,
116
- "Component"
117
- );
118
- const hasDependencies = Boolean(input.component.config?.dependencies);
119
- const assets = hasDependencies && canResolveAssets ? await processComponentDependencies([input.component]) : void 0;
120
- const queuedForeignSubtreeResolution = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
121
- const mergedAssets = dedupeProcessedAssets([...assets ?? [], ...queuedForeignSubtreeResolution.assets]);
122
- return {
123
- html: queuedForeignSubtreeResolution.html,
124
- canAttachAttributes: true,
125
- rootTag: getRootTagName(queuedForeignSubtreeResolution.html),
126
- integrationName,
127
- assets: mergedAssets.length > 0 ? mergedAssets : void 0
128
- };
129
125
  }
130
- async function renderReactManagedComponent(options) {
131
- const {
132
- input,
133
- runtimeContext,
134
- runtime,
135
- integrationName,
136
- normalizeUnresolvedMarkerArtifactHtml,
137
- resolveQueuedForeignSubtreeHtml,
138
- getRootTagName,
139
- dedupeProcessedAssets,
140
- hydrationAssetService,
141
- canBuildIslandAssets
142
- } = options;
143
- const componentConfig = input.component.config;
144
- const context = {
145
- componentInstanceId: input.integrationContext?.componentInstanceId
146
- };
147
- const hasResolvedChildHtml = input.children !== void 0;
148
- let html = renderComponentHtml({
149
- input,
150
- context,
151
- runtimeContext,
152
- runtime,
153
- normalizeUnresolvedMarkerArtifactHtml
154
- });
155
- const queuedForeignSubtreeResolution = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
156
- html = queuedForeignSubtreeResolution.html;
157
- const canAttachAttributes = hasSingleRootElement(html);
158
- const rootTag = getRootTagName(html);
159
- const componentFile = componentConfig?.__eco?.file;
160
- let rootAttributes;
161
- let assets;
162
- if (canAttachAttributes && componentFile && context.componentInstanceId && canBuildIslandAssets && !hasResolvedChildHtml) {
163
- const componentInstanceId = context.componentInstanceId;
164
- assets = await hydrationAssetService.buildComponentRenderAssets(componentFile, componentConfig);
165
- rootAttributes = {
166
- "data-eco-component-id": componentInstanceId,
167
- "data-eco-component-key": getIslandComponentKey(componentFile, componentConfig),
168
- "data-eco-props": btoa(JSON.stringify(buildHydrationProps(input.props)))
126
+ export async function renderReactManagedComponent(options) {
127
+ const { input, runtimeContext, runtime, integrationName, normalizeUnresolvedMarkerArtifactHtml, resolveQueuedForeignSubtreeHtml, getRootTagName, dedupeProcessedAssets, hydrationAssetService, canBuildIslandAssets, } = options;
128
+ const componentConfig = input.component.config;
129
+ const context = {
130
+ componentInstanceId: input.integrationContext?.componentInstanceId,
131
+ };
132
+ const hasResolvedChildHtml = input.children !== undefined;
133
+ let html = renderComponentHtml({
134
+ input,
135
+ context,
136
+ runtimeContext,
137
+ runtime,
138
+ normalizeUnresolvedMarkerArtifactHtml,
139
+ });
140
+ const queuedForeignSubtreeResolution = await resolveQueuedForeignSubtreeHtml(html, runtimeContext);
141
+ html = queuedForeignSubtreeResolution.html;
142
+ const canAttachAttributes = hasSingleRootElement(html);
143
+ const rootTag = getRootTagName(html);
144
+ const componentFile = componentConfig?.__eco?.file;
145
+ let rootAttributes;
146
+ let assets;
147
+ if (canAttachAttributes &&
148
+ componentFile &&
149
+ context.componentInstanceId &&
150
+ canBuildIslandAssets &&
151
+ !hasResolvedChildHtml) {
152
+ const componentInstanceId = context.componentInstanceId;
153
+ assets = await hydrationAssetService.buildComponentRenderAssets(componentFile, componentConfig);
154
+ rootAttributes = buildIslandHostAttributes({
155
+ integrationName,
156
+ componentInstanceId,
157
+ componentKey: getIslandComponentKey(componentFile, componentConfig),
158
+ props: buildHydrationProps(input.props),
159
+ });
160
+ }
161
+ const mergedAssets = dedupeProcessedAssets([...(assets ?? []), ...queuedForeignSubtreeResolution.assets]);
162
+ return {
163
+ html,
164
+ canAttachAttributes,
165
+ rootTag,
166
+ integrationName,
167
+ rootAttributes,
168
+ assets: mergedAssets.length > 0 ? mergedAssets : undefined,
169
169
  };
170
- }
171
- const mergedAssets = dedupeProcessedAssets([...assets ?? [], ...queuedForeignSubtreeResolution.assets]);
172
- return {
173
- html,
174
- canAttachAttributes,
175
- rootTag,
176
- integrationName,
177
- rootAttributes,
178
- assets: mergedAssets.length > 0 ? mergedAssets : void 0
179
- };
180
170
  }
181
- function createForeignSubtreeRuntimeContext(options) {
182
- return {
183
- rendererCache: options.rendererCache,
184
- componentInstanceScope: options.componentInstanceScope,
185
- nextForeignSubtreeId: 0,
186
- queuedResolutions: [],
187
- rawChildrenToken: void 0,
188
- rawChildrenHtml: void 0
189
- };
171
+ export function createForeignSubtreeRuntimeContext(options) {
172
+ return {
173
+ rendererCache: options.rendererCache,
174
+ componentInstanceScope: options.componentInstanceScope,
175
+ nextForeignSubtreeId: 0,
176
+ queuedResolutions: [],
177
+ rawChildrenToken: undefined,
178
+ rawChildrenHtml: undefined,
179
+ };
190
180
  }
191
- export {
192
- buildHydrationProps,
193
- createForeignSubtreeRuntimeContext,
194
- renderComponentHtml,
195
- renderForeignComponentWithSerializedHtml,
196
- renderNonReactShellComponent,
197
- renderQueuedChildrenToHtml,
198
- renderReactManagedComponent,
199
- renderReactQueuedForeignSubtreeChildren,
200
- restoreRuntimeChildHtml,
201
- toReactNode
202
- };
@@ -1,11 +1,9 @@
1
- import { eco } from "@ecopages/core";
2
- function EcoEmbed({
3
- component,
4
- props,
5
- children
6
- }) {
7
- return eco.embed(component, props, children);
1
+ /** @jsxImportSource react */
2
+ import { eco } from '@ecopages/core';
3
+ /**
4
+ * Renders a foreign or same-integration eco component from a React JSX file
5
+ * without forcing inline mixed-JSX authoring.
6
+ */
7
+ export function EcoEmbed({ component, props, children, }) {
8
+ return eco.embed(component, props, children);
8
9
  }
9
- export {
10
- EcoEmbed
11
- };
@@ -1,18 +1,23 @@
1
- class ReactRenderError extends Error {
2
- constructor(message) {
3
- super(message);
4
- this.name = "ReactRenderError";
5
- }
1
+ /**
2
+ * Errors thrown by the React integration renderer and bundling paths.
3
+ */
4
+ /**
5
+ * Error thrown when an error occurs while rendering a React component.
6
+ */
7
+ export class ReactRenderError extends Error {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = 'ReactRenderError';
11
+ }
6
12
  }
7
- class BundleError extends Error {
8
- logs;
9
- constructor(message, logs) {
10
- super(message);
11
- this.name = "BundleError";
12
- this.logs = logs;
13
- }
13
+ /**
14
+ * Error thrown when an error occurs while bundling a React component.
15
+ */
16
+ export class BundleError extends Error {
17
+ logs;
18
+ constructor(message, logs) {
19
+ super(message);
20
+ this.name = 'BundleError';
21
+ this.logs = logs;
22
+ }
14
23
  }
15
- export {
16
- BundleError,
17
- ReactRenderError
18
- };
@@ -1,55 +1,59 @@
1
- const VOID_TAGS = /* @__PURE__ */ new Set([
2
- "area",
3
- "base",
4
- "br",
5
- "col",
6
- "embed",
7
- "hr",
8
- "img",
9
- "input",
10
- "link",
11
- "meta",
12
- "param",
13
- "source",
14
- "track",
15
- "wbr"
1
+ const VOID_TAGS = new Set([
2
+ 'area',
3
+ 'base',
4
+ 'br',
5
+ 'col',
6
+ 'embed',
7
+ 'hr',
8
+ 'img',
9
+ 'input',
10
+ 'link',
11
+ 'meta',
12
+ 'param',
13
+ 'source',
14
+ 'track',
15
+ 'wbr',
16
16
  ]);
17
- function hasSingleRootElement(html) {
18
- const firstTagMatch = html.match(/^\s*<([a-zA-Z][a-zA-Z0-9:-]*)\b[^>]*>/);
19
- if (!firstTagMatch) {
20
- return false;
21
- }
22
- const firstTag = firstTagMatch[1].toLowerCase();
23
- const firstTagText = firstTagMatch[0];
24
- const firstTagStart = firstTagMatch.index ?? 0;
25
- const firstTagEnd = firstTagStart + firstTagText.length;
26
- const isSelfClosing = /\/\s*>$/.test(firstTagText);
27
- if (isSelfClosing || VOID_TAGS.has(firstTag)) {
28
- return html.slice(firstTagEnd).trim().length === 0;
29
- }
30
- const tokenRegex = /<\/?([a-zA-Z][a-zA-Z0-9:-]*)\b[^>]*>/g;
31
- tokenRegex.lastIndex = firstTagEnd;
32
- let depth = 1;
33
- for (let token = tokenRegex.exec(html); token; token = tokenRegex.exec(html)) {
34
- const tagText = token[0];
35
- const tagName = token[1].toLowerCase();
36
- const isClosing = tagText.startsWith("</");
37
- const tokenSelfClosing = /\/\s*>$/.test(tagText);
38
- if (VOID_TAGS.has(tagName) || tokenSelfClosing) {
39
- continue;
17
+ /**
18
+ * Returns true when HTML contains exactly one root element node.
19
+ *
20
+ * Used by component-level React rendering to decide whether root attributes can
21
+ * be attached safely without introducing synthetic wrapper nodes.
22
+ */
23
+ export function hasSingleRootElement(html) {
24
+ const firstTagMatch = html.match(/^\s*<([a-zA-Z][a-zA-Z0-9:-]*)\b[^>]*>/);
25
+ if (!firstTagMatch) {
26
+ return false;
27
+ }
28
+ const firstTag = firstTagMatch[1].toLowerCase();
29
+ const firstTagText = firstTagMatch[0];
30
+ const firstTagStart = firstTagMatch.index ?? 0;
31
+ const firstTagEnd = firstTagStart + firstTagText.length;
32
+ const isSelfClosing = /\/\s*>$/.test(firstTagText);
33
+ if (isSelfClosing || VOID_TAGS.has(firstTag)) {
34
+ return html.slice(firstTagEnd).trim().length === 0;
40
35
  }
41
- if (isClosing) {
42
- depth--;
43
- if (depth === 0) {
44
- const afterRoot = html.slice(token.index + token[0].length).trim();
45
- return afterRoot.length === 0;
46
- }
47
- } else {
48
- depth++;
36
+ const tokenRegex = /<\/?([a-zA-Z][a-zA-Z0-9:-]*)\b[^>]*>/g;
37
+ tokenRegex.lastIndex = firstTagEnd;
38
+ let depth = 1;
39
+ for (let token = tokenRegex.exec(html); token; token = tokenRegex.exec(html)) {
40
+ const tagText = token[0];
41
+ const tagName = token[1].toLowerCase();
42
+ const isClosing = tagText.startsWith('</');
43
+ const tokenSelfClosing = /\/\s*>$/.test(tagText);
44
+ if (VOID_TAGS.has(tagName) || tokenSelfClosing) {
45
+ continue;
46
+ }
47
+ if (isClosing) {
48
+ depth--;
49
+ if (depth === 0) {
50
+ const afterRoot = html.slice(token.index + token[0].length).trim();
51
+ return afterRoot.length === 0;
52
+ }
53
+ }
54
+ else {
55
+ depth++;
56
+ }
49
57
  }
50
- }
51
- return false;
58
+ return false;
52
59
  }
53
- export {
54
- hasSingleRootElement
55
- };