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

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.
@@ -0,0 +1,13 @@
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 declare const DEFAULT_OPTIMIZE_PACKAGE_IMPORTS: readonly string[];
13
+ //# sourceMappingURL=optimize-package-imports-defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optimize-package-imports-defaults.d.ts","sourceRoot":"","sources":["../../src/plugins/optimize-package-imports-defaults.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,gCAAgC,EAAE,SAAS,MAAM,EA4E7D,CAAC"}
@@ -0,0 +1,113 @@
1
+ /**
2
+ * timber-optimize-package-imports — rewrites named imports from a listed
3
+ * barrel package into imports of only the modules that define those names.
4
+ *
5
+ * In dev, `@vitejs/plugin-rsc` keeps packages with a `react` peer dependency
6
+ * in the rsc and ssr module graphs without pre-bundling them, so on the server
7
+ * `import { Play } from 'lucide-react'` enters lucide's barrel and Vite
8
+ * transforms all ~2,000 icon modules it re-exports, once per environment. A
9
+ * build loads all of them too, before tree-shaking. The
10
+ * rewrite turns that import into one of the icon's own file:
11
+ *
12
+ * import { Play } from 'lucide-react'
13
+ * → import { default as Play } from "/…/lucide-react/dist/esm/icons/play.mjs";
14
+ *
15
+ * Next.js does the same for its `optimizePackageImports` list
16
+ * (next-barrel-loader.ts, optimize_barrel.rs). It applies to every environment
17
+ * in builds; in dev, to the server environments and only for packages Vite
18
+ * compiles itself (the client environment pre-bundles them; the server
19
+ * environments hand most other packages to Node). See design/21-dev-server.md
20
+ * §"Barrel Import Optimization".
21
+ *
22
+ * Every rule here leans toward leaving the import alone. A missed rewrite costs
23
+ * compile time; a wrong one breaks the app. An import is rewritten only when
24
+ * the barrel is a pure barrel (nothing but imports and re-exports) and every
25
+ * imported name is in its export map with a target that resolves to a file.
26
+ */
27
+ import { MagicString } from 'magic-string';
28
+ import { type Plugin } from 'vite';
29
+ import type { PluginContext } from '../plugin-context.ts';
30
+ type Lang = 'js' | 'jsx' | 'ts' | 'tsx';
31
+ /** Where a barrel's export comes from. */
32
+ export interface BarrelExport {
33
+ /** The specifier as written in the barrel (`'./icons/play.mjs'`). */
34
+ source: string;
35
+ /** The name in `source`: an export name, `'default'`, or `'*'` for a namespace. */
36
+ imported: string;
37
+ }
38
+ /** Export name → origin, for the names a barrel states explicitly. */
39
+ export type BarrelExportMap = ReadonlyMap<string, BarrelExport>;
40
+ /** A barrel the rewrite may import past, as seen from one importer. */
41
+ export interface ResolvedBarrel {
42
+ exports: BarrelExportMap;
43
+ /** Resolve a source from `exports` relative to the barrel; null = leave the import alone. */
44
+ resolveTarget(source: string): Promise<string | null>;
45
+ }
46
+ /**
47
+ * Merge the user's list with the defaults (Next's semantics: the user list
48
+ * adds, never replaces). Config validation reports invalid entries without
49
+ * stopping the dev server, so only the valid ones (non-empty strings) are used
50
+ * here; a string list is not spread into characters, and an empty entry does
51
+ * not match every quoted empty string.
52
+ */
53
+ export declare function resolveOptimizedPackages(user: readonly string[] | undefined): ReadonlySet<string>;
54
+ /**
55
+ * Read a barrel's export map, or null when the file is not a pure barrel.
56
+ *
57
+ * A pure barrel holds only imports, re-exports, and `export { local }` of an
58
+ * imported binding (`import * as icons from './icons'; export { icons }`, which
59
+ * lucide-react's barrel opens with). Anything that runs code of its own, a
60
+ * declaration, an expression, a bare side-effect import, makes it not a barrel:
61
+ * importing past it would skip that code. So does any directive except
62
+ * `'use strict'`: a `'use client'` or `'use server'` barrel is a boundary
63
+ * plugin-rsc draws around the barrel module itself, and importing past it would
64
+ * load the re-exported files on the wrong side of it.
65
+ *
66
+ * `export default local` is not followed even when `local` is imported: it
67
+ * exports the value `local` held when the barrel ran, not a live binding, so
68
+ * importing `local` directly would see later reassignments the barrel's
69
+ * default never does. `export { local as default }` is live and is followed.
70
+ *
71
+ * A source-phase or deferred import, or import attributes, change what the
72
+ * barrel's binding is, so a barrel using them is not followed. An
73
+ * `export {} from` with no names only runs the module, like a bare import.
74
+ *
75
+ * `export * from` adds no entries. A name it might supply is not in the map, so
76
+ * an import of that name falls back; explicit exports shadow star exports, so
77
+ * every name that is in the map is exact.
78
+ */
79
+ export declare function readBarrelExportMap(code: string, lang: Lang): BarrelExportMap | null;
80
+ /**
81
+ * Rewrite every import and `export { … } from` of a listed specifier whose
82
+ * names the barrel's export map covers. Returns null when nothing changed.
83
+ *
84
+ * Replacements stay on the statement's own lines, so positions after it keep
85
+ * their line numbers even before the source map is applied.
86
+ */
87
+ export declare function rewriteBarrelImports(code: string, packages: ReadonlySet<string>, loadBarrel: (specifier: string) => Promise<ResolvedBarrel | null>): Promise<MagicString | null>;
88
+ /** The resolve options that decide whether a server environment externalizes a package. */
89
+ export interface ExternalizationConfig {
90
+ noExternal: string | RegExp | readonly (string | RegExp)[] | true;
91
+ external: readonly string[] | true;
92
+ }
93
+ /**
94
+ * Whether the environment loads `specifier` through Vite rather than handing
95
+ * it to Node. Only those imports are rewritten.
96
+ *
97
+ * A server environment externalizes most of node_modules in dev: Node loads
98
+ * the package natively, so its barrel costs nothing to compile. Rewriting such
99
+ * an import to a file path would pull that file into Vite's module graph
100
+ * while any other import of the package stayed external, two instances of one
101
+ * module. In dev, plugin-rsc keeps packages with a `react` peer dependency
102
+ * (lucide-react, the icon sets, the component libraries) out of
103
+ * externalization, and timber's production build sets `noExternal: true`.
104
+ *
105
+ * Vite's own check is internal, so this is its conservative subset: the
106
+ * package must match `resolve.noExternal` exactly (a string or a RegExp) and
107
+ * `resolve.external` must not name it. A glob string in `noExternal` does
108
+ * not match here, which only costs the optimization.
109
+ */
110
+ export declare function isLoadedThroughVite(specifier: string, config: ExternalizationConfig): boolean;
111
+ export declare function timberOptimizePackageImports(ctx: PluginContext): Plugin;
112
+ export {};
113
+ //# sourceMappingURL=optimize-package-imports.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optimize-package-imports.d.ts","sourceRoot":"","sources":["../../src/plugins/optimize-package-imports.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAIH,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAQ1D,KAAK,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,CAAC;AAExC,0CAA0C;AAC1C,MAAM,WAAW,YAAY;IAC3B,qEAAqE;IACrE,MAAM,EAAE,MAAM,CAAC;IACf,mFAAmF;IACnF,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,sEAAsE;AACtE,MAAM,MAAM,eAAe,GAAG,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAEhE,uEAAuE;AACvE,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,eAAe,CAAC;IACzB,6FAA6F;IAC7F,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CACvD;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,GAAG,WAAW,CAAC,MAAM,CAAC,CAKjG;AAMD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,eAAe,GAAG,IAAI,CAmEpF;AA4ED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,EAC7B,UAAU,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,GAChE,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CA2B7B;AAQD,2FAA2F;AAC3F,MAAM,WAAW,qBAAqB;IACpC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,IAAI,CAAC;IAClE,QAAQ,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC;CACpC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAO7F;AA4BD,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAyFvE"}
@@ -29,6 +29,7 @@ interface TimberUserConfig {
29
29
  stripSensitiveFields?: boolean | readonly string[];
30
30
  };
31
31
  pageExtensions?: string[];
32
+ optimizePackageImports?: string[];
32
33
  slowRequestMs?: number;
33
34
  renderTimeoutMs?: number;
34
35
  devBrowserLogs?: 'error' | 'warn' | 'info' | 'none';
@@ -167,6 +168,25 @@ Status/error conventions (`error.*`, `global-error.*`, status files like `404.*`
167
168
 
168
169
  Two files claiming the same convention in one directory (e.g. `page.tsx` + `page.jsx`) are a build error — filesystem order is platform-dependent, so timber refuses to pick one silently.
169
170
 
171
+ ### `optimizePackageImports`
172
+
173
+ - **Type:** `string[]`
174
+ - **Default:** a built-in list (`lucide-react`, `date-fns`, `lodash-es`, `@heroicons/react/24/solid`, `react-icons/fa`, `@mui/material`, `recharts`, …, the same one Next.js uses)
175
+
176
+ Packages whose entry file (a "barrel") re-exports many modules. For these packages, timber imports each name from the module that defines it, so `import { Play } from 'lucide-react'` compiles the one icon module that defines `Play`, not every icon the package re-exports. On relisten.net this cut the first dev request from 18s to 5s and the production build from 14s to 8s.
177
+
178
+ Your list adds to the built-in one. Entries are exact import specifiers, so list each entry point you import from (`'react-icons/fa'`, not `'react-icons'`). Only list a package if its modules don't rely on each other's import-time side effects. The modules you don't import are no longer loaded:
179
+
180
+ ```ts
181
+ export default defineConfig({
182
+ optimizePackageImports: ['@acme/icons'],
183
+ });
184
+ ```
185
+
186
+ It applies to production builds. In `timber dev` it applies to server rendering of packages that Vite compiles itself, which in practice means packages with a `react` peer dependency (icon sets, component libraries). Dev hands other packages to Node, which loads them without compiling.
187
+
188
+ When timber can't map an import exactly, it leaves the import as written. For example: a namespace import, a name the barrel only re-exports through `export *` or from another package, or a barrel that runs code of its own or carries a `'use client'` or `'use server'` directive.
189
+
170
190
  ### `debug`
171
191
 
172
192
  - **Type:** `boolean`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timber-js/app",
3
- "version": "0.2.0-alpha.223",
3
+ "version": "0.2.0-alpha.224",
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",
@@ -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,7 @@ 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';
38
39
  import { timberPrebuilt } from './plugins/prebuilt.ts';
39
40
  import { timberPrerenderSugar } from './plugins/prerender-sugar.ts';
40
41
  import { timberCloudflareWasm } from './plugins/cloudflare-wasm.ts';
@@ -577,6 +578,7 @@ export function timber(config?: TimberUserConfig): PluginOption[] {
577
578
  timberBuildManifest(ctx),
578
579
  fonts,
579
580
  timberMdx(ctx),
581
+ timberOptimizePackageImports(ctx), // Listed barrels → the modules that define the imported names
580
582
  timberRequestDep(ctx),
581
583
  timberCacheTransform(ctx),
582
584
  timberPrerenderSugar(ctx), // export const prerender → cache.component() desugaring
@@ -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
+ ];