clarusjs 0.3.0 → 0.5.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 +5 -5
- package/dist/check/generate.d.ts +27 -0
- package/dist/check/generate.js +234 -0
- package/dist/cli/check.d.ts +17 -0
- package/dist/cli/check.js +101 -0
- package/dist/cli/explain.d.ts +9 -0
- package/dist/cli/explain.js +56 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +38 -0
- package/dist/compiler/emit-client.js +2 -0
- package/dist/compiler/emit-server.d.ts +1 -1
- package/dist/compiler/emit-server.js +4 -2
- package/dist/compiler/errors.d.ts +1 -1
- package/dist/compiler/errors.js +1 -1
- package/dist/compiler/index.d.ts +5 -0
- package/dist/compiler/index.js +1 -1
- package/dist/compiler/ir.d.ts +2 -1
- package/dist/compiler/ir.js +14 -4
- package/dist/compiler/parse.d.ts +22 -0
- package/dist/compiler/parse.js +52 -14
- package/dist/compiler/script.d.ts +5 -1
- package/dist/compiler/script.js +2 -1
- package/dist/runtime/router.d.ts +15 -0
- package/dist/runtime/router.js +101 -0
- package/dist/server/handler.d.ts +25 -1
- package/dist/server/handler.js +170 -9
- package/dist/server/manifest.d.ts +4 -0
- package/dist/server/manifest.js +17 -0
- package/dist/server/modes.d.ts +6 -0
- package/dist/server/modes.js +28 -0
- package/dist/server/views.d.ts +17 -0
- package/dist/server/views.js +23 -0
- package/dist/vite/build.d.ts +18 -5
- package/dist/vite/build.js +49 -12
- package/dist/vite/manifest.d.ts +49 -0
- package/dist/vite/manifest.js +86 -0
- package/dist/vite/netlify.d.ts +4 -0
- package/dist/vite/netlify.js +46 -0
- package/dist/vite/plugin.d.ts +8 -1
- package/dist/vite/plugin.js +134 -20
- package/package.json +14 -2
package/dist/vite/plugin.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
1
9
|
// The clarusjs Vite plugin: a thin adapter around the compiler.
|
|
2
10
|
// Server environment gets render(), client environment gets attach(). Dev requests go through the fetch handler.
|
|
3
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
-
import { isAbsolute, relative, resolve } from 'node:path';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
11
|
+
import { existsSync, readFileSync, rmSync } from 'node:fs';
|
|
12
|
+
import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
13
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
14
|
import { isRunnableDevEnvironment } from 'vite';
|
|
7
15
|
import { compile, ClarusError } from '../compiler/index.js';
|
|
8
16
|
import { findClarusFiles, prerender } from './build.js';
|
|
17
|
+
import { writeManifest } from './manifest.js';
|
|
18
|
+
import { writeNetlifyFunction } from './netlify.js';
|
|
19
|
+
import { checkModes, modeFor } from '../server/modes.js';
|
|
20
|
+
import { routeFromFile } from '../server/handler.js';
|
|
9
21
|
import { sendResponse, toRequest } from './node.js';
|
|
10
22
|
// From src/ (workspace, tests) our siblings are .ts files; from the published dist/ they are .js.
|
|
11
23
|
const ext = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
|
|
@@ -17,12 +29,30 @@ export function clientUrlFor(root, file) {
|
|
|
17
29
|
return rel.startsWith('..') || isAbsolute(rel) ? `/@fs${file}?import` : `/${rel}?import`;
|
|
18
30
|
}
|
|
19
31
|
const LOADER_ENTRY = 'cl-loader';
|
|
32
|
+
const ROUTER_ENTRY = 'cl-router';
|
|
33
|
+
// Client entry name of a client-mode page's browser render module.
|
|
34
|
+
const renderEntry = (name) => `render/${name}`;
|
|
20
35
|
const CLIENT_OUT = 'dist/client';
|
|
21
36
|
// Placeholder for a nested island's chunk URL in the client build, replaced once the bundle's file names are known.
|
|
22
37
|
const CHUNK_TOKEN = '__CLARUS_CHUNK__';
|
|
23
38
|
const SERVER_OUT = 'dist/server';
|
|
24
|
-
const entryName = (root, file) => relative(root, file).replace(/\.
|
|
25
|
-
|
|
39
|
+
const entryName = (root, file) => relative(root, file).replace(/\.tscl$/, '');
|
|
40
|
+
/** Client-mode pages: route (as the file names it) → absolute file. */
|
|
41
|
+
function clientPages(appRoot, modes) {
|
|
42
|
+
const pages = new Map();
|
|
43
|
+
for (const file of findClarusFiles(resolve(appRoot, 'src/pages'))) {
|
|
44
|
+
const route = routeFromFile(`/${relative(appRoot, file).split(sep).join('/')}`);
|
|
45
|
+
if (modeFor(route, modes) === 'client')
|
|
46
|
+
pages.set(route, file);
|
|
47
|
+
}
|
|
48
|
+
return pages;
|
|
49
|
+
}
|
|
50
|
+
export default function clarusjs(options = {}) {
|
|
51
|
+
const modes = options.routes ?? {};
|
|
52
|
+
checkModes(modes);
|
|
53
|
+
if (options.adapter !== undefined && options.adapter !== 'netlify') {
|
|
54
|
+
throw new Error(`clarusjs: adapter "${options.adapter}" is not supported; use 'netlify'`);
|
|
55
|
+
}
|
|
26
56
|
let root = process.cwd();
|
|
27
57
|
let command = 'serve';
|
|
28
58
|
let base = '/';
|
|
@@ -41,9 +71,14 @@ export default function clarusjs() {
|
|
|
41
71
|
}
|
|
42
72
|
const manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
|
|
43
73
|
built = new Map();
|
|
44
|
-
|
|
45
|
-
|
|
74
|
+
// By entry name, and by source key: names are sanitized ([id] → _id_), keys keep the real path.
|
|
75
|
+
for (const [key, chunk] of Object.entries(manifest)) {
|
|
76
|
+
if (!chunk.isEntry)
|
|
77
|
+
continue;
|
|
78
|
+
if (chunk.name)
|
|
46
79
|
built.set(chunk.name, base + chunk.file);
|
|
80
|
+
built.set(key, base + chunk.file);
|
|
81
|
+
}
|
|
47
82
|
return built;
|
|
48
83
|
};
|
|
49
84
|
return {
|
|
@@ -73,6 +108,12 @@ export default function clarusjs() {
|
|
|
73
108
|
if (compile(readFileSync(file, 'utf8'), { file, target: 'client' }).stateful)
|
|
74
109
|
input[entryName(appRoot, file)] = file;
|
|
75
110
|
}
|
|
111
|
+
// Client routes: the router, and each page's render function for the browser.
|
|
112
|
+
const client = clientPages(appRoot, modes);
|
|
113
|
+
if (client.size)
|
|
114
|
+
input[ROUTER_ENTRY] = here('../runtime/router');
|
|
115
|
+
for (const file of client.values())
|
|
116
|
+
input[renderEntry(entryName(appRoot, file))] = `${file}?render`;
|
|
76
117
|
config.builder = {};
|
|
77
118
|
config.environments = {
|
|
78
119
|
// Island chunks are loaded with import() and must keep their `attach` export.
|
|
@@ -89,16 +130,22 @@ export default function clarusjs() {
|
|
|
89
130
|
if (this.environment.config.consumer !== 'client' || command !== 'build')
|
|
90
131
|
return;
|
|
91
132
|
const byName = new Map();
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
133
|
+
// By sanitized name and by the raw entry name from the source path ([id] stays [id]).
|
|
134
|
+
for (const out of Object.values(bundle)) {
|
|
135
|
+
if (out.type !== 'chunk' || !out.isEntry)
|
|
136
|
+
continue;
|
|
137
|
+
byName.set(out.name, base + out.fileName);
|
|
138
|
+
// Only island chunks: a client page's ?render chunk would otherwise take its island's place.
|
|
139
|
+
if (out.facadeModuleId && !/[?&]render\b/.test(out.facadeModuleId))
|
|
140
|
+
byName.set(entryName(root, out.facadeModuleId.replace(/\?.*$/, '')), base + out.fileName);
|
|
141
|
+
}
|
|
95
142
|
for (const out of Object.values(bundle)) {
|
|
96
143
|
if (out.type !== 'chunk' || !out.code.includes(CHUNK_TOKEN))
|
|
97
144
|
continue;
|
|
98
145
|
out.code = out.code.replace(/__CLARUS_CHUNK__\[(.+?)\]__/g, (_, name) => {
|
|
99
146
|
const url = byName.get(name);
|
|
100
147
|
if (!url)
|
|
101
|
-
this.error(`clarusjs: no client chunk for ${name}.
|
|
148
|
+
this.error(`clarusjs: no client chunk for ${name}.tscl (stateful components must be under src/)`);
|
|
102
149
|
return url;
|
|
103
150
|
});
|
|
104
151
|
}
|
|
@@ -115,40 +162,81 @@ export default function clarusjs() {
|
|
|
115
162
|
return;
|
|
116
163
|
await builder.build(client);
|
|
117
164
|
await builder.build(ssr);
|
|
118
|
-
|
|
165
|
+
const entry = resolve(builder.config.root, SERVER_OUT, 'index.mjs');
|
|
166
|
+
const out = resolve(builder.config.root, CLIENT_OUT);
|
|
167
|
+
const dist = dirname(out);
|
|
168
|
+
const manifestFile = resolve(dist, 'clarusjs-manifest.json');
|
|
169
|
+
rmSync(manifestFile, { force: true }); // a page reading the manifest must never see the previous build's
|
|
170
|
+
const { routes, readers, loaded, server } = await prerender(entry, out, manifestFile);
|
|
171
|
+
writeManifest(dist, routes, builder.config.base, loaded, server);
|
|
172
|
+
// Pages that read the manifest saw none on the first pass: render them again now that it exists.
|
|
173
|
+
if (readers.length) {
|
|
174
|
+
await prerender(entry, out, manifestFile, readers);
|
|
175
|
+
writeManifest(dist, routes, builder.config.base, loaded, server);
|
|
176
|
+
}
|
|
177
|
+
if (options.adapter === 'netlify')
|
|
178
|
+
writeNetlifyFunction(builder.config.root, entry, server.map((s) => s.route));
|
|
119
179
|
},
|
|
120
180
|
resolveId(id) {
|
|
121
181
|
if (id === '/cl-loader.js')
|
|
122
182
|
return here('../runtime/loader');
|
|
183
|
+
if (id === '/cl-router.js')
|
|
184
|
+
return here('../runtime/router');
|
|
123
185
|
if (id === ENTRY)
|
|
124
186
|
return '\0' + ENTRY;
|
|
125
187
|
},
|
|
126
188
|
load(id) {
|
|
127
189
|
if (id !== '\0' + ENTRY)
|
|
128
190
|
return;
|
|
129
|
-
const
|
|
191
|
+
const urls = builtUrls();
|
|
192
|
+
const loaderUrl = urls?.get(LOADER_ENTRY) ?? '/cl-loader.js';
|
|
193
|
+
const routerUrl = urls?.get(ROUTER_ENTRY) ?? '/cl-router.js';
|
|
194
|
+
// Built: hashed chunk URLs. Dev: the source, served as JS by Vite.
|
|
195
|
+
const client = {};
|
|
196
|
+
for (const [route, file] of clientPages(root, modes)) {
|
|
197
|
+
const key = `${relative(root, file).split(sep).join('/')}?render`;
|
|
198
|
+
const url = urls ? urls.get(key) : `${clientUrlFor(root, file).replace(/\?.*$/, '')}?render&import`;
|
|
199
|
+
if (!url)
|
|
200
|
+
throw new Error(`clarusjs: no client chunk for ${key}`);
|
|
201
|
+
client[route] = url;
|
|
202
|
+
}
|
|
130
203
|
return `import { createHandler } from ${JSON.stringify(here('../server/handler'))}\n` +
|
|
131
|
-
`
|
|
204
|
+
`import { registerViews, render } from ${JSON.stringify(here('../server/views'))}\n` +
|
|
205
|
+
// Every view under src/, for render(name, props) in server code. Also exported for apps that import the build.
|
|
206
|
+
`registerViews(import.meta.glob('/src/**/*.tscl'))\n` +
|
|
207
|
+
`export { render }\n` +
|
|
208
|
+
`export default createHandler(import.meta.glob('/src/pages/**/*.tscl'), {\n` +
|
|
209
|
+
` loaderUrl: ${JSON.stringify(loaderUrl)},\n` +
|
|
210
|
+
` data: import.meta.glob('/src/pages/**/*.load.ts'),\n` +
|
|
211
|
+
` modes: ${JSON.stringify(modes)},\n` +
|
|
212
|
+
` routerUrl: ${JSON.stringify(routerUrl)},\n` +
|
|
213
|
+
` client: ${JSON.stringify(client)},\n` +
|
|
214
|
+
` base: ${JSON.stringify(base)},\n` +
|
|
215
|
+
`})\n`;
|
|
132
216
|
},
|
|
133
217
|
transform: {
|
|
134
|
-
filter: { id: /\.
|
|
218
|
+
filter: { id: /\.tscl(\?|$)/ },
|
|
135
219
|
handler(code, id) {
|
|
136
220
|
const file = id.replace(/\?.*$/, '');
|
|
137
221
|
// ?render: a component's server render function, used in the browser to render children of islands.
|
|
138
|
-
const
|
|
139
|
-
|
|
222
|
+
const renderQuery = /[?&]render\b/.test(id);
|
|
223
|
+
const target = this.environment.config.consumer === 'server' || renderQuery ? 'server' : 'client';
|
|
224
|
+
// ponytail: ?import makes Vite's dev transform middleware serve .tscl as JS for the loader's dynamic import; build output uses hashed chunk URLs from the client manifest (stage 3)
|
|
140
225
|
const consumer = this.environment.config.consumer;
|
|
141
226
|
// Server build: chunk URLs come from the client manifest. Client build (a ?render child): the chunk doesn't
|
|
142
227
|
// exist yet, so write a placeholder that generateBundle swaps for the real file name. Dev: the source URL.
|
|
143
228
|
const urls = consumer === 'server' ? builtUrls() : undefined;
|
|
229
|
+
// Built URLs by entry name, or by source key when the name was sanitized ([tag] → _tag_).
|
|
144
230
|
const clientUrl = urls
|
|
145
|
-
? urls.get(entryName(root, file))
|
|
231
|
+
? urls.get(entryName(root, file)) ?? urls.get(relative(root, file).split(sep).join('/'))
|
|
146
232
|
: command === 'build' ? `${CHUNK_TOKEN}[${entryName(root, file)}]__` : clientUrlFor(root, file);
|
|
147
233
|
if (urls && !clientUrl && compile(code, { file, target: 'client' }).stateful) {
|
|
148
234
|
this.error(`${file}: stateful component is not under src/, so it has no client chunk`);
|
|
149
235
|
}
|
|
150
236
|
try {
|
|
151
|
-
|
|
237
|
+
// A ?render module runs in the browser: its child components must be render modules too.
|
|
238
|
+
const renderImports = renderQuery && this.environment.config.consumer !== 'server';
|
|
239
|
+
return { code: compile(code, { file, target, clientUrl, renderImports }).code, map: null };
|
|
152
240
|
}
|
|
153
241
|
catch (err) {
|
|
154
242
|
if (err instanceof ClarusError)
|
|
@@ -162,7 +250,13 @@ export default function clarusjs() {
|
|
|
162
250
|
const outDir = resolve(server.config.root, server.config.build.outDir);
|
|
163
251
|
const base = server.config.base; // e.g. '/' or '/docs/'; Vite strips it after this middleware
|
|
164
252
|
server.middlewares.use((req, _res, next) => {
|
|
165
|
-
|
|
253
|
+
let url;
|
|
254
|
+
try {
|
|
255
|
+
url = decodeURIComponent((req.url ?? '/').split('?')[0]);
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
return next(); // a malformed %-escape names no built page
|
|
259
|
+
}
|
|
166
260
|
const inBase = url === base.slice(0, -1) ? '/' : url.startsWith(base) ? '/' + url.slice(base.length) : undefined;
|
|
167
261
|
if (inBase !== undefined) {
|
|
168
262
|
// A built page wins; anything else (assets, /x.css) falls through to Vite's static files.
|
|
@@ -172,6 +266,26 @@ export default function clarusjs() {
|
|
|
172
266
|
}
|
|
173
267
|
next();
|
|
174
268
|
});
|
|
269
|
+
// Server routes go to the built handler, as they would in production; anything else that isn't a file is a 404.
|
|
270
|
+
return () => {
|
|
271
|
+
server.middlewares.use(async (req, res, next) => {
|
|
272
|
+
try {
|
|
273
|
+
const entry = resolve(server.config.root, SERVER_OUT, 'index.mjs');
|
|
274
|
+
if (!existsSync(entry))
|
|
275
|
+
return next();
|
|
276
|
+
const app = (await import(__rewriteRelativeImportExtension(pathToFileURL(entry).href)));
|
|
277
|
+
if (!app.default.isServerPath((req.url ?? '/').split('?')[0]))
|
|
278
|
+
return next();
|
|
279
|
+
const response = await app.default.fetch(toRequest(req));
|
|
280
|
+
if (response.status === 404)
|
|
281
|
+
return next();
|
|
282
|
+
await sendResponse(res, response);
|
|
283
|
+
}
|
|
284
|
+
catch (err) {
|
|
285
|
+
next(err);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
};
|
|
175
289
|
},
|
|
176
290
|
configureServer(server) {
|
|
177
291
|
return () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clarusjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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": [
|
|
@@ -48,8 +48,19 @@
|
|
|
48
48
|
"./vite": {
|
|
49
49
|
"types": "./dist/vite/plugin.d.ts",
|
|
50
50
|
"default": "./dist/vite/plugin.js"
|
|
51
|
+
},
|
|
52
|
+
"./manifest": {
|
|
53
|
+
"types": "./dist/server/manifest.d.ts",
|
|
54
|
+
"default": "./dist/server/manifest.js"
|
|
55
|
+
},
|
|
56
|
+
"./views": {
|
|
57
|
+
"types": "./dist/server/views.d.ts",
|
|
58
|
+
"default": "./dist/server/views.js"
|
|
51
59
|
}
|
|
52
60
|
},
|
|
61
|
+
"bin": {
|
|
62
|
+
"clarusjs": "./dist/cli/index.js"
|
|
63
|
+
},
|
|
53
64
|
"files": [
|
|
54
65
|
"dist",
|
|
55
66
|
"LICENSE",
|
|
@@ -59,7 +70,8 @@
|
|
|
59
70
|
"node": "^22.12.0 || ^24.0.0 || >=26.0.0"
|
|
60
71
|
},
|
|
61
72
|
"dependencies": {
|
|
62
|
-
"acorn": "^8.19.0"
|
|
73
|
+
"acorn": "^8.19.0",
|
|
74
|
+
"commander": "^15.1.0"
|
|
63
75
|
},
|
|
64
76
|
"peerDependencies": {
|
|
65
77
|
"vite": "^8.0.0"
|