@cloudflare/worker-bundler 0.1.2 → 0.2.0

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/README.md CHANGED
@@ -128,6 +128,7 @@ Bundles source files into a Worker.
128
128
  | `define` | `Record<string, string>` | `{}` | Constant replacements applied at bundle time |
129
129
  | `loader` | `Record<string, BundlerLoader>` | `{}` | Per-extension loader overrides (e.g. `{ ".svg": "text", ".wasm": "binary" }`). See [Advanced bundler options](#advanced-bundler-options) for the supported set. |
130
130
  | `conditions` | `string[]` | esbuild default | Package export conditions to honour during resolution |
131
+ | `virtualModules` | `Record<string, string>` | `{}` | Exact import-specifier aliases backed by generated JavaScript module source. See [Advanced bundler options](#advanced-bundler-options). |
131
132
  | `__dangerouslyUseEsBuildPluginsDoNotUseOrYouWillBeFired` | `unknown[]` | `[]` | Escape hatch: extra esbuild plugins, run before the internal virtual-fs plugin. Not covered by semver — see [Advanced bundler options](#advanced-bundler-options). |
132
133
 
133
134
  Returns:
@@ -163,6 +164,7 @@ Builds a full-stack app: server Worker + client bundle + static assets.
163
164
  | `define` | `Record<string, string>` | `{}` | Constant replacements applied at bundle time (both server and client) |
164
165
  | `loader` | `Record<string, BundlerLoader>` | `{}` | Per-extension loader overrides (e.g. `{ ".svg": "text" }`). See [Advanced bundler options](#advanced-bundler-options) for the supported set. |
165
166
  | `conditions` | `string[]` | esbuild default | Package export conditions to honour during resolution |
167
+ | `virtualModules` | `Record<string, string>` | `{}` | Exact import-specifier aliases backed by generated JavaScript module source |
166
168
  | `__dangerouslyUseEsBuildPluginsDoNotUseOrYouWillBeFired` | `unknown[]` | `[]` | Escape hatch: extra esbuild plugins. See [Advanced bundler options](#advanced-bundler-options). |
167
169
 
168
170
  Returns everything from `createWorker` plus:
@@ -571,6 +573,43 @@ await createWorker({
571
573
  });
572
574
  ```
573
575
 
576
+ ### `virtualModules`
577
+
578
+ Alias exact import specifiers to generated JavaScript modules. This is useful
579
+ when a host framework wants to provide virtual runtime modules without exposing
580
+ esbuild plugin details to callers.
581
+
582
+ ```ts
583
+ const { mainModule, modules } = await createWorker({
584
+ files: {
585
+ "src/index.ts": `
586
+ import { readFileSync } from "node:fs";
587
+
588
+ export default {
589
+ fetch() {
590
+ return new Response(readFileSync("/message.txt", "utf8"));
591
+ }
592
+ };
593
+ `
594
+ },
595
+ virtualModules: {
596
+ "node:fs": `
597
+ const files = { "/message.txt": "hello from a virtual module" };
598
+ export function readFileSync(path) {
599
+ return files[path];
600
+ }
601
+ export default { readFileSync };
602
+ `,
603
+ fs: `export * from "node:fs"; export { default } from "node:fs";`
604
+ }
605
+ });
606
+ ```
607
+
608
+ `virtualModules` keys are exact specifier matches. If you want both `"fs"` and
609
+ `"node:fs"` to work, provide both aliases. The generated source is bundled with
610
+ the rest of the Worker, so it can be imported by the entry point or by transitive
611
+ dependencies.
612
+
574
613
  ### `__dangerouslyUseEsBuildPluginsDoNotUseOrYouWillBeFired`
575
614
 
576
615
  > **Read this before using.** This option is **not** covered by semver. The name is the API contract: it can change shape, be renamed, or be removed in any release. The runtime ties you to esbuild — if this package switches bundlers (e.g. to rolldown), plugins authored against this API will break.
package/dist/index.d.ts CHANGED
@@ -145,6 +145,15 @@ interface CreateWorkerOptions {
145
145
  * Only applies when `bundle: true`.
146
146
  */
147
147
  conditions?: string[];
148
+ /**
149
+ * Exact virtual module aliases available during bundling. Each key is an
150
+ * import specifier (for example `"node:fs"` or `"virtual:app/config"`) and
151
+ * each value is JavaScript module source loaded by the bundler.
152
+ *
153
+ * Only applies when `bundle: true`. In transform-only mode this option is
154
+ * ignored and a warning is added to the result's `warnings` array.
155
+ */
156
+ virtualModules?: Record<string, string>;
148
157
  /**
149
158
  * Escape hatch for advanced users: extra esbuild plugins to run **before**
150
159
  * the bundler's internal virtual-filesystem plugin.
package/dist/index.js CHANGED
@@ -223,6 +223,7 @@ function bundlerOnlyOptionsWarning(opts) {
223
223
  if (opts.define !== void 0) set.push("define");
224
224
  if (opts.loader !== void 0) set.push("loader");
225
225
  if (opts.conditions !== void 0) set.push("conditions");
226
+ if (opts.virtualModules !== void 0) set.push("virtualModules");
226
227
  if (opts.plugins !== void 0) set.push("__dangerouslyUseEsBuildPluginsDoNotUseOrYouWillBeFired");
227
228
  if (set.length === 0) return null;
228
229
  return `${set.join(", ")} ${set.length === 1 ? "is" : "are"} ignored when \`bundle: false\` (transform-only mode does not run esbuild). Set \`bundle: true\` (the default) to apply them.`;
@@ -231,7 +232,7 @@ function bundlerOnlyOptionsWarning(opts) {
231
232
  * Bundle files using esbuild-wasm
232
233
  */
233
234
  async function bundleWithEsbuild(options) {
234
- const { files, entryPoint, externals, target, minify, sourcemap, nodejsCompat, jsx, jsxImportSource, define, loader: loaderOverrides, conditions, plugins: extraPlugins = [] } = options;
235
+ const { files, entryPoint, externals, target, minify, sourcemap, nodejsCompat, jsx, jsxImportSource, define, loader: loaderOverrides, conditions, virtualModules, plugins: extraPlugins = [] } = options;
235
236
  await initializeEsbuild();
236
237
  const virtualFsPlugin = {
237
238
  name: "virtual-fs",
@@ -291,6 +292,25 @@ async function bundleWithEsbuild(options) {
291
292
  });
292
293
  }
293
294
  };
295
+ const virtualModulePlugin = virtualModules && Object.keys(virtualModules).length > 0 ? {
296
+ name: "virtual-modules",
297
+ setup(build) {
298
+ build.onResolve({ filter: /.*/ }, (args) => {
299
+ if (Object.hasOwn(virtualModules, args.path)) return {
300
+ path: args.path,
301
+ namespace: "virtual-module"
302
+ };
303
+ });
304
+ build.onLoad({
305
+ filter: /.*/,
306
+ namespace: "virtual-module"
307
+ }, (args) => ({
308
+ contents: virtualModules[args.path] ?? "",
309
+ loader: "js",
310
+ resolveDir: ""
311
+ }));
312
+ }
313
+ } : null;
294
314
  for (let i = 0; i < extraPlugins.length; i++) {
295
315
  const p = extraPlugins[i];
296
316
  if (!p || typeof p !== "object" || typeof p.name !== "string" || typeof p.setup !== "function") throw new TypeError(`__dangerouslyUseEsBuildPluginsDoNotUseOrYouWillBeFired[${i}] is not a valid esbuild plugin (expected an object with \`name: string\` and \`setup: (build) => void\`).`);
@@ -305,7 +325,11 @@ async function bundleWithEsbuild(options) {
305
325
  target,
306
326
  minify,
307
327
  sourcemap: sourcemap ? "inline" : false,
308
- plugins: [...userPlugins, virtualFsPlugin],
328
+ plugins: [
329
+ ...userPlugins,
330
+ ...virtualModulePlugin ? [virtualModulePlugin] : [],
331
+ virtualFsPlugin
332
+ ],
309
333
  outfile: "bundle.js",
310
334
  ...jsx !== void 0 ? { jsx } : {},
311
335
  ...jsxImportSource !== void 0 ? { jsxImportSource } : {},
@@ -1594,7 +1618,7 @@ async function createApp(options) {
1594
1618
  */
1595
1619
  async function createWorker(options) {
1596
1620
  showExperimentalWarning("createWorker");
1597
- let { files, bundle = true, externals = [], target = "es2022", minify = false, sourcemap = false, registry, jsx, jsxImportSource, define, loader, conditions, __dangerouslyUseEsBuildPluginsDoNotUseOrYouWillBeFired: plugins } = options;
1621
+ let { files, bundle = true, externals = [], target = "es2022", minify = false, sourcemap = false, registry, jsx, jsxImportSource, define, loader, conditions, virtualModules, __dangerouslyUseEsBuildPluginsDoNotUseOrYouWillBeFired: plugins } = options;
1598
1622
  let fileSystem;
1599
1623
  if (isFileSystem(files)) fileSystem = files;
1600
1624
  else fileSystem = new InMemoryFileSystem(files);
@@ -1623,6 +1647,7 @@ async function createWorker(options) {
1623
1647
  define,
1624
1648
  loader,
1625
1649
  conditions,
1650
+ virtualModules,
1626
1651
  plugins
1627
1652
  });
1628
1653
  if (wranglerConfig !== void 0) result.wranglerConfig = wranglerConfig;
@@ -1636,6 +1661,7 @@ async function createWorker(options) {
1636
1661
  define,
1637
1662
  loader,
1638
1663
  conditions,
1664
+ virtualModules,
1639
1665
  plugins
1640
1666
  });
1641
1667
  if (wranglerConfig !== void 0) result.wranglerConfig = wranglerConfig;