crelte 0.5.15 → 0.6.0-alpha.2
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/blocks/Blocks.svelte +5 -4
- package/dist/blocks/Blocks.svelte.d.ts +4 -19
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
- package/dist/crelte.d.ts +2 -2
- package/dist/crelte.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/init/InternalApp.d.ts.map +1 -1
- package/dist/init/InternalApp.js +2 -2
- package/dist/init/server.js +4 -4
- package/dist/loadData/loadData.d.ts +1 -1
- package/dist/loadData/loadData.d.ts.map +1 -1
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +15 -11
- package/dist/queries/Queries.d.ts +16 -27
- package/dist/queries/Queries.d.ts.map +1 -1
- package/dist/queries/Queries.js +13 -20
- package/dist/queries/gql.d.ts +2 -2
- package/dist/queries/gql.d.ts.map +1 -1
- package/dist/queries/gql.js +3 -2
- package/dist/queries/index.d.ts +13 -13
- package/dist/queries/index.d.ts.map +1 -1
- package/dist/queries/index.js +2 -2
- package/dist/queries/vars.d.ts +3 -3
- package/dist/queries/vars.d.ts.map +1 -1
- package/dist/queries/vars.js +3 -3
- package/dist/routing/route/BaseRoute.d.ts +4 -0
- package/dist/routing/route/BaseRoute.d.ts.map +1 -1
- package/dist/routing/route/BaseRoute.js +6 -0
- package/dist/routing/router/BaseRouter.d.ts.map +1 -1
- package/dist/routing/router/BaseRouter.js +4 -3
- package/dist/routing/router/ClientRouter.d.ts +1 -0
- package/dist/routing/router/ClientRouter.d.ts.map +1 -1
- package/dist/routing/router/ClientRouter.js +22 -7
- package/dist/server/CrelteServer.d.ts +1 -1
- package/dist/server/CrelteServer.d.ts.map +1 -1
- package/dist/server/ServerRouter.d.ts +1 -1
- package/dist/server/ServerRouter.d.ts.map +1 -1
- package/dist/server/queries/QueriesCaching.d.ts +0 -1
- package/dist/server/queries/QueriesCaching.d.ts.map +1 -1
- package/dist/server/queries/QueriesCaching.js +0 -2
- package/dist/server/queries/QueryGqlRoute.d.ts +2 -0
- package/dist/server/queries/QueryGqlRoute.d.ts.map +1 -1
- package/dist/server/queries/QueryGqlRoute.js +22 -14
- package/dist/server/queries/QueryHandleRoute.d.ts +1 -2
- package/dist/server/queries/QueryHandleRoute.d.ts.map +1 -1
- package/dist/server/queries/QueryHandleRoute.js +2 -2
- package/dist/server/queries/queries.d.ts.map +1 -1
- package/dist/server/queries/queries.js +2 -1
- package/dist/server/queries/routes.d.ts +3 -4
- package/dist/server/queries/routes.d.ts.map +1 -1
- package/dist/server/queries/routes.js +3 -3
- package/dist/server/shared.d.ts +5 -4
- package/dist/server/shared.d.ts.map +1 -1
- package/dist/server/shared.js +26 -16
- package/dist/ssr/SsrCache.d.ts +2 -2
- package/dist/ssr/SsrCache.d.ts.map +1 -1
- package/dist/ssr/SsrCache.js +10 -16
- package/dist/std/encdec/escaping.d.ts +2 -0
- package/dist/std/encdec/escaping.d.ts.map +1 -0
- package/dist/std/encdec/escaping.js +41 -0
- package/dist/std/encdec/index.d.ts +13 -0
- package/dist/std/encdec/index.d.ts.map +1 -0
- package/dist/std/encdec/index.js +15 -0
- package/dist/std/index.d.ts.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +17 -0
- package/dist/vite/index.d.ts +1 -5
- package/dist/vite/index.d.ts.map +1 -1
- package/dist/vite/index.js +39 -79
- package/package.json +12 -7
- package/src/blocks/Blocks.svelte +4 -3
- package/src/crelte.ts +2 -2
- package/src/index.ts +2 -2
- package/src/init/InternalApp.ts +3 -4
- package/src/init/server.ts +4 -4
- package/src/loadData/loadData.ts +0 -2
- package/src/node/index.ts +24 -9
- package/src/queries/Queries.ts +31 -36
- package/src/queries/gql.ts +7 -3
- package/src/queries/index.ts +19 -16
- package/src/queries/vars.ts +5 -7
- package/src/routing/route/BaseRoute.ts +8 -7
- package/src/routing/router/BaseRouter.ts +6 -3
- package/src/routing/router/ClientRouter.ts +29 -8
- package/src/server/CrelteServer.ts +1 -1
- package/src/server/ServerRouter.ts +1 -1
- package/src/server/queries/QueriesCaching.ts +0 -2
- package/src/server/queries/QueryGqlRoute.ts +24 -20
- package/src/server/queries/QueryHandleRoute.ts +2 -6
- package/src/server/queries/queries.ts +2 -3
- package/src/server/queries/routes.ts +9 -6
- package/src/server/shared.ts +34 -16
- package/src/ssr/SsrCache.ts +13 -23
- package/src/std/encdec/escaping.ts +47 -0
- package/src/std/encdec/index.ts +16 -0
- package/src/std/index.ts +1 -6
- package/src/utils.ts +21 -0
- package/src/vite/index.ts +51 -87
- package/types.d.ts +1 -1
package/src/server/shared.ts
CHANGED
|
@@ -27,7 +27,7 @@ export type EnvData = {
|
|
|
27
27
|
env: Map<string, string>;
|
|
28
28
|
endpointUrl: string;
|
|
29
29
|
endpointToken?: string;
|
|
30
|
-
|
|
30
|
+
craftBaseCpUrl: string;
|
|
31
31
|
frontendUrl: string;
|
|
32
32
|
viteEnv: Map<string, string>;
|
|
33
33
|
};
|
|
@@ -47,8 +47,8 @@ export async function initEnvData(os: Platform): Promise<EnvData> {
|
|
|
47
47
|
|
|
48
48
|
const endpointToken = env.get('ENDPOINT_TOKEN');
|
|
49
49
|
|
|
50
|
-
const
|
|
51
|
-
if (!
|
|
50
|
+
const craftBaseCpUrl = env.get('CRAFT_BASE_CP_URL');
|
|
51
|
+
if (!craftBaseCpUrl) throw new Error('CRAFT_BASE_CP_URL not set');
|
|
52
52
|
|
|
53
53
|
const frontendUrl = env.get('FRONTEND_URL');
|
|
54
54
|
if (!frontendUrl) throw new Error('FRONTEND_URL not set');
|
|
@@ -62,7 +62,7 @@ export async function initEnvData(os: Platform): Promise<EnvData> {
|
|
|
62
62
|
env,
|
|
63
63
|
endpointUrl,
|
|
64
64
|
endpointToken,
|
|
65
|
-
|
|
65
|
+
craftBaseCpUrl,
|
|
66
66
|
frontendUrl,
|
|
67
67
|
viteEnv,
|
|
68
68
|
};
|
|
@@ -77,6 +77,7 @@ export type SitesCache = {
|
|
|
77
77
|
export async function initSites(
|
|
78
78
|
env: EnvData,
|
|
79
79
|
cache?: SitesCache,
|
|
80
|
+
siteToken?: string,
|
|
80
81
|
): Promise<SiteFromGraphQl[]> {
|
|
81
82
|
const queries = Queries.new(
|
|
82
83
|
env.endpointUrl,
|
|
@@ -84,19 +85,34 @@ export async function initSites(
|
|
|
84
85
|
new SsrCache(),
|
|
85
86
|
{ bearerToken: env.endpointToken },
|
|
86
87
|
);
|
|
87
|
-
return await loadSites(queries, cache);
|
|
88
|
+
return await loadSites(queries, cache, siteToken);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function initRequestSites(
|
|
92
|
+
env: EnvData,
|
|
93
|
+
sites: SiteFromGraphQl[],
|
|
94
|
+
req: Request,
|
|
95
|
+
): Promise<SiteFromGraphQl[]> {
|
|
96
|
+
const siteToken = new URL(req.url).searchParams.get('siteToken');
|
|
97
|
+
if (!siteToken) return sites;
|
|
98
|
+
|
|
99
|
+
// Preview sites belong to this request, not the shared site cache.
|
|
100
|
+
return await initSites(env, undefined, siteToken);
|
|
88
101
|
}
|
|
89
102
|
|
|
90
103
|
// requires, GraphQl, SsrCache
|
|
91
104
|
async function loadSites(
|
|
92
105
|
queries: Queries,
|
|
93
106
|
cache?: SitesCache,
|
|
107
|
+
siteToken?: string,
|
|
94
108
|
): Promise<SiteFromGraphQl[]> {
|
|
95
|
-
|
|
109
|
+
const useCache = !siteToken;
|
|
110
|
+
|
|
111
|
+
if (useCache && 'CRAFT_SITES_CACHED' in globalThis) {
|
|
96
112
|
return (globalThis as any)['CRAFT_SITES_CACHED'];
|
|
97
113
|
}
|
|
98
114
|
|
|
99
|
-
if (cache?.enabled && cache?.readSitesCache) {
|
|
115
|
+
if (useCache && cache?.enabled && cache?.readSitesCache) {
|
|
100
116
|
try {
|
|
101
117
|
const sites = (await cache.readSitesCache()) as SiteFromGraphQl[];
|
|
102
118
|
// @ts-ignore
|
|
@@ -122,13 +138,15 @@ async function loadSites(
|
|
|
122
138
|
`,
|
|
123
139
|
{},
|
|
124
140
|
// don't cache since we cache ourself
|
|
125
|
-
{ caching: false },
|
|
141
|
+
{ caching: false, craftCaching: useCache, siteToken },
|
|
126
142
|
)) as { crelteSites: SiteFromGraphQl[] };
|
|
127
143
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
144
|
+
if (useCache) {
|
|
145
|
+
// @ts-ignore
|
|
146
|
+
globalThis['CRAFT_SITES_CACHED'] = resp.crelteSites;
|
|
147
|
+
if (cache?.enabled && cache?.writeSitesCache) {
|
|
148
|
+
await cache.writeSitesCache(resp.crelteSites);
|
|
149
|
+
}
|
|
132
150
|
}
|
|
133
151
|
return resp.crelteSites;
|
|
134
152
|
}
|
|
@@ -145,7 +163,7 @@ export type RenderRequest = {
|
|
|
145
163
|
htmlTemplate: string;
|
|
146
164
|
ssrManifest: Record<string, string[]>;
|
|
147
165
|
endpoint: string;
|
|
148
|
-
|
|
166
|
+
craftUrl: string;
|
|
149
167
|
frontend: string;
|
|
150
168
|
viteEnv: Map<string, string>;
|
|
151
169
|
sites: SiteFromGraphQl[];
|
|
@@ -170,7 +188,7 @@ export async function modRender(
|
|
|
170
188
|
htmlTemplate: template,
|
|
171
189
|
ssrManifest: {},
|
|
172
190
|
endpoint: env.endpointUrl,
|
|
173
|
-
|
|
191
|
+
craftUrl: env.craftBaseCpUrl,
|
|
174
192
|
frontend: env.frontendUrl,
|
|
175
193
|
viteEnv: env.viteEnv,
|
|
176
194
|
sites,
|
|
@@ -199,7 +217,7 @@ export type RenderErrorRequest = {
|
|
|
199
217
|
ssrManifest: Record<string, string[]>;
|
|
200
218
|
acceptLang: string | null;
|
|
201
219
|
endpoint: string;
|
|
202
|
-
|
|
220
|
+
craftUrl: string;
|
|
203
221
|
frontend: string;
|
|
204
222
|
viteEnv: Map<string, string>;
|
|
205
223
|
};
|
|
@@ -239,7 +257,7 @@ export async function modRenderError(
|
|
|
239
257
|
ssrManifest: {},
|
|
240
258
|
acceptLang,
|
|
241
259
|
endpoint: env.endpointUrl,
|
|
242
|
-
|
|
260
|
+
craftUrl: env.craftBaseCpUrl,
|
|
243
261
|
frontend: env.frontendUrl,
|
|
244
262
|
viteEnv: env.viteEnv,
|
|
245
263
|
...opts,
|
package/src/ssr/SsrCache.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { uneval } from 'devalue';
|
|
2
|
+
import { cryptoSha1Hash } from '../utils.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Calculates a key based on the json representation of the data
|
|
3
6
|
*
|
|
@@ -9,20 +12,7 @@ export async function calcKey(data: any): Promise<string> {
|
|
|
9
12
|
// specifically in the craft preview locally
|
|
10
13
|
if (!crypto?.subtle) return json;
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
const encoder = new TextEncoder();
|
|
14
|
-
const dataBuffer = encoder.encode(json);
|
|
15
|
-
|
|
16
|
-
// Use the Web Crypto API to hash the data with SHA-1
|
|
17
|
-
const hashBuffer = await crypto.subtle.digest('SHA-1', dataBuffer);
|
|
18
|
-
|
|
19
|
-
// Convert the ArrayBuffer to a hex string
|
|
20
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer)); // Create an array from the buffer
|
|
21
|
-
const hashHex = hashArray
|
|
22
|
-
.map(b => b.toString(16).padStart(2, '0'))
|
|
23
|
-
.join(''); // Convert to hex string
|
|
24
|
-
|
|
25
|
-
return hashHex;
|
|
15
|
+
return await cryptoSha1Hash(json);
|
|
26
16
|
}
|
|
27
17
|
|
|
28
18
|
/**
|
|
@@ -147,15 +137,15 @@ export default class SsrCache {
|
|
|
147
137
|
delete window._SSR_STORE;
|
|
148
138
|
}
|
|
149
139
|
|
|
150
|
-
private exportAsJson(): string {
|
|
151
|
-
return JSON.stringify(Array.from(this.store.entries())).replace(
|
|
152
|
-
/</g,
|
|
153
|
-
'\\u003c',
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
140
|
/** @hidden */
|
|
158
|
-
|
|
159
|
-
return
|
|
141
|
+
z_toData(): any {
|
|
142
|
+
return this.store;
|
|
160
143
|
}
|
|
161
144
|
}
|
|
145
|
+
|
|
146
|
+
export function ssrCacheExportToHead(ssr: SsrCache) {
|
|
147
|
+
const data = Array.from(ssr.z_toData().entries());
|
|
148
|
+
|
|
149
|
+
// safety, uneval already escapes data: https://github.com/sveltejs/devalue#xss-mitigation
|
|
150
|
+
return `\n\t\t<script>window._SSR_STORE = ${uneval(data)};</script>`;
|
|
151
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Code from Svelte (https://github.com/sveltejs/svelte/blob/b4968584df6be22222f58c273bdd8e04307ad36b/packages/svelte/src/escaping.js)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2016-24 these people (https://github.com/sveltejs/svelte/graphs/contributors)
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const ATTR_REGEX = /[&"<]/g;
|
|
26
|
+
const CONTENT_REGEX = /[&<]/g;
|
|
27
|
+
|
|
28
|
+
export function escape_html<V>(value: V, is_attr: boolean = false) {
|
|
29
|
+
const str = String(value ?? '');
|
|
30
|
+
|
|
31
|
+
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX;
|
|
32
|
+
pattern.lastIndex = 0;
|
|
33
|
+
|
|
34
|
+
let escaped = '';
|
|
35
|
+
let last = 0;
|
|
36
|
+
|
|
37
|
+
while (pattern.test(str)) {
|
|
38
|
+
const i = pattern.lastIndex - 1;
|
|
39
|
+
const ch = str[i];
|
|
40
|
+
escaped +=
|
|
41
|
+
str.substring(last, i) +
|
|
42
|
+
(ch === '&' ? '&' : ch === '"' ? '"' : '<');
|
|
43
|
+
last = i + 1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return escaped + str.substring(last);
|
|
47
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { escape_html } from './escaping.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert newlines to <br> tags and escape HTML.
|
|
5
|
+
*
|
|
6
|
+
* #### Warning
|
|
7
|
+
* When used in an attribute, the `isAttr` parameter needs to be set to `true`.
|
|
8
|
+
*
|
|
9
|
+
* #### Example
|
|
10
|
+
* ```svelte
|
|
11
|
+
* {@html nl2br("Hello\nWorld")}
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function nl2br(str: string, isAttr: boolean = false): string {
|
|
15
|
+
return escape_html(str, isAttr).replace(/\n/g, '<br>');
|
|
16
|
+
}
|
package/src/std/index.ts
CHANGED
|
@@ -31,12 +31,7 @@ export interface Cloneable {
|
|
|
31
31
|
* A type that represents a value that can be cloned.
|
|
32
32
|
*/
|
|
33
33
|
export type CloneableOrPrimitive =
|
|
34
|
-
|
|
|
35
|
-
| string
|
|
36
|
-
| number
|
|
37
|
-
| boolean
|
|
38
|
-
| null
|
|
39
|
-
| undefined;
|
|
34
|
+
Cloneable | string | number | boolean | null | undefined;
|
|
40
35
|
|
|
41
36
|
/**
|
|
42
37
|
* Creates a clone of the provided value. The values need to be either
|
package/src/utils.ts
CHANGED
|
@@ -34,3 +34,24 @@ export function urlWithPath(url: string, path?: string): URL {
|
|
|
34
34
|
u.pathname = path ?? '';
|
|
35
35
|
return u;
|
|
36
36
|
}
|
|
37
|
+
|
|
38
|
+
export async function cryptoSha1Hash(str: string): Promise<string> {
|
|
39
|
+
// Convert the string data to an ArrayBuffer
|
|
40
|
+
const encoder = new TextEncoder();
|
|
41
|
+
const dataBuffer = encoder.encode(str);
|
|
42
|
+
|
|
43
|
+
// Use the Web Crypto API to hash the data with SHA-1
|
|
44
|
+
const hashBuffer = await crypto.subtle.digest('SHA-1', dataBuffer);
|
|
45
|
+
|
|
46
|
+
const bytes = new Uint8Array(hashBuffer);
|
|
47
|
+
// @ts-ignore using 2025 baseline feature
|
|
48
|
+
if (typeof bytes.toHex === 'function') return bytes.toHex();
|
|
49
|
+
|
|
50
|
+
// Convert the ArrayBuffer to a hex string
|
|
51
|
+
const hashArray = Array.from(bytes); // Create an array from the buffer
|
|
52
|
+
const hashHex = hashArray
|
|
53
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
54
|
+
.join(''); // Convert to hex string
|
|
55
|
+
|
|
56
|
+
return hashHex;
|
|
57
|
+
}
|
package/src/vite/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { relative } from 'node:path';
|
|
2
|
-
import MagicString from 'magic-string';
|
|
3
2
|
import {
|
|
4
3
|
ConfigEnv,
|
|
5
4
|
Plugin,
|
|
@@ -15,11 +14,11 @@ import {
|
|
|
15
14
|
EnvData,
|
|
16
15
|
initEnvData,
|
|
17
16
|
initSites,
|
|
17
|
+
initRequestSites,
|
|
18
18
|
modRender,
|
|
19
19
|
modRenderError,
|
|
20
20
|
} from '../server/shared.js';
|
|
21
21
|
import ServerRouter from '../server/ServerRouter.js';
|
|
22
|
-
import sveltePackage from 'svelte/package.json' with { type: 'json' };
|
|
23
22
|
import { initQueryRoutes } from '../server/queries/queries.js';
|
|
24
23
|
import {
|
|
25
24
|
PLATFORM,
|
|
@@ -29,6 +28,11 @@ import {
|
|
|
29
28
|
import { SiteFromGraphQl } from '../routing/Site.js';
|
|
30
29
|
import { ServerResponse } from 'node:http';
|
|
31
30
|
import QueryError from '../queries/QueryError.js';
|
|
31
|
+
import {
|
|
32
|
+
CodeTransform,
|
|
33
|
+
generateTransform,
|
|
34
|
+
rolldownString,
|
|
35
|
+
} from 'rolldown-string';
|
|
32
36
|
|
|
33
37
|
const VIRT_MOD_ID = 'virtual:crelte/svelteComponents';
|
|
34
38
|
const RESOLVED_VIRT_MOD_ID = '\0' + VIRT_MOD_ID;
|
|
@@ -45,43 +49,33 @@ function usedSsrComponents(
|
|
|
45
49
|
code: string,
|
|
46
50
|
id: string,
|
|
47
51
|
options: { ssr?: boolean } | undefined,
|
|
48
|
-
|
|
49
|
-
): TransformResult | undefined {
|
|
52
|
+
): CodeTransform | undefined {
|
|
50
53
|
if (!options?.ssr || !id.endsWith('.svelte')) return;
|
|
51
54
|
|
|
52
55
|
const file = relative('.', id);
|
|
53
56
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
idx = code.indexOf(defFnSign);
|
|
58
|
-
if (idx < 0) return;
|
|
59
|
-
|
|
60
|
-
const withPropsIdx = code.indexOf('($$renderer, $$props) {', idx);
|
|
61
|
-
const noPropsIdx = code.indexOf('($$renderer) {', idx);
|
|
62
|
-
idx = withPropsIdx >= 0 ? withPropsIdx : noPropsIdx;
|
|
63
|
-
if (idx < 0) {
|
|
64
|
-
console.error('could not find svelte5 init', id);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
57
|
+
const defFnSign = 'export default function ';
|
|
58
|
+
let idx = code.indexOf(defFnSign);
|
|
59
|
+
if (idx < 0) return;
|
|
67
60
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (idx < 0) return;
|
|
75
|
-
idx += 1; // \n
|
|
76
|
-
} else {
|
|
77
|
-
const initFnSign =
|
|
78
|
-
'create_ssr_component(($$result, $$props, $$bindings, slots) => {';
|
|
79
|
-
idx = code.indexOf(initFnSign);
|
|
80
|
-
if (idx < 0) return;
|
|
81
|
-
idx += initFnSign.length;
|
|
61
|
+
const withPropsIdx = code.indexOf('($$renderer, $$props) {', idx);
|
|
62
|
+
const noPropsIdx = code.indexOf('($$renderer) {', idx);
|
|
63
|
+
idx = withPropsIdx >= 0 ? withPropsIdx : noPropsIdx;
|
|
64
|
+
if (idx < 0) {
|
|
65
|
+
console.error('could not find svelte5 init', id);
|
|
66
|
+
return;
|
|
82
67
|
}
|
|
83
68
|
|
|
84
|
-
const
|
|
69
|
+
const renderCompSign = '$$renderer.component(($$renderer) => {';
|
|
70
|
+
const renderCompIdx = code.indexOf(renderCompSign, idx);
|
|
71
|
+
if (renderCompIdx >= 0) idx = renderCompIdx;
|
|
72
|
+
|
|
73
|
+
// Find the end of the line where the pattern was found
|
|
74
|
+
idx = code.indexOf('\n', idx);
|
|
75
|
+
if (idx < 0) return;
|
|
76
|
+
idx += 1; // \n
|
|
77
|
+
|
|
78
|
+
const s = rolldownString(code, id, options);
|
|
85
79
|
|
|
86
80
|
s.prepend(`import { getContext as __modulesGetContext } from 'svelte';\n`);
|
|
87
81
|
|
|
@@ -95,14 +89,7 @@ if (ctx && ctx instanceof Set) {
|
|
|
95
89
|
`;
|
|
96
90
|
s.appendLeft(idx, ctxAdd);
|
|
97
91
|
|
|
98
|
-
return
|
|
99
|
-
map: s.generateMap({
|
|
100
|
-
source: id,
|
|
101
|
-
includeContent: true,
|
|
102
|
-
hires: 'boundary',
|
|
103
|
-
}),
|
|
104
|
-
code: s.toString(),
|
|
105
|
-
};
|
|
92
|
+
return generateTransform(s, id);
|
|
106
93
|
}
|
|
107
94
|
|
|
108
95
|
// transform graphql files into a GraphQlQuery which can called with
|
|
@@ -121,9 +108,7 @@ function graphQlFiles(
|
|
|
121
108
|
|
|
122
109
|
if (!ssr) {
|
|
123
110
|
return `
|
|
124
|
-
export default {
|
|
125
|
-
queryName: ${JSON.stringify(name)}
|
|
126
|
-
};
|
|
111
|
+
export default ${JSON.stringify(name)};
|
|
127
112
|
`;
|
|
128
113
|
}
|
|
129
114
|
|
|
@@ -132,9 +117,7 @@ export default {
|
|
|
132
117
|
.replace(/\u2029/g, '\\u2029');
|
|
133
118
|
|
|
134
119
|
return `
|
|
135
|
-
export default {
|
|
136
|
-
queryName: ${JSON.stringify(name)}
|
|
137
|
-
};
|
|
120
|
+
export default ${JSON.stringify(name)};
|
|
138
121
|
|
|
139
122
|
export const query = {
|
|
140
123
|
path: ${JSON.stringify(path)},
|
|
@@ -143,30 +126,14 @@ export const query = {
|
|
|
143
126
|
`;
|
|
144
127
|
}
|
|
145
128
|
|
|
146
|
-
function isSvelte5(): boolean {
|
|
147
|
-
if (sveltePackage && sveltePackage.version) {
|
|
148
|
-
return sveltePackage.version.startsWith('5.');
|
|
149
|
-
}
|
|
150
|
-
// Fallback or error handling if version cannot be determined
|
|
151
|
-
console.warn('Could not determine Svelte version. Assuming not Svelte 5.');
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
129
|
// outside of crelte because each build executes crelte again
|
|
156
130
|
let isSsrBuild = false;
|
|
157
131
|
|
|
158
|
-
export
|
|
159
|
-
/** If not specified tries to detect which version is installed */
|
|
160
|
-
svelte4?: boolean;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
export default function crelte(opts?: CrelteOptions): Plugin {
|
|
132
|
+
export default function crelte(): Plugin {
|
|
164
133
|
let viteConfig: ResolvedConfig;
|
|
165
134
|
let viteConfigEnv: ConfigEnv;
|
|
166
135
|
let initialConfig: UserConfig;
|
|
167
136
|
|
|
168
|
-
const svelte5 = opts?.svelte4 ? false : isSvelte5();
|
|
169
|
-
|
|
170
137
|
return {
|
|
171
138
|
name: 'crelte',
|
|
172
139
|
|
|
@@ -229,33 +196,22 @@ export default function crelte(opts?: CrelteOptions): Plugin {
|
|
|
229
196
|
}
|
|
230
197
|
|
|
231
198
|
if (id === RESOLVED_VIRT_MOD_ID) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
);
|
|
243
|
-
} else {
|
|
244
|
-
return (
|
|
245
|
-
`export function internalSvelteMount(comp, options) {` +
|
|
246
|
-
`return new comp({ ...options, hydrate: true });` +
|
|
247
|
-
`}` +
|
|
248
|
-
`export function internalSvelteRender(comp, options) {` +
|
|
249
|
-
`return comp.render(options.props, options);` +
|
|
250
|
-
`}`
|
|
251
|
-
);
|
|
252
|
-
}
|
|
199
|
+
return (
|
|
200
|
+
`import { hydrate } from 'svelte';` +
|
|
201
|
+
`import { render } from 'svelte/server';` +
|
|
202
|
+
`export function internalSvelteMount(comp, options) {` +
|
|
203
|
+
`return hydrate(comp, options);` +
|
|
204
|
+
`}` +
|
|
205
|
+
`export function internalSvelteRender(comp, options) {` +
|
|
206
|
+
`return render(comp, options);` +
|
|
207
|
+
`}`
|
|
208
|
+
);
|
|
253
209
|
}
|
|
254
210
|
},
|
|
255
211
|
|
|
256
212
|
transform(code, id, options) {
|
|
257
213
|
return (
|
|
258
|
-
usedSsrComponents(code, id, options
|
|
214
|
+
usedSsrComponents(code, id, options) ||
|
|
259
215
|
graphQlFiles(code, id, !!options?.ssr)
|
|
260
216
|
);
|
|
261
217
|
},
|
|
@@ -351,11 +307,13 @@ async function serveVite(env: EnvData, vite: ViteDevServer) {
|
|
|
351
307
|
sites = await initSites(env);
|
|
352
308
|
}
|
|
353
309
|
|
|
310
|
+
const requestSites = await initRequestSites(env, sites, req);
|
|
311
|
+
|
|
354
312
|
const router = new ServerRouter(
|
|
355
313
|
env.endpointUrl,
|
|
356
314
|
env.frontendUrl,
|
|
357
315
|
env.env,
|
|
358
|
-
|
|
316
|
+
requestSites,
|
|
359
317
|
{ endpointToken: env.endpointToken },
|
|
360
318
|
);
|
|
361
319
|
|
|
@@ -400,7 +358,13 @@ async function serveVite(env: EnvData, vite: ViteDevServer) {
|
|
|
400
358
|
return;
|
|
401
359
|
}
|
|
402
360
|
|
|
403
|
-
const response = await modRender(
|
|
361
|
+
const response = await modRender(
|
|
362
|
+
env,
|
|
363
|
+
requestSites,
|
|
364
|
+
serverMod,
|
|
365
|
+
template,
|
|
366
|
+
req,
|
|
367
|
+
);
|
|
404
368
|
await webResponseToResponse(response, res);
|
|
405
369
|
};
|
|
406
370
|
|
package/types.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ declare module '*.graphql' {
|
|
|
2
2
|
// todo this would be a GraphQL query object
|
|
3
3
|
// but node doesn't import GraphQl and the type might change?
|
|
4
4
|
// this just removes the anoying typescript error
|
|
5
|
-
const value:
|
|
5
|
+
const value: string;
|
|
6
6
|
export default value;
|
|
7
7
|
|
|
8
8
|
export const query: {
|