@ecopages/ecopages-jsx 0.2.0-beta.9 → 0.2.0-rc.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.
@@ -1,120 +1,171 @@
1
- import {
2
- IntegrationPlugin
3
- } from "@ecopages/core/plugins/integration-plugin";
4
- import { AssetFactory } from "@ecopages/core/services/asset-processing-service";
5
- import { ECOPAGES_JSX_PLUGIN_NAME } from "./ecopages-jsx.constants.js";
6
- import {
7
- appendMdxExtensions,
8
- createMdxLoaderPlugin,
9
- registerBunMdxPlugin,
10
- resolveMdxCompilerOptions
11
- } from "./ecopages-jsx-mdx.js";
12
- import { EcopagesJsxRenderer } from "./ecopages-jsx-renderer.js";
13
- const RADIANT_HYDRATOR_SCRIPT_ID = "ecopages-jsx-radiant-hydrator";
1
+ import { IntegrationPlugin, } from '@ecopages/core/plugins/integration-plugin';
2
+ import { AssetFactory } from '@ecopages/core/services/asset-processing-service';
3
+ import { ECOPAGES_JSX_PLUGIN_NAME } from './ecopages-jsx.constants.js';
4
+ import { RADIANT_INSTALL_HYDRATOR_FILEPATH } from './resolve-radiant-install-hydrator.js';
5
+ import { appendMdxExtensions, createMdxLoaderPlugin, registerBunMdxPlugin, resolveMdxCompilerOptions, } from './ecopages-jsx-mdx.js';
6
+ import { EcopagesJsxRenderer } from './ecopages-jsx-renderer.js';
7
+ import { EcopagesJsxHmrStrategy } from './ecopages-jsx-hmr-strategy.js';
8
+ import { EcopagesJsxDevTransformContributor } from './dev-transform/ecopages-jsx-dev-transform-contributor.js';
9
+ import { assignPageOwnedContentScriptGroupedBundles } from './page-owned-content-script-grouping.js';
10
+ import { invalidateRadiantRegisteredScriptSsrRegistration } from './radiant-registered-script-ssr-invalidation.js';
11
+ const RADIANT_HYDRATOR_SCRIPT_ID = 'ecopages-jsx-radiant-hydrator';
12
+ /**
13
+ * Resolves user-facing plugin options into a fully-defaulted internal config.
14
+ *
15
+ * Defaults:
16
+ * - `extensions`: `['.tsx']`
17
+ * - `radiant`: `true`
18
+ * - `mdx.enabled`: `false`
19
+ * - `mdx.extensions`: `['.mdx']`
20
+ */
14
21
  const resolvePluginOptions = (options) => {
15
- const { extensions: userExtensions, radiant, mdx, ...baseConfig } = options ?? {};
16
- const extensions = [...userExtensions ?? [".tsx"]];
17
- const mdxExtensions = mdx?.extensions ?? [".mdx"];
18
- const mdxEnabled = mdx?.enabled ?? false;
19
- if (mdxEnabled) {
20
- appendMdxExtensions(extensions, mdxExtensions);
21
- }
22
- return {
23
- ...baseConfig,
24
- extensions,
25
- includeRadiant: radiant ?? true,
26
- mdxEnabled,
27
- mdxExtensions,
28
- mdxCompilerOptions: mdxEnabled && mdx ? resolveMdxCompilerOptions(mdx) : void 0
29
- };
22
+ const { extensions: userExtensions, radiant, mdx, ...baseConfig } = options ?? {};
23
+ const extensions = [...(userExtensions ?? ['.tsx'])];
24
+ const mdxExtensions = mdx?.extensions ?? ['.mdx'];
25
+ const mdxEnabled = mdx?.enabled ?? false;
26
+ if (mdxEnabled) {
27
+ appendMdxExtensions(extensions, mdxExtensions);
28
+ }
29
+ return {
30
+ ...baseConfig,
31
+ extensions,
32
+ includeRadiant: radiant ?? true,
33
+ mdxEnabled,
34
+ mdxExtensions,
35
+ mdxCompilerOptions: mdxEnabled && mdx ? resolveMdxCompilerOptions(mdx) : undefined,
36
+ };
30
37
  };
31
- class EcopagesJsxPlugin extends IntegrationPlugin {
32
- renderer = EcopagesJsxRenderer;
33
- includeRadiant;
34
- mdxEnabled;
35
- mdxCompilerOptions;
36
- mdxExtensions;
37
- mdxLoaderPlugin;
38
- /** Returns the build plugins required by the JSX integration. */
39
- get plugins() {
40
- return [this.mdxLoaderPlugin].filter((plugin) => plugin !== void 0);
41
- }
42
- /** Creates the renderer instance with the resolved JSX integration runtime options. */
43
- initializeRenderer(options) {
44
- return this.attachRendererRuntimeServices(
45
- new this.renderer({
46
- ...this.createRendererOptions(options),
47
- jsxConfig: {
48
- mdxExtensions: this.mdxExtensions,
49
- radiantSsrEnabled: this.includeRadiant
38
+ /** JSX integration plugin for Ecopages, supporting `.tsx` templates and optional Radiant web components. */
39
+ export class EcopagesJsxPlugin extends IntegrationPlugin {
40
+ renderer = EcopagesJsxRenderer;
41
+ includeRadiant;
42
+ mdxEnabled;
43
+ mdxCompilerOptions;
44
+ mdxExtensions;
45
+ mdxLoaderPlugin;
46
+ /** Returns the build plugins required by the JSX integration. */
47
+ get plugins() {
48
+ return [this.mdxLoaderPlugin].filter((plugin) => plugin !== undefined);
49
+ }
50
+ /** Creates the renderer instance with the resolved JSX integration runtime options. */
51
+ initializeRenderer(options) {
52
+ return this.attachRendererRuntimeServices(new this.renderer({
53
+ ...this.createRendererOptions(options),
54
+ jsxConfig: {
55
+ mdxExtensions: this.mdxExtensions,
56
+ radiantSsrEnabled: this.includeRadiant,
57
+ },
58
+ }));
59
+ }
60
+ constructor(options) {
61
+ const config = resolvePluginOptions(options);
62
+ const { extensions, includeRadiant, mdxEnabled, mdxExtensions, mdxCompilerOptions, ...baseConfig } = config;
63
+ super({
64
+ name: ECOPAGES_JSX_PLUGIN_NAME,
65
+ extensions,
66
+ jsxImportSource: '@ecopages/jsx',
67
+ ...baseConfig,
68
+ });
69
+ this.includeRadiant = includeRadiant;
70
+ this.mdxEnabled = mdxEnabled;
71
+ this.mdxExtensions = mdxExtensions;
72
+ this.mdxCompilerOptions = mdxCompilerOptions;
73
+ if (this.includeRadiant) {
74
+ this.integrationDependencies.unshift(...this.getDependencies());
75
+ }
76
+ }
77
+ getDependencies() {
78
+ return [
79
+ AssetFactory.createNodeModuleScript({
80
+ position: 'head',
81
+ importPath: RADIANT_INSTALL_HYDRATOR_FILEPATH,
82
+ bundle: false,
83
+ attributes: {
84
+ 'data-eco-script-id': RADIANT_HYDRATOR_SCRIPT_ID,
85
+ },
86
+ }),
87
+ ];
88
+ }
89
+ /** Ensures MDX build hooks are ready before Ecopages collects contributions. */
90
+ async prepareBuildContributions() {
91
+ this.ensureMdxLoaderPlugin();
92
+ }
93
+ /** Assigns grouped-build metadata for page-owned JSX content scripts. */
94
+ prepareAssetDependencies(dependencies) {
95
+ return assignPageOwnedContentScriptGroupedBundles(dependencies);
96
+ }
97
+ /** Registers JSX HMR strategy, dev-transform plugins, and Radiant SSR invalidation hooks. */
98
+ setHmrManager(hmrManager) {
99
+ super.setHmrManager(hmrManager);
100
+ const strategy = this.getHmrStrategy();
101
+ if (strategy) {
102
+ hmrManager.registerDevTransformContributor(new EcopagesJsxDevTransformContributor({
103
+ strategy: strategy,
104
+ getMdxLoaderPlugin: () => this.mdxLoaderPlugin,
105
+ }));
106
+ }
107
+ if (!this.includeRadiant || !this.appConfig) {
108
+ return;
50
109
  }
51
- })
52
- );
53
- }
54
- constructor(options) {
55
- const config = resolvePluginOptions(options);
56
- const { extensions, includeRadiant, mdxEnabled, mdxExtensions, mdxCompilerOptions, ...baseConfig } = config;
57
- super({
58
- name: ECOPAGES_JSX_PLUGIN_NAME,
59
- extensions,
60
- jsxImportSource: "@ecopages/jsx",
61
- ...baseConfig
62
- });
63
- this.includeRadiant = includeRadiant;
64
- this.mdxEnabled = mdxEnabled;
65
- this.mdxExtensions = mdxExtensions;
66
- this.mdxCompilerOptions = mdxCompilerOptions;
67
- if (this.includeRadiant) {
68
- this.integrationDependencies.unshift(...this.getDependencies());
110
+ const runtime = this.appConfig.runtime ?? {};
111
+ this.appConfig.runtime = runtime;
112
+ runtime.registeredScriptEntrypointChangeHandlers ??= [];
113
+ runtime.registeredScriptEntrypointChangeHandlers.push(invalidateRadiantRegisteredScriptSsrRegistration);
69
114
  }
70
- }
71
- getDependencies() {
72
- return [
73
- AssetFactory.createNodeModuleScript({
74
- position: "head",
75
- importPath: "@ecopages/radiant/client/install-hydrator",
76
- bundle: false,
77
- attributes: {
78
- "data-eco-script-id": RADIANT_HYDRATOR_SCRIPT_ID
115
+ /**
116
+ * Returns the JSX-integration HMR strategy.
117
+ *
118
+ * @remarks
119
+ * The strategy handles `layout-update` broadcasts for page sources, layout
120
+ * sources, and any component file in the active render tree. It defers
121
+ * registered script entrypoints to the generic JS strategy and includes /
122
+ * explicit server views to `ServerRenderedTemplateHmrStrategy`.
123
+ */
124
+ getHmrStrategy() {
125
+ if (!this.hmrManager || !this.appConfig) {
126
+ return undefined;
79
127
  }
80
- })
81
- ];
82
- }
83
- /** Ensures MDX build hooks are ready before Ecopages collects contributions. */
84
- async prepareBuildContributions() {
85
- this.ensureMdxLoaderPlugin();
86
- }
87
- /**
88
- * Registers MDX tooling and completes the base integration setup.
89
- */
90
- async setup() {
91
- this.ensureMdxLoaderPlugin();
92
- if (typeof Bun !== "undefined" && this.mdxEnabled && this.mdxCompilerOptions) {
93
- await this.registerMdxBunPlugin();
128
+ const context = this.hmrManager.getDefaultContext();
129
+ const absolutePaths = this.appConfig.absolutePaths;
130
+ return new EcopagesJsxHmrStrategy({
131
+ getRegisteredEntrypoints: context.getRegisteredEntrypoints,
132
+ getSrcDir: context.getSrcDir,
133
+ getPagesDir: context.getPagesDir,
134
+ getLayoutsDir: context.getLayoutsDir,
135
+ getIncludesDir: () => absolutePaths.includesDir,
136
+ getTemplateExtensions: () => this.extensions,
137
+ });
94
138
  }
95
- await super.setup();
96
- }
97
- ensureMdxLoaderPlugin() {
98
- if (!this.mdxEnabled || !this.mdxCompilerOptions || this.mdxLoaderPlugin) {
99
- return;
139
+ /**
140
+ * Registers MDX tooling and completes the base integration setup.
141
+ */
142
+ async setup() {
143
+ this.ensureMdxLoaderPlugin();
144
+ if (typeof Bun !== 'undefined' && this.mdxEnabled && this.mdxCompilerOptions) {
145
+ await this.registerMdxBunPlugin();
146
+ }
147
+ await super.setup();
100
148
  }
101
- this.mdxLoaderPlugin = createMdxLoaderPlugin(this.mdxCompilerOptions, this.mdxExtensions);
102
- }
103
- /**
104
- * Registers Bun's MDX loader at runtime setup time.
105
- *
106
- * Build-time contribution collection may run where Bun is absent, so
107
- * this hook stays isolated from manifest preparation.
108
- */
109
- async registerMdxBunPlugin() {
110
- if (typeof Bun === "undefined" || !this.mdxCompilerOptions) {
111
- return;
149
+ ensureMdxLoaderPlugin() {
150
+ if (!this.mdxEnabled || !this.mdxCompilerOptions || this.mdxLoaderPlugin) {
151
+ return;
152
+ }
153
+ this.mdxLoaderPlugin = createMdxLoaderPlugin(this.mdxCompilerOptions, this.mdxExtensions);
154
+ }
155
+ /**
156
+ * Registers Bun's MDX loader at runtime setup time.
157
+ *
158
+ * Build-time contribution collection may run where Bun is absent, so
159
+ * this hook stays isolated from manifest preparation.
160
+ */
161
+ async registerMdxBunPlugin() {
162
+ if (typeof Bun === 'undefined' || !this.mdxCompilerOptions) {
163
+ return;
164
+ }
165
+ await registerBunMdxPlugin(this.mdxCompilerOptions, this.mdxExtensions);
112
166
  }
113
- await registerBunMdxPlugin(this.mdxCompilerOptions, this.mdxExtensions);
114
- }
115
167
  }
116
- const ecopagesJsxPlugin = (options) => new EcopagesJsxPlugin(options);
117
- export {
118
- EcopagesJsxPlugin,
119
- ecopagesJsxPlugin
120
- };
168
+ /**
169
+ * Creates the JSX integration plugin with resolved defaults.
170
+ */
171
+ export const ecopagesJsxPlugin = (options) => new EcopagesJsxPlugin(options);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ import type { AssetDefinition } from '@ecopages/core/services/asset-processing-service';
2
+ /** Returns the stable grouped-build id for one integration's page-owned content scripts. */
3
+ export declare function createPageOwnedContentScriptBundleId(integrationName: string): string;
4
+ /**
5
+ * Tags page-owned content scripts in one dependency batch with a shared grouped-build id.
6
+ *
7
+ * @remarks
8
+ * Lazy hydration entries (`excludeFromHtml`), component file scripts, and integration runtime
9
+ * assets stay ungrouped. Integrations that already assign a grouped id (for example React
10
+ * Router pages) are left unchanged.
11
+ */
12
+ export declare function assignPageOwnedContentScriptGroupedBundles(dependencies: AssetDefinition[]): AssetDefinition[];
@@ -0,0 +1,63 @@
1
+ import { rapidhash } from '@ecopages/core/utils/hash';
2
+ import { ECOPAGES_JSX_PLUGIN_NAME } from './ecopages-jsx.constants.js';
3
+ /** Shared grouped-build id prefix for page-owned content scripts. */
4
+ const PAGE_OWNED_CONTENT_SCRIPT_BUNDLE_PREFIX = 'ecopages';
5
+ /** Returns the stable grouped-build id for one integration's page-owned content scripts. */
6
+ export function createPageOwnedContentScriptBundleId(integrationName) {
7
+ return `${PAGE_OWNED_CONTENT_SCRIPT_BUNDLE_PREFIX}-${integrationName}-page-content-scripts`;
8
+ }
9
+ function isPageOwnedGroupableContentScript(dep) {
10
+ if (dep.kind !== 'script' || dep.source !== 'content' || dep.inline) {
11
+ return false;
12
+ }
13
+ if (dep.excludeFromHtml) {
14
+ return false;
15
+ }
16
+ if (dep.groupedBundle?.id) {
17
+ return false;
18
+ }
19
+ if (dep.packageRole === 'keep-separate' || dep.packageRole === 'runtime' || dep.packageRole === 'dynamic-chunk') {
20
+ return false;
21
+ }
22
+ return true;
23
+ }
24
+ function createPageOwnedGroupedEntryName(dep, usedEntryNames) {
25
+ const baseName = dep.name ?? `script-${rapidhash(dep.content).toString(16)}`;
26
+ if (!usedEntryNames.has(baseName)) {
27
+ usedEntryNames.add(baseName);
28
+ return baseName;
29
+ }
30
+ let suffix = 2;
31
+ let candidate = `${baseName}-${suffix}`;
32
+ while (usedEntryNames.has(candidate)) {
33
+ suffix += 1;
34
+ candidate = `${baseName}-${suffix}`;
35
+ }
36
+ usedEntryNames.add(candidate);
37
+ return candidate;
38
+ }
39
+ /**
40
+ * Tags page-owned content scripts in one dependency batch with a shared grouped-build id.
41
+ *
42
+ * @remarks
43
+ * Lazy hydration entries (`excludeFromHtml`), component file scripts, and integration runtime
44
+ * assets stay ungrouped. Integrations that already assign a grouped id (for example React
45
+ * Router pages) are left unchanged.
46
+ */
47
+ export function assignPageOwnedContentScriptGroupedBundles(dependencies) {
48
+ const bundleId = createPageOwnedContentScriptBundleId(ECOPAGES_JSX_PLUGIN_NAME);
49
+ const usedEntryNames = new Set();
50
+ for (const dependency of dependencies) {
51
+ if (!isPageOwnedGroupableContentScript(dependency)) {
52
+ continue;
53
+ }
54
+ dependency.groupedBundle = {
55
+ id: bundleId,
56
+ entryName: createPageOwnedGroupedEntryName(dependency, usedEntryNames),
57
+ };
58
+ if (dependency.bundle === false) {
59
+ dependency.bundle = true;
60
+ }
61
+ }
62
+ return dependencies;
63
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Reads the custom-element tag name from a registered script module source file.
3
+ */
4
+ export declare function resolveRadiantCustomElementTag(scriptPath: string): string | undefined;
5
+ /**
6
+ * Removes one custom-element definition from the active runtime registry.
7
+ *
8
+ * @remarks
9
+ * Radiant's `@customElement` decorator and the SSR light-DOM shim both skip
10
+ * re-registration when a tag is already defined. During dev, server requests
11
+ * reuse the same process-global registry, so script edits would otherwise keep
12
+ * serving stale SSR markup until a full dev-server restart.
13
+ */
14
+ export declare function clearRadiantCustomElementDefinition(tagName: string): void;
15
+ /**
16
+ * Clears the SSR custom-element registry entry for one registered Radiant script module.
17
+ */
18
+ export declare function invalidateRadiantRegisteredScriptSsrRegistration(scriptPath: string): boolean;
@@ -0,0 +1,47 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ const CUSTOM_ELEMENT_TAG_PATTERN = /@customElement\s*\(\s*['"`]([^'"`]+)['"`]/u;
3
+ /**
4
+ * Reads the custom-element tag name from a registered script module source file.
5
+ */
6
+ export function resolveRadiantCustomElementTag(scriptPath) {
7
+ if (!existsSync(scriptPath)) {
8
+ return undefined;
9
+ }
10
+ const content = readFileSync(scriptPath, 'utf8');
11
+ const match = content.match(CUSTOM_ELEMENT_TAG_PATTERN);
12
+ return match?.[1];
13
+ }
14
+ /**
15
+ * Removes one custom-element definition from the active runtime registry.
16
+ *
17
+ * @remarks
18
+ * Radiant's `@customElement` decorator and the SSR light-DOM shim both skip
19
+ * re-registration when a tag is already defined. During dev, server requests
20
+ * reuse the same process-global registry, so script edits would otherwise keep
21
+ * serving stale SSR markup until a full dev-server restart.
22
+ */
23
+ export function clearRadiantCustomElementDefinition(tagName) {
24
+ const registry = globalThis.customElements;
25
+ if (!registry) {
26
+ return;
27
+ }
28
+ if (typeof registry.delete === 'function') {
29
+ registry.delete(tagName);
30
+ return;
31
+ }
32
+ const definitions = registry.definitions;
33
+ if (definitions instanceof Map && definitions.has(tagName)) {
34
+ definitions.delete(tagName);
35
+ }
36
+ }
37
+ /**
38
+ * Clears the SSR custom-element registry entry for one registered Radiant script module.
39
+ */
40
+ export function invalidateRadiantRegisteredScriptSsrRegistration(scriptPath) {
41
+ const tagName = resolveRadiantCustomElementTag(scriptPath);
42
+ if (!tagName) {
43
+ return false;
44
+ }
45
+ clearRadiantCustomElementDefinition(tagName);
46
+ return true;
47
+ }
@@ -0,0 +1,2 @@
1
+ /** Absolute path to `@ecopages/radiant/client/install-hydrator` from this integration package. */
2
+ export declare const RADIANT_INSTALL_HYDRATOR_FILEPATH: string;
@@ -0,0 +1,3 @@
1
+ import { fileURLToPath } from 'node:url';
2
+ /** Absolute path to `@ecopages/radiant/client/install-hydrator` from this integration package. */
3
+ export const RADIANT_INSTALL_HYDRATOR_FILEPATH = fileURLToPath(import.meta.resolve('@ecopages/radiant/client/install-hydrator'));