clarusjs 0.16.0 → 0.18.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.
- package/dist/check/generate.js +97 -8
- package/dist/cli/check.d.ts +3 -0
- package/dist/cli/check.js +55 -3
- package/dist/cli/explain.js +3 -0
- package/dist/cli/index.js +21 -3
- package/dist/cli/project.d.ts +5 -0
- package/dist/cli/project.js +1 -1
- package/dist/compiler/emit-client.js +33 -8
- package/dist/compiler/events.d.ts +9 -0
- package/dist/compiler/events.js +46 -0
- package/dist/compiler/html-expr.js +14 -7
- package/dist/compiler/index.js +32 -5
- package/dist/compiler/ir.d.ts +15 -0
- package/dist/compiler/ir.js +87 -11
- package/dist/compiler/lint.d.ts +6 -2
- package/dist/compiler/lint.js +114 -5
- package/dist/compiler/loops.d.ts +13 -0
- package/dist/compiler/loops.js +49 -0
- package/dist/compiler/parse.d.ts +23 -0
- package/dist/compiler/parse.js +149 -1
- package/dist/compiler/script.d.ts +2 -0
- package/dist/compiler/script.js +78 -1
- package/dist/forms/index.js +35 -15
- package/dist/forms/types.d.ts +7 -0
- package/dist/node/serve.d.ts +26 -0
- package/dist/node/serve.js +150 -0
- package/dist/runtime/attrs.d.ts +13 -0
- package/dist/runtime/attrs.js +13 -0
- package/dist/runtime/dom.d.ts +32 -2
- package/dist/runtime/dom.js +170 -45
- package/dist/runtime/hot.js +3 -2
- package/dist/runtime/index.d.ts +2 -2
- package/dist/runtime/index.js +2 -2
- package/dist/runtime/loader.js +26 -7
- package/dist/runtime/motion.d.ts +59 -0
- package/dist/runtime/motion.js +339 -0
- package/dist/runtime/navigate.d.ts +2 -0
- package/dist/runtime/navigate.js +7 -1
- package/dist/runtime/navigation.d.ts +44 -0
- package/dist/runtime/navigation.js +69 -3
- package/dist/runtime/signals.d.ts +2 -0
- package/dist/runtime/signals.js +31 -0
- package/dist/seo.d.ts +140 -0
- package/dist/seo.js +64 -0
- package/dist/server/handler.d.ts +1 -0
- package/dist/server/handler.js +1 -0
- package/dist/server/head.js +5 -0
- package/dist/vite/cloudflare.d.ts +3 -0
- package/dist/vite/cloudflare.js +55 -0
- package/dist/vite/env.d.ts +19 -0
- package/dist/vite/env.js +34 -0
- package/dist/vite/headers.d.ts +27 -0
- package/dist/vite/headers.js +70 -0
- package/dist/vite/manifest.d.ts +11 -1
- package/dist/vite/manifest.js +10 -1
- package/dist/vite/netlify.d.ts +5 -2
- package/dist/vite/netlify.js +4 -2
- package/dist/vite/node.d.ts +4 -0
- package/dist/vite/node.js +9 -2
- package/dist/vite/plugin.d.ts +25 -2
- package/dist/vite/plugin.js +84 -9
- package/dist/vite/vercel.d.ts +3 -0
- package/dist/vite/vercel.js +53 -0
- package/package.json +14 -1
package/dist/vite/netlify.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
/** '/account/[id]' → ['/account/:id', '/account/:id/'] (Netlify paths use URLPattern syntax). */
|
|
2
2
|
export declare function netlifyPaths(route: string, fragments?: boolean): string[];
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Write (or, without server routes, remove) the function in `<root>/.netlify/v1/functions`. `client`: dist/client,
|
|
5
|
+
* whose _headers the function applies (Netlify applies them to static files only).
|
|
6
|
+
*/
|
|
7
|
+
export declare function writeNetlifyFunction(root: string, serverEntry: string, serverRoutes: string[], fragments?: boolean, client?: string): void;
|
package/dist/vite/netlify.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { dirname, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { headersCode } from './headers.js';
|
|
3
4
|
const FUNCTION = '.netlify/v1/functions/clarusjs-server.mjs';
|
|
4
5
|
export function netlifyPaths(route, fragments = false) {
|
|
5
6
|
const segments = route.split('/');
|
|
@@ -24,7 +25,7 @@ export function netlifyPaths(route, fragments = false) {
|
|
|
24
25
|
paths.push(path === '/' || path === '' ? '/index.frag.html' : `${path}/index.frag.html`);
|
|
25
26
|
return paths;
|
|
26
27
|
}
|
|
27
|
-
export function writeNetlifyFunction(root, serverEntry, serverRoutes, fragments = false) {
|
|
28
|
+
export function writeNetlifyFunction(root, serverEntry, serverRoutes, fragments = false, client) {
|
|
28
29
|
const file = resolve(root, FUNCTION);
|
|
29
30
|
if (!serverRoutes.length) {
|
|
30
31
|
rmSync(file, { force: true });
|
|
@@ -35,8 +36,9 @@ export function writeNetlifyFunction(root, serverEntry, serverRoutes, fragments
|
|
|
35
36
|
writeFileSync(file, [
|
|
36
37
|
'// Generated by clarusjs: server routes on Netlify Functions. Static routes are files.',
|
|
37
38
|
`import handler from ${JSON.stringify(entry.startsWith('.') ? entry : `./${entry}`)}`,
|
|
39
|
+
...(client ? [headersCode(client)] : []),
|
|
38
40
|
'',
|
|
39
|
-
'export default (request) => handler.fetch(request)',
|
|
41
|
+
client ? 'export default async (request) => withHeaders(await handler.fetch(request), new URL(request.url).pathname)' : 'export default (request) => handler.fetch(request)',
|
|
40
42
|
'',
|
|
41
43
|
`export const config = ${JSON.stringify({ path: serverRoutes.flatMap((r) => netlifyPaths(r, fragments)), preferStatic: true })}`,
|
|
42
44
|
'',
|
package/dist/vite/node.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
2
|
export declare function toRequest(req: IncomingMessage): Request;
|
|
3
|
+
/**
|
|
4
|
+
* Streamed: a body is sent as it comes (an event stream, a long render), with backpressure. A client that goes away
|
|
5
|
+
* cancels the body (pipeline destroys the source, which cancels the web stream).
|
|
6
|
+
*/
|
|
3
7
|
export declare function sendResponse(res: ServerResponse, response: Response): Promise<void>;
|
|
4
8
|
/** Whether an Accept-Encoding header allows `encoding`: named, or through `*`, and not with q=0. */
|
|
5
9
|
export declare function acceptsEncoding(header: string, encoding: string): boolean;
|
package/dist/vite/node.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
|
+
import { pipeline } from 'node:stream/promises';
|
|
2
3
|
export function toRequest(req) {
|
|
3
4
|
const headers = new Headers();
|
|
4
5
|
for (const [key, value] of Object.entries(req.headers)) {
|
|
@@ -13,8 +14,14 @@ export function toRequest(req) {
|
|
|
13
14
|
}
|
|
14
15
|
export async function sendResponse(res, response) {
|
|
15
16
|
res.statusCode = response.status;
|
|
16
|
-
response.headers.forEach((value, key) =>
|
|
17
|
-
|
|
17
|
+
response.headers.forEach((value, key) => { if (key !== 'set-cookie')
|
|
18
|
+
res.setHeader(key, value); });
|
|
19
|
+
const cookies = response.headers.getSetCookie();
|
|
20
|
+
if (cookies.length)
|
|
21
|
+
res.setHeader('set-cookie', cookies);
|
|
22
|
+
if (!response.body)
|
|
23
|
+
return void res.end();
|
|
24
|
+
await pipeline(Readable.fromWeb(response.body), res).catch(() => { });
|
|
18
25
|
}
|
|
19
26
|
export function acceptsEncoding(header, encoding) {
|
|
20
27
|
let star = false;
|
package/dist/vite/plugin.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ import type { LintConfig, LintRuleDef } from '../compiler/lint.ts';
|
|
|
4
4
|
export { packageOf } from './build.ts';
|
|
5
5
|
/** Newest commit time (unix seconds) of every file under a folder, from one `git log` (themes use it for "last updated"). */
|
|
6
6
|
export { gitDates } from './sitemap.ts';
|
|
7
|
+
import { type EnvOption } from './env.ts';
|
|
8
|
+
import { type HeaderRules } from './headers.ts';
|
|
7
9
|
import { type ModeTable } from '../server/modes.ts';
|
|
8
10
|
import type { HeadTag, SiteOptions } from '../server/head.ts';
|
|
9
11
|
/**
|
|
@@ -41,8 +43,13 @@ export type ClarusOptions = {
|
|
|
41
43
|
auto?: boolean;
|
|
42
44
|
libraries?: LibraryOption[];
|
|
43
45
|
};
|
|
44
|
-
/**
|
|
45
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Where server routes deploy. 'netlify': a Netlify Function (Frameworks API) next to the static files. 'vercel': the
|
|
48
|
+
* Build Output API in .vercel/output (static files, one Node function, routes), with the server bundled.
|
|
49
|
+
* 'cloudflare': a worker in dist/client/_worker.js/ and _routes.json (Pages, or a Worker with static assets).
|
|
50
|
+
* @see https://clarusjs.com/guide/deploy
|
|
51
|
+
*/
|
|
52
|
+
adapter?: 'netlify' | 'vercel' | 'cloudflare';
|
|
46
53
|
/** Fragment navigation: each static and server page also answers at <path>/index.frag.html. */
|
|
47
54
|
fragments?: boolean;
|
|
48
55
|
/** Page sources for other file types, such as Markdown from @clarusjs/content: `sources: [markdown()]`. */
|
|
@@ -72,6 +79,22 @@ export type ClarusOptions = {
|
|
|
72
79
|
* @see https://clarusjs.com/guide/routing
|
|
73
80
|
*/
|
|
74
81
|
redirects?: Record<string, string>;
|
|
82
|
+
/**
|
|
83
|
+
* Response headers by path pattern (`*` anything, `:name` one segment): `{ '/*': { 'X-Frame-Options': 'DENY' } }`.
|
|
84
|
+
* The build writes them to `_headers` (read as is by Netlify and Cloudflare Pages; applied by `clarusjs serve` and
|
|
85
|
+
* vite preview), after `X-Content-Type-Options: nosniff`, `Referrer-Policy: strict-origin-when-cross-origin` and
|
|
86
|
+
* immutable caching for hashed assets. A name set by several matching patterns gets their values joined. `false`: no
|
|
87
|
+
* `_headers` written.
|
|
88
|
+
* @see https://clarusjs.com/guide/deploy#headers
|
|
89
|
+
*/
|
|
90
|
+
headers?: HeaderRules | false;
|
|
91
|
+
/**
|
|
92
|
+
* The environment variables the site needs: `{ server: ['DATABASE_URL'], browser: ['VITE_API_URL'] }`. `vite build`
|
|
93
|
+
* fails naming any that is missing (dev warns), and `.clarusjs/env.d.ts` types them as strings. Browser ones start
|
|
94
|
+
* with Vite's prefix (VITE_). Server code reads every .env variable from process.env, whether named here or not.
|
|
95
|
+
* @see https://clarusjs.com/guide/environment-variables
|
|
96
|
+
*/
|
|
97
|
+
env?: EnvOption;
|
|
75
98
|
/**
|
|
76
99
|
* Every page's <head>: `titleTemplate` ('%s · Site') wraps the document title, `tags` are added unless the page has
|
|
77
100
|
* its own with the same key. Anything else: export `transformHead(head, page)` from src/head.ts.
|
package/dist/vite/plugin.js
CHANGED
|
@@ -19,7 +19,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync,
|
|
|
19
19
|
import { brotliCompressSync, constants as zlibConstants, gzipSync } from 'node:zlib';
|
|
20
20
|
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
21
21
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
22
|
-
import { isRunnableDevEnvironment, normalizePath } from 'vite';
|
|
22
|
+
import { isRunnableDevEnvironment, loadEnv, normalizePath } from 'vite';
|
|
23
23
|
import { compile, scopeId, ClarusError } from '../compiler/index.js';
|
|
24
24
|
import { parse } from '../compiler/parse.js';
|
|
25
25
|
import { autoImport, COMPONENT_DIRS, componentIndex } from '../compiler/components.js';
|
|
@@ -29,8 +29,12 @@ export { packageOf } from './build.js';
|
|
|
29
29
|
export { gitDates } from './sitemap.js';
|
|
30
30
|
import { writeManifest } from './manifest.js';
|
|
31
31
|
import { writeNetlifyFunction } from './netlify.js';
|
|
32
|
+
import { writeVercelOutput } from './vercel.js';
|
|
33
|
+
import { writeCloudflareWorker } from './cloudflare.js';
|
|
32
34
|
import { checkLinks } from './links.js';
|
|
33
35
|
import { checkRedirects, redirectFor, writeRedirects } from './redirects.js';
|
|
36
|
+
import { loadServerEnv, releaseServerEnv, writeEnvTypes } from './env.js';
|
|
37
|
+
import { canonical, checkHeaders, headersFor, readHeaders, writeHeaders } from './headers.js';
|
|
34
38
|
import { projectAliases, resolveAlias } from './aliases.js';
|
|
35
39
|
import { gitDates, sitemapFiles } from './sitemap.js';
|
|
36
40
|
import { checkModes, modeFor, routeMatches } from '../server/modes.js';
|
|
@@ -107,12 +111,36 @@ function pageGraph(root, files, aliases = []) {
|
|
|
107
111
|
for (const file of files.pages(root)) {
|
|
108
112
|
const css = [];
|
|
109
113
|
const seen = new Set();
|
|
114
|
+
for (const layout of pageLayouts(root, file, files))
|
|
115
|
+
collect(resolve(root, `.${layout}`), css, seen);
|
|
110
116
|
collect(file, css, seen);
|
|
111
117
|
seen.delete(file);
|
|
112
118
|
pages.set(routeFromFile(`/${relative(root, file).split(sep).join('/')}`), { css, components: [...seen] });
|
|
113
119
|
}
|
|
114
120
|
return pages;
|
|
115
121
|
}
|
|
122
|
+
function pageLayouts(root, file, files) {
|
|
123
|
+
const layouts = files.layouts(file, root) ?? [];
|
|
124
|
+
try {
|
|
125
|
+
return layouts.length && parse(files.read(file, root), file).layout === 'none' ? [] : layouts;
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return layouts;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function routeParts(root, files) {
|
|
132
|
+
return files.pages(root).map((file) => {
|
|
133
|
+
const rel = relative(root, file).split(sep).join('/');
|
|
134
|
+
const folders = rel.replace(/^src\/pages\//, '').split('/').slice(0, -1);
|
|
135
|
+
const guards = [];
|
|
136
|
+
for (let i = 0; i <= folders.length; i++) {
|
|
137
|
+
const guard = ['src/pages', ...folders.slice(0, i), '+guard.ts'].join('/');
|
|
138
|
+
if (existsSync(resolve(root, guard)))
|
|
139
|
+
guards.push(guard);
|
|
140
|
+
}
|
|
141
|
+
return { route: routeFromFile(`/${rel}`), layouts: pageLayouts(root, file, files).map((l) => l.replace(/^\//, '')), guards };
|
|
142
|
+
});
|
|
143
|
+
}
|
|
116
144
|
function layoutsFor(root, file, config) {
|
|
117
145
|
const pages = resolve(root, 'src/pages');
|
|
118
146
|
const rel = relative(pages, file);
|
|
@@ -131,7 +159,9 @@ function layoutsFor(root, file, config) {
|
|
|
131
159
|
return out;
|
|
132
160
|
}
|
|
133
161
|
function componentLoaderOf(root, file) {
|
|
134
|
-
if (!file.endsWith('.tscl')
|
|
162
|
+
if (!file.endsWith('.tscl'))
|
|
163
|
+
return undefined;
|
|
164
|
+
if (real(file).startsWith(real(resolve(root, 'src/pages')) + sep) && !file.endsWith(`${sep}+layout.tscl`) && !file.endsWith('/+layout.tscl'))
|
|
135
165
|
return undefined;
|
|
136
166
|
const loader = file.replace(/\.tscl$/, '.load.ts');
|
|
137
167
|
return existsSync(loader) ? loader : undefined;
|
|
@@ -226,12 +256,13 @@ export default function clarusjs(options = {}) {
|
|
|
226
256
|
};
|
|
227
257
|
const fileRe = new RegExp(`\\.(${extensions.map((e) => e.slice(1)).join('|')})(\\?|$)`);
|
|
228
258
|
const glob = (dir) => JSON.stringify(extensions.map((e) => `${dir}/**/*${e}`));
|
|
229
|
-
if (options.adapter !== undefined &&
|
|
230
|
-
throw new Error(`clarusjs: adapter "${options.adapter}" is not supported; use 'netlify'`);
|
|
259
|
+
if (options.adapter !== undefined && !['netlify', 'vercel', 'cloudflare'].includes(options.adapter)) {
|
|
260
|
+
throw new Error(`clarusjs: adapter "${options.adapter}" is not supported; use 'netlify', 'vercel' or 'cloudflare'`);
|
|
231
261
|
}
|
|
232
262
|
let root = process.cwd();
|
|
233
263
|
let command = 'serve';
|
|
234
264
|
let base = '/';
|
|
265
|
+
let envWarning;
|
|
235
266
|
let built;
|
|
236
267
|
const builtUrls = () => {
|
|
237
268
|
if (command !== 'build')
|
|
@@ -257,7 +288,7 @@ export default function clarusjs(options = {}) {
|
|
|
257
288
|
};
|
|
258
289
|
return {
|
|
259
290
|
name: 'clarusjs',
|
|
260
|
-
api: { sources, lint: options.lint, lintRules: options.lintRules, components: options.components },
|
|
291
|
+
api: { sources, lint: options.lint, lintRules: options.lintRules, components: options.components, env: options.env, routes: options.routes },
|
|
261
292
|
config(userConfig, env) {
|
|
262
293
|
extra = (appRoot) => pluginsOf(userConfig.plugins).flatMap((p) => p.api?.clarusComponents?.(appRoot) ?? []);
|
|
263
294
|
const projectRoot = resolve(userConfig.root ?? process.cwd());
|
|
@@ -270,12 +301,22 @@ export default function clarusjs(options = {}) {
|
|
|
270
301
|
alias: [
|
|
271
302
|
{ find: 'clarusjs/runtime/server', replacement: here('../runtime/server') },
|
|
272
303
|
{ find: 'clarusjs/runtime', replacement: here('../runtime/index') },
|
|
304
|
+
{ find: 'clarusjs/motion', replacement: here('../runtime/motion') },
|
|
273
305
|
...own,
|
|
274
306
|
],
|
|
275
307
|
},
|
|
276
308
|
};
|
|
277
309
|
if (env.isPreview)
|
|
278
310
|
config.build = { outDir: CLIENT_OUT };
|
|
311
|
+
releaseServerEnv();
|
|
312
|
+
const envFiles = userConfig.envDir === false ? {} : loadEnv(env.mode, resolve(projectRoot, userConfig.envDir ?? '.'), '');
|
|
313
|
+
const prefixes = [userConfig.envPrefix ?? 'VITE_'].flat();
|
|
314
|
+
const missing = loadServerEnv({ ...Object.fromEntries(Object.entries(process.env).filter(([k, v]) => v !== undefined && prefixes.some((p) => k.startsWith(p)))), ...envFiles }, prefixes, options.env);
|
|
315
|
+
if (missing && env.command === 'build' && !env.isPreview)
|
|
316
|
+
throw new Error(missing);
|
|
317
|
+
envWarning = missing && !env.isPreview ? missing : undefined;
|
|
318
|
+
if (options.env && !env.isPreview)
|
|
319
|
+
writeEnvTypes(projectRoot, options.env);
|
|
279
320
|
const shipping = new Set();
|
|
280
321
|
if (!env.isPreview) {
|
|
281
322
|
for (const file of files.components(resolve(userConfig.root ?? process.cwd()))) {
|
|
@@ -285,6 +326,8 @@ export default function clarusjs(options = {}) {
|
|
|
285
326
|
}
|
|
286
327
|
if (shipping.size)
|
|
287
328
|
config.ssr = { noExternal: [...shipping] };
|
|
329
|
+
if (options.adapter === 'vercel' || options.adapter === 'cloudflare')
|
|
330
|
+
config.ssr = { noExternal: true };
|
|
288
331
|
}
|
|
289
332
|
if (env.command !== 'build')
|
|
290
333
|
return config;
|
|
@@ -343,6 +386,8 @@ export default function clarusjs(options = {}) {
|
|
|
343
386
|
}
|
|
344
387
|
},
|
|
345
388
|
configResolved(config) {
|
|
389
|
+
if (envWarning)
|
|
390
|
+
config.logger.warn(envWarning);
|
|
346
391
|
root = config.root;
|
|
347
392
|
command = config.command;
|
|
348
393
|
base = config.base;
|
|
@@ -362,6 +407,8 @@ export default function clarusjs(options = {}) {
|
|
|
362
407
|
if (watching)
|
|
363
408
|
client.config.build.watch = ssr.config.build.watch = null;
|
|
364
409
|
const once = async () => {
|
|
410
|
+
if (options.headers)
|
|
411
|
+
checkHeaders(options.headers);
|
|
365
412
|
checkRedirects(options.redirects ?? {}, files.pages(builder.config.root).map((f) => routeFromFile(`/${relative(builder.config.root, f).split(sep).join('/')}`)));
|
|
366
413
|
await builder.build(client);
|
|
367
414
|
await builder.build(ssr);
|
|
@@ -371,10 +418,11 @@ export default function clarusjs(options = {}) {
|
|
|
371
418
|
const manifestFile = resolve(dist, 'clarusjs-manifest.json');
|
|
372
419
|
rmSync(manifestFile, { force: true });
|
|
373
420
|
const { routes, readers, loaded, server } = await prerender(entry, out, manifestFile);
|
|
374
|
-
|
|
421
|
+
const parts = routeParts(builder.config.root, files);
|
|
422
|
+
writeManifest(dist, routes, builder.config.base, loaded, server, parts);
|
|
375
423
|
if (readers.length) {
|
|
376
424
|
await prerender(entry, out, manifestFile, readers);
|
|
377
|
-
writeManifest(dist, routes, builder.config.base, loaded, server);
|
|
425
|
+
writeManifest(dist, routes, builder.config.base, loaded, server, parts);
|
|
378
426
|
}
|
|
379
427
|
const pageRoutes = routes.filter((r) => !/\/(__data\.json|index\.frag\.html)$/.test(r));
|
|
380
428
|
const clientRoutes = pageRoutes.filter((r) => routes.includes(r === '/' ? '/__data.json' : `${r}/__data.json`));
|
|
@@ -410,6 +458,8 @@ export default function clarusjs(options = {}) {
|
|
|
410
458
|
writeFileSync(resolve(out, routeToFile('/500')), html);
|
|
411
459
|
writeFileSync(resolve(out, '500.html'), html);
|
|
412
460
|
}
|
|
461
|
+
if (options.headers !== false)
|
|
462
|
+
writeHeaders(out, options.headers ?? {}, basePath(builder.config.base).replace(/\/$/, ''), builder.config.build.assetsDir);
|
|
413
463
|
if (options.redirects) {
|
|
414
464
|
writeRedirects(out, options.redirects, basePath(builder.config.base).replace(/\/$/, ''), routes);
|
|
415
465
|
const moved = Object.keys(options.redirects).map((from) => (from.length > 1 ? from.replace(/\/+$/, '') : from));
|
|
@@ -495,10 +545,14 @@ export default function clarusjs(options = {}) {
|
|
|
495
545
|
writeFileSync(resolve(out, 'robots.txt'), `User-agent: *\nAllow: /\n${sitemap}`);
|
|
496
546
|
}
|
|
497
547
|
if (options.adapter === 'netlify')
|
|
498
|
-
writeNetlifyFunction(builder.config.root, entry, server.map((s) => s.route), options.fragments === true);
|
|
548
|
+
writeNetlifyFunction(builder.config.root, entry, server.map((s) => s.route), options.fragments === true, out);
|
|
499
549
|
rmSync(resolve(out, '.vite'), { recursive: true, force: true });
|
|
500
550
|
if (options.compress)
|
|
501
551
|
precompress(out);
|
|
552
|
+
if (options.adapter === 'vercel')
|
|
553
|
+
writeVercelOutput(builder.config.root, out, dirname(entry), server.map((s) => s.route), basePath(builder.config.base), options.fragments === true);
|
|
554
|
+
if (options.adapter === 'cloudflare')
|
|
555
|
+
writeCloudflareWorker(out, dirname(entry), server.map((s) => s.route), basePath(builder.config.base), options.fragments === true);
|
|
502
556
|
};
|
|
503
557
|
await once();
|
|
504
558
|
if (watching)
|
|
@@ -568,7 +622,7 @@ export default function clarusjs(options = {}) {
|
|
|
568
622
|
` loaderUrl: ${JSON.stringify(loaderUrl)},\n` +
|
|
569
623
|
` data: import.meta.glob('/src/pages/**/*.load.ts'),\n` +
|
|
570
624
|
` componentLoaders: ${JSON.stringify(componentLoaders)},\n` +
|
|
571
|
-
` componentData: import.meta.glob(['/src/**/*.load.ts', '!/src/pages/**']),\n` +
|
|
625
|
+
` componentData: { ...import.meta.glob(['/src/**/*.load.ts', '!/src/pages/**']), ...import.meta.glob('/src/pages/**/+layout.load.ts') },\n` +
|
|
572
626
|
` guards: import.meta.glob('/src/pages/**/+guard.ts'),\n` +
|
|
573
627
|
` endpoints: import.meta.glob(['/src/pages/**/*.ts', '!/src/pages/**/*.load.ts', '!/src/pages/**/*.d.ts', '!/src/pages/**/+*']),\n` +
|
|
574
628
|
` modes: ${JSON.stringify(modes)},\n` +
|
|
@@ -646,6 +700,7 @@ export default function clarusjs(options = {}) {
|
|
|
646
700
|
configurePreviewServer(server) {
|
|
647
701
|
const outDir = resolve(server.config.root, server.config.build.outDir);
|
|
648
702
|
const base = server.config.base;
|
|
703
|
+
const rules = readHeaders(outDir);
|
|
649
704
|
server.middlewares.use((req, res, next) => {
|
|
650
705
|
let url;
|
|
651
706
|
try {
|
|
@@ -663,6 +718,8 @@ export default function clarusjs(options = {}) {
|
|
|
663
718
|
req.url = base + file.slice(outDir.length + 1).replace(/\\/g, '/');
|
|
664
719
|
precompressed(req, res, outDir, base);
|
|
665
720
|
}
|
|
721
|
+
for (const [name, value] of headersFor(rules, canonical(url)))
|
|
722
|
+
res.setHeader(name, value);
|
|
666
723
|
next();
|
|
667
724
|
});
|
|
668
725
|
return () => {
|
|
@@ -706,6 +763,24 @@ export default function clarusjs(options = {}) {
|
|
|
706
763
|
server.watcher.on('change', restyle);
|
|
707
764
|
server.watcher.on('add', restyle);
|
|
708
765
|
server.watcher.on('unlink', restyle);
|
|
766
|
+
const relayout = (file) => {
|
|
767
|
+
const folder = dirname(normalizePath(file));
|
|
768
|
+
if (!/\/\+layout\.tscl$/.test(normalizePath(file)) || !(folder + '/').startsWith(normalizePath(resolve(server.config.root, 'src/pages')) + '/'))
|
|
769
|
+
return;
|
|
770
|
+
const pages = files.pages(server.config.root).filter((p) => normalizePath(p).startsWith(folder + '/'));
|
|
771
|
+
for (const env of Object.values(server.environments)) {
|
|
772
|
+
for (const page of pages) {
|
|
773
|
+
for (const mod of env.moduleGraph.getModulesByFile(normalizePath(page)) ?? [])
|
|
774
|
+
env.moduleGraph.invalidateModule(mod);
|
|
775
|
+
if (isRunnableDevEnvironment(env))
|
|
776
|
+
for (const mod of env.runner.evaluatedModules.getModulesByFile(normalizePath(page)) ?? [])
|
|
777
|
+
env.runner.evaluatedModules.invalidateModule(mod);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
server.environments.client.hot.send({ type: 'full-reload' });
|
|
781
|
+
};
|
|
782
|
+
server.watcher.on('add', relayout);
|
|
783
|
+
server.watcher.on('unlink', relayout);
|
|
709
784
|
return () => {
|
|
710
785
|
server.middlewares.use(async (req, res, next) => {
|
|
711
786
|
try {
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** '/account/[id]' under '/docs' → '^/docs/account/[^/]+/?$' (and its fragment, with fragments on). */
|
|
2
|
+
export declare function vercelSource(route: string, prefix: string, fragments?: boolean): string;
|
|
3
|
+
export declare function writeVercelOutput(root: string, client: string, server: string, serverRoutes: string[], base: string, fragments?: boolean): void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { cpSync, existsSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { readHeaders } from './headers.js';
|
|
4
|
+
const FUNCTION = 'clarusjs-server';
|
|
5
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
6
|
+
export function vercelSource(route, prefix, fragments = false) {
|
|
7
|
+
const path = route === '/' ? '' : route.split('/').map((seg) => (/^\[[^\]]+\]$/.test(seg) ? '[^/]+' : escape(seg))).join('/');
|
|
8
|
+
return `^${escape(prefix)}${path}/?${fragments ? '(?:index\\.frag\\.html)?' : ''}$`;
|
|
9
|
+
}
|
|
10
|
+
export function writeVercelOutput(root, client, server, serverRoutes, base, fragments = false) {
|
|
11
|
+
const prefix = base.replace(/\/$/, '');
|
|
12
|
+
const output = resolve(root, '.vercel/output');
|
|
13
|
+
rmSync(output, { recursive: true, force: true });
|
|
14
|
+
const hostFiles = new Set(['_headers', '_redirects']);
|
|
15
|
+
cpSync(client, resolve(output, 'static', `.${prefix}`), { recursive: true, filter: (src) => !hostFiles.has(src.slice(client.length + 1)) && !/\.(br|gz)$/.test(src) });
|
|
16
|
+
const overrides = {};
|
|
17
|
+
for (const file of readdirSync(client, { recursive: true }).map(String).map((f) => f.split('\\').join('/'))) {
|
|
18
|
+
if (file !== 'index.html' && !file.endsWith('/index.html'))
|
|
19
|
+
continue;
|
|
20
|
+
overrides[`${prefix.slice(1)}${prefix ? '/' : ''}${file}`] = { path: `${prefix.slice(1)}${prefix && file !== 'index.html' ? '/' : ''}${file.replace(/\/?index\.html$/, '')}` };
|
|
21
|
+
}
|
|
22
|
+
const routes = [
|
|
23
|
+
...readHeaders(client).map((rule) => ({ src: rule.regex.source, headers: Object.fromEntries(rule.headers), continue: true })),
|
|
24
|
+
...(existsSync(resolve(client, '_redirects')) ? readFileSync(resolve(client, '_redirects'), 'utf8').split('\n') : [])
|
|
25
|
+
.map((line) => line.trim().split(/\s+/))
|
|
26
|
+
.filter((parts) => parts.length >= 2 && parts[0].startsWith('/'))
|
|
27
|
+
.map(([from, to, status]) => ({ src: `^${escape(from.replace(/\/$/, '') || '/')}/?$`, status: Number(status ?? 301), headers: { Location: to } })),
|
|
28
|
+
{ src: '^/(.+)/$', dest: '/$1', continue: true },
|
|
29
|
+
{ handle: 'filesystem' },
|
|
30
|
+
...serverRoutes.map((route) => ({ src: vercelSource(route, prefix, fragments), dest: `/${FUNCTION}` })),
|
|
31
|
+
...(existsSync(resolve(client, '404.html')) ? [{ handle: 'error' }, { src: '.*', status: 404, dest: `${prefix}/404.html` }] : []),
|
|
32
|
+
];
|
|
33
|
+
writeFileSync(resolve(output, 'config.json'), JSON.stringify({ version: 3, routes, overrides }, null, 2));
|
|
34
|
+
if (!serverRoutes.length)
|
|
35
|
+
return;
|
|
36
|
+
const func = resolve(output, `functions/${FUNCTION}.func`);
|
|
37
|
+
cpSync(server, resolve(func, 'server'), { recursive: true });
|
|
38
|
+
writeFileSync(resolve(func, 'index.mjs'), [
|
|
39
|
+
'// Generated by clarusjs: server routes on Vercel. The handler\'s routes are base-free.',
|
|
40
|
+
"import handler from './server/index.mjs'",
|
|
41
|
+
`const base = ${JSON.stringify(prefix)}`,
|
|
42
|
+
'export default {',
|
|
43
|
+
' fetch(request) {',
|
|
44
|
+
' const url = new URL(request.url)',
|
|
45
|
+
" if (base && url.pathname.startsWith(base)) url.pathname = url.pathname.slice(base.length) || '/'",
|
|
46
|
+
' return handler.fetch(base ? new Request(url, request) : request)',
|
|
47
|
+
' },',
|
|
48
|
+
'}',
|
|
49
|
+
'',
|
|
50
|
+
].join('\n'));
|
|
51
|
+
writeFileSync(resolve(func, '.vc-config.json'), JSON.stringify({ runtime: 'nodejs22.x', handler: 'index.mjs', launcherType: 'Nodejs', supportsResponseStreaming: true }, null, 2));
|
|
52
|
+
writeFileSync(resolve(func, 'package.json'), JSON.stringify({ type: 'module' }));
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clarusjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "Clear, compiled views: templates compiled to server HTML, with JavaScript only for interactive islands. SSR, SSG and CSR from one component model, built on Vite.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -82,6 +82,18 @@
|
|
|
82
82
|
"./views": {
|
|
83
83
|
"types": "./dist/server/views.d.ts",
|
|
84
84
|
"default": "./dist/server/views.js"
|
|
85
|
+
},
|
|
86
|
+
"./node": {
|
|
87
|
+
"types": "./dist/node/serve.d.ts",
|
|
88
|
+
"default": "./dist/node/serve.js"
|
|
89
|
+
},
|
|
90
|
+
"./seo": {
|
|
91
|
+
"types": "./dist/seo.d.ts",
|
|
92
|
+
"default": "./dist/seo.js"
|
|
93
|
+
},
|
|
94
|
+
"./motion": {
|
|
95
|
+
"types": "./dist/runtime/motion.d.ts",
|
|
96
|
+
"default": "./dist/runtime/motion.js"
|
|
85
97
|
}
|
|
86
98
|
},
|
|
87
99
|
"bin": {
|
|
@@ -106,6 +118,7 @@
|
|
|
106
118
|
"@tailwindcss/vite": "^4.3.5",
|
|
107
119
|
"@types/node": "^26.6.6",
|
|
108
120
|
"happy-dom": "^21.0.4",
|
|
121
|
+
"miniflare": "^4.20260730.0",
|
|
109
122
|
"tailwindcss": "^4.3.5",
|
|
110
123
|
"typescript": "^7.0.2",
|
|
111
124
|
"typescript-beta": "npm:typescript@7.1.0-beta",
|