clarusjs 0.20.0 → 0.21.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 +1 -1
- package/dist/check/generate.js +10 -4
- package/dist/cli/migrate.js +33 -9
- package/dist/compiler/emit-client.js +4 -3
- package/dist/compiler/emit-server.js +3 -1
- package/dist/compiler/factories.d.ts +4 -2
- package/dist/compiler/factories.js +116 -15
- package/dist/compiler/html-expr.d.ts +4 -0
- package/dist/compiler/html-expr.js +5 -1
- package/dist/compiler/index.js +6 -7
- package/dist/compiler/ir.d.ts +2 -0
- package/dist/compiler/ir.js +3 -5
- package/dist/compiler/parse.js +33 -13
- package/dist/compiler/script.d.ts +5 -0
- package/dist/compiler/script.js +14 -2
- package/dist/forms/index.js +26 -5
- package/dist/runtime/context.d.ts +16 -8
- package/dist/runtime/fragments.js +6 -2
- package/dist/runtime/index.d.ts +1 -1
- package/dist/runtime/navigate.js +2 -1
- package/dist/runtime/signals.d.ts +11 -6
- package/dist/runtime/signals.js +12 -3
- package/dist/vite/base-clash.d.ts +9 -0
- package/dist/vite/base-clash.js +54 -0
- package/dist/vite/build-app.d.ts +5 -0
- package/dist/vite/build-app.js +185 -0
- package/dist/vite/dev-server.d.ts +5 -0
- package/dist/vite/dev-server.js +67 -0
- package/dist/vite/graph.d.ts +45 -0
- package/dist/vite/graph.js +125 -0
- package/dist/vite/hmr.d.ts +19 -0
- package/dist/vite/hmr.js +53 -0
- package/dist/vite/links.d.ts +2 -0
- package/dist/vite/links.js +2 -0
- package/dist/vite/options.d.ts +147 -0
- package/dist/vite/options.js +1 -0
- package/dist/vite/output.d.ts +25 -0
- package/dist/vite/output.js +49 -0
- package/dist/vite/plugin.d.ts +4 -150
- package/dist/vite/plugin.js +45 -624
- package/dist/vite/preview.d.ts +3 -0
- package/dist/vite/preview.js +100 -0
- package/dist/vite/server-entry.d.ts +18 -0
- package/dist/vite/server-entry.js +83 -0
- package/dist/vite/shared.d.ts +34 -0
- package/dist/vite/shared.js +47 -0
- package/dist/vite/styles.d.ts +16 -0
- package/dist/vite/styles.js +56 -0
- package/package.json +1 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx, extraExtensions, ignoreCase) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
if (extraExtensions) {
|
|
4
|
+
for (var extension in extraExtensions) {
|
|
5
|
+
var suffix = path.slice(-extension.length);
|
|
6
|
+
if (suffix === extension || ignoreCase && suffix.toLowerCase() === extension.toLowerCase()) {
|
|
7
|
+
var output = extraExtensions[extension];
|
|
8
|
+
return output === extension ? path : path.slice(0, -extension.length) + output;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
13
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return path;
|
|
17
|
+
};
|
|
18
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
19
|
+
import { resolve, sep } from 'node:path';
|
|
20
|
+
import { pathToFileURL } from 'node:url';
|
|
21
|
+
import { canonical, headersFor, readHeaders } from './headers.js';
|
|
22
|
+
import { acceptsEncoding, sendResponse, toRequest } from './node.js';
|
|
23
|
+
import { SERVER_OUT } from './shared.js';
|
|
24
|
+
export function configurePreview(server) {
|
|
25
|
+
const outDir = resolve(server.config.root, server.config.build.outDir);
|
|
26
|
+
const base = server.config.base;
|
|
27
|
+
const rules = readHeaders(outDir);
|
|
28
|
+
server.middlewares.use((req, res, next) => {
|
|
29
|
+
let url;
|
|
30
|
+
try {
|
|
31
|
+
url = decodeURIComponent((req.url ?? '/').split('?')[0]);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
res.statusCode = 400;
|
|
35
|
+
res.setHeader('content-type', 'text/plain; charset=utf-8');
|
|
36
|
+
return res.end('Bad Request: malformed URL');
|
|
37
|
+
}
|
|
38
|
+
const inBase = url === base.slice(0, -1) ? '/' : url.startsWith(base) ? '/' + url.slice(base.length) : undefined;
|
|
39
|
+
if (inBase !== undefined) {
|
|
40
|
+
const file = resolve(outDir, `.${inBase.replace(/\/?$/, '/')}index.html`);
|
|
41
|
+
if (file.startsWith(outDir + sep) && existsSync(file))
|
|
42
|
+
req.url = base + file.slice(outDir.length + 1).replace(/\\/g, '/');
|
|
43
|
+
precompressed(req, res, outDir, base);
|
|
44
|
+
}
|
|
45
|
+
for (const [name, value] of headersFor(rules, canonical(url)))
|
|
46
|
+
res.setHeader(name, value);
|
|
47
|
+
next();
|
|
48
|
+
});
|
|
49
|
+
return () => {
|
|
50
|
+
server.middlewares.use(async (req, res, next) => {
|
|
51
|
+
try {
|
|
52
|
+
const entry = resolve(server.config.root, SERVER_OUT, 'index.mjs');
|
|
53
|
+
if (!existsSync(entry))
|
|
54
|
+
return next();
|
|
55
|
+
const app = (await import(__rewriteRelativeImportExtension(pathToFileURL(entry).href)));
|
|
56
|
+
if (!app.default.isServerPath((req.url ?? '/').split('?')[0]))
|
|
57
|
+
return next();
|
|
58
|
+
const response = await app.default.fetch(toRequest(req));
|
|
59
|
+
if (response.status === 404)
|
|
60
|
+
return next();
|
|
61
|
+
await sendResponse(res, response);
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
next(err);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
server.middlewares.use((req, res, next) => {
|
|
68
|
+
const page = resolve(outDir, '404.html');
|
|
69
|
+
if ((req.method !== 'GET' && req.method !== 'HEAD') || !existsSync(page))
|
|
70
|
+
return next();
|
|
71
|
+
res.statusCode = 404;
|
|
72
|
+
res.setHeader('content-type', 'text/html; charset=utf-8');
|
|
73
|
+
res.end(req.method === 'HEAD' ? undefined : readFileSync(page));
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function precompressed(req, res, outDir, base) {
|
|
78
|
+
if ((req.method !== 'GET' && req.method !== 'HEAD') || req.headers.range)
|
|
79
|
+
return;
|
|
80
|
+
const [path, query] = (req.url ?? '/').split('?');
|
|
81
|
+
if (!path.startsWith(base))
|
|
82
|
+
return;
|
|
83
|
+
let file;
|
|
84
|
+
try {
|
|
85
|
+
file = resolve(outDir, decodeURIComponent(path.slice(base.length)));
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (!file.startsWith(outDir + sep) || !existsSync(file))
|
|
91
|
+
return;
|
|
92
|
+
const copies = ['br', 'gzip'].filter((enc) => existsSync(file + (enc === 'br' ? '.br' : '.gz')));
|
|
93
|
+
if (!copies.length)
|
|
94
|
+
return;
|
|
95
|
+
res.setHeader('Vary', 'Accept-Encoding');
|
|
96
|
+
const accept = String(req.headers['accept-encoding'] ?? '');
|
|
97
|
+
const pick = copies.find((enc) => acceptsEncoding(accept, enc));
|
|
98
|
+
if (pick)
|
|
99
|
+
req.url = `${path}${pick === 'br' ? '.br' : '.gz'}${query === undefined ? '' : `?${query}`}`;
|
|
100
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ModeTable } from '../server/modes.ts';
|
|
2
|
+
import type { Alias } from './aliases.ts';
|
|
3
|
+
import { type Files } from './graph.ts';
|
|
4
|
+
import type { ClarusOptions } from './options.ts';
|
|
5
|
+
/** What the server entry is generated from. `urls`: the client build's URLs by entry name or source key; none in dev. */
|
|
6
|
+
export type EntryContext = {
|
|
7
|
+
root: string;
|
|
8
|
+
files: Files;
|
|
9
|
+
aliases: Alias[];
|
|
10
|
+
modes: ModeTable;
|
|
11
|
+
extensions: string[];
|
|
12
|
+
options: Pick<ClarusOptions, 'fragments' | 'site' | 'head'>;
|
|
13
|
+
base: string;
|
|
14
|
+
command: 'build' | 'serve';
|
|
15
|
+
urls: Map<string, string> | undefined;
|
|
16
|
+
};
|
|
17
|
+
/** The server entry's code. */
|
|
18
|
+
export declare function serverEntry({ root, files, aliases, modes, extensions, options, base, command, urls }: EntryContext): string;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { relative, sep } from 'node:path';
|
|
2
|
+
import { normalizePath } from 'vite';
|
|
3
|
+
import { routeFromFile } from '../server/handler.js';
|
|
4
|
+
import { clientPages, componentLoaderOf, pageGraph } from './graph.js';
|
|
5
|
+
import { clientUrlFor, here, LOADER_ENTRY, NAV_ENTRY, real, ROUTER_ENTRY, STYLES_ENTRY } from './shared.js';
|
|
6
|
+
import { componentCss, STYLE_SUFFIX, styleOrder } from './styles.js';
|
|
7
|
+
export function serverEntry({ root, files, aliases, modes, extensions, options, base, command, urls }) {
|
|
8
|
+
const glob = (dir) => JSON.stringify(extensions.map((e) => `${dir}/**/*${e}`));
|
|
9
|
+
const loaderUrl = urls?.get(LOADER_ENTRY) ?? '/cl-loader.js';
|
|
10
|
+
const routerUrl = urls?.get(ROUTER_ENTRY) ?? '/cl-router.js';
|
|
11
|
+
const navUrl = urls?.get(NAV_ENTRY) ?? '/cl-nav.js';
|
|
12
|
+
const css = {};
|
|
13
|
+
const graph = pageGraph(root, files, aliases);
|
|
14
|
+
const componentLoaders = {};
|
|
15
|
+
for (const [route, { components }] of graph) {
|
|
16
|
+
const loaders = components.map((c) => componentLoaderOf(root, c)).filter((f) => !!f && !relative(root, f).startsWith('..'));
|
|
17
|
+
if (loaders.length)
|
|
18
|
+
componentLoaders[route] = loaders.map((f) => `/${relative(root, f).split(sep).join('/')}`).sort();
|
|
19
|
+
}
|
|
20
|
+
const pageOf = new Map(files.pages(root).map((p) => [routeFromFile(`/${relative(root, p).split(sep).join('/')}`), p]));
|
|
21
|
+
const key = (f) => normalizePath(real(f));
|
|
22
|
+
const used = new Map();
|
|
23
|
+
if (!urls)
|
|
24
|
+
for (const [route, { components }] of graph)
|
|
25
|
+
for (const f of [...components, pageOf.get(route) ?? ''])
|
|
26
|
+
if (f && !used.has(key(f)))
|
|
27
|
+
used.set(key(f), f);
|
|
28
|
+
const order = styleOrder(root, files);
|
|
29
|
+
const styled = [...used.keys()]
|
|
30
|
+
.filter((k) => { try {
|
|
31
|
+
return componentCss(root, files, used.get(k)) !== '';
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
} })
|
|
36
|
+
.sort((a, b) => order(used.get(a), used.get(b)));
|
|
37
|
+
for (const [route, { css: sheets, components }] of graph) {
|
|
38
|
+
const own = new Set([...components, pageOf.get(route) ?? ''].map(key));
|
|
39
|
+
const mine = styled.filter((k) => own.has(k)).map((k) => clientUrlFor(root, used.get(k)).replace(/\?.*$/, '') + STYLE_SUFFIX);
|
|
40
|
+
if (!sheets.length && !mine.length)
|
|
41
|
+
continue;
|
|
42
|
+
css[route] = [...sheets.map((sheet) => {
|
|
43
|
+
const key = relative(root, sheet).split(sep).join('/');
|
|
44
|
+
const url = urls ? urls.get(key) ?? urls.get(real(sheet)) : clientUrlFor(root, sheet).replace(/\?.*$/, '');
|
|
45
|
+
if (!url)
|
|
46
|
+
throw new Error(`clarusjs: no built stylesheet for ${key}`);
|
|
47
|
+
return url;
|
|
48
|
+
}), ...mine];
|
|
49
|
+
}
|
|
50
|
+
const stylesUrl = urls ? urls.get(STYLES_ENTRY) : undefined;
|
|
51
|
+
const client = {};
|
|
52
|
+
for (const [route, file] of clientPages(root, modes, files)) {
|
|
53
|
+
const key = `${relative(root, file).split(sep).join('/')}?render`;
|
|
54
|
+
const url = urls ? urls.get(key) ?? urls.get(`${real(file)}?render`) : `${clientUrlFor(root, file).replace(/\?.*$/, '')}?render&import`;
|
|
55
|
+
if (!url)
|
|
56
|
+
throw new Error(`clarusjs: no client chunk for ${key}`);
|
|
57
|
+
client[route] = url;
|
|
58
|
+
}
|
|
59
|
+
return `import { createHandler } from ${JSON.stringify(here('../server/handler'))}\n` +
|
|
60
|
+
`import { registerViews, render } from ${JSON.stringify(here('../server/views'))}\n` +
|
|
61
|
+
`registerViews(import.meta.glob(${JSON.stringify(['/src/**/*.tscl', ...extensions.slice(1).map((e) => `/src/pages/**/*${e}`)])}))\n` +
|
|
62
|
+
`export { render }\n` +
|
|
63
|
+
`export default createHandler(import.meta.glob(${glob('/src/pages')}), {\n` +
|
|
64
|
+
` loaderUrl: ${JSON.stringify(loaderUrl)},\n` +
|
|
65
|
+
` data: import.meta.glob('/src/pages/**/*.load.ts'),\n` +
|
|
66
|
+
` componentLoaders: ${JSON.stringify(componentLoaders)},\n` +
|
|
67
|
+
` componentData: { ...import.meta.glob(['/src/**/*.load.ts', '!/src/pages/**']), ...import.meta.glob('/src/pages/**/+layout.load.ts') },\n` +
|
|
68
|
+
` guards: import.meta.glob('/src/pages/**/+guard.ts'),\n` +
|
|
69
|
+
` endpoints: import.meta.glob(['/src/pages/**/*.ts', '!/src/pages/**/*.load.ts', '!/src/pages/**/*.d.ts', '!/src/pages/**/+*']),\n` +
|
|
70
|
+
` modes: ${JSON.stringify(modes)},\n` +
|
|
71
|
+
` routerUrl: ${JSON.stringify(routerUrl)},\n` +
|
|
72
|
+
` client: ${JSON.stringify(client)},\n` +
|
|
73
|
+
` base: ${JSON.stringify(base)},\n` +
|
|
74
|
+
` fragments: ${options.fragments === true},\n` +
|
|
75
|
+
` dev: ${command === 'serve'},\n` +
|
|
76
|
+
` navUrl: ${JSON.stringify(navUrl)},\n` +
|
|
77
|
+
` stylesUrl: ${JSON.stringify(stylesUrl)},\n` +
|
|
78
|
+
` css: ${JSON.stringify(css)},\n` +
|
|
79
|
+
` site: ${JSON.stringify(options.site)},\n` +
|
|
80
|
+
` head: { ...${JSON.stringify(options.head ?? {})}, transform: Object.values(import.meta.glob('/src/head.ts', { eager: true }))[0]?.transformHead },\n` +
|
|
81
|
+
` hooks: Object.values(import.meta.glob('/src/server.ts', { eager: true }))[0],\n` +
|
|
82
|
+
`})\n`;
|
|
83
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/** <icon> names, found in the sets installed for the package `file` is in (clarusjs/icons). */
|
|
3
|
+
export declare const iconsFor: (file: string) => (name: string) => import("../icons.ts").IconLookup;
|
|
4
|
+
export declare const ext: string;
|
|
5
|
+
export declare const here: (path: string) => string;
|
|
6
|
+
export declare const ENTRY = "virtual:clarusjs/server-entry";
|
|
7
|
+
/**
|
|
8
|
+
* Dev URL of a component's client module: root-relative, or Vite's /@fs/ form for files outside the root. Always with
|
|
9
|
+
* forward slashes (`/@fs/C:/libs/c.tscl` on Windows). `path`: the platform's (a parameter for tests). @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function clientUrlFor(root: string, file: string, path?: Pick<typeof import('node:path'), 'relative' | 'isAbsolute' | 'sep'>): string;
|
|
12
|
+
/** A path as the file system names it (symlinks and Windows short names resolved), or as given if it doesn't exist. */
|
|
13
|
+
export declare const real: (path: string) => string;
|
|
14
|
+
export declare const LOADER_ENTRY = "cl-loader";
|
|
15
|
+
export declare const ROUTER_ENTRY = "cl-router";
|
|
16
|
+
export declare const NAV_ENTRY = "cl-nav";
|
|
17
|
+
export declare const STYLES_ENTRY = "cl-styles";
|
|
18
|
+
export declare const STYLES_FILE = ".clarusjs/styles.css";
|
|
19
|
+
/** Scope ids come from the path relative to the app root, so server, client and stylesheet always agree. */
|
|
20
|
+
export declare const scopeFor: (root: string, file: string) => string;
|
|
21
|
+
export declare const renderEntry: (name: string) => string;
|
|
22
|
+
export declare const CLIENT_OUT = "dist/client";
|
|
23
|
+
export declare const CHUNK_TOKEN = "__CLARUS_CHUNK__";
|
|
24
|
+
export declare const SERVER_OUT = "dist/server";
|
|
25
|
+
/**
|
|
26
|
+
* A client entry's name, from its path: `src/components/counter.tscl` → `src/components/counter`. A file from an
|
|
27
|
+
* installed package is named from the package (`_pkg/acme-theme/src/counter`), not from pnpm's `node_modules/.pnpm/…`:
|
|
28
|
+
* a folder starting with a dot is hidden, and GitHub Pages' upload skips hidden folders, so a theme's stylesheet and
|
|
29
|
+
* islands went missing. Any other dot folder becomes `_…` too. Two files that end up with one name stop the build (add()).
|
|
30
|
+
*/
|
|
31
|
+
export declare const sourceName: (root: string, file: string) => string;
|
|
32
|
+
export declare const entryName: (root: string, file: string) => string;
|
|
33
|
+
/** Vite's plugins option, flattened: nested arrays followed, falsy entries and promises (not resolved yet) left out. */
|
|
34
|
+
export declare function pluginsOf(list: unknown): Plugin[];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, relative, sep } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { scopeId } from '../compiler/index.js';
|
|
5
|
+
import { resolveIcon } from '../icons.js';
|
|
6
|
+
export const iconsFor = (file) => (name) => resolveIcon(name, file);
|
|
7
|
+
export const ext = import.meta.url.endsWith('.ts') ? '.ts' : '.js';
|
|
8
|
+
export const here = (path) => fileURLToPath(new URL(path + ext, import.meta.url));
|
|
9
|
+
export const ENTRY = 'virtual:clarusjs/server-entry';
|
|
10
|
+
export function clientUrlFor(root, file, path = { relative, isAbsolute, sep }) {
|
|
11
|
+
const rel = path.relative(root, file);
|
|
12
|
+
const slashes = (p) => p.split(path.sep).join('/');
|
|
13
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
14
|
+
const abs = slashes(file);
|
|
15
|
+
return `/@fs${abs.startsWith('/') ? '' : '/'}${abs}?import`;
|
|
16
|
+
}
|
|
17
|
+
return `/${slashes(rel)}?import`;
|
|
18
|
+
}
|
|
19
|
+
export const real = (path) => { try {
|
|
20
|
+
return realpathSync.native(path);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return path;
|
|
24
|
+
} };
|
|
25
|
+
export const LOADER_ENTRY = 'cl-loader';
|
|
26
|
+
export const ROUTER_ENTRY = 'cl-router';
|
|
27
|
+
export const NAV_ENTRY = 'cl-nav';
|
|
28
|
+
export const STYLES_ENTRY = 'cl-styles';
|
|
29
|
+
export const STYLES_FILE = '.clarusjs/styles.css';
|
|
30
|
+
export const scopeFor = (root, file) => scopeId(relative(root, file).split(sep).join('/'));
|
|
31
|
+
export const renderEntry = (name) => `render/${name}`;
|
|
32
|
+
export const CLIENT_OUT = 'dist/client';
|
|
33
|
+
export const CHUNK_TOKEN = '__CLARUS_CHUNK__';
|
|
34
|
+
export const SERVER_OUT = 'dist/server';
|
|
35
|
+
export const sourceName = (root, file) => {
|
|
36
|
+
let path = relative(root, file).split(sep).join('/');
|
|
37
|
+
const pkg = path.lastIndexOf('node_modules/');
|
|
38
|
+
if (pkg !== -1)
|
|
39
|
+
path = '_pkg/' + path.slice(pkg + 'node_modules/'.length);
|
|
40
|
+
return path.replace(/^(\.\.\/)+/, (ups) => '_/'.repeat(ups.length / 3)).replace(/(^|\/)\./g, '$1_');
|
|
41
|
+
};
|
|
42
|
+
export const entryName = (root, file) => sourceName(root, file).replace(/\.[^./]+$/, '');
|
|
43
|
+
export function pluginsOf(list) {
|
|
44
|
+
if (Array.isArray(list))
|
|
45
|
+
return list.flatMap(pluginsOf);
|
|
46
|
+
return list && typeof list === 'object' && !(list instanceof Promise) ? [list] : [];
|
|
47
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Files } from './graph.ts';
|
|
2
|
+
/** A component's compiled CSS ('' without a top-level <style>), from the cache while its source is unchanged. */
|
|
3
|
+
export declare function componentCss(root: string, files: Files, file: string, text?: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* A component's stylesheet in dev (#728): its own CSS module, served by Vite and linked by the pages that use it, at
|
|
6
|
+
* the component's path plus .css (note.tscl.css). A path, not a query: Vite's client relinks an updated stylesheet as
|
|
7
|
+
* its path plus ?t=…, dropping any other query.
|
|
8
|
+
*/
|
|
9
|
+
export declare const STYLE_SUFFIX = ".css";
|
|
10
|
+
/**
|
|
11
|
+
* The order components' styles come in, the same in dev (a link each) and in a build (one stylesheet), so rules of equal
|
|
12
|
+
* specificity resolve alike: src/ files in the order src/ lists them, then the rest (packages) by path.
|
|
13
|
+
*/
|
|
14
|
+
export declare function styleOrder(root: string, files: Pick<Files, 'find'>): (a: string, b: string) => number;
|
|
15
|
+
/** Write .clarusjs/styles.css (the build's one stylesheet) from every component under src/; false when none has a <style>. */
|
|
16
|
+
export declare function writeStyles(root: string, files: Files): boolean;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { compile } from '../compiler/index.js';
|
|
4
|
+
import { normalizePath } from 'vite';
|
|
5
|
+
import { iconsFor, real, scopeFor, STYLES_FILE } from './shared.js';
|
|
6
|
+
const compiledStyles = new Map();
|
|
7
|
+
export function componentCss(root, files, file, text = files.read(file, root)) {
|
|
8
|
+
if (!/<style[\s>]/i.test(text))
|
|
9
|
+
return '';
|
|
10
|
+
const scope = scopeFor(root, file);
|
|
11
|
+
const known = compiledStyles.get(file);
|
|
12
|
+
if (known?.text === text && known.scope === scope)
|
|
13
|
+
return known.css;
|
|
14
|
+
const css = compile(text, { file, target: 'client', scope, icon: iconsFor(file), layouts: files.layouts(file, root), components: files.names(root) }).css;
|
|
15
|
+
compiledStyles.set(file, { text, scope, css });
|
|
16
|
+
return css;
|
|
17
|
+
}
|
|
18
|
+
export const STYLE_SUFFIX = '.css';
|
|
19
|
+
export function styleOrder(root, files) {
|
|
20
|
+
const key = (f) => normalizePath(real(f));
|
|
21
|
+
const order = new Map(files.find(resolve(root, 'src')).map((f, i) => [key(f), i]));
|
|
22
|
+
return (a, b) => {
|
|
23
|
+
const [x, y] = [order.get(key(a)) ?? Infinity, order.get(key(b)) ?? Infinity];
|
|
24
|
+
if (x !== y)
|
|
25
|
+
return x < y ? -1 : 1;
|
|
26
|
+
return key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function writeStyles(root, files) {
|
|
30
|
+
let css = '';
|
|
31
|
+
const head = new Set();
|
|
32
|
+
const outDir = resolve(root, '.clarusjs');
|
|
33
|
+
for (const file of [...files.components(root)].sort(styleOrder(root, files))) {
|
|
34
|
+
try {
|
|
35
|
+
let out = componentCss(root, files, file);
|
|
36
|
+
if (!out)
|
|
37
|
+
continue;
|
|
38
|
+
out = out.replace(/^(?:[ \t]*@(?:reference|import)\b[^;{]*;[ \t]*\n?)+/, (statements) => {
|
|
39
|
+
for (const statement of statements.split(';').map((x) => x.trim()).filter(Boolean)) {
|
|
40
|
+
head.add(statement.replace(/(["'])(\.{1,2}\/[^"']*)\1/, (_, q, spec) => {
|
|
41
|
+
const target = relative(outDir, resolve(file, '..', spec)).split(sep).join('/');
|
|
42
|
+
return q + (target.startsWith('.') ? target : `./${target}`) + q;
|
|
43
|
+
}) + ';');
|
|
44
|
+
}
|
|
45
|
+
return '';
|
|
46
|
+
});
|
|
47
|
+
if (out)
|
|
48
|
+
css += `/* ${relative(root, file).split(sep).join('/')} */\n${out}`;
|
|
49
|
+
}
|
|
50
|
+
catch { }
|
|
51
|
+
}
|
|
52
|
+
css = [...head].map((x) => `${x}\n`).join('') + css;
|
|
53
|
+
mkdirSync(outDir, { recursive: true });
|
|
54
|
+
writeFileSync(resolve(root, STYLES_FILE), css);
|
|
55
|
+
return css !== '';
|
|
56
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clarusjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.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": [
|