@ecopages/core 0.2.0-rc.7 → 0.2.0-rc.9
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 +27 -3
- package/src/adapters/abstract/segment-path-matcher.js +56 -38
- package/src/adapters/bun/create-app.d.ts +4 -0
- package/src/adapters/bun/create-app.js +46 -31
- package/src/adapters/shared/runtime/server-adapter.d.ts +1 -0
- package/src/adapters/shared/runtime/server-adapter.js +9 -22
- package/src/build/rolldown/rolldown-adapter-helpers.js +41 -25
- package/src/build/rolldown/rolldown-plugin-bridge.js +25 -21
- package/src/eco/README.md +3 -1
- package/src/eco/lazy-injector-map.js +57 -41
- package/src/hmr/strategies/js-hmr-strategy.d.ts +2 -0
- package/src/hmr/strategies/js-hmr-strategy.js +24 -16
- package/src/plugins/component-import-discovery.js +115 -77
- package/src/plugins/eco-component-meta-plugin.js +47 -30
- package/src/route-renderer/README.md +1 -0
- package/src/route-renderer/orchestration/custom-element-scripts/README.md +9 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preload-invalidation.d.ts +16 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preload-invalidation.js +34 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.d.ts +80 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.js +190 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.d.ts +17 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.js +41 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint.d.ts +10 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-ssr-preload-entrypoint.js +46 -0
- package/src/route-renderer/orchestration/integration-renderer.d.ts +3 -0
- package/src/route-renderer/orchestration/integration-renderer.js +31 -17
- package/src/route-renderer/orchestration/ownership-graph/component-graph.js +55 -39
- package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph.service.d.ts +6 -0
- package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph.service.js +59 -36
- package/src/route-renderer/page-loading/component-dependency-collection.js +95 -74
- package/src/route-renderer/page-loading/ecopages-virtual-imports.js +41 -28
- package/src/route-renderer/page-loading/file-scoped-dependency-components.d.ts +0 -3
- package/src/route-renderer/page-loading/file-scoped-dependency-components.js +37 -34
- package/src/route-renderer/page-loading/page-dependency-bundling.js +14 -8
- package/src/router/README.md +2 -0
- package/src/services/assets/asset-processing-service/page-package.js +42 -39
- package/src/services/module-loading/page-module-import.service.d.ts +6 -0
- package/src/services/module-loading/page-module-import.service.js +60 -27
- package/src/services/validation/schema-validation-service.d.ts +1 -0
- package/src/services/validation/schema-validation-service.js +23 -36
- package/src/static-site-generator/static-site-generator.d.ts +4 -0
- package/src/static-site-generator/static-site-generator.js +75 -48
- package/src/utils/parse-cli-args.js +29 -22
- package/src/watchers/project-watcher.d.ts +5 -0
- package/src/watchers/project-watcher.js +46 -32
|
@@ -39,5 +39,7 @@ export declare class JsHmrStrategy extends HmrStrategy {
|
|
|
39
39
|
private context;
|
|
40
40
|
constructor(context: JsHmrContext);
|
|
41
41
|
matches(filePath: string): boolean;
|
|
42
|
+
private resolveImpactedEntrypoints;
|
|
43
|
+
private collectDevTransformHmrOutcome;
|
|
42
44
|
process(filePath: string): Promise<HmrAction>;
|
|
43
45
|
}
|
|
@@ -45,16 +45,7 @@ export class JsHmrStrategy extends HmrStrategy {
|
|
|
45
45
|
}
|
|
46
46
|
return true;
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
appLogger.debug(`[JsHmrStrategy] Processing ${filePath}`);
|
|
50
|
-
const watchedFiles = this.context.getWatchedFiles();
|
|
51
|
-
const resolvedChanged = path.resolve(filePath);
|
|
52
|
-
const registeredEntrypoints = this.context.getRegisteredEntrypoints();
|
|
53
|
-
const isRegisteredEntrypointEdit = isRegisteredDevTransformEntrypoint(registeredEntrypoints, resolvedChanged);
|
|
54
|
-
if (watchedFiles.size === 0) {
|
|
55
|
-
appLogger.debug(`[JsHmrStrategy] No watched files to rebuild`);
|
|
56
|
-
return { type: 'none' };
|
|
57
|
-
}
|
|
48
|
+
resolveImpactedEntrypoints(filePath, watchedFiles, resolvedChanged, isRegisteredEntrypointEdit) {
|
|
58
49
|
const dependencyHits = this.context.getEntrypointDependencyGraph().getDependencyEntrypoints(filePath);
|
|
59
50
|
const hasDependencyHit = dependencyHits.size > 0;
|
|
60
51
|
const impactedEntrypoints = isRegisteredEntrypointEdit
|
|
@@ -63,12 +54,9 @@ export class JsHmrStrategy extends HmrStrategy {
|
|
|
63
54
|
? Array.from(dependencyHits).filter((entrypoint) => watchedFiles.has(path.resolve(entrypoint)))
|
|
64
55
|
: Array.from(watchedFiles.keys());
|
|
65
56
|
const buildableEntrypoints = impactedEntrypoints.filter((entrypoint) => this.context.shouldProcessEntrypoint?.(entrypoint) ?? true);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (buildableEntrypoints.length === 0) {
|
|
70
|
-
return { type: 'none' };
|
|
71
|
-
}
|
|
57
|
+
return { buildableEntrypoints, hasDependencyHit };
|
|
58
|
+
}
|
|
59
|
+
collectDevTransformHmrOutcome(buildableEntrypoints, watchedFiles, registeredEntrypoints) {
|
|
72
60
|
const devTransformUpdates = [];
|
|
73
61
|
let devTransformReloadRequired = false;
|
|
74
62
|
for (const entrypoint of buildableEntrypoints) {
|
|
@@ -91,6 +79,26 @@ export class JsHmrStrategy extends HmrStrategy {
|
|
|
91
79
|
devTransformUpdates.push(outputUrl);
|
|
92
80
|
}
|
|
93
81
|
}
|
|
82
|
+
return { devTransformUpdates, devTransformReloadRequired };
|
|
83
|
+
}
|
|
84
|
+
async process(filePath) {
|
|
85
|
+
appLogger.debug(`[JsHmrStrategy] Processing ${filePath}`);
|
|
86
|
+
const watchedFiles = this.context.getWatchedFiles();
|
|
87
|
+
const resolvedChanged = path.resolve(filePath);
|
|
88
|
+
const registeredEntrypoints = this.context.getRegisteredEntrypoints();
|
|
89
|
+
const isRegisteredEntrypointEdit = isRegisteredDevTransformEntrypoint(registeredEntrypoints, resolvedChanged);
|
|
90
|
+
if (watchedFiles.size === 0) {
|
|
91
|
+
appLogger.debug(`[JsHmrStrategy] No watched files to rebuild`);
|
|
92
|
+
return { type: 'none' };
|
|
93
|
+
}
|
|
94
|
+
const { buildableEntrypoints, hasDependencyHit } = this.resolveImpactedEntrypoints(filePath, watchedFiles, resolvedChanged, isRegisteredEntrypointEdit);
|
|
95
|
+
if (!hasDependencyHit && !isRegisteredEntrypointEdit) {
|
|
96
|
+
appLogger.debug('[JsHmrStrategy] Dependency graph miss, rebuilding all watched entrypoints');
|
|
97
|
+
}
|
|
98
|
+
if (buildableEntrypoints.length === 0) {
|
|
99
|
+
return { type: 'none' };
|
|
100
|
+
}
|
|
101
|
+
const { devTransformUpdates, devTransformReloadRequired } = this.collectDevTransformHmrOutcome(buildableEntrypoints, watchedFiles, registeredEntrypoints);
|
|
94
102
|
if (devTransformReloadRequired) {
|
|
95
103
|
appLogger.debug(`[JsHmrStrategy] Full reload required (no HMR accept found)`);
|
|
96
104
|
return {
|
|
@@ -27,22 +27,9 @@ function isFactory(value) {
|
|
|
27
27
|
function isServerSpecifier(source) {
|
|
28
28
|
return /\.server(?:\.[cm]?[jt]sx?)?$/.test(source);
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
* Indexes local `eco.*()` factory exports and named `export { X } from` re-exports.
|
|
32
|
-
*
|
|
33
|
-
* @remarks
|
|
34
|
-
* `export *` is ignored so a barrel cannot pull an entire kit into the graph.
|
|
35
|
-
*/
|
|
36
|
-
function indexModuleExports(file, cache) {
|
|
37
|
-
const cached = cache.get(file);
|
|
38
|
-
if (cached)
|
|
39
|
-
return cached;
|
|
40
|
-
const source = readFileSync(file, 'utf8');
|
|
41
|
-
const parsed = cachedParseSync(file, source, { sourceType: 'module' });
|
|
42
|
-
const body = nodes(parsed.program.body);
|
|
30
|
+
function collectDeclaredFactoryBindings(body) {
|
|
43
31
|
const declared = new Set();
|
|
44
32
|
const factories = new Set();
|
|
45
|
-
const namedReexports = new Map();
|
|
46
33
|
for (const statement of body) {
|
|
47
34
|
const declaration = statement.type === 'ExportNamedDeclaration' ? node(statement.declaration) : statement;
|
|
48
35
|
for (const variable of nodes(declaration?.declarations)) {
|
|
@@ -56,32 +43,59 @@ function indexModuleExports(file, cache) {
|
|
|
56
43
|
if (statement.type === 'ExportDefaultDeclaration' && isFactory(statement.declaration))
|
|
57
44
|
factories.add('default');
|
|
58
45
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (statement.type !== 'ExportNamedDeclaration' || statement.exportKind === 'type')
|
|
65
|
-
continue;
|
|
66
|
-
const from = node(statement.source)?.value;
|
|
67
|
-
if (typeof from === 'string') {
|
|
68
|
-
for (const specifier of nodes(statement.specifiers)) {
|
|
69
|
-
if (specifier.exportKind === 'type')
|
|
70
|
-
continue;
|
|
71
|
-
const exported = identifierName(specifier.exported);
|
|
72
|
-
const imported = identifierName(specifier.local) ?? exported;
|
|
73
|
-
if (exported && imported)
|
|
74
|
-
namedReexports.set(exported, { source: from, imported });
|
|
75
|
-
}
|
|
46
|
+
return { declared, factories };
|
|
47
|
+
}
|
|
48
|
+
function indexReexportFromSource(from, specifiers, namedReexports) {
|
|
49
|
+
for (const specifier of specifiers) {
|
|
50
|
+
if (specifier.exportKind === 'type')
|
|
76
51
|
continue;
|
|
52
|
+
const exported = identifierName(specifier.exported);
|
|
53
|
+
const imported = identifierName(specifier.local) ?? exported;
|
|
54
|
+
if (exported && imported)
|
|
55
|
+
namedReexports.set(exported, { source: from, imported });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function indexLocalNamedExportSpecifiers(specifiers, declared, factories) {
|
|
59
|
+
for (const specifier of specifiers) {
|
|
60
|
+
if (specifier.exportKind !== 'type' && declared.has(String(identifierName(specifier.local)))) {
|
|
61
|
+
const exported = identifierName(specifier.exported);
|
|
62
|
+
if (exported)
|
|
63
|
+
factories.add(exported);
|
|
77
64
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function indexNamedExportStatement(statement, declared, factories, namedReexports) {
|
|
68
|
+
if (statement.type === 'ExportDefaultDeclaration' && declared.has(String(identifierName(statement.declaration)))) {
|
|
69
|
+
factories.add('default');
|
|
70
|
+
}
|
|
71
|
+
if (statement.type !== 'ExportNamedDeclaration' || statement.exportKind === 'type') {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const from = node(statement.source)?.value;
|
|
75
|
+
const specifiers = nodes(statement.specifiers);
|
|
76
|
+
if (typeof from === 'string') {
|
|
77
|
+
indexReexportFromSource(from, specifiers, namedReexports);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
indexLocalNamedExportSpecifiers(specifiers, declared, factories);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Indexes local `eco.*()` factory exports and named `export { X } from` re-exports.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* `export *` is ignored so a barrel cannot pull an entire kit into the graph.
|
|
87
|
+
*/
|
|
88
|
+
function indexModuleExports(file, cache) {
|
|
89
|
+
const cached = cache.get(file);
|
|
90
|
+
if (cached)
|
|
91
|
+
return cached;
|
|
92
|
+
const source = readFileSync(file, 'utf8');
|
|
93
|
+
const parsed = cachedParseSync(file, source, { sourceType: 'module' });
|
|
94
|
+
const body = nodes(parsed.program.body);
|
|
95
|
+
const { declared, factories } = collectDeclaredFactoryBindings(body);
|
|
96
|
+
const namedReexports = new Map();
|
|
97
|
+
for (const statement of body) {
|
|
98
|
+
indexNamedExportStatement(statement, declared, factories, namedReexports);
|
|
85
99
|
}
|
|
86
100
|
const indexed = { factories, namedReexports };
|
|
87
101
|
cache.set(file, indexed);
|
|
@@ -132,6 +146,66 @@ function isFactoryExport(file, exportName, projectRoot, pathPrefixes, state) {
|
|
|
132
146
|
function isCssImport(source) {
|
|
133
147
|
return source.endsWith('.css') && !source.endsWith('.module.css');
|
|
134
148
|
}
|
|
149
|
+
function resolveStylesheetImportPath(source, ownerFile, projectRoot, pathPrefixes) {
|
|
150
|
+
const isRelative = source.startsWith('.');
|
|
151
|
+
const isAlias = matchesTsconfigPathPrefix(source, pathPrefixes);
|
|
152
|
+
if (!isRelative && !isAlias) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
if (isRelative) {
|
|
156
|
+
const candidate = path.resolve(path.dirname(ownerFile), source);
|
|
157
|
+
return existsSync(candidate) ? realpathSync(candidate) : undefined;
|
|
158
|
+
}
|
|
159
|
+
return resolveProjectModulePath(projectRoot, ownerFile, source, { preserveBarrel: true });
|
|
160
|
+
}
|
|
161
|
+
function tryRecordSideEffectStylesheetImport(statement, source, ownerFile, projectRoot, pathPrefixes, result) {
|
|
162
|
+
const specifiers = nodes(statement.specifiers);
|
|
163
|
+
if (!isCssImport(source) || specifiers.length > 0) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
const resolved = resolveStylesheetImportPath(source, ownerFile, projectRoot, pathPrefixes);
|
|
167
|
+
if (!resolved) {
|
|
168
|
+
if (source.startsWith('.') || matchesTsconfigPathPrefix(source, pathPrefixes)) {
|
|
169
|
+
throw new Error(`[ecopages] Cannot resolve stylesheet import ${JSON.stringify(source)} from ${ownerFile}`);
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (!existsSync(resolved)) {
|
|
174
|
+
throw new Error(`[ecopages] Cannot resolve stylesheet import ${JSON.stringify(source)} from ${ownerFile}`);
|
|
175
|
+
}
|
|
176
|
+
result.stylesheets.push(resolved);
|
|
177
|
+
result.removals.push({ start: statement.start, end: statement.end, replacement: '' });
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
function collectFactoryComponentsFromImport(statement, source, ownerFile, projectRoot, pathPrefixes, factoryState, result) {
|
|
181
|
+
const specifiers = nodes(statement.specifiers);
|
|
182
|
+
const values = specifiers.filter((entry) => entry.importKind !== 'type' &&
|
|
183
|
+
(entry.type === 'ImportSpecifier' || entry.type === 'ImportDefaultSpecifier'));
|
|
184
|
+
if (!values.length || (!source.startsWith('.') && !matchesTsconfigPathPrefix(source, pathPrefixes))) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (isServerSpecifier(source)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const resolved = resolveProjectModulePath(projectRoot, ownerFile, source, { preserveBarrel: true });
|
|
191
|
+
if (!resolved)
|
|
192
|
+
throw new Error(`[ecopages] Cannot resolve import ${JSON.stringify(source)} from ${ownerFile}`);
|
|
193
|
+
if (!/\.[jt]sx?$/.test(resolved) || resolved.includes(`${path.sep}node_modules${path.sep}`)) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
for (const specifier of values) {
|
|
197
|
+
const imported = specifier.type === 'ImportDefaultSpecifier' ? 'default' : identifierName(specifier.imported);
|
|
198
|
+
const local = identifierName(specifier.local);
|
|
199
|
+
if (typeof local === 'string' &&
|
|
200
|
+
imported &&
|
|
201
|
+
isFactoryExport(resolved, imported, projectRoot, pathPrefixes, {
|
|
202
|
+
...factoryState,
|
|
203
|
+
path: new Set(),
|
|
204
|
+
})) {
|
|
205
|
+
result.components.push(local);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
135
209
|
/**
|
|
136
210
|
* Discovers local Eco Component imports and relative or aliased side-effect CSS.
|
|
137
211
|
*
|
|
@@ -147,6 +221,7 @@ export function discoverComponentImports(program, ownerFile, projectRoot) {
|
|
|
147
221
|
moduleIndex: new Map(),
|
|
148
222
|
resolved: new Map(),
|
|
149
223
|
watchFiles: new Set(),
|
|
224
|
+
path: new Set(),
|
|
150
225
|
};
|
|
151
226
|
for (const statement of nodes(node(program)?.body)) {
|
|
152
227
|
if (statement.type !== 'ImportDeclaration' || statement.importKind === 'type')
|
|
@@ -156,47 +231,10 @@ export function discoverComponentImports(program, ownerFile, projectRoot) {
|
|
|
156
231
|
const source = node(statement.source)?.value;
|
|
157
232
|
if (typeof source !== 'string')
|
|
158
233
|
continue;
|
|
159
|
-
|
|
160
|
-
if (isCssImport(source) && !specifiers.length) {
|
|
161
|
-
const isRelative = source.startsWith('.');
|
|
162
|
-
const isAlias = matchesTsconfigPathPrefix(source, pathPrefixes);
|
|
163
|
-
if (isRelative || isAlias) {
|
|
164
|
-
const resolved = isRelative
|
|
165
|
-
? existsSync(path.resolve(path.dirname(ownerFile), source))
|
|
166
|
-
? realpathSync(path.resolve(path.dirname(ownerFile), source))
|
|
167
|
-
: undefined
|
|
168
|
-
: resolveProjectModulePath(projectRoot, ownerFile, source, { preserveBarrel: true });
|
|
169
|
-
if (!resolved || !existsSync(resolved)) {
|
|
170
|
-
throw new Error(`[ecopages] Cannot resolve stylesheet import ${JSON.stringify(source)} from ${ownerFile}`);
|
|
171
|
-
}
|
|
172
|
-
result.stylesheets.push(resolved);
|
|
173
|
-
result.removals.push({ start: statement.start, end: statement.end, replacement: '' });
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
const values = specifiers.filter((entry) => entry.importKind !== 'type' &&
|
|
178
|
-
(entry.type === 'ImportSpecifier' || entry.type === 'ImportDefaultSpecifier'));
|
|
179
|
-
if (!values.length || (!source.startsWith('.') && !matchesTsconfigPathPrefix(source, pathPrefixes)))
|
|
180
|
-
continue;
|
|
181
|
-
if (isServerSpecifier(source))
|
|
234
|
+
if (tryRecordSideEffectStylesheetImport(statement, source, ownerFile, projectRoot, pathPrefixes, result)) {
|
|
182
235
|
continue;
|
|
183
|
-
const resolved = resolveProjectModulePath(projectRoot, ownerFile, source, { preserveBarrel: true });
|
|
184
|
-
if (!resolved)
|
|
185
|
-
throw new Error(`[ecopages] Cannot resolve import ${JSON.stringify(source)} from ${ownerFile}`);
|
|
186
|
-
if (!/\.[jt]sx?$/.test(resolved) || resolved.includes(`${path.sep}node_modules${path.sep}`))
|
|
187
|
-
continue;
|
|
188
|
-
for (const specifier of values) {
|
|
189
|
-
const imported = specifier.type === 'ImportDefaultSpecifier' ? 'default' : identifierName(specifier.imported);
|
|
190
|
-
const local = identifierName(specifier.local);
|
|
191
|
-
if (typeof local === 'string' &&
|
|
192
|
-
imported &&
|
|
193
|
-
isFactoryExport(resolved, imported, projectRoot, pathPrefixes, {
|
|
194
|
-
...factoryState,
|
|
195
|
-
path: new Set(),
|
|
196
|
-
})) {
|
|
197
|
-
result.components.push(local);
|
|
198
|
-
}
|
|
199
236
|
}
|
|
237
|
+
collectFactoryComponentsFromImport(statement, source, ownerFile, projectRoot, pathPrefixes, factoryState, result);
|
|
200
238
|
}
|
|
201
239
|
result.watchFiles.push(...factoryState.watchFiles);
|
|
202
240
|
return result;
|
|
@@ -135,6 +135,44 @@ export function attributeComponentIdentity(contents, filePath, integration, proj
|
|
|
135
135
|
}
|
|
136
136
|
return addIdentityBindingImport(transformed, cachedParseSync(filePath, transformed, { sourceType: 'module' }).program);
|
|
137
137
|
}
|
|
138
|
+
function findMdxConfigDeclarator(program) {
|
|
139
|
+
for (const statement of nodes(program?.body)) {
|
|
140
|
+
if (statement.type !== 'ExportNamedDeclaration' || !statement.declaration) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const decl = node(statement.declaration);
|
|
144
|
+
if (decl?.type !== 'VariableDeclaration') {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
for (const declarator of nodes(decl.declarations)) {
|
|
148
|
+
if (node(declarator.id)?.name === 'config') {
|
|
149
|
+
return declarator;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
function buildMdxConfigIdentityEdits(options) {
|
|
156
|
+
const edits = [...options.removals];
|
|
157
|
+
const init = options.configDeclarator ? node(options.configDeclarator.init) : undefined;
|
|
158
|
+
if (!init || typeof init.start !== 'number' || typeof init.end !== 'number') {
|
|
159
|
+
return edits;
|
|
160
|
+
}
|
|
161
|
+
edits.push({
|
|
162
|
+
start: init.start,
|
|
163
|
+
end: init.end,
|
|
164
|
+
replacement: `bindComponentIdentity(${options.identityLiteral}, ${options.contents.slice(init.start, init.end)}${options.discoveryArgument})`,
|
|
165
|
+
});
|
|
166
|
+
return edits;
|
|
167
|
+
}
|
|
168
|
+
function buildMdxIdentityAppend(options) {
|
|
169
|
+
let appended = '';
|
|
170
|
+
if (!options.configDeclarator) {
|
|
171
|
+
appended += `\nexport const config = bindComponentIdentity(${options.identityLiteral}, {}${options.discoveryArgument});\n`;
|
|
172
|
+
}
|
|
173
|
+
appended += `attachDiscoveredDependencies(config);\nif (typeof MDXContent === 'function') MDXContent.config = config;\n`;
|
|
174
|
+
return appended;
|
|
175
|
+
}
|
|
138
176
|
/** Attributes compiled MDX module with canonical component identity and discovered dependencies. */
|
|
139
177
|
export function attributeMdxComponentIdentity(contents, filePath, integration, projectRoot) {
|
|
140
178
|
if (!projectRoot) {
|
|
@@ -148,42 +186,21 @@ export function attributeMdxComponentIdentity(contents, filePath, integration, p
|
|
|
148
186
|
catch {
|
|
149
187
|
return contents;
|
|
150
188
|
}
|
|
151
|
-
|
|
152
|
-
for (const statement of nodes(program?.body)) {
|
|
153
|
-
if (statement.type === 'ExportNamedDeclaration' && statement.declaration) {
|
|
154
|
-
const decl = node(statement.declaration);
|
|
155
|
-
if (decl?.type === 'VariableDeclaration') {
|
|
156
|
-
for (const declarator of nodes(decl.declarations)) {
|
|
157
|
-
if (node(declarator.id)?.name === 'config') {
|
|
158
|
-
configDeclarator = declarator;
|
|
159
|
-
break;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
189
|
+
const configDeclarator = findMdxConfigDeclarator(program);
|
|
165
190
|
if (configDeclarator && isIdentityBinding(configDeclarator.init)) {
|
|
166
191
|
return contents;
|
|
167
192
|
}
|
|
168
193
|
const discovered = discoverComponentImports(program, filePath, projectRoot);
|
|
169
194
|
const identityLiteral = `{ id: ${JSON.stringify(rapidhash(filePath).toString(36))}, file: ${JSON.stringify(filePath)}, integration: ${JSON.stringify(integration)} }`;
|
|
170
195
|
const discoveryArgument = serializeDiscoveryArgument(discovered);
|
|
171
|
-
const edits =
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
let appended = '';
|
|
183
|
-
if (!configDeclarator) {
|
|
184
|
-
appended += `\nexport const config = bindComponentIdentity(${identityLiteral}, {}${discoveryArgument});\n`;
|
|
185
|
-
}
|
|
186
|
-
appended += `attachDiscoveredDependencies(config);\nif (typeof MDXContent === 'function') MDXContent.config = config;\n`;
|
|
196
|
+
const edits = buildMdxConfigIdentityEdits({
|
|
197
|
+
contents,
|
|
198
|
+
configDeclarator,
|
|
199
|
+
identityLiteral,
|
|
200
|
+
discoveryArgument,
|
|
201
|
+
removals: discovered.removals,
|
|
202
|
+
});
|
|
203
|
+
const appended = buildMdxIdentityAppend({ configDeclarator, identityLiteral, discoveryArgument });
|
|
187
204
|
let transformed = contents;
|
|
188
205
|
for (const edit of edits.sort((left, right) => right.start - left.start)) {
|
|
189
206
|
transformed = `${transformed.slice(0, edit.start)}${edit.replacement}${transformed.slice(edit.end)}`;
|
|
@@ -66,6 +66,7 @@ Domain folders:
|
|
|
66
66
|
- `document-shell/`: shared page/layout/html shell composition, layout prop resolution, and attribute stamping helpers
|
|
67
67
|
- `route-pipeline/`: route preparation, final HTML capture, marker-artifact enforcement
|
|
68
68
|
- `page-browser-graph/`: browser asset graph build, session cache, and injector assets
|
|
69
|
+
- `custom-element-scripts/`: shared `ssr: true` script collection and server preload for custom-element registration
|
|
69
70
|
|
|
70
71
|
**Page Browser Graph session behavior:**
|
|
71
72
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Custom element SSR script preload
|
|
2
|
+
|
|
3
|
+
Shared orchestration for `dependencies.scripts` entries marked `ssr: true`.
|
|
4
|
+
|
|
5
|
+
Lit and Ecopages JSX construct a {@link CustomElementScriptPreloader} with a stable `cacheScope` so imports are deduplicated across concurrent renders and dev invalidation can clear preload state without per-renderer registries.
|
|
6
|
+
|
|
7
|
+
Server evaluation uses `appConfig.runtime.appModuleLoader` via {@link createCustomElementServerModuleImporter} when no {@link createCustomElementSsrPreloadEntrypointResolver} is configured. Lit passes a resolver so Node preloads through the asset pipeline and Bun can import source. The resolver skips {@link importServerModule}; Lit's static-render worker therefore omits it and uses the app module loader so `@lit-labs/ssr` sees registered custom elements. Ecopages JSX does not pass a resolver. On Bun, `preferSourceImports` imports the script source directly. On Node it imports through the page server-module loader with external packages, because the dev asset pipeline still points at `.tsx` source and a browser bundle would register a second Radiant graph.
|
|
8
|
+
|
|
9
|
+
Lit only preloads lazy `ssr: true` script entries (`requireLazyScriptEntry`) so eager registrations are not evaluated in an isolated graph that would break `@lit-labs/ssr`. Ecopages JSX sets `collectForIntegration` so foreign integration scripts in shared dependency trees are not preloaded here; Lit and other hosts keep owning their registrations.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type ScopePreloadState = {
|
|
2
|
+
activeRegistry?: CustomElementRegistry;
|
|
3
|
+
preloadedScripts: Set<string>;
|
|
4
|
+
preloadFailedScripts: Set<string>;
|
|
5
|
+
inFlightImports: Map<string, Promise<void>>;
|
|
6
|
+
};
|
|
7
|
+
export declare function getCustomElementScriptPreloadScopeState(cacheScope: string): ScopePreloadState;
|
|
8
|
+
/**
|
|
9
|
+
* Clears all preload state for one integration scope.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Intended for tests; production invalidation should use {@link invalidateCustomElementScriptPreload}.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resetCustomElementScriptPreloadScope(cacheScope: string): void;
|
|
15
|
+
export declare function invalidateCustomElementScriptPreload(scriptPath: string, cacheScope?: string): void;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const scopeStates = new Map();
|
|
2
|
+
export function getCustomElementScriptPreloadScopeState(cacheScope) {
|
|
3
|
+
let state = scopeStates.get(cacheScope);
|
|
4
|
+
if (!state) {
|
|
5
|
+
state = {
|
|
6
|
+
preloadedScripts: new Set(),
|
|
7
|
+
preloadFailedScripts: new Set(),
|
|
8
|
+
inFlightImports: new Map(),
|
|
9
|
+
};
|
|
10
|
+
scopeStates.set(cacheScope, state);
|
|
11
|
+
}
|
|
12
|
+
return state;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Clears all preload state for one integration scope.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Intended for tests; production invalidation should use {@link invalidateCustomElementScriptPreload}.
|
|
19
|
+
*/
|
|
20
|
+
export function resetCustomElementScriptPreloadScope(cacheScope) {
|
|
21
|
+
scopeStates.delete(cacheScope);
|
|
22
|
+
}
|
|
23
|
+
export function invalidateCustomElementScriptPreload(scriptPath, cacheScope) {
|
|
24
|
+
const scopes = cacheScope ? [cacheScope] : [...scopeStates.keys()];
|
|
25
|
+
for (const scope of scopes) {
|
|
26
|
+
const state = scopeStates.get(scope);
|
|
27
|
+
if (!state) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
state.preloadedScripts.delete(scriptPath);
|
|
31
|
+
state.preloadFailedScripts.delete(scriptPath);
|
|
32
|
+
state.inFlightImports.delete(scriptPath);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { EcoComponent } from '../../../types/public-types.js';
|
|
2
|
+
import { invalidateCustomElementScriptPreload } from './custom-element-script-preload-invalidation.js';
|
|
3
|
+
export { invalidateCustomElementScriptPreload };
|
|
4
|
+
export type CustomElementSsrPreloadComponent = EcoComponent | Partial<EcoComponent>;
|
|
5
|
+
export declare const CUSTOM_ELEMENT_SSR_PRELOAD_CACHE_SCOPES: {
|
|
6
|
+
readonly lit: "lit";
|
|
7
|
+
readonly ecopagesJsx: "ecopages-jsx";
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Detects preload failures that are expected for browser-only modules.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isExpectedCustomElementSsrPreloadError(error: unknown): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Collects `dependencies.scripts` paths marked `ssr: true` from a component tree.
|
|
15
|
+
*/
|
|
16
|
+
export declare function collectCustomElementSsrPreloadScripts(components: Array<CustomElementSsrPreloadComponent | undefined>, resolveDependencyPath: (componentDir: string, sourcePath: string) => string, requireLazyScriptEntry?: boolean, collectForIntegration?: string): string[];
|
|
17
|
+
export interface CustomElementScriptPreloaderOptions {
|
|
18
|
+
cacheScope: string;
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* When true, only lazy script entries with `ssr: true` are preloaded.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Lit keeps this enabled so eager `ssr: true` scripts are not evaluated in an
|
|
25
|
+
* isolated module graph that would register incompatible custom elements.
|
|
26
|
+
*/
|
|
27
|
+
requireLazyScriptEntry?: boolean;
|
|
28
|
+
resolveDependencyPath: (componentDir: string, sourcePath: string) => string;
|
|
29
|
+
preferSourceImports?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Resolves a script path to the module entry the SSR renderer should import.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* When this is set, {@link importServerModule} is skipped. Callers that need
|
|
35
|
+
* the app module loader (Lit's static-render worker) must omit the resolver.
|
|
36
|
+
*/
|
|
37
|
+
resolvePreloadEntrypoint?: (scriptPath: string) => Promise<string>;
|
|
38
|
+
importServerModule?: (scriptPath: string, registryKey: string) => Promise<unknown>;
|
|
39
|
+
/**
|
|
40
|
+
* Limits script collection to components owned by one integration.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Dependency trees may include foreign integrations whose `ssr: true` scripts
|
|
44
|
+
* are preloaded by their own renderer. Ownership uses
|
|
45
|
+
* `config.integration ?? identity.integration`, matching render routing.
|
|
46
|
+
* Ecopages JSX still walks nested components so it can find owned Radiant
|
|
47
|
+
* hosts deeper in the graph.
|
|
48
|
+
*/
|
|
49
|
+
collectForIntegration?: string;
|
|
50
|
+
logLabel?: string;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Preloads `dependencies.scripts` entries marked `ssr: true` before SSR.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* `lazy` controls browser delivery only; `ssr: true` opts into server evaluation
|
|
57
|
+
* for both eager and lazy script entries. State is keyed by `cacheScope` so
|
|
58
|
+
* concurrent renders and dev invalidation share one deduplicated import cache.
|
|
59
|
+
*/
|
|
60
|
+
export declare class CustomElementScriptPreloader {
|
|
61
|
+
private readonly cacheScope;
|
|
62
|
+
private readonly enabled;
|
|
63
|
+
private readonly requireLazyScriptEntry;
|
|
64
|
+
private readonly resolveDependencyPath;
|
|
65
|
+
private readonly preferSourceImports;
|
|
66
|
+
private readonly resolvePreloadEntrypoint?;
|
|
67
|
+
private readonly importServerModule?;
|
|
68
|
+
private readonly collectForIntegration?;
|
|
69
|
+
private readonly logLabel;
|
|
70
|
+
constructor({ cacheScope, enabled, requireLazyScriptEntry, resolveDependencyPath, preferSourceImports, resolvePreloadEntrypoint, importServerModule, collectForIntegration, logLabel, }: CustomElementScriptPreloaderOptions);
|
|
71
|
+
invalidateScript(scriptPath: string): void;
|
|
72
|
+
isExpectedSsrPreloadError(error: unknown): boolean;
|
|
73
|
+
collectSsrPreloadScripts(components: Array<CustomElementSsrPreloadComponent | undefined>): string[];
|
|
74
|
+
/**
|
|
75
|
+
* Preloads SSR-eligible scripts to register custom elements before render.
|
|
76
|
+
*/
|
|
77
|
+
preloadSsrScripts(components: Array<CustomElementSsrPreloadComponent | undefined>): Promise<void>;
|
|
78
|
+
private preloadOneScript;
|
|
79
|
+
private importScriptForPreload;
|
|
80
|
+
}
|