clarusjs 0.1.0 → 0.3.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 +61 -0
- package/dist/compiler/emit-client.js +88 -17
- package/dist/compiler/emit-server.js +34 -15
- package/dist/compiler/html-expr.d.ts +29 -0
- package/dist/compiler/html-expr.js +85 -0
- package/dist/compiler/ir.d.ts +41 -0
- package/dist/compiler/ir.js +153 -23
- package/dist/compiler/parse.d.ts +34 -0
- package/dist/compiler/parse.js +199 -4
- package/dist/compiler/script.d.ts +17 -2
- package/dist/compiler/script.js +66 -6
- package/dist/runtime/attrs.d.ts +5 -0
- package/dist/runtime/attrs.js +56 -0
- package/dist/runtime/dom.d.ts +61 -5
- package/dist/runtime/dom.js +257 -17
- package/dist/runtime/html.d.ts +10 -0
- package/dist/runtime/html.js +30 -0
- package/dist/runtime/index.d.ts +4 -2
- package/dist/runtime/index.js +4 -2
- package/dist/runtime/loader.d.ts +7 -2
- package/dist/runtime/loader.js +125 -28
- package/dist/runtime/serialize.d.ts +8 -0
- package/dist/runtime/serialize.js +93 -0
- package/dist/runtime/server.d.ts +9 -4
- package/dist/runtime/server.js +13 -24
- package/dist/runtime/signals.d.ts +12 -0
- package/dist/runtime/signals.js +44 -1
- package/dist/server/handler.d.ts +4 -1
- package/dist/server/handler.js +30 -5
- package/dist/vite/build.d.ts +9 -0
- package/dist/vite/build.js +55 -0
- package/dist/vite/plugin.js +130 -14
- package/package.json +3 -2
package/dist/vite/plugin.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// The clarusjs Vite plugin: a thin adapter around the compiler.
|
|
2
2
|
// Server environment gets render(), client environment gets attach(). Dev requests go through the fetch handler.
|
|
3
|
-
import {
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
+
import { isAbsolute, relative, resolve } from 'node:path';
|
|
4
5
|
import { fileURLToPath } from 'node:url';
|
|
5
6
|
import { isRunnableDevEnvironment } from 'vite';
|
|
6
7
|
import { compile, ClarusError } from '../compiler/index.js';
|
|
8
|
+
import { findClarusFiles, prerender } from './build.js';
|
|
7
9
|
import { sendResponse, toRequest } from './node.js';
|
|
8
10
|
// From src/ (workspace, tests) our siblings are .ts files; from the published dist/ they are .js.
|
|
9
11
|
const ext = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
|
|
@@ -14,20 +16,107 @@ export function clientUrlFor(root, file) {
|
|
|
14
16
|
const rel = relative(root, file);
|
|
15
17
|
return rel.startsWith('..') || isAbsolute(rel) ? `/@fs${file}?import` : `/${rel}?import`;
|
|
16
18
|
}
|
|
19
|
+
const LOADER_ENTRY = 'cl-loader';
|
|
20
|
+
const CLIENT_OUT = 'dist/client';
|
|
21
|
+
// Placeholder for a nested island's chunk URL in the client build, replaced once the bundle's file names are known.
|
|
22
|
+
const CHUNK_TOKEN = '__CLARUS_CHUNK__';
|
|
23
|
+
const SERVER_OUT = 'dist/server';
|
|
24
|
+
const entryName = (root, file) => relative(root, file).replace(/\.clarus$/, '');
|
|
17
25
|
export default function clarusjs() {
|
|
18
26
|
let root = process.cwd();
|
|
27
|
+
let command = 'serve';
|
|
28
|
+
let base = '/';
|
|
29
|
+
// `vite build` only: client entry name (see entryName) → public URL of its hashed chunk.
|
|
30
|
+
// Read from the client manifest on disk, because Vite resolves config (and so creates a new plugin instance) per
|
|
31
|
+
// environment: the server build cannot see state set during the client build.
|
|
32
|
+
let built;
|
|
33
|
+
const builtUrls = () => {
|
|
34
|
+
if (command !== 'build')
|
|
35
|
+
return undefined;
|
|
36
|
+
if (built)
|
|
37
|
+
return built;
|
|
38
|
+
const manifestFile = resolve(root, CLIENT_OUT, '.vite/manifest.json');
|
|
39
|
+
if (!existsSync(manifestFile)) {
|
|
40
|
+
throw new Error(`clarusjs: client manifest not found at ${manifestFile}; the server build must run after the client build`);
|
|
41
|
+
}
|
|
42
|
+
const manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
|
|
43
|
+
built = new Map();
|
|
44
|
+
for (const chunk of Object.values(manifest))
|
|
45
|
+
if (chunk.isEntry && chunk.name)
|
|
46
|
+
built.set(chunk.name, base + chunk.file);
|
|
47
|
+
return built;
|
|
48
|
+
};
|
|
19
49
|
return {
|
|
20
50
|
name: 'clarusjs',
|
|
21
|
-
config
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
51
|
+
config(userConfig, env) {
|
|
52
|
+
const config = {
|
|
53
|
+
appType: 'custom',
|
|
54
|
+
resolve: {
|
|
55
|
+
alias: [
|
|
56
|
+
{ find: 'clarusjs/runtime/server', replacement: here('../runtime/server') },
|
|
57
|
+
{ find: 'clarusjs/runtime', replacement: here('../runtime/index') },
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
if (env.isPreview)
|
|
62
|
+
config.build = { outDir: CLIENT_OUT }; // preview serves the static site
|
|
63
|
+
if (env.command !== 'build')
|
|
64
|
+
return config;
|
|
65
|
+
if (userConfig.base !== undefined && !userConfig.base.startsWith('/') && !/^https?:\/\//.test(userConfig.base)) {
|
|
66
|
+
// Pages live at different depths and islands are imported from the loader, so URLs must not be relative.
|
|
67
|
+
throw new Error(`clarusjs: relative base "${userConfig.base}" is not supported; use an absolute base such as "/" or "/docs/"`);
|
|
68
|
+
}
|
|
69
|
+
// Client entries: the island loader plus every stateful component. Stateless components never reach the client.
|
|
70
|
+
const appRoot = resolve(userConfig.root ?? process.cwd());
|
|
71
|
+
const input = { [LOADER_ENTRY]: here('../runtime/loader') };
|
|
72
|
+
for (const file of findClarusFiles(resolve(appRoot, 'src'))) {
|
|
73
|
+
if (compile(readFileSync(file, 'utf8'), { file, target: 'client' }).stateful)
|
|
74
|
+
input[entryName(appRoot, file)] = file;
|
|
75
|
+
}
|
|
76
|
+
config.builder = {};
|
|
77
|
+
config.environments = {
|
|
78
|
+
// Island chunks are loaded with import() and must keep their `attach` export.
|
|
79
|
+
client: { build: { outDir: CLIENT_OUT, manifest: true, rolldownOptions: { input, preserveEntrySignatures: 'exports-only' } } },
|
|
80
|
+
// Always .mjs, so Node loads it as ESM whatever the app's package.json says.
|
|
81
|
+
ssr: { build: { outDir: SERVER_OUT, rolldownOptions: { input: { index: ENTRY }, output: { entryFileNames: '[name].mjs' } } } },
|
|
82
|
+
};
|
|
83
|
+
return config;
|
|
84
|
+
},
|
|
85
|
+
// Each build re-reads the client manifest: a builder can run buildApp() more than once.
|
|
86
|
+
buildStart() { built = undefined; },
|
|
87
|
+
// Client build: nested islands rendered in the browser get their chunk's real URL.
|
|
88
|
+
generateBundle(_options, bundle) {
|
|
89
|
+
if (this.environment.config.consumer !== 'client' || command !== 'build')
|
|
90
|
+
return;
|
|
91
|
+
const byName = new Map();
|
|
92
|
+
for (const out of Object.values(bundle))
|
|
93
|
+
if (out.type === 'chunk' && out.isEntry)
|
|
94
|
+
byName.set(out.name, base + out.fileName);
|
|
95
|
+
for (const out of Object.values(bundle)) {
|
|
96
|
+
if (out.type !== 'chunk' || !out.code.includes(CHUNK_TOKEN))
|
|
97
|
+
continue;
|
|
98
|
+
out.code = out.code.replace(/__CLARUS_CHUNK__\[(.+?)\]__/g, (_, name) => {
|
|
99
|
+
const url = byName.get(name);
|
|
100
|
+
if (!url)
|
|
101
|
+
this.error(`clarusjs: no client chunk for ${name}.clarus (stateful components must be under src/)`);
|
|
102
|
+
return url;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
configResolved(config) {
|
|
107
|
+
root = config.root;
|
|
108
|
+
command = config.command;
|
|
109
|
+
base = config.base;
|
|
110
|
+
},
|
|
111
|
+
// Order matters: the server build needs the client's hashed URLs, and prerendering needs the server build.
|
|
112
|
+
async buildApp(builder) {
|
|
113
|
+
const { client, ssr } = builder.environments;
|
|
114
|
+
if (!client || !ssr)
|
|
115
|
+
return;
|
|
116
|
+
await builder.build(client);
|
|
117
|
+
await builder.build(ssr);
|
|
118
|
+
await prerender(resolve(builder.config.root, SERVER_OUT, 'index.mjs'), resolve(builder.config.root, CLIENT_OUT));
|
|
119
|
+
},
|
|
31
120
|
resolveId(id) {
|
|
32
121
|
if (id === '/cl-loader.js')
|
|
33
122
|
return here('../runtime/loader');
|
|
@@ -37,16 +126,27 @@ export default function clarusjs() {
|
|
|
37
126
|
load(id) {
|
|
38
127
|
if (id !== '\0' + ENTRY)
|
|
39
128
|
return;
|
|
129
|
+
const loaderUrl = builtUrls()?.get(LOADER_ENTRY) ?? '/cl-loader.js';
|
|
40
130
|
return `import { createHandler } from ${JSON.stringify(here('../server/handler'))}\n` +
|
|
41
|
-
`export default createHandler(import.meta.glob('/src/pages/**/*.clarus'))\n`;
|
|
131
|
+
`export default createHandler(import.meta.glob('/src/pages/**/*.clarus'), { loaderUrl: ${JSON.stringify(loaderUrl)} })\n`;
|
|
42
132
|
},
|
|
43
133
|
transform: {
|
|
44
134
|
filter: { id: /\.clarus(\?|$)/ },
|
|
45
135
|
handler(code, id) {
|
|
46
136
|
const file = id.replace(/\?.*$/, '');
|
|
47
|
-
|
|
137
|
+
// ?render: a component's server render function, used in the browser to render children of islands.
|
|
138
|
+
const target = this.environment.config.consumer === 'server' || /[?&]render\b/.test(id) ? 'server' : 'client';
|
|
48
139
|
// ponytail: ?import makes Vite's dev transform middleware serve .clarus as JS for the loader's dynamic import; build output uses hashed chunk URLs from the client manifest (stage 3)
|
|
49
|
-
const
|
|
140
|
+
const consumer = this.environment.config.consumer;
|
|
141
|
+
// Server build: chunk URLs come from the client manifest. Client build (a ?render child): the chunk doesn't
|
|
142
|
+
// exist yet, so write a placeholder that generateBundle swaps for the real file name. Dev: the source URL.
|
|
143
|
+
const urls = consumer === 'server' ? builtUrls() : undefined;
|
|
144
|
+
const clientUrl = urls
|
|
145
|
+
? urls.get(entryName(root, file))
|
|
146
|
+
: command === 'build' ? `${CHUNK_TOKEN}[${entryName(root, file)}]__` : clientUrlFor(root, file);
|
|
147
|
+
if (urls && !clientUrl && compile(code, { file, target: 'client' }).stateful) {
|
|
148
|
+
this.error(`${file}: stateful component is not under src/, so it has no client chunk`);
|
|
149
|
+
}
|
|
50
150
|
try {
|
|
51
151
|
return { code: compile(code, { file, target, clientUrl }).code, map: null };
|
|
52
152
|
}
|
|
@@ -57,6 +157,22 @@ export default function clarusjs() {
|
|
|
57
157
|
}
|
|
58
158
|
},
|
|
59
159
|
},
|
|
160
|
+
// The built site is static files: /guide/x is guide/x/index.html. appType 'custom' turns off Vite's own lookup.
|
|
161
|
+
configurePreviewServer(server) {
|
|
162
|
+
const outDir = resolve(server.config.root, server.config.build.outDir);
|
|
163
|
+
const base = server.config.base; // e.g. '/' or '/docs/'; Vite strips it after this middleware
|
|
164
|
+
server.middlewares.use((req, _res, next) => {
|
|
165
|
+
const url = decodeURIComponent((req.url ?? '/').split('?')[0]);
|
|
166
|
+
const inBase = url === base.slice(0, -1) ? '/' : url.startsWith(base) ? '/' + url.slice(base.length) : undefined;
|
|
167
|
+
if (inBase !== undefined) {
|
|
168
|
+
// A built page wins; anything else (assets, /x.css) falls through to Vite's static files.
|
|
169
|
+
const file = resolve(outDir, `.${inBase.replace(/\/?$/, '/')}index.html`);
|
|
170
|
+
if (file.startsWith(outDir) && existsSync(file))
|
|
171
|
+
req.url = base + file.slice(outDir.length + 1).replace(/\\/g, '/');
|
|
172
|
+
}
|
|
173
|
+
next();
|
|
174
|
+
});
|
|
175
|
+
},
|
|
60
176
|
configureServer(server) {
|
|
61
177
|
return () => {
|
|
62
178
|
server.middlewares.use(async (req, res, next) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clarusjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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": [
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
},
|
|
53
53
|
"files": [
|
|
54
54
|
"dist",
|
|
55
|
-
"LICENSE"
|
|
55
|
+
"LICENSE",
|
|
56
|
+
"README.md"
|
|
56
57
|
],
|
|
57
58
|
"engines": {
|
|
58
59
|
"node": "^22.12.0 || ^24.0.0 || >=26.0.0"
|