@stacksjs/stx 0.2.136 → 0.2.138
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/dist/cli.js +860 -853
- package/dist/component-client-factories.d.ts +9 -0
- package/dist/component-client-factories.js +47 -0
- package/dist/craft.js +321 -314
- package/dist/process.js +5 -0
- package/dist/pwa.js +284 -277
- package/dist/ssg.js +263 -256
- package/dist/utils.js +10 -6
- package/package.json +5 -5
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function initializeComponentClientFactories(context: Record<string, unknown>): void;
|
|
2
|
+
export declare function registerComponentClientFactory(context: Record<string, unknown>, factoryBody: string): string | null;
|
|
3
|
+
export declare function injectComponentClientFactories(html: string, context: Record<string, unknown>): string;
|
|
4
|
+
export declare const COMPONENT_CLIENT_FACTORIES_CONTEXT_KEY: '__stx_component_client_factories';
|
|
5
|
+
declare interface ComponentClientFactory {
|
|
6
|
+
body: string
|
|
7
|
+
instances: number
|
|
8
|
+
}
|
|
9
|
+
declare type ComponentClientFactoryRegistry = Map<string, ComponentClientFactory>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
export const COMPONENT_CLIENT_FACTORIES_CONTEXT_KEY = "__stx_component_client_factories";
|
|
3
|
+
function registryFrom(context) {
|
|
4
|
+
const registry = context[COMPONENT_CLIENT_FACTORIES_CONTEXT_KEY];
|
|
5
|
+
return registry instanceof Map ? registry : null;
|
|
6
|
+
}
|
|
7
|
+
export function initializeComponentClientFactories(context) {
|
|
8
|
+
context[COMPONENT_CLIENT_FACTORIES_CONTEXT_KEY] = new Map;
|
|
9
|
+
}
|
|
10
|
+
export function registerComponentClientFactory(context, factoryBody) {
|
|
11
|
+
const registry = registryFrom(context);
|
|
12
|
+
if (!registry)
|
|
13
|
+
return null;
|
|
14
|
+
const id = createHash("sha256").update(factoryBody).digest("hex").slice(0, 16), existing = registry.get(id);
|
|
15
|
+
if (existing)
|
|
16
|
+
existing.instances++;
|
|
17
|
+
else
|
|
18
|
+
registry.set(id, { body: factoryBody, instances: 1 });
|
|
19
|
+
return id;
|
|
20
|
+
}
|
|
21
|
+
export function injectComponentClientFactories(html, context) {
|
|
22
|
+
const registry = registryFrom(context);
|
|
23
|
+
if (!registry?.size)
|
|
24
|
+
return html;
|
|
25
|
+
let output = html;
|
|
26
|
+
for (const [id, factory] of registry) {
|
|
27
|
+
if (factory.instances !== 1)
|
|
28
|
+
continue;
|
|
29
|
+
const invocation = new RegExp(`window\\.__stxComponentFactories\\[${JSON.stringify(id)}\\]\\(("[^"]+")\\);`, "g");
|
|
30
|
+
output = output.replace(invocation, (_match, scopeId) => `(${factory.body})(${scopeId});`);
|
|
31
|
+
}
|
|
32
|
+
const repeatedFactories = [...registry].filter(([, factory]) => factory.instances > 1);
|
|
33
|
+
if (repeatedFactories.length === 0)
|
|
34
|
+
return output;
|
|
35
|
+
const prelude = `<script data-stx-scoped data-stx-component-factories>
|
|
36
|
+
(function() {
|
|
37
|
+
const factories = window.__stxComponentFactories = window.__stxComponentFactories || {};
|
|
38
|
+
${repeatedFactories.map(([id, factory]) => ` factories[${JSON.stringify(id)}] = factories[${JSON.stringify(id)}] || ${factory.body};`).join(`
|
|
39
|
+
`)}
|
|
40
|
+
})();
|
|
41
|
+
</script>`, firstScopedScript = output.search(/<script\b(?=[^>]*\bdata-stx-scoped\b)/i);
|
|
42
|
+
if (firstScopedScript < 0)
|
|
43
|
+
return `${prelude}
|
|
44
|
+
${output}`;
|
|
45
|
+
return `${output.slice(0, firstScopedScript)}${prelude}
|
|
46
|
+
${output.slice(firstScopedScript)}`;
|
|
47
|
+
}
|