@openworkers/adapter-sveltekit 0.3.3 → 0.3.5
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/README.md +5 -5
- package/index.js +76 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,10 +35,10 @@ export default {
|
|
|
35
35
|
|
|
36
36
|
```
|
|
37
37
|
dist/
|
|
38
|
-
├──
|
|
39
|
-
├──
|
|
40
|
-
├── assets/
|
|
41
|
-
└── functions/
|
|
38
|
+
├── _worker.js # Main SSR worker
|
|
39
|
+
├── _routes.json # Route manifest for edge routing
|
|
40
|
+
├── assets/ # Static assets and prerendered pages
|
|
41
|
+
└── functions/ # Mini-workers for API routes (if functions: true)
|
|
42
42
|
├── api-hello.js
|
|
43
43
|
└── api-users.js
|
|
44
44
|
```
|
|
@@ -50,7 +50,7 @@ When `functions: true`, the adapter generates a separate mini-worker for each `+
|
|
|
50
50
|
- `/api/hello/+server.ts` → `functions/api-hello.js`
|
|
51
51
|
- `/api/users/+server.ts` → `functions/api-users.js`
|
|
52
52
|
|
|
53
|
-
The route mappings are included in `
|
|
53
|
+
The route mappings are included in `_routes.json`:
|
|
54
54
|
|
|
55
55
|
```js
|
|
56
56
|
{
|
package/index.js
CHANGED
|
@@ -63,7 +63,7 @@ export default function (options = {}) {
|
|
|
63
63
|
);
|
|
64
64
|
|
|
65
65
|
// Bundle main worker with esbuild
|
|
66
|
-
const workerDest = `${dest}/
|
|
66
|
+
const workerDest = `${dest}/_worker.js`;
|
|
67
67
|
const shimAsyncHooks = posixify(path.resolve(files, 'shims/async_hooks.js'));
|
|
68
68
|
|
|
69
69
|
await build({
|
|
@@ -91,15 +91,18 @@ export default function (options = {}) {
|
|
|
91
91
|
if (functionsEnabled) {
|
|
92
92
|
builder.mkdirp(`${dest}/functions`);
|
|
93
93
|
|
|
94
|
-
const
|
|
94
|
+
const serverDir = builder.getServerDirectory();
|
|
95
|
+
const viteManifestPath = path.join(serverDir, '.vite/manifest.json');
|
|
95
96
|
const functionTemplate = posixify(path.resolve(files, 'function-worker.js'));
|
|
96
97
|
|
|
97
|
-
if (existsSync(
|
|
98
|
-
const
|
|
98
|
+
if (existsSync(viteManifestPath)) {
|
|
99
|
+
const viteManifest = JSON.parse(readFileSync(viteManifestPath, 'utf-8'));
|
|
100
|
+
const endpoints = extractEndpointsFromManifest(viteManifest, serverDir);
|
|
99
101
|
|
|
100
102
|
for (const endpoint of endpoints) {
|
|
101
|
-
const routePattern = endpoint.
|
|
102
|
-
|
|
103
|
+
const routePattern = endpoint.pattern;
|
|
104
|
+
// Use SvelteKit route syntax for worker filename
|
|
105
|
+
const workerName = endpoint.route.replace(/\//g, '-').replace(/^-/, '') || 'index';
|
|
103
106
|
const workerFile = `functions/${workerName}.js`;
|
|
104
107
|
|
|
105
108
|
// Bundle using the template with ENDPOINT alias
|
|
@@ -130,7 +133,7 @@ export default function (options = {}) {
|
|
|
130
133
|
}
|
|
131
134
|
}
|
|
132
135
|
|
|
133
|
-
// Generate
|
|
136
|
+
// Generate _routes.json for edge routing
|
|
134
137
|
const routes = {
|
|
135
138
|
// Immutable assets (hashed filenames, cache forever)
|
|
136
139
|
immutable: [`/${appPath}/immutable/*`],
|
|
@@ -145,14 +148,12 @@ export default function (options = {}) {
|
|
|
145
148
|
};
|
|
146
149
|
|
|
147
150
|
writeFileSync(
|
|
148
|
-
`${dest}/
|
|
149
|
-
|
|
150
|
-
`// Used by OpenWorkers edge router to bypass worker for static content\n\n` +
|
|
151
|
-
`export default ${JSON.stringify(routes, null, '\t')};\n`
|
|
151
|
+
`${dest}/_routes.json`,
|
|
152
|
+
JSON.stringify(routes, null, 2)
|
|
152
153
|
);
|
|
153
154
|
|
|
154
155
|
builder.log.minor(`Wrote ${workerDest}`);
|
|
155
|
-
builder.log.minor(`Wrote ${dest}/
|
|
156
|
+
builder.log.minor(`Wrote ${dest}/_routes.json`);
|
|
156
157
|
|
|
157
158
|
if (functions.length > 0) {
|
|
158
159
|
builder.log.minor(`Wrote ${functions.length} function workers to ${dest}/functions/`);
|
|
@@ -164,30 +165,72 @@ export default function (options = {}) {
|
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
/**
|
|
167
|
-
*
|
|
168
|
-
* @param {
|
|
169
|
-
* @param {string}
|
|
170
|
-
* @returns {Array<{route: string, file: string}>}
|
|
168
|
+
* Extract endpoints from Vite manifest and convert SvelteKit routes to simple patterns
|
|
169
|
+
* @param {object} manifest - Vite manifest object
|
|
170
|
+
* @param {string} serverDir - Server build directory
|
|
171
|
+
* @returns {Array<{pattern: string, route: string, file: string}>}
|
|
171
172
|
*/
|
|
172
|
-
function
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
173
|
+
function extractEndpointsFromManifest(manifest, serverDir) {
|
|
174
|
+
const endpoints = [];
|
|
175
|
+
|
|
176
|
+
for (const [key, entry] of Object.entries(manifest)) {
|
|
177
|
+
// Only process endpoint files (+server.ts)
|
|
178
|
+
if (!key.includes('+server.ts')) continue;
|
|
179
|
+
|
|
180
|
+
const sourcePath = entry.src;
|
|
181
|
+
if (!sourcePath || !sourcePath.startsWith('src/routes/')) continue;
|
|
182
|
+
|
|
183
|
+
// Extract route from source path: src/routes/status/[code]/[[reason]]/+server.ts
|
|
184
|
+
const routePart = sourcePath
|
|
185
|
+
.replace(/^src\/routes/, '')
|
|
186
|
+
.replace(/\/\+server\.(ts|js)$/, '');
|
|
187
|
+
|
|
188
|
+
// Convert SvelteKit route syntax to simple wildcard patterns
|
|
189
|
+
const pattern = convertRouteToPattern(routePart);
|
|
190
|
+
|
|
191
|
+
// Get the built file path
|
|
192
|
+
const file = path.join(serverDir, entry.file);
|
|
193
|
+
|
|
194
|
+
endpoints.push({
|
|
195
|
+
pattern, // For routing: /status/*/*
|
|
196
|
+
route: routePart, // For filename: /status/[code]/[[reason]]
|
|
197
|
+
file
|
|
198
|
+
});
|
|
188
199
|
}
|
|
189
200
|
|
|
190
|
-
return
|
|
201
|
+
return endpoints;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Convert SvelteKit route syntax to simple wildcard patterns
|
|
206
|
+
* Examples:
|
|
207
|
+
* /status/[code]/[[reason]] becomes /status/star/star
|
|
208
|
+
* /range/[n] becomes /range/star
|
|
209
|
+
* /drip/[...params] becomes /drip/doublestar
|
|
210
|
+
* @param {string} route - SvelteKit route (e.g. "/status/[code]/[[reason]]")
|
|
211
|
+
* @returns {string} - Simple pattern with wildcards
|
|
212
|
+
*/
|
|
213
|
+
function convertRouteToPattern(route) {
|
|
214
|
+
if (!route || route === '/') return '/';
|
|
215
|
+
|
|
216
|
+
const segments = route.split('/').filter(Boolean);
|
|
217
|
+
const patternSegments = segments.map(segment => {
|
|
218
|
+
// Rest parameter: [...params] -> **
|
|
219
|
+
if (segment.startsWith('[...') && segment.endsWith(']')) {
|
|
220
|
+
return '**';
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Required or optional parameter: [param] or [[param]] -> *
|
|
224
|
+
if ((segment.startsWith('[') && segment.endsWith(']')) ||
|
|
225
|
+
(segment.startsWith('[[') && segment.endsWith(']]'))) {
|
|
226
|
+
return '*';
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Static segment: keep as-is
|
|
230
|
+
return segment;
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
return '/' + patternSegments.join('/');
|
|
191
234
|
}
|
|
192
235
|
|
|
193
236
|
/** @param {string} str */
|