@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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAU,YAAY,EAAE,MAAM,MAAM,CAAC;AA+CjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAa1D,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAQ1E,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAClG,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEpF;;;;;;;;;;;;;;GAcG;AAEH,MAAM,WAAW,MAAM;CAAG;AAE1B;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,gBAAgB,GAAG,gBAAgB,CAEvE;AAmHD,wBAAgB,MAAM,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAAG,YAAY,EAAE,CAoXhE;eAEc,MAAM"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAU,YAAY,EAAE,MAAM,MAAM,CAAC;AAgDjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAa1D,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAQ1E,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAClG,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEpF;;;;;;;;;;;;;;GAcG;AAEH,MAAM,WAAW,MAAM;CAAG;AAE1B;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,gBAAgB,GAAG,gBAAgB,CAEvE;AAmHD,wBAAgB,MAAM,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAAG,YAAY,EAAE,CAqXhE;eAEc,MAAM"}
package/dist/index.js CHANGED
@@ -5,12 +5,12 @@ import { c as writeRouteCodegen, i as readRouteOptions, l as buildDirToRouteMap,
5
5
  import { r as validateSchemaAgainstRoutes, t as collectLeafRoutes } from "./_chunks/walkers-BAZoAlfI.js";
6
6
  import { i as poisonPillSource, r as markerFor, t as EXPLICIT_MARKERS } from "./_chunks/poison-rules-Bmz-lCPi.js";
7
7
  import { a as EMPTY_BUILD_MANIFEST, n as DEV_SPAN_REPLAY_EVENT, r as DEV_SPAN_SINK_KEY, t as DEV_SPAN_EVENT } from "./_chunks/dev-span-0aG7Q_Ph.js";
8
+ import { i as detectDirective } from "./_chunks/classify-Bbb_lKjH.js";
8
9
  import { t as writeFileAtomic } from "./_chunks/write-file-atomic-DW8m_Atv.js";
9
10
  import { i as withBasePath, n as normalizeBasePath, t as isRelativeBase } from "./_chunks/base-path-DDIx0FXe.js";
10
11
  import { n as rscPayloadPath, t as RSC_PAYLOAD_DIR } from "./_chunks/rsc-payload-path-EphpDB-J.js";
11
12
  import { a as REDIRECTS_FILE, n as detectFsIdentity, o as RESERVED_OUTPUT_PATHS, r as fsKey, t as EXACT_FS_IDENTITY } from "./_chunks/fs-identity-BmQ_Agg-.js";
12
13
  import { t as fnv1aHash } from "./_chunks/fast-hash-DddxkU5Y.js";
13
- import { i as detectDirective } from "./_chunks/classify-Bbb_lKjH.js";
14
14
  import { a as contextAdapter, c as loadTimberConfigFile, d as resolveBuildDir, f as resolveClientJavascript, l as mergeFileConfig, m as createNoopTimer, o as createPluginContext, r as adapterConfig, u as resolveAppDir } from "./_chunks/plugin-context-BtaQcMuh.js";
15
15
  import { x as swallow } from "./_chunks/logger-D1sWX2I0.js";
16
16
  import { t as formatSize } from "./_chunks/format-DuPOoXUP.js";
@@ -5665,6 +5665,205 @@ function timberFonts(ctx) {
5665
5665
  }
5666
5666
  };
5667
5667
  }
5668
+ // src/plugins/route-css.ts
5669
+ /**
5670
+ * timber-route-css — link font CSS that a route file reaches only through
5671
+ * a plain (non-component) module.
5672
+ *
5673
+ * @vitejs/plugin-rsc renders a server component's stylesheets by wrapping
5674
+ * the component in `import.meta.viteRsc.loadCss()`, which collects every
5675
+ * CSS file the module reaches, directly or through other modules. But it
5676
+ * only applies that wrap to a module that imports CSS **itself**
5677
+ * (`rsc:rsc-css-export-transform`). A font is usually defined in a plain
5678
+ * module, and the font's CSS import is injected there:
5679
+ *
5680
+ * ```ts
5681
+ * // app/fonts.ts — the font's @font-face CSS is imported here
5682
+ * export const body = localFont({ src: './TX-02.woff2' });
5683
+ *
5684
+ * // app/layout.tsx — imports no CSS of its own, so nothing is wrapped
5685
+ * import { body } from './fonts';
5686
+ * ```
5687
+ *
5688
+ * The page rendered with the font's class and no `@font-face`.
5689
+ *
5690
+ * This plugin gives such a route file a static import of an empty
5691
+ * stylesheet, so plugin-rsc wraps it exactly as it wraps a layout that
5692
+ * imports `globals.css`, and `loadCss()` collects the font CSS. The anchor
5693
+ * is imported with `?inline`: plugin-rsc's wrap check counts it as a CSS
5694
+ * import, while its collector skips `?inline` ids (no `<link>` in dev) and
5695
+ * the build compiles it to a JS string (no asset).
5696
+ *
5697
+ * **Only route files that reach a font module are touched.** The wrap is
5698
+ * not free: in dev it renders an async `Resources` component beside the
5699
+ * route component, which shifts shell timing (a delayed `deny()` can land
5700
+ * before the shell commits). Wrapping every route file changed dev status
5701
+ * codes in apps with no fonts at all. Route files that import CSS are
5702
+ * wrapped by plugin-rsc already; this adds only the font case.
5703
+ *
5704
+ * A route file is transformed before its imports, so the font pipeline
5705
+ * can't answer "does this reach a font?" yet. The plugin scans source
5706
+ * instead: it follows the route file's static imports through local,
5707
+ * server-side modules (not node_modules, not `'use client'` — a client
5708
+ * module's CSS rides its client reference) and stops at the first module
5709
+ * that imports a font loader. Scans are cached per file by mtime.
5710
+ *
5711
+ * Not touched: `'use client'` route files (a statement before the directive
5712
+ * would cancel it), MDX (frontmatter must stay on line 1), and route.ts /
5713
+ * middleware.ts / access.ts / metadata routes (not components).
5714
+ *
5715
+ * Known limit (dev): adding the first font call to a module a route file
5716
+ * already imports takes effect when the route file is next transformed
5717
+ * (edit or restart), because the route file's own source did not change.
5718
+ *
5719
+ * Design doc: 24-fonts.md §"Step 3: @font-face Generation"
5720
+ */
5721
+ var ANCHOR_ID = "virtual:timber-route-css.css";
5722
+ var RESOLVED_ANCHOR_ID = "\0" + ANCHOR_ID;
5723
+ /** The import the transform adds. See the module comment for `?inline`. */
5724
+ var ROUTE_CSS_ANCHOR_IMPORT = `import '${ANCHOR_ID}?inline';\n`;
5725
+ var SCRIPT_RE$1 = /\.[cm]?[jt]sx?$/;
5726
+ /** Module specifiers that make a module a font module (same as the fonts transform). */
5727
+ var FONT_SPECIFIER_PREFIXES = [
5728
+ "@timber/fonts/",
5729
+ "@timber-js/app/fonts/",
5730
+ "next/font/"
5731
+ ];
5732
+ var isFontSpecifier = (spec) => FONT_SPECIFIER_PREFIXES.some((p) => spec.startsWith(p));
5733
+ /** True when an import/export node carries TS's `type` modifier (not in ESTree). */
5734
+ function isTypeOnly(node, key) {
5735
+ return key in node && Reflect.get(node, key) === "type";
5736
+ }
5737
+ /** Static value import/re-export specifiers of a module. */
5738
+ function staticSpecifiers(code, filename) {
5739
+ const ext = filename.slice(filename.lastIndexOf(".") + 1);
5740
+ const lang = ext.includes("ts") ? ext.endsWith("x") ? "tsx" : "ts" : ext.endsWith("x") ? "jsx" : "js";
5741
+ let program;
5742
+ try {
5743
+ program = parseAst(code, { lang });
5744
+ } catch {
5745
+ return [];
5746
+ }
5747
+ const specs = [];
5748
+ for (const node of program.body) if (node.type === "ImportDeclaration") {
5749
+ if (!isTypeOnly(node, "importKind") && typeof node.source.value === "string") specs.push(node.source.value);
5750
+ } else if (node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") {
5751
+ if (!isTypeOnly(node, "exportKind") && typeof node.source?.value === "string") specs.push(node.source.value);
5752
+ }
5753
+ return specs;
5754
+ }
5755
+ /** Every route file that exports a server component plugin-rsc may wrap. */
5756
+ function collectComponentRouteFiles(node, into = /* @__PURE__ */ new Set()) {
5757
+ for (const file of [
5758
+ node.page,
5759
+ node.layout,
5760
+ node.default,
5761
+ node.denied,
5762
+ node.error
5763
+ ]) if (file) into.add(file.filePath);
5764
+ for (const file of Object.values(node.statusFiles ?? {})) into.add(file.filePath);
5765
+ for (const child of node.children) collectComponentRouteFiles(child, into);
5766
+ for (const slot of Object.values(node.slots)) collectComponentRouteFiles(slot, into);
5767
+ return into;
5768
+ }
5769
+ function timberRouteCss(ctx) {
5770
+ const scans = /* @__PURE__ */ new Map();
5771
+ const scan = (id, code) => {
5772
+ let stat;
5773
+ try {
5774
+ stat = statSync(id);
5775
+ } catch {
5776
+ return null;
5777
+ }
5778
+ const cached = scans.get(id);
5779
+ if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) return cached;
5780
+ const source = code ?? readFileSync(id, "utf8");
5781
+ const specifiers = staticSpecifiers(source, id);
5782
+ const result = {
5783
+ mtimeMs: stat.mtimeMs,
5784
+ size: stat.size,
5785
+ definesFont: specifiers.some(isFontSpecifier),
5786
+ directive: detectDirective(source, id) !== null,
5787
+ specifiers
5788
+ };
5789
+ scans.set(id, result);
5790
+ return result;
5791
+ };
5792
+ let tree = null;
5793
+ let routeFiles = /* @__PURE__ */ new Set();
5794
+ const isRouteFile = (id) => {
5795
+ if (ctx.routeTree !== tree) {
5796
+ tree = ctx.routeTree;
5797
+ routeFiles = tree ? collectComponentRouteFiles(tree.root) : /* @__PURE__ */ new Set();
5798
+ }
5799
+ return routeFiles.has(id);
5800
+ };
5801
+ return {
5802
+ name: "timber-route-css",
5803
+ enforce: "pre",
5804
+ resolveId: {
5805
+ filter: { id: /virtual:timber-route-css\.css/ },
5806
+ handler(id) {
5807
+ const bare = id.startsWith("\0") ? id.slice(1) : id;
5808
+ if (!bare.startsWith(ANCHOR_ID)) return null;
5809
+ return RESOLVED_ANCHOR_ID + bare.slice(28);
5810
+ }
5811
+ },
5812
+ load: {
5813
+ filter: { id: /virtual:timber-route-css\.css/ },
5814
+ handler(id) {
5815
+ return id.startsWith(RESOLVED_ANCHOR_ID) ? "" : null;
5816
+ }
5817
+ },
5818
+ transform: {
5819
+ filter: { id: {
5820
+ include: SCRIPT_RE$1,
5821
+ exclude: /(?:^\0|[?&]|\/node_modules\/)/
5822
+ } },
5823
+ async handler(code, id) {
5824
+ if (this.environment.name !== "rsc") return null;
5825
+ if (!isRouteFile(id) || detectDirective(code, id) !== null) return null;
5826
+ const self = scan(id, code);
5827
+ if (!self || self.definesFont) return null;
5828
+ let reachesFont = false;
5829
+ const visited = /* @__PURE__ */ new Set([id]);
5830
+ const queue = [{
5831
+ importer: id,
5832
+ specifiers: self.specifiers
5833
+ }];
5834
+ for (let item = queue.shift(); item && !reachesFont; item = queue.shift()) {
5835
+ const { importer, specifiers } = item;
5836
+ for (const spec of specifiers) {
5837
+ if (isFontSpecifier(spec)) continue;
5838
+ const resolved = await this.resolve(spec, importer);
5839
+ if (!resolved || resolved.external) continue;
5840
+ const next = resolved.id;
5841
+ if (visited.has(next) || next.startsWith("\0") || next.includes("/node_modules/")) continue;
5842
+ visited.add(next);
5843
+ if (!SCRIPT_RE$1.test(next)) continue;
5844
+ const info = scan(next);
5845
+ if (!info || info.directive) continue;
5846
+ if (info.definesFont) {
5847
+ reachesFont = true;
5848
+ break;
5849
+ }
5850
+ queue.push({
5851
+ importer: next,
5852
+ specifiers: info.specifiers
5853
+ });
5854
+ }
5855
+ }
5856
+ if (!reachesFont) return null;
5857
+ const s = new MagicString(code);
5858
+ s.prepend(ROUTE_CSS_ANCHOR_IMPORT);
5859
+ return {
5860
+ code: s.toString(),
5861
+ map: s.generateMap({ hires: "boundary" })
5862
+ };
5863
+ }
5864
+ }
5865
+ };
5866
+ }
5668
5867
  // src/server/flight-scripts.ts
5669
5868
  /**
5670
5869
  * Escape a JSON string for safe embedding inside an HTML `<script>` tag.
@@ -11129,6 +11328,7 @@ function timber(config) {
11129
11328
  timberEntries(ctx),
11130
11329
  timberBuildManifest(ctx),
11131
11330
  fonts,
11331
+ timberRouteCss(ctx),
11132
11332
  timberMdx(ctx),
11133
11333
  timberOptimizePackageImports(ctx),
11134
11334
  timberClientInServerPackageDeps(),