clarusjs 0.6.0 → 0.8.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/README.md +2 -0
- package/dist/check/generate.js +1 -21
- package/dist/cli/check.js +44 -11
- package/dist/cli/explain.js +0 -1
- package/dist/cli/index.js +0 -1
- package/dist/compiler/emit-client.js +1 -11
- package/dist/compiler/emit-server.js +1 -2
- package/dist/compiler/errors.js +0 -1
- package/dist/compiler/html-expr.js +2 -7
- package/dist/compiler/index.d.ts +6 -0
- package/dist/compiler/index.js +45 -4
- package/dist/compiler/ir.d.ts +1 -0
- package/dist/compiler/ir.js +9 -14
- package/dist/compiler/parse.d.ts +10 -0
- package/dist/compiler/parse.js +48 -20
- package/dist/compiler/script.js +2 -31
- package/dist/compiler/shared.js +0 -5
- package/dist/runtime/fragments.js +23 -3
- package/dist/runtime/html.d.ts +2 -0
- package/dist/runtime/html.js +4 -0
- package/dist/runtime/loader.js +46 -3
- package/dist/runtime/router.d.ts +1 -1
- package/dist/runtime/router.js +23 -15
- package/dist/runtime/server.d.ts +1 -1
- package/dist/runtime/server.js +1 -1
- package/dist/server/handler.d.ts +15 -1
- package/dist/server/handler.js +61 -52
- package/dist/server/head.d.ts +36 -0
- package/dist/server/head.js +129 -0
- package/dist/server/manifest.js +0 -3
- package/dist/server/modes.js +0 -5
- package/dist/server/views.js +1 -8
- package/dist/vite/build.d.ts +10 -2
- package/dist/vite/build.js +44 -19
- package/dist/vite/links.d.ts +20 -0
- package/dist/vite/links.js +94 -0
- package/dist/vite/manifest.js +2 -10
- package/dist/vite/netlify.js +1 -7
- package/dist/vite/plugin.d.ts +55 -0
- package/dist/vite/plugin.js +282 -82
- package/dist/vite/sitemap.d.ts +11 -0
- package/dist/vite/sitemap.js +31 -0
- package/package.json +5 -3
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { routeToFile } from './build.js';
|
|
4
|
+
const decode = (s) => { try {
|
|
5
|
+
return decodeURIComponent(s);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return s;
|
|
9
|
+
} };
|
|
10
|
+
const isFile = (path) => { try {
|
|
11
|
+
return statSync(path).isFile();
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
} };
|
|
16
|
+
const trim = (path) => (path.length > 1 ? path.replace(/\/+$/, '') : path);
|
|
17
|
+
function distance(a, b) {
|
|
18
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
19
|
+
for (let i = 1; i <= a.length; i++) {
|
|
20
|
+
let prev = row[0];
|
|
21
|
+
row[0] = i;
|
|
22
|
+
for (let j = 1; j <= b.length; j++) {
|
|
23
|
+
const next = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
24
|
+
prev = row[j];
|
|
25
|
+
row[j] = next;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return row[b.length];
|
|
29
|
+
}
|
|
30
|
+
function closest(target, candidates) {
|
|
31
|
+
let best;
|
|
32
|
+
let score = Infinity;
|
|
33
|
+
for (const c of candidates) {
|
|
34
|
+
const d = distance(target, c);
|
|
35
|
+
if (d < score) {
|
|
36
|
+
best = c;
|
|
37
|
+
score = d;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return best !== undefined && score <= Math.max(2, Math.floor(target.length / 3)) ? best : undefined;
|
|
41
|
+
}
|
|
42
|
+
const ids = (html) => new Set([...html.matchAll(/\s(?:id|name)="([^"]+)"/g)].map((m) => m[1]));
|
|
43
|
+
const hrefs = (html) => [...html.replace(/<!--[\s\S]*?-->/g, '').matchAll(/<a\b[^>]*?\shref="([^"]*)"/g)].map((m) => m[1].replace(/&/g, '&'));
|
|
44
|
+
export function checkLinks(options) {
|
|
45
|
+
const { outDir } = options;
|
|
46
|
+
const base = /^https?:\/\//.test(options.base) ? new URL(options.base).pathname : options.base;
|
|
47
|
+
const siteRoot = options.site ? new URL(base, options.site.replace(/\/?$/, '/')) : undefined;
|
|
48
|
+
const routes = new Set(options.routes.map(trim));
|
|
49
|
+
const client = new Set(options.clientRoutes.map(trim));
|
|
50
|
+
const serverPatterns = options.serverRoutes.map((r) => new RegExp(`^${r.split('/').map((seg) => (/^\[[^\]]+\]$/.test(seg) ? '[^/]+' : seg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))).join('/')}$`));
|
|
51
|
+
const html = new Map();
|
|
52
|
+
const read = (route) => {
|
|
53
|
+
if (!html.has(route)) {
|
|
54
|
+
const file = resolve(outDir, routeToFile(route));
|
|
55
|
+
html.set(route, existsSync(file) ? readFileSync(file, 'utf8') : '');
|
|
56
|
+
}
|
|
57
|
+
return html.get(route);
|
|
58
|
+
};
|
|
59
|
+
const issues = [];
|
|
60
|
+
for (const page of [...routes].filter((r) => !client.has(r))) {
|
|
61
|
+
for (let href of hrefs(read(page))) {
|
|
62
|
+
if (siteRoot && /^https?:/i.test(href)) {
|
|
63
|
+
const u = new URL(href);
|
|
64
|
+
if (u.origin === siteRoot.origin && (u.pathname.startsWith(siteRoot.pathname) || `${u.pathname}/` === siteRoot.pathname)) {
|
|
65
|
+
href = base + u.pathname.slice(Math.min(u.pathname.length, siteRoot.pathname.length)) + u.search + u.hash;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (/^[a-z][\w+.-]*:|^\/\//i.test(href) || href === '')
|
|
69
|
+
continue;
|
|
70
|
+
const url = new URL(href, `http://site${base}${page === '/' ? '' : page.slice(1) + '/'}`);
|
|
71
|
+
if (!url.pathname.startsWith(base) && `${url.pathname}/` !== base)
|
|
72
|
+
continue;
|
|
73
|
+
let path = trim(decode(url.pathname.slice(base.length - 1)) || '/');
|
|
74
|
+
if (path.endsWith('/index.html'))
|
|
75
|
+
path = trim(path.slice(0, -'index.html'.length));
|
|
76
|
+
const anchor = decode(url.hash.slice(1));
|
|
77
|
+
const isPage = routes.has(path);
|
|
78
|
+
const exists = isPage || serverPatterns.some((re) => re.test(path)) || isFile(resolve(outDir, `.${path}`));
|
|
79
|
+
if (!exists) {
|
|
80
|
+
const guess = closest(path, routes);
|
|
81
|
+
issues.push({ kind: 'pages', page, href, message: `no page at ${path}${guess ? ` (did you mean ${guess}?)` : ''}` });
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (!anchor || !isPage || client.has(path))
|
|
85
|
+
continue;
|
|
86
|
+
const targets = ids(read(path));
|
|
87
|
+
if (targets.has(anchor))
|
|
88
|
+
continue;
|
|
89
|
+
const guess = closest(anchor, targets);
|
|
90
|
+
issues.push({ kind: 'anchors', page, href, message: `no #${anchor} on ${path}${guess ? ` (did you mean #${guess}?)` : ''}` });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return issues;
|
|
94
|
+
}
|
package/dist/vite/manifest.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// The route manifest: after `vite build`, what each page ships, read back from its prerendered HTML.
|
|
2
|
-
// dist/clarusjs-manifest.json is what `clarusjs explain` prints. Written outside dist/client, so it isn't served.
|
|
3
1
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
4
2
|
import { resolve } from 'node:path';
|
|
5
3
|
import { gzipSync } from 'node:zlib';
|
|
@@ -11,12 +9,10 @@ const attrsOf = (tag) => {
|
|
|
11
9
|
out[m[1]] = m[2].replace(/&/g, '&');
|
|
12
10
|
return out;
|
|
13
11
|
};
|
|
14
|
-
/** What one prerendered page ships. `size` returns a built file's gzip size. */
|
|
15
12
|
export function describeRoute(route, html, opts) {
|
|
16
13
|
const local = (url) => (url.startsWith(opts.base) ? url.slice(opts.base.length) : url.replace(/^\//, ''));
|
|
17
14
|
const byFile = new Map(Object.values(opts.chunks).map((c) => [c.file, c]));
|
|
18
15
|
const keyFile = new Map(Object.entries(opts.chunks).map(([k, c]) => [k, c.file]));
|
|
19
|
-
// A chunk and everything it imports, in load order.
|
|
20
16
|
const closure = (file, into) => {
|
|
21
17
|
if (into.includes(file))
|
|
22
18
|
return;
|
|
@@ -41,8 +37,7 @@ export function describeRoute(route, html, opts) {
|
|
|
41
37
|
closure(local(m[1]), all);
|
|
42
38
|
closure(local(m[1]), initial);
|
|
43
39
|
}
|
|
44
|
-
|
|
45
|
-
const table = /<script type="application\/json" id="cl-routes">(.*?)<\/script>/.exec(html)?.[1];
|
|
40
|
+
const table = /<script type="application\/json" id="cl-routes"[^>]*>(.*?)<\/script>/.exec(html)?.[1];
|
|
46
41
|
const render = opts.client && table ? matchRoute(JSON.parse(table), route)?.r : undefined;
|
|
47
42
|
if (render) {
|
|
48
43
|
closure(local(render), all);
|
|
@@ -57,7 +52,6 @@ export function describeRoute(route, html, opts) {
|
|
|
57
52
|
const css = [...html.matchAll(/<link\b[^>]*rel="stylesheet"[^>]*>/g)].map((m) => attrsOf(m[0]).href).filter(Boolean);
|
|
58
53
|
return { mode: opts.client ? 'client' : 'static', html: routeToFile(route), loader: opts.loaded ? 'build' : null, islands, js: { files: all, gzip: sum(all), initialGzip: sum(initial) }, css };
|
|
59
54
|
}
|
|
60
|
-
/** Describe every prerendered route and write `<distDir>/clarusjs-manifest.json`. */
|
|
61
55
|
export function writeManifest(distDir, routes, base, loaded = [], server = []) {
|
|
62
56
|
const client = resolve(distDir, 'client');
|
|
63
57
|
const chunks = JSON.parse(readFileSync(resolve(client, '.vite/manifest.json'), 'utf8'));
|
|
@@ -71,13 +65,11 @@ export function writeManifest(distDir, routes, base, loaded = [], server = []) {
|
|
|
71
65
|
const dataFile = (route) => (route === '/' ? '/__data.json' : `${route}/__data.json`);
|
|
72
66
|
for (const route of routes) {
|
|
73
67
|
if (route.endsWith('/__data.json') || route.endsWith('/index.frag.html'))
|
|
74
|
-
continue;
|
|
68
|
+
continue;
|
|
75
69
|
const isClient = routes.includes(dataFile(route));
|
|
76
|
-
// A client route's loader runs when its data file is written.
|
|
77
70
|
const ran = loaded.includes(route) || (isClient && loaded.includes(dataFile(route)));
|
|
78
71
|
manifest.routes[route] = describeRoute(route, readFileSync(resolve(client, routeToFile(route)), 'utf8'), { base, chunks, size, loaded: ran, client: isClient });
|
|
79
72
|
}
|
|
80
|
-
// Server routes have no HTML at build time: what they ship is decided per request.
|
|
81
73
|
for (const { route, loader } of server) {
|
|
82
74
|
manifest.routes[route] = { mode: 'server', html: null, loader: loader ? 'request' : null, islands: [], js: { files: [], gzip: 0, initialGzip: 0 }, css: [] };
|
|
83
75
|
}
|
package/dist/vite/netlify.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
// Netlify adapter: server routes run as one Netlify Function (Frameworks API) calling the same fetch handler.
|
|
2
|
-
// Static routes stay files; preferStatic makes Netlify serve those first.
|
|
3
1
|
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
2
|
import { dirname, relative, resolve, sep } from 'node:path';
|
|
5
3
|
const FUNCTION = '.netlify/v1/functions/clarusjs-server.mjs';
|
|
6
|
-
/** '/account/[id]' → ['/account/:id', '/account/:id/'] (Netlify paths use URLPattern syntax). */
|
|
7
4
|
export function netlifyPaths(route, fragments = false) {
|
|
8
5
|
const segments = route.split('/');
|
|
9
6
|
const keyOf = (seg) => /^\[([A-Za-z_$][\w$]*)\]$/.exec(seg)?.[1];
|
|
10
7
|
const valid = (key) => /^[A-Za-z_]\w*$/.test(key);
|
|
11
|
-
// Names already valid in URLPattern are kept; others get p0, p1, … that aren't taken (URLPattern rejects duplicates).
|
|
12
8
|
const taken = new Set(segments.map(keyOf).filter((k) => k !== undefined && valid(k)));
|
|
13
9
|
let n = 0;
|
|
14
10
|
const fresh = () => {
|
|
@@ -21,15 +17,13 @@ export function netlifyPaths(route, fragments = false) {
|
|
|
21
17
|
const key = keyOf(seg);
|
|
22
18
|
if (key !== undefined)
|
|
23
19
|
return `:${valid(key) ? key : fresh()}`;
|
|
24
|
-
return seg.replace(/[:*?+(){}\\]/g, '\\$&');
|
|
20
|
+
return seg.replace(/[:*?+(){}\\]/g, '\\$&');
|
|
25
21
|
}).join('/');
|
|
26
22
|
const paths = path === '/' || path === '' ? ['/'] : [path, `${path}/`];
|
|
27
|
-
// The page's fragment, for fragment navigation, comes from the same function.
|
|
28
23
|
if (fragments)
|
|
29
24
|
paths.push(path === '/' || path === '' ? '/index.frag.html' : `${path}/index.frag.html`);
|
|
30
25
|
return paths;
|
|
31
26
|
}
|
|
32
|
-
/** Write (or, without server routes, remove) the function in `<root>/.netlify/v1/functions`. */
|
|
33
27
|
export function writeNetlifyFunction(root, serverEntry, serverRoutes, fragments = false) {
|
|
34
28
|
const file = resolve(root, FUNCTION);
|
|
35
29
|
if (!serverRoutes.length) {
|
package/dist/vite/plugin.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { type Plugin } from 'vite';
|
|
2
|
+
export { packageOf } from './build.ts';
|
|
3
|
+
/** Newest commit time (unix seconds) of every file under a folder, from one `git log` (themes use it for "last updated"). */
|
|
4
|
+
export { gitDates } from './sitemap.ts';
|
|
2
5
|
import { type ModeTable } from '../server/modes.ts';
|
|
6
|
+
import type { HeadTag, SiteOptions } from '../server/head.ts';
|
|
3
7
|
/** Dev URL of a component's client module: root-relative, or Vite's /@fs/ form for files outside the root. */
|
|
4
8
|
export declare function clientUrlFor(root: string, file: string): string;
|
|
5
9
|
export type ClarusOptions = {
|
|
@@ -9,5 +13,56 @@ export type ClarusOptions = {
|
|
|
9
13
|
adapter?: 'netlify';
|
|
10
14
|
/** Fragment navigation: each static and server page also answers at <path>/index.frag.html. */
|
|
11
15
|
fragments?: boolean;
|
|
16
|
+
/** Page sources for other file types, such as Markdown from @clarusjs/content: `sources: [markdown()]`. */
|
|
17
|
+
sources?: Source[];
|
|
18
|
+
/**
|
|
19
|
+
* Build-time link checking, after every page is built. `pages`: links to pages or files that don't exist (default
|
|
20
|
+
* 'error'). `anchors`: links to a #id the target page doesn't have (default 'warn'). `strict`: warnings fail too.
|
|
21
|
+
*/
|
|
22
|
+
links?: {
|
|
23
|
+
pages?: LinkLevel;
|
|
24
|
+
anchors?: LinkLevel;
|
|
25
|
+
strict?: boolean;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Every page's <head>: `titleTemplate` ('%s · Site') wraps the document title, `tags` are added unless the page has
|
|
29
|
+
* its own with the same key. Anything else: export `transformHead(head, page)` from src/head.ts.
|
|
30
|
+
*/
|
|
31
|
+
head?: {
|
|
32
|
+
titleTemplate?: string;
|
|
33
|
+
tags?: HeadTag[];
|
|
34
|
+
jsonLd?: object | object[];
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The site's public address. Every page gets a canonical URL and social cards (Open Graph, Twitter) from its title,
|
|
38
|
+
* description and an image (the page's og:image, or `image`); absolute links to the site are link-checked.
|
|
39
|
+
*/
|
|
40
|
+
site?: SiteOptions;
|
|
41
|
+
/**
|
|
42
|
+
* With site.url, vite build writes sitemap.xml (static pages, dated from git). false: no sitemap. `exclude`: paths
|
|
43
|
+
* left out ('/legal', '/tags/*' one segment, '/drafts/**' any depth). A page opts out itself with
|
|
44
|
+
* <cl-head><meta name="sitemap" content="false"></cl-head> (removed from the built page) or noindex.
|
|
45
|
+
*/
|
|
46
|
+
sitemap?: boolean | {
|
|
47
|
+
exclude?: string[];
|
|
48
|
+
};
|
|
49
|
+
/** With site.url, vite build writes robots.txt (allow all, and the sitemap), unless public/robots.txt exists. */
|
|
50
|
+
robots?: boolean;
|
|
51
|
+
};
|
|
52
|
+
export type { HeadTag };
|
|
53
|
+
export type LinkLevel = 'error' | 'warn' | 'off';
|
|
54
|
+
/**
|
|
55
|
+
* A page source: files with another extension (such as Markdown) turned into .tscl text, then compiled like any
|
|
56
|
+
* page. `route` is the page's route when the file is under src/pages.
|
|
57
|
+
*/
|
|
58
|
+
export type Source = {
|
|
59
|
+
extension: string;
|
|
60
|
+
toTscl(code: string, file: string, context: SourceContext): string;
|
|
61
|
+
};
|
|
62
|
+
/** `root`: the app root. `addDependency(file)`: another file the output depends on (rebuilt when it changes in dev). */
|
|
63
|
+
export type SourceContext = {
|
|
64
|
+
route: string;
|
|
65
|
+
root: string;
|
|
66
|
+
addDependency(file: string): void;
|
|
12
67
|
};
|
|
13
68
|
export default function clarusjs(options?: ClarusOptions): Plugin;
|