@timber-js/app 0.2.0-alpha.248 → 0.2.0-alpha.249
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/index.d.ts.map +1 -1
- package/dist/index.js +201 -1
- package/dist/index.js.map +1 -1
- package/dist/plugins/route-css.d.ts +63 -0
- package/dist/plugins/route-css.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/plugins/route-css.ts +237 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* timber-route-css — link font CSS that a route file reaches only through
|
|
3
|
+
* a plain (non-component) module.
|
|
4
|
+
*
|
|
5
|
+
* @vitejs/plugin-rsc renders a server component's stylesheets by wrapping
|
|
6
|
+
* the component in `import.meta.viteRsc.loadCss()`, which collects every
|
|
7
|
+
* CSS file the module reaches, directly or through other modules. But it
|
|
8
|
+
* only applies that wrap to a module that imports CSS **itself**
|
|
9
|
+
* (`rsc:rsc-css-export-transform`). A font is usually defined in a plain
|
|
10
|
+
* module, and the font's CSS import is injected there:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* // app/fonts.ts — the font's @font-face CSS is imported here
|
|
14
|
+
* export const body = localFont({ src: './TX-02.woff2' });
|
|
15
|
+
*
|
|
16
|
+
* // app/layout.tsx — imports no CSS of its own, so nothing is wrapped
|
|
17
|
+
* import { body } from './fonts';
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The page rendered with the font's class and no `@font-face`.
|
|
21
|
+
*
|
|
22
|
+
* This plugin gives such a route file a static import of an empty
|
|
23
|
+
* stylesheet, so plugin-rsc wraps it exactly as it wraps a layout that
|
|
24
|
+
* imports `globals.css`, and `loadCss()` collects the font CSS. The anchor
|
|
25
|
+
* is imported with `?inline`: plugin-rsc's wrap check counts it as a CSS
|
|
26
|
+
* import, while its collector skips `?inline` ids (no `<link>` in dev) and
|
|
27
|
+
* the build compiles it to a JS string (no asset).
|
|
28
|
+
*
|
|
29
|
+
* **Only route files that reach a font module are touched.** The wrap is
|
|
30
|
+
* not free: in dev it renders an async `Resources` component beside the
|
|
31
|
+
* route component, which shifts shell timing (a delayed `deny()` can land
|
|
32
|
+
* before the shell commits). Wrapping every route file changed dev status
|
|
33
|
+
* codes in apps with no fonts at all. Route files that import CSS are
|
|
34
|
+
* wrapped by plugin-rsc already; this adds only the font case.
|
|
35
|
+
*
|
|
36
|
+
* A route file is transformed before its imports, so the font pipeline
|
|
37
|
+
* can't answer "does this reach a font?" yet. The plugin scans source
|
|
38
|
+
* instead: it follows the route file's static imports through local,
|
|
39
|
+
* server-side modules (not node_modules, not `'use client'` — a client
|
|
40
|
+
* module's CSS rides its client reference) and stops at the first module
|
|
41
|
+
* that imports a font loader. Scans are cached per file by mtime.
|
|
42
|
+
*
|
|
43
|
+
* Not touched: `'use client'` route files (a statement before the directive
|
|
44
|
+
* would cancel it), MDX (frontmatter must stay on line 1), and route.ts /
|
|
45
|
+
* middleware.ts / access.ts / metadata routes (not components).
|
|
46
|
+
*
|
|
47
|
+
* Known limit (dev): adding the first font call to a module a route file
|
|
48
|
+
* already imports takes effect when the route file is next transformed
|
|
49
|
+
* (edit or restart), because the route file's own source did not change.
|
|
50
|
+
*
|
|
51
|
+
* Design doc: 24-fonts.md §"Step 3: @font-face Generation"
|
|
52
|
+
*/
|
|
53
|
+
import { type Plugin } from 'vite';
|
|
54
|
+
import type { PluginContext } from '../plugin-context.ts';
|
|
55
|
+
import type { SegmentNode } from '../routing/types.ts';
|
|
56
|
+
/** The import the transform adds. See the module comment for `?inline`. */
|
|
57
|
+
export declare const ROUTE_CSS_ANCHOR_IMPORT = "import 'virtual:timber-route-css.css?inline';\n";
|
|
58
|
+
/** Static value import/re-export specifiers of a module. */
|
|
59
|
+
export declare function staticSpecifiers(code: string, filename: string): string[];
|
|
60
|
+
/** Every route file that exports a server component plugin-rsc may wrap. */
|
|
61
|
+
export declare function collectComponentRouteFiles(node: SegmentNode, into?: Set<string>): Set<string>;
|
|
62
|
+
export declare function timberRouteCss(ctx: PluginContext): Plugin;
|
|
63
|
+
//# sourceMappingURL=route-css.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-css.d.ts","sourceRoot":"","sources":["../../src/plugins/route-css.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AAIH,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAa,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAIlE,2EAA2E;AAC3E,eAAO,MAAM,uBAAuB,oDAAoC,CAAC;AAyBzE,4DAA4D;AAC5D,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CA4BzE;AAED,4EAA4E;AAC5E,wBAAgB,0BAA0B,CACxC,IAAI,EAAE,WAAW,EACjB,IAAI,cAAoB,GACvB,GAAG,CAAC,MAAM,CAAC,CAQb;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAuGzD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@timber-js/app",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.249",
|
|
4
4
|
"description": "Vite-native React framework built for Servers and Serverless Platforms — correct HTTP semantics, real status codes, pages that work without JavaScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare-workers",
|
package/src/index.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { timberMdx } from './plugins/mdx.ts';
|
|
|
22
22
|
import { timberRouting } from './plugins/routing.ts';
|
|
23
23
|
import { timberShims } from './plugins/shims.ts';
|
|
24
24
|
import { timberFonts } from './plugins/fonts.ts';
|
|
25
|
+
import { timberRouteCss } from './plugins/route-css.ts';
|
|
25
26
|
import { timberStaticBuild } from './plugins/static-build.ts';
|
|
26
27
|
import { timberBuildManifest } from './plugins/build-manifest.ts';
|
|
27
28
|
import { timberDevLogs, originalConsole, wrapLoggerWithOriginalConsole } from './dev-tools/logs.ts';
|
|
@@ -589,6 +590,7 @@ export function timber(config?: TimberUserConfig): PluginOption[] {
|
|
|
589
590
|
timberEntries(ctx),
|
|
590
591
|
timberBuildManifest(ctx),
|
|
591
592
|
fonts,
|
|
593
|
+
timberRouteCss(ctx), // Route files collect transitive CSS (font CSS from app/fonts.ts)
|
|
592
594
|
timberMdx(ctx),
|
|
593
595
|
timberOptimizePackageImports(ctx), // Listed barrels → the modules that define the imported names
|
|
594
596
|
timberClientInServerPackageDeps(), // Dev: a package's inner 'use client' file → its bare path, so the browser pre-bundles it
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* timber-route-css — link font CSS that a route file reaches only through
|
|
3
|
+
* a plain (non-component) module.
|
|
4
|
+
*
|
|
5
|
+
* @vitejs/plugin-rsc renders a server component's stylesheets by wrapping
|
|
6
|
+
* the component in `import.meta.viteRsc.loadCss()`, which collects every
|
|
7
|
+
* CSS file the module reaches, directly or through other modules. But it
|
|
8
|
+
* only applies that wrap to a module that imports CSS **itself**
|
|
9
|
+
* (`rsc:rsc-css-export-transform`). A font is usually defined in a plain
|
|
10
|
+
* module, and the font's CSS import is injected there:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* // app/fonts.ts — the font's @font-face CSS is imported here
|
|
14
|
+
* export const body = localFont({ src: './TX-02.woff2' });
|
|
15
|
+
*
|
|
16
|
+
* // app/layout.tsx — imports no CSS of its own, so nothing is wrapped
|
|
17
|
+
* import { body } from './fonts';
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* The page rendered with the font's class and no `@font-face`.
|
|
21
|
+
*
|
|
22
|
+
* This plugin gives such a route file a static import of an empty
|
|
23
|
+
* stylesheet, so plugin-rsc wraps it exactly as it wraps a layout that
|
|
24
|
+
* imports `globals.css`, and `loadCss()` collects the font CSS. The anchor
|
|
25
|
+
* is imported with `?inline`: plugin-rsc's wrap check counts it as a CSS
|
|
26
|
+
* import, while its collector skips `?inline` ids (no `<link>` in dev) and
|
|
27
|
+
* the build compiles it to a JS string (no asset).
|
|
28
|
+
*
|
|
29
|
+
* **Only route files that reach a font module are touched.** The wrap is
|
|
30
|
+
* not free: in dev it renders an async `Resources` component beside the
|
|
31
|
+
* route component, which shifts shell timing (a delayed `deny()` can land
|
|
32
|
+
* before the shell commits). Wrapping every route file changed dev status
|
|
33
|
+
* codes in apps with no fonts at all. Route files that import CSS are
|
|
34
|
+
* wrapped by plugin-rsc already; this adds only the font case.
|
|
35
|
+
*
|
|
36
|
+
* A route file is transformed before its imports, so the font pipeline
|
|
37
|
+
* can't answer "does this reach a font?" yet. The plugin scans source
|
|
38
|
+
* instead: it follows the route file's static imports through local,
|
|
39
|
+
* server-side modules (not node_modules, not `'use client'` — a client
|
|
40
|
+
* module's CSS rides its client reference) and stops at the first module
|
|
41
|
+
* that imports a font loader. Scans are cached per file by mtime.
|
|
42
|
+
*
|
|
43
|
+
* Not touched: `'use client'` route files (a statement before the directive
|
|
44
|
+
* would cancel it), MDX (frontmatter must stay on line 1), and route.ts /
|
|
45
|
+
* middleware.ts / access.ts / metadata routes (not components).
|
|
46
|
+
*
|
|
47
|
+
* Known limit (dev): adding the first font call to a module a route file
|
|
48
|
+
* already imports takes effect when the route file is next transformed
|
|
49
|
+
* (edit or restart), because the route file's own source did not change.
|
|
50
|
+
*
|
|
51
|
+
* Design doc: 24-fonts.md §"Step 3: @font-face Generation"
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
55
|
+
import MagicString from 'magic-string';
|
|
56
|
+
import { parseAst, type Plugin } from 'vite';
|
|
57
|
+
import type { PluginContext } from '../plugin-context.ts';
|
|
58
|
+
import { detectDirective } from '../analyze/classify.ts';
|
|
59
|
+
import type { RouteTree, SegmentNode } from '../routing/types.ts';
|
|
60
|
+
|
|
61
|
+
const ANCHOR_ID = 'virtual:timber-route-css.css';
|
|
62
|
+
const RESOLVED_ANCHOR_ID = '\0' + ANCHOR_ID;
|
|
63
|
+
/** The import the transform adds. See the module comment for `?inline`. */
|
|
64
|
+
export const ROUTE_CSS_ANCHOR_IMPORT = `import '${ANCHOR_ID}?inline';\n`;
|
|
65
|
+
|
|
66
|
+
const SCRIPT_RE = /\.[cm]?[jt]sx?$/;
|
|
67
|
+
/** Module specifiers that make a module a font module (same as the fonts transform). */
|
|
68
|
+
const FONT_SPECIFIER_PREFIXES = ['@timber/fonts/', '@timber-js/app/fonts/', 'next/font/'];
|
|
69
|
+
|
|
70
|
+
const isFontSpecifier = (spec: string) => FONT_SPECIFIER_PREFIXES.some((p) => spec.startsWith(p));
|
|
71
|
+
|
|
72
|
+
/** What a module's source says, for the reachability walk. */
|
|
73
|
+
interface ModuleScan {
|
|
74
|
+
mtimeMs: number;
|
|
75
|
+
size: number;
|
|
76
|
+
/** Imports a font loader (so the fonts transform injects its CSS here). */
|
|
77
|
+
definesFont: boolean;
|
|
78
|
+
/** A 'use client' / 'use server' module — not walked into. */
|
|
79
|
+
directive: boolean;
|
|
80
|
+
/** Static, value (non-type) import and re-export specifiers. */
|
|
81
|
+
specifiers: string[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** True when an import/export node carries TS's `type` modifier (not in ESTree). */
|
|
85
|
+
function isTypeOnly(node: object, key: 'importKind' | 'exportKind'): boolean {
|
|
86
|
+
return key in node && Reflect.get(node, key) === 'type';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Static value import/re-export specifiers of a module. */
|
|
90
|
+
export function staticSpecifiers(code: string, filename: string): string[] {
|
|
91
|
+
const ext = filename.slice(filename.lastIndexOf('.') + 1);
|
|
92
|
+
const lang = ext.includes('ts')
|
|
93
|
+
? ext.endsWith('x')
|
|
94
|
+
? 'tsx'
|
|
95
|
+
: 'ts'
|
|
96
|
+
: ext.endsWith('x')
|
|
97
|
+
? 'jsx'
|
|
98
|
+
: 'js';
|
|
99
|
+
let program: ReturnType<typeof parseAst>;
|
|
100
|
+
try {
|
|
101
|
+
program = parseAst(code, { lang });
|
|
102
|
+
} catch {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
const specs: string[] = [];
|
|
106
|
+
for (const node of program.body) {
|
|
107
|
+
if (node.type === 'ImportDeclaration') {
|
|
108
|
+
if (!isTypeOnly(node, 'importKind') && typeof node.source.value === 'string') {
|
|
109
|
+
specs.push(node.source.value);
|
|
110
|
+
}
|
|
111
|
+
} else if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportAllDeclaration') {
|
|
112
|
+
if (!isTypeOnly(node, 'exportKind') && typeof node.source?.value === 'string') {
|
|
113
|
+
specs.push(node.source.value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return specs;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Every route file that exports a server component plugin-rsc may wrap. */
|
|
121
|
+
export function collectComponentRouteFiles(
|
|
122
|
+
node: SegmentNode,
|
|
123
|
+
into = new Set<string>()
|
|
124
|
+
): Set<string> {
|
|
125
|
+
for (const file of [node.page, node.layout, node.default, node.denied, node.error]) {
|
|
126
|
+
if (file) into.add(file.filePath);
|
|
127
|
+
}
|
|
128
|
+
for (const file of Object.values(node.statusFiles ?? {})) into.add(file.filePath);
|
|
129
|
+
for (const child of node.children) collectComponentRouteFiles(child, into);
|
|
130
|
+
for (const slot of Object.values(node.slots)) collectComponentRouteFiles(slot, into);
|
|
131
|
+
return into;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function timberRouteCss(ctx: PluginContext): Plugin {
|
|
135
|
+
const scans = new Map<string, ModuleScan>();
|
|
136
|
+
const scan = (id: string, code?: string): ModuleScan | null => {
|
|
137
|
+
let stat;
|
|
138
|
+
try {
|
|
139
|
+
stat = statSync(id);
|
|
140
|
+
} catch {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
const cached = scans.get(id);
|
|
144
|
+
if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) return cached;
|
|
145
|
+
const source = code ?? readFileSync(id, 'utf8');
|
|
146
|
+
const specifiers = staticSpecifiers(source, id);
|
|
147
|
+
const result: ModuleScan = {
|
|
148
|
+
mtimeMs: stat.mtimeMs,
|
|
149
|
+
size: stat.size,
|
|
150
|
+
definesFont: specifiers.some(isFontSpecifier),
|
|
151
|
+
// Anywhere in the directive prologue (`'use strict'; 'use client'`).
|
|
152
|
+
directive: detectDirective(source, id) !== null,
|
|
153
|
+
specifiers,
|
|
154
|
+
};
|
|
155
|
+
scans.set(id, result);
|
|
156
|
+
return result;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Recomputed when the routing plugin rescans (a new tree object).
|
|
160
|
+
let tree: RouteTree | null = null;
|
|
161
|
+
let routeFiles = new Set<string>();
|
|
162
|
+
const isRouteFile = (id: string): boolean => {
|
|
163
|
+
if (ctx.routeTree !== tree) {
|
|
164
|
+
tree = ctx.routeTree;
|
|
165
|
+
routeFiles = tree ? collectComponentRouteFiles(tree.root) : new Set();
|
|
166
|
+
}
|
|
167
|
+
return routeFiles.has(id);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
name: 'timber-route-css',
|
|
172
|
+
// Before plugin-rsc's CSS export transform reads the module's imports.
|
|
173
|
+
enforce: 'pre',
|
|
174
|
+
|
|
175
|
+
resolveId: {
|
|
176
|
+
filter: { id: /virtual:timber-route-css\.css/ },
|
|
177
|
+
handler(id) {
|
|
178
|
+
const bare = id.startsWith('\0') ? id.slice(1) : id;
|
|
179
|
+
if (!bare.startsWith(ANCHOR_ID)) return null;
|
|
180
|
+
return RESOLVED_ANCHOR_ID + bare.slice(ANCHOR_ID.length);
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
load: {
|
|
185
|
+
filter: { id: /virtual:timber-route-css\.css/ },
|
|
186
|
+
handler(id) {
|
|
187
|
+
return id.startsWith(RESOLVED_ANCHOR_ID) ? '' : null;
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
transform: {
|
|
192
|
+
filter: { id: { include: SCRIPT_RE, exclude: /(?:^\0|[?&]|\/node_modules\/)/ } },
|
|
193
|
+
async handler(code, id) {
|
|
194
|
+
if (this.environment.name !== 'rsc') return null;
|
|
195
|
+
if (!isRouteFile(id) || detectDirective(code, id) !== null) return null;
|
|
196
|
+
const self = scan(id, code);
|
|
197
|
+
// A route file that calls a font loader itself already imports the
|
|
198
|
+
// font CSS, and plugin-rsc wraps it — nothing to add.
|
|
199
|
+
if (!self || self.definesFont) return null;
|
|
200
|
+
|
|
201
|
+
// Breadth-first over local server modules, stopping at the first
|
|
202
|
+
// font module.
|
|
203
|
+
let reachesFont = false;
|
|
204
|
+
const visited = new Set([id]);
|
|
205
|
+
const queue: Array<{ importer: string; specifiers: string[] }> = [
|
|
206
|
+
{ importer: id, specifiers: self.specifiers },
|
|
207
|
+
];
|
|
208
|
+
for (let item = queue.shift(); item && !reachesFont; item = queue.shift()) {
|
|
209
|
+
const { importer, specifiers } = item;
|
|
210
|
+
for (const spec of specifiers) {
|
|
211
|
+
if (isFontSpecifier(spec)) continue;
|
|
212
|
+
const resolved = await this.resolve(spec, importer);
|
|
213
|
+
if (!resolved || resolved.external) continue;
|
|
214
|
+
const next = resolved.id;
|
|
215
|
+
if (visited.has(next) || next.startsWith('\0') || next.includes('/node_modules/')) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
visited.add(next);
|
|
219
|
+
if (!SCRIPT_RE.test(next)) continue;
|
|
220
|
+
const info = scan(next);
|
|
221
|
+
if (!info || info.directive) continue;
|
|
222
|
+
if (info.definesFont) {
|
|
223
|
+
reachesFont = true;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
queue.push({ importer: next, specifiers: info.specifiers });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!reachesFont) return null;
|
|
230
|
+
|
|
231
|
+
const s = new MagicString(code);
|
|
232
|
+
s.prepend(ROUTE_CSS_ANCHOR_IMPORT);
|
|
233
|
+
return { code: s.toString(), map: s.generateMap({ hires: 'boundary' }) };
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|