@timber-js/app 0.2.0-alpha.223 → 0.2.0-alpha.225

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.
Files changed (91) hide show
  1. package/dist/_chunks/{actions-7dTXYJfu.js → actions-B1xSIv32.js} +2 -2
  2. package/dist/_chunks/{actions-7dTXYJfu.js.map → actions-B1xSIv32.js.map} +1 -1
  3. package/dist/_chunks/{cache-api-TDZ4hlDc.js → cache-api-DsSXA4Sp.js} +68 -47
  4. package/dist/_chunks/cache-api-DsSXA4Sp.js.map +1 -0
  5. package/dist/_chunks/{cli-check-Dv3f8d2j.js → cli-check-B33yhAPa.js} +2 -2
  6. package/dist/_chunks/{cli-check-Dv3f8d2j.js.map → cli-check-B33yhAPa.js.map} +1 -1
  7. package/dist/_chunks/cli-context-BPdyA7Fp.js +1 -0
  8. package/dist/_chunks/{cli-context-CbZp32SK.js → cli-context-CwTKMjVe.js} +10 -2
  9. package/dist/_chunks/cli-context-CwTKMjVe.js.map +1 -0
  10. package/dist/_chunks/{dev-server-C61GWpGg.js → dev-server-B7o4As0l.js} +15 -1
  11. package/dist/_chunks/dev-server-B7o4As0l.js.map +1 -0
  12. package/dist/_chunks/dev-server-hZzmbMvK.js +1 -0
  13. package/dist/_chunks/{error-boundary-DRQXGWCp.js → error-boundary-D3YyVVuP.js} +2 -2
  14. package/dist/_chunks/{error-boundary-DRQXGWCp.js.map → error-boundary-D3YyVVuP.js.map} +1 -1
  15. package/dist/_chunks/{singleflight-BChPMjwH.js → singleflight-CzoOVB9Y.js} +2 -3
  16. package/dist/_chunks/singleflight-CzoOVB9Y.js.map +1 -0
  17. package/dist/_chunks/{status-page-marker-XIQXlTOb.js → status-page-marker-C8POawuF.js} +2 -2
  18. package/dist/_chunks/{status-page-marker-XIQXlTOb.js.map → status-page-marker-C8POawuF.js.map} +1 -1
  19. package/dist/adapters/cloudflare-kv-cache.d.ts +0 -1
  20. package/dist/adapters/cloudflare-kv-cache.d.ts.map +1 -1
  21. package/dist/adapters/cloudflare-kv-cache.js.map +1 -1
  22. package/dist/cache/index.d.ts +5 -5
  23. package/dist/cache/index.d.ts.map +1 -1
  24. package/dist/cache/index.js +1 -1
  25. package/dist/cache/redis-handler.d.ts +0 -1
  26. package/dist/cache/redis-handler.d.ts.map +1 -1
  27. package/dist/cache/singleflight.d.ts +12 -2
  28. package/dist/cache/singleflight.d.ts.map +1 -1
  29. package/dist/cache/stable-stringify.d.ts +6 -0
  30. package/dist/cache/stable-stringify.d.ts.map +1 -1
  31. package/dist/cache/tag-aware-handler.d.ts +0 -1
  32. package/dist/cache/tag-aware-handler.d.ts.map +1 -1
  33. package/dist/cache/timber-cache.d.ts +1 -1
  34. package/dist/cache/timber-cache.d.ts.map +1 -1
  35. package/dist/cli-context.d.ts +8 -0
  36. package/dist/cli-context.d.ts.map +1 -1
  37. package/dist/cli.d.ts.map +1 -1
  38. package/dist/cli.js +9 -3
  39. package/dist/cli.js.map +1 -1
  40. package/dist/client/error-boundary.js +1 -1
  41. package/dist/client/internal.js +3 -3
  42. package/dist/config-types.d.ts +18 -0
  43. package/dist/config-types.d.ts.map +1 -1
  44. package/dist/config-validation.d.ts.map +1 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +545 -9
  47. package/dist/index.js.map +1 -1
  48. package/dist/plugins/client-in-server-package-deps.d.ts +63 -0
  49. package/dist/plugins/client-in-server-package-deps.d.ts.map +1 -0
  50. package/dist/plugins/optimize-package-imports-defaults.d.ts +13 -0
  51. package/dist/plugins/optimize-package-imports-defaults.d.ts.map +1 -0
  52. package/dist/plugins/optimize-package-imports.d.ts +124 -0
  53. package/dist/plugins/optimize-package-imports.d.ts.map +1 -0
  54. package/dist/server/flight-injector-core.d.ts.map +1 -1
  55. package/dist/server/index.js +1 -1
  56. package/dist/server/internal.js +162 -33
  57. package/dist/server/internal.js.map +1 -1
  58. package/dist/server/pipeline-helpers.d.ts +5 -2
  59. package/dist/server/pipeline-helpers.d.ts.map +1 -1
  60. package/dist/server/pipeline.d.ts.map +1 -1
  61. package/dist/server/prebuilt-runtime.d.ts.map +1 -1
  62. package/dist/server/response-framing.d.ts +43 -0
  63. package/dist/server/response-framing.d.ts.map +1 -0
  64. package/docs/api/34-api-config.mdx +20 -0
  65. package/package.json +2 -2
  66. package/src/adapters/cloudflare-kv-cache.ts +1 -5
  67. package/src/cache/index.ts +7 -27
  68. package/src/cache/redis-handler.ts +1 -5
  69. package/src/cache/singleflight.ts +17 -5
  70. package/src/cache/stable-stringify.ts +53 -0
  71. package/src/cache/tag-aware-handler.ts +1 -5
  72. package/src/cache/timber-cache.ts +55 -52
  73. package/src/cli-context.ts +9 -0
  74. package/src/cli.ts +16 -0
  75. package/src/config-types.ts +18 -0
  76. package/src/config-validation.ts +20 -0
  77. package/src/index.ts +4 -0
  78. package/src/plugins/client-in-server-package-deps.ts +139 -0
  79. package/src/plugins/optimize-package-imports-defaults.ts +88 -0
  80. package/src/plugins/optimize-package-imports.ts +439 -0
  81. package/src/server/flight-injector-core.ts +13 -9
  82. package/src/server/pipeline-helpers.ts +9 -5
  83. package/src/server/pipeline.ts +7 -0
  84. package/src/server/prebuilt-runtime.ts +125 -121
  85. package/src/server/response-framing.ts +59 -0
  86. package/dist/_chunks/cache-api-TDZ4hlDc.js.map +0 -1
  87. package/dist/_chunks/cli-context-CbZp32SK.js.map +0 -1
  88. package/dist/_chunks/cli-context-n5hmkdla.js +0 -1
  89. package/dist/_chunks/dev-server-C61GWpGg.js.map +0 -1
  90. package/dist/_chunks/dev-server-CBvvM7Lr.js +0 -1
  91. package/dist/_chunks/singleflight-BChPMjwH.js.map +0 -1
@@ -85,6 +85,24 @@ export interface TimberUserConfig {
85
85
  stripSensitiveFields?: boolean | readonly string[];
86
86
  };
87
87
  pageExtensions?: string[];
88
+ /**
89
+ * Import specifiers whose barrel imports timber rewrites to the
90
+ * modules that define the imported names, so `import { Play } from
91
+ * 'lucide-react'` compiles one icon instead of every icon the package
92
+ * re-exports. Applies to production builds, and in dev to server rendering
93
+ * of packages Vite compiles itself: those with a `react` peer dependency
94
+ * (icon sets, component libraries). Dev hands other packages to Node, and
95
+ * the dev client pre-bundles them.
96
+ *
97
+ * Merged with a built-in list (the same one Next.js uses: lucide-react,
98
+ * date-fns, lodash-es, @heroicons/react/*, react-icons/*, …); entries here
99
+ * add to it. Only list a package if its modules do not rely on each other's
100
+ * import-time side effects: the modules not imported no longer load. Entries
101
+ * are exact specifiers: list `react-icons/fa`, not `react-icons`.
102
+ *
103
+ * See design/21-dev-server.md §"Barrel Import Optimization".
104
+ */
105
+ optimizePackageImports?: string[];
88
106
  /**
89
107
  * Slow request threshold in milliseconds. Requests exceeding this emit
90
108
  * a warning via the logger. Set to 0 to disable. Default: 3000.
@@ -43,6 +43,7 @@ const KNOWN_CONFIG_KEYS = {
43
43
  limits: true,
44
44
  forms: true,
45
45
  pageExtensions: true,
46
+ optimizePackageImports: true,
46
47
  slowRequestMs: true,
47
48
  renderTimeoutMs: true,
48
49
  devBrowserLogs: true,
@@ -115,6 +116,25 @@ export function validateConfig(config: TimberUserConfig): ConfigError[] {
115
116
  }
116
117
  }
117
118
 
119
+ // optimizePackageImports
120
+ if (config.optimizePackageImports !== undefined) {
121
+ const list: unknown = config.optimizePackageImports;
122
+ const badIndex = Array.isArray(list)
123
+ ? list.findIndex((entry) => typeof entry !== 'string' || entry.trim() === '')
124
+ : -1;
125
+ if (!Array.isArray(list) || badIndex !== -1) {
126
+ const value = Array.isArray(list) ? list[badIndex] : list;
127
+ errors.push({
128
+ field: 'optimizePackageImports',
129
+ message: Array.isArray(list)
130
+ ? `optimizePackageImports contains an invalid entry: ${String(JSON.stringify(value))}`
131
+ : 'optimizePackageImports must be an array of import specifiers.',
132
+ value,
133
+ suggestion: 'Example: optimizePackageImports: ["@acme/icons", "react-icons/fa"]',
134
+ });
135
+ }
136
+ }
137
+
118
138
  // allowedOrigins
119
139
  if (config.allowedOrigins !== undefined) {
120
140
  if (!Array.isArray(config.allowedOrigins)) {
package/src/index.ts CHANGED
@@ -35,6 +35,8 @@ import { timberServerBundle } from './plugins/server-bundle.ts';
35
35
  import { timberEncryptionRuntime } from './plugins/encryption-runtime.ts';
36
36
  import { timberCacheTransform } from './plugins/cache.ts';
37
37
  import { timberRequestDep } from './plugins/request-dep/index.ts';
38
+ import { timberOptimizePackageImports } from './plugins/optimize-package-imports.ts';
39
+ import { timberClientInServerPackageDeps } from './plugins/client-in-server-package-deps.ts';
38
40
  import { timberPrebuilt } from './plugins/prebuilt.ts';
39
41
  import { timberPrerenderSugar } from './plugins/prerender-sugar.ts';
40
42
  import { timberCloudflareWasm } from './plugins/cloudflare-wasm.ts';
@@ -577,6 +579,8 @@ export function timber(config?: TimberUserConfig): PluginOption[] {
577
579
  timberBuildManifest(ctx),
578
580
  fonts,
579
581
  timberMdx(ctx),
582
+ timberOptimizePackageImports(ctx), // Listed barrels → the modules that define the imported names
583
+ timberClientInServerPackageDeps(), // Dev: a package's inner 'use client' file → its bare path, so the browser pre-bundles it
580
584
  timberRequestDep(ctx),
581
585
  timberCacheTransform(ctx),
582
586
  timberPrerenderSugar(ctx), // export const prerender → cache.component() desugaring
@@ -0,0 +1,139 @@
1
+ /**
2
+ * timber-client-in-server-package-deps — in dev, the RSC environment resolves
3
+ * a package's inner 'use client' file through its bare package path, so the
4
+ * browser loads it from the client pre-bundle.
5
+ *
6
+ * plugin-rsc records a package source only for a file the RSC environment
7
+ * reaches through a bare import. A server component that reaches a 'use
8
+ * client' file through the package's own relative imports (lucide-react's
9
+ * `icons/play.mjs` → `createLucideIcon.mjs` → `./Icon.mjs`, or
10
+ * `@mui/material/Button` → `./Button.mjs`) gets a client reference whose
11
+ * browser module re-exports the file's absolute path
12
+ * (`client-in-server-package-proxy`). Vite serves that file, and everything it
13
+ * imports, raw:
14
+ *
15
+ * - If an app client component also imports the package, the browser holds
16
+ * two instances of those modules, the pre-bundled one and the raw one, so a
17
+ * context the package provides (`LucideProvider`) does not reach icons
18
+ * rendered by server components. plugin-rsc detects this and warns "client
19
+ * component dependency is inconsistently optimized" on every dev start.
20
+ * - If the file imports a CommonJS package (`prop-types`), the browser
21
+ * cannot link it and the page fails (vitejs/vite-plugin-react#1538).
22
+ *
23
+ * This plugin resolves such an import as the bare specifier the file has from
24
+ * the app root (`lucide-react/dist/esm/Icon.mjs`). plugin-rsc then records that
25
+ * specifier as the file's package source and gives the browser
26
+ * `client-package-proxy`, which imports the bare specifier. The client deps
27
+ * optimizer pre-bundles it like any package import, splitting the modules it
28
+ * shares with the package's own entry into one chunk, so the browser loads one
29
+ * instance. No inconsistency remains, so plugin-rsc does not warn.
30
+ *
31
+ * Every rule leans toward leaving the import alone, which keeps plugin-rsc's
32
+ * raw-file behavior:
33
+ * - only in the RSC environment, in dev (a build has no proxy modules);
34
+ * - only for a non-bare source (a bare one already gets a package source);
35
+ * - only from a package file, or for an absolute path into `node_modules`
36
+ * (what the barrel rewrite emits), so an app module's own relative imports
37
+ * are not resolved twice;
38
+ * - only when the resolved id is a plain file (no query but Vite's `?v=`; a
39
+ * `?raw` or `?url` import asks for something other than the module), under
40
+ * `node_modules`, whose directive prologue holds 'use client';
41
+ * - only when the bare specifier resolves to the same file, from the importer
42
+ * here and from the app root in plugin-rsc. A package whose `exports` hides
43
+ * the file, or a transitive dependency the app cannot import by name, stays
44
+ * as it is.
45
+ *
46
+ * A file found after startup is a new client dep, so the client optimizer
47
+ * re-runs once, as for any import discovered late; Vite keeps it in the dep
48
+ * cache for later starts. See design/21-dev-server.md §"Inner 'use client'
49
+ * Files of Packages".
50
+ *
51
+ * Remove once plugin-rsc imports these files through the client pre-bundle
52
+ * itself.
53
+ */
54
+
55
+ import { readFile } from 'node:fs/promises';
56
+ import type { Plugin } from 'vite';
57
+ import { detectDirective } from '../analyze/classify.ts';
58
+ import { asFilePath } from './optimize-package-imports.ts';
59
+
60
+ const NODE_MODULES = '/node_modules/';
61
+
62
+ // Vite's own test for a bare import (packages/vite/src/node/utils.ts).
63
+ const BARE_IMPORT_RE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/;
64
+
65
+ /**
66
+ * The bare specifier `file` has as a package path, or null when the file is not
67
+ * inside a package. The last `node_modules` segment is the package, so a pnpm
68
+ * store path (`.pnpm/lucide-react@1.66.0/node_modules/lucide-react/…`) names
69
+ * the package, not the store.
70
+ */
71
+ export function packagePathOf(file: string): string | null {
72
+ const index = file.lastIndexOf(NODE_MODULES);
73
+ if (index === -1) return null;
74
+ const id = file.slice(index + NODE_MODULES.length);
75
+ return id === '' || id.startsWith('.') ? null : id;
76
+ }
77
+
78
+ export function timberClientInServerPackageDeps(rscEnvironmentName = 'rsc'): Plugin {
79
+ // File path → whether its directive prologue holds 'use client'. Installed
80
+ // packages are assumed not to change while the process runs, as the barrel
81
+ // rewrite assumes. The read is cached as a promise so concurrent resolves
82
+ // share it; a failed read caches false (the import is left alone).
83
+ const isClientFile = new Map<string, Promise<boolean>>();
84
+
85
+ function hasUseClient(file: string): Promise<boolean> {
86
+ let cached = isClientFile.get(file);
87
+ if (!cached) {
88
+ cached = readFile(file, 'utf8').then(
89
+ // A text check skips parsing the files that cannot carry it.
90
+ (code) => code.includes('use client') && detectDirective(code, file) === 'use client',
91
+ () => false
92
+ );
93
+ isClientFile.set(file, cached);
94
+ }
95
+ return cached;
96
+ }
97
+
98
+ return {
99
+ name: 'timber-client-in-server-package-deps',
100
+ apply: 'serve',
101
+ resolveId: {
102
+ order: 'pre',
103
+ async handler(source, importer, options) {
104
+ if (this.environment.name !== rscEnvironmentName || !importer) return;
105
+ if (BARE_IMPORT_RE.test(source) || source.startsWith('\0')) return;
106
+ // A package's own relative imports, or an absolute path into a
107
+ // package (what the barrel rewrite emits). An app module's relative
108
+ // imports stay in the app and are not resolved twice.
109
+ if (!importer.includes(NODE_MODULES) && !source.includes(NODE_MODULES)) return;
110
+ const resolved = await this.resolve(source, importer, { ...options, skipSelf: true });
111
+ if (!resolved || resolved.external) return resolved;
112
+ // Vite's `?v=<hash>` is dropped; any other query (`?raw`, `?url`)
113
+ // asks for something other than the module, so it is left alone.
114
+ const file = asFilePath(resolved.id);
115
+ const bare = file && packagePathOf(file);
116
+ if (!file || !bare || !(await hasUseClient(file))) return resolved;
117
+ // Resolving the bare specifier runs plugin-rsc's resolver for bare
118
+ // imports, which records it as the file's package source when it
119
+ // resolves from the app root to this same file. Otherwise it records
120
+ // nothing and the file keeps its raw proxy.
121
+ //
122
+ // The bare specifier is one this plugin made up, not one the package
123
+ // wrote, so it may not resolve at all: a package whose `exports` map
124
+ // hides the file still imports it by a relative path, and Vite throws
125
+ // ("is not exported under the conditions") instead of returning
126
+ // null. That only means the file has no package path; the relative
127
+ // import that already resolved stays.
128
+ let viaPackage: Awaited<ReturnType<typeof this.resolve>>;
129
+ try {
130
+ viaPackage = await this.resolve(bare, importer, { ...options, skipSelf: true });
131
+ } catch {
132
+ return resolved;
133
+ }
134
+ if (!viaPackage || asFilePath(viaPackage.id) !== file) return resolved;
135
+ return viaPackage;
136
+ },
137
+ },
138
+ };
139
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Import specifiers whose barrel imports timber rewrites by default.
3
+ *
4
+ * Copied from Next.js's built-in `optimizePackageImports` list
5
+ * (packages/next/src/server/config.ts, vercel/next.js@50bf604f2), so an app
6
+ * moving from Next gets the same set without configuring it. Entries are exact
7
+ * import specifiers, not package names: a package with several icon entry
8
+ * points (`react-icons/fa`, `@heroicons/react/24/solid`) lists each one.
9
+ *
10
+ * See design/21-dev-server.md §"Barrel Import Optimization".
11
+ */
12
+ export const DEFAULT_OPTIMIZE_PACKAGE_IMPORTS: readonly string[] = [
13
+ 'lucide-react',
14
+ 'date-fns',
15
+ 'lodash-es',
16
+ 'ramda',
17
+ 'antd',
18
+ 'react-bootstrap',
19
+ 'ahooks',
20
+ '@ant-design/icons',
21
+ '@headlessui/react',
22
+ '@headlessui-float/react',
23
+ '@heroicons/react/20/solid',
24
+ '@heroicons/react/24/solid',
25
+ '@heroicons/react/24/outline',
26
+ '@visx/visx',
27
+ '@tremor/react',
28
+ 'rxjs',
29
+ '@mui/material',
30
+ '@mui/icons-material',
31
+ 'recharts',
32
+ 'react-use',
33
+ 'effect',
34
+ '@effect/schema',
35
+ '@effect/platform',
36
+ '@effect/platform-node',
37
+ '@effect/platform-browser',
38
+ '@effect/platform-bun',
39
+ '@effect/sql',
40
+ '@effect/sql-mssql',
41
+ '@effect/sql-mysql2',
42
+ '@effect/sql-pg',
43
+ '@effect/sql-sqlite-node',
44
+ '@effect/sql-sqlite-bun',
45
+ '@effect/sql-sqlite-wasm',
46
+ '@effect/sql-sqlite-react-native',
47
+ '@effect/rpc',
48
+ '@effect/rpc-http',
49
+ '@effect/typeclass',
50
+ '@effect/experimental',
51
+ '@effect/opentelemetry',
52
+ '@material-ui/core',
53
+ '@material-ui/icons',
54
+ '@tabler/icons-react',
55
+ 'mui-core',
56
+ 'react-icons/ai',
57
+ 'react-icons/bi',
58
+ 'react-icons/bs',
59
+ 'react-icons/cg',
60
+ 'react-icons/ci',
61
+ 'react-icons/di',
62
+ 'react-icons/fa',
63
+ 'react-icons/fa6',
64
+ 'react-icons/fc',
65
+ 'react-icons/fi',
66
+ 'react-icons/gi',
67
+ 'react-icons/go',
68
+ 'react-icons/gr',
69
+ 'react-icons/hi',
70
+ 'react-icons/hi2',
71
+ 'react-icons/im',
72
+ 'react-icons/io',
73
+ 'react-icons/io5',
74
+ 'react-icons/lia',
75
+ 'react-icons/lib',
76
+ 'react-icons/lu',
77
+ 'react-icons/md',
78
+ 'react-icons/pi',
79
+ 'react-icons/ri',
80
+ 'react-icons/rx',
81
+ 'react-icons/si',
82
+ 'react-icons/sl',
83
+ 'react-icons/tb',
84
+ 'react-icons/tfi',
85
+ 'react-icons/ti',
86
+ 'react-icons/vsc',
87
+ 'react-icons/wi',
88
+ ];