@cloudflare/worker-bundler 0.0.0 → 0.0.3

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,383 @@
1
+ //#region src/types.d.ts
2
+ /**
3
+ * Input files for the bundler
4
+ * Keys are file paths, values are file contents
5
+ */
6
+ type Files = Record<string, string>;
7
+ /**
8
+ * Module format for Worker Loader binding
9
+ */
10
+ interface Module {
11
+ js?: string;
12
+ cjs?: string;
13
+ text?: string;
14
+ data?: ArrayBuffer;
15
+ json?: object;
16
+ }
17
+ /**
18
+ * Output modules for Worker Loader binding
19
+ */
20
+ type Modules = Record<string, string | Module>;
21
+ /**
22
+ * Options for createWorker
23
+ */
24
+ interface CreateWorkerOptions {
25
+ /**
26
+ * Input files - keys are paths relative to project root, values are file contents
27
+ */
28
+ files: Files;
29
+ /**
30
+ * Entry point file path (relative to project root)
31
+ * If not specified, will try to determine from wrangler.toml main field,
32
+ * then package.json, then default paths (src/index.ts, etc.)
33
+ */
34
+ entryPoint?: string;
35
+ /**
36
+ * Whether to bundle all dependencies into a single file
37
+ * @default true
38
+ */
39
+ bundle?: boolean;
40
+ /**
41
+ * External modules that should not be bundled.
42
+ * Note: `cloudflare:*` modules are always treated as external.
43
+ */
44
+ externals?: string[];
45
+ /**
46
+ * Target environment
47
+ * @default 'es2022'
48
+ */
49
+ target?: string;
50
+ /**
51
+ * Whether to minify the output
52
+ * @default false
53
+ */
54
+ minify?: boolean;
55
+ /**
56
+ * Generate inline source maps for better debugging and error stack traces.
57
+ * Only applies when `bundle: true`. Has no effect in transform-only mode
58
+ * since the output closely mirrors the input structure.
59
+ * @default false
60
+ */
61
+ sourcemap?: boolean;
62
+ /**
63
+ * npm registry URL for fetching packages.
64
+ * @default 'https://registry.npmjs.org'
65
+ */
66
+ registry?: string;
67
+ }
68
+ /**
69
+ * Parsed wrangler configuration relevant to Worker Loader
70
+ */
71
+ interface WranglerConfig {
72
+ main?: string;
73
+ compatibilityDate?: string;
74
+ compatibilityFlags?: string[];
75
+ }
76
+ /**
77
+ * Result from createWorker
78
+ */
79
+ interface CreateWorkerResult {
80
+ /**
81
+ * The main module entry point path
82
+ */
83
+ mainModule: string;
84
+ /**
85
+ * All modules in the bundle
86
+ */
87
+ modules: Modules;
88
+ /**
89
+ * Parsed wrangler configuration (from wrangler.toml/json/jsonc).
90
+ */
91
+ wranglerConfig?: WranglerConfig;
92
+ /**
93
+ * Any warnings generated during bundling
94
+ */
95
+ warnings?: string[];
96
+ }
97
+ //#endregion
98
+ //#region src/asset-handler.d.ts
99
+ /**
100
+ * Asset request handler for serving static assets.
101
+ *
102
+ * Key design: the manifest (routing metadata) is separated from the
103
+ * storage (content retrieval). This lets you plug in any backend —
104
+ * in-memory, KV, R2, Workspace, etc.
105
+ *
106
+ * Inspired by Cloudflare's Workers Static Assets behavior and
107
+ * cloudflare-asset-worker by Timo Wilhelm.
108
+ */
109
+ /**
110
+ * Pluggable storage backend for asset content.
111
+ * Implement this to serve assets from KV, R2, Workspace, or any other source.
112
+ */
113
+ interface AssetStorage {
114
+ get(pathname: string): Promise<ReadableStream | ArrayBuffer | string | null>;
115
+ }
116
+ /**
117
+ * Metadata for a single asset (no content — that comes from storage).
118
+ */
119
+ interface AssetMetadata {
120
+ contentType: string | undefined;
121
+ etag: string;
122
+ }
123
+ /**
124
+ * The manifest maps pathnames to metadata. Used for routing decisions,
125
+ * ETag checks, and content-type headers — all without touching storage.
126
+ */
127
+ type AssetManifest = Map<string, AssetMetadata>;
128
+ /**
129
+ * Create an in-memory storage backend from a pathname->content map.
130
+ * This is the zero-config default for small asset sets.
131
+ */
132
+ declare function createMemoryStorage(
133
+ assets: Record<string, string | ArrayBuffer>
134
+ ): AssetStorage;
135
+ /**
136
+ * Configuration for asset serving behavior.
137
+ */
138
+ interface AssetConfig {
139
+ /**
140
+ * How to handle HTML file resolution and trailing slashes.
141
+ * @default 'auto-trailing-slash'
142
+ */
143
+ html_handling?:
144
+ | "auto-trailing-slash"
145
+ | "force-trailing-slash"
146
+ | "drop-trailing-slash"
147
+ | "none";
148
+ /**
149
+ * How to handle requests that don't match any asset.
150
+ * - 'single-page-application': Serve /index.html for 404s
151
+ * - '404-page': Serve nearest 404.html walking up the directory tree
152
+ * - 'none': Return null (fall through)
153
+ * @default 'none'
154
+ */
155
+ not_found_handling?: "single-page-application" | "404-page" | "none";
156
+ /**
157
+ * Static redirect rules. Keys are URL pathnames (or https://host/path for cross-host).
158
+ * Supports * glob and :placeholder tokens.
159
+ */
160
+ redirects?: {
161
+ static?: Record<
162
+ string,
163
+ {
164
+ status: number;
165
+ to: string;
166
+ }
167
+ >;
168
+ dynamic?: Record<
169
+ string,
170
+ {
171
+ status: number;
172
+ to: string;
173
+ }
174
+ >;
175
+ };
176
+ /**
177
+ * Custom response headers per pathname pattern (glob syntax).
178
+ */
179
+ headers?: Record<
180
+ string,
181
+ {
182
+ set?: Record<string, string>;
183
+ unset?: string[];
184
+ }
185
+ >;
186
+ }
187
+ /**
188
+ * Build an AssetManifest from a pathname->content mapping.
189
+ * Only computes metadata (content types, ETags) — doesn't store content.
190
+ */
191
+ declare function buildAssetManifest(
192
+ assets: Record<string, string | ArrayBuffer>
193
+ ): Promise<AssetManifest>;
194
+ /**
195
+ * Convenience: build both a manifest and an in-memory storage from assets.
196
+ */
197
+ declare function buildAssets(
198
+ assets: Record<string, string | ArrayBuffer>
199
+ ): Promise<{
200
+ manifest: AssetManifest;
201
+ storage: AssetStorage;
202
+ }>;
203
+ /**
204
+ * Handle an asset request. Returns a Response if an asset matches,
205
+ * or null if the request should fall through to the user's Worker.
206
+ *
207
+ * @param request - The incoming HTTP request
208
+ * @param manifest - Asset manifest (pathname -> metadata)
209
+ * @param storage - Storage backend for fetching content
210
+ * @param config - Asset serving configuration
211
+ */
212
+ declare function handleAssetRequest(
213
+ request: Request,
214
+ manifest: AssetManifest,
215
+ storage: AssetStorage,
216
+ config?: AssetConfig
217
+ ): Promise<Response | null>;
218
+ //#endregion
219
+ //#region src/app.d.ts
220
+ /**
221
+ * Options for createApp
222
+ */
223
+ interface CreateAppOptions {
224
+ /**
225
+ * Input files — keys are paths relative to project root, values are file contents.
226
+ * Should include both server and client source files.
227
+ */
228
+ files: Files;
229
+ /**
230
+ * Server entry point (the Worker fetch handler).
231
+ * If not specified, detected from wrangler config / package.json / defaults.
232
+ */
233
+ server?: string;
234
+ /**
235
+ * Client entry point(s) to bundle for the browser.
236
+ * These are bundled with esbuild targeting the browser.
237
+ */
238
+ client?: string | string[];
239
+ /**
240
+ * Static assets to serve as-is (pathname -> content).
241
+ * Keys should be URL pathnames (e.g., "/favicon.ico", "/robots.txt").
242
+ * These are NOT processed by the bundler.
243
+ */
244
+ assets?: Record<string, string | ArrayBuffer>;
245
+ /**
246
+ * Asset serving configuration.
247
+ */
248
+ assetConfig?: AssetConfig;
249
+ /**
250
+ * Whether to bundle server dependencies.
251
+ * @default true
252
+ */
253
+ bundle?: boolean;
254
+ /**
255
+ * External modules that should not be bundled.
256
+ */
257
+ externals?: string[];
258
+ /**
259
+ * Target environment for server bundle.
260
+ * @default 'es2022'
261
+ */
262
+ target?: string;
263
+ /**
264
+ * Whether to minify the output.
265
+ * @default false
266
+ */
267
+ minify?: boolean;
268
+ /**
269
+ * Generate source maps.
270
+ * @default false
271
+ */
272
+ sourcemap?: boolean;
273
+ /**
274
+ * npm registry URL for fetching packages.
275
+ */
276
+ registry?: string;
277
+ /**
278
+ * Generate a Durable Object class wrapper instead of a module worker.
279
+ * When set, the output exports a named class that can be used with
280
+ * ctx.facets.get() / getDurableObjectClass() for persistent storage.
281
+ *
282
+ * If the user's server exports a DurableObject subclass (default export),
283
+ * the wrapper extends it. Otherwise, it wraps the fetch handler in a DO.
284
+ *
285
+ * Pass `true` for className "App", or an object with a custom className.
286
+ */
287
+ durableObject?:
288
+ | {
289
+ className?: string;
290
+ }
291
+ | boolean;
292
+ }
293
+ /**
294
+ * Result from createApp
295
+ */
296
+ interface CreateAppResult extends CreateWorkerResult {
297
+ /**
298
+ * The asset manifest for runtime request handling.
299
+ * Contains metadata (content types, ETags) for each asset.
300
+ */
301
+ assetManifest: AssetManifest;
302
+ /**
303
+ * The asset config for runtime request handling.
304
+ */
305
+ assetConfig?: AssetConfig;
306
+ /**
307
+ * Client bundle output paths (relative to asset root).
308
+ */
309
+ clientBundles?: string[];
310
+ /**
311
+ * The Durable Object class name exported by the wrapper.
312
+ * Only set when `durableObject` option was used.
313
+ * Use with `worker.getDurableObjectClass(className)` and `ctx.facets.get()`.
314
+ */
315
+ durableObjectClassName?: string;
316
+ }
317
+ /**
318
+ * Creates a full-stack app bundle from source files.
319
+ *
320
+ * This function:
321
+ * 1. Bundles client entry point(s) for the browser (if provided)
322
+ * 2. Collects static assets
323
+ * 3. Bundles the server Worker
324
+ * 4. Generates a server wrapper that serves assets and falls through to user code
325
+ * 5. Returns everything ready for the Worker Loader
326
+ */
327
+ declare function createApp(options: CreateAppOptions): Promise<CreateAppResult>;
328
+ //#endregion
329
+ //#region src/mime.d.ts
330
+ /**
331
+ * MIME type inference from file extensions.
332
+ */
333
+ /**
334
+ * Infer MIME type from a file path.
335
+ * Returns undefined if the type is unknown.
336
+ */
337
+ declare function inferContentType(path: string): string | undefined;
338
+ /**
339
+ * Whether a content type represents a text-based format
340
+ * (used to decide text vs binary module storage).
341
+ */
342
+ declare function isTextContentType(contentType: string): boolean;
343
+ //#endregion
344
+ //#region src/index.d.ts
345
+ /**
346
+ * Creates a worker bundle from source files.
347
+ *
348
+ * This function performs:
349
+ * 1. Entry point detection (from package.json or defaults)
350
+ * 2. Auto-installation of npm dependencies (if package.json has dependencies)
351
+ * 3. TypeScript/JSX transformation (via Sucrase)
352
+ * 4. Module resolution (handling imports/exports)
353
+ * 5. Optional bundling (combining all modules into one)
354
+ *
355
+ * @param options - Configuration options
356
+ * @returns The main module path and all modules
357
+ */
358
+ declare function createWorker(
359
+ options: CreateWorkerOptions
360
+ ): Promise<CreateWorkerResult>;
361
+ //#endregion
362
+ export {
363
+ type AssetConfig,
364
+ type AssetManifest,
365
+ type AssetMetadata,
366
+ type AssetStorage,
367
+ type CreateAppOptions,
368
+ type CreateAppResult,
369
+ type CreateWorkerOptions,
370
+ type CreateWorkerResult,
371
+ type Files,
372
+ type Modules,
373
+ type WranglerConfig,
374
+ buildAssetManifest,
375
+ buildAssets,
376
+ createApp,
377
+ createMemoryStorage,
378
+ createWorker,
379
+ handleAssetRequest,
380
+ inferContentType,
381
+ isTextContentType
382
+ };
383
+ //# sourceMappingURL=index.d.ts.map