@cloudflare/vite-plugin 0.0.0-5388447d7 → 0.0.0-53ba97df6

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.
@@ -1985,7 +1985,7 @@ var we = class {
1985
1985
  this.hasWritten || this.readyAnalytics && (this.hasWritten = true, this.readyAnalytics.logEvent({ version: 1, accountId: this.data.accountId, indexId: this.data.scriptId?.toString(), doubles: [this.data.requestTime ?? -1, this.data.coloId ?? -1, this.data.metalId ?? -1, this.data.coloTier ?? -1, this.data.userWorkerAhead === void 0 ? -1 : Number(this.data.userWorkerAhead)], blobs: [this.data.hostname?.substring(0, 256), this.data.dispatchtype, this.data.error?.substring(0, 256), this.data.version, this.data.coloRegion] }));
1986
1986
  }
1987
1987
  };
1988
- var On = (t4) => ({ invoke_user_worker_ahead_of_assets: t4?.invoke_user_worker_ahead_of_assets ?? false, has_user_worker: t4?.has_user_worker ?? false, account_id: t4?.account_id ?? -1, script_id: t4?.script_id ?? -1 });
1988
+ var On = (t4) => ({ invoke_user_worker_ahead_of_assets: t4?.invoke_user_worker_ahead_of_assets ?? false, has_user_worker: t4?.has_user_worker ?? false, account_id: t4?.account_id ?? -1, script_id: t4?.script_id ?? -1, debug: t4?.debug ?? false });
1989
1989
  var Bc = { async fetch(t4, e, n) {
1990
1990
  let r, o = false, s = new we(e.ANALYTICS), i = new ue(e.UNSAFE_PERFORMANCE), a = i.now();
1991
1991
  try {
package/dist/index.js CHANGED
@@ -489,7 +489,7 @@ import assert10 from "node:assert";
489
489
  import * as fs5 from "node:fs";
490
490
  import * as fsp2 from "node:fs/promises";
491
491
  import * as path9 from "node:path";
492
- import { createMiddleware } from "@hattip/adapter-node";
492
+ import { createRequest, sendResponse } from "@mjackson/node-fetch-server";
493
493
  import replace from "@rollup/plugin-replace";
494
494
 
495
495
  // ../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
@@ -1558,7 +1558,7 @@ var MagicString = class _MagicString {
1558
1558
 
1559
1559
  // src/index.ts
1560
1560
  import { Miniflare } from "miniflare";
1561
- import colors2 from "picocolors";
1561
+ import colors3 from "picocolors";
1562
1562
  import * as vite7 from "vite";
1563
1563
 
1564
1564
  // src/constants.ts
@@ -5784,7 +5784,8 @@ var z = /* @__PURE__ */ Object.freeze({
5784
5784
  // ../workers-shared/utils/types.ts
5785
5785
  var InternalConfigSchema = z.object({
5786
5786
  account_id: z.number().optional(),
5787
- script_id: z.number().optional()
5787
+ script_id: z.number().optional(),
5788
+ debug: z.boolean().optional()
5788
5789
  });
5789
5790
  var RouterConfigSchema = z.object({
5790
5791
  invoke_user_worker_ahead_of_assets: z.boolean().optional(),
@@ -12880,6 +12881,15 @@ function isNodeCompat(workerConfig) {
12880
12881
  }
12881
12882
  return false;
12882
12883
  }
12884
+ function isNodeAls(workerConfig) {
12885
+ return workerConfig !== void 0 && getNodeCompat(
12886
+ workerConfig.compatibility_date,
12887
+ workerConfig.compatibility_flags ?? []
12888
+ ).mode === "als";
12889
+ }
12890
+ function isNodeAlsModule(path10) {
12891
+ return /^(node:)?async_hooks$/.test(path10);
12892
+ }
12883
12893
  function injectGlobalCode(id, code) {
12884
12894
  const injectedCode = Object.entries(env.inject).map(([globalName, globalInject]) => {
12885
12895
  if (typeof globalInject === "string") {
@@ -13345,6 +13355,7 @@ import {
13345
13355
  LogLevel,
13346
13356
  Response as MiniflareResponse
13347
13357
  } from "miniflare";
13358
+ import colors2 from "picocolors";
13348
13359
  import { globSync } from "tinyglobby";
13349
13360
  import "vite";
13350
13361
  import { unstable_getMiniflareWorkerOptions } from "wrangler";
@@ -13449,6 +13460,29 @@ function getEntryWorkerConfig(resolvedPluginConfig) {
13449
13460
  }
13450
13461
  return resolvedPluginConfig.workers[resolvedPluginConfig.entryWorkerEnvironmentName];
13451
13462
  }
13463
+ function filterTails(tails, userWorkers, log) {
13464
+ return tails?.filter((tailService) => {
13465
+ let name;
13466
+ if (typeof tailService === "string") {
13467
+ name = tailService;
13468
+ } else if (typeof tailService === "object" && "name" in tailService && typeof tailService.name === "string") {
13469
+ name = tailService.name;
13470
+ } else {
13471
+ return true;
13472
+ }
13473
+ const found = userWorkers.some((w) => w.name === name);
13474
+ if (!found) {
13475
+ log(
13476
+ colors2.dim(
13477
+ colors2.yellow(
13478
+ `Tail consumer "${name}" was not found in your config. Make sure you add it if you'd like to simulate receiving tail events locally.`
13479
+ )
13480
+ )
13481
+ );
13482
+ }
13483
+ return found;
13484
+ });
13485
+ }
13452
13486
  function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspectorPort) {
13453
13487
  const resolvedViteConfig = viteDevServer.config;
13454
13488
  const entryWorkerConfig = getEntryWorkerConfig(resolvedPluginConfig);
@@ -13640,6 +13674,11 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspectorPo
13640
13674
  }
13641
13675
  return {
13642
13676
  ...workerOptions,
13677
+ tails: filterTails(
13678
+ workerOptions.tails,
13679
+ userWorkers,
13680
+ viteDevServer.config.logger.warn
13681
+ ),
13643
13682
  modules: [
13644
13683
  {
13645
13684
  type: "ESModule",
@@ -13730,6 +13769,11 @@ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistSta
13730
13769
  return [
13731
13770
  {
13732
13771
  ...workerOptions,
13772
+ tails: filterTails(
13773
+ workerOptions.tails,
13774
+ workerConfigs,
13775
+ vitePreviewServer.config.logger.warn
13776
+ ),
13733
13777
  name: workerOptions.name ?? config.name,
13734
13778
  unsafeInspectorProxy: inspectorPort !== false,
13735
13779
  ...miniflareWorkerOptions.main ? getPreviewModules(miniflareWorkerOptions.main, modulesRules) : { modules: true, script: "" }
@@ -14187,6 +14231,9 @@ function validateWorkerEnvironmentsResolvedConfigs(resolvedPluginConfig, resolve
14187
14231
  if (cloudflareBuiltInModules.includes(entry)) {
14188
14232
  return false;
14189
14233
  }
14234
+ if (isNodeAlsModule(entry) && isNodeAls(resolvedPluginConfig.workers[envName])) {
14235
+ return false;
14236
+ }
14190
14237
  if (NODEJS_MODULES_RE.test(entry) && isNodeCompat(resolvedPluginConfig.workers[envName])) {
14191
14238
  return false;
14192
14239
  }
@@ -14413,15 +14460,11 @@ function cloudflare2(pluginConfig = {}) {
14413
14460
  }
14414
14461
  },
14415
14462
  async configureServer(viteDevServer) {
14416
- assert10(
14417
- viteDevServer.httpServer,
14418
- "Unexpected error: No Vite HTTP server"
14463
+ const inputInspectorPort = await getInputInspectorPortOption(
14464
+ pluginConfig,
14465
+ viteDevServer
14419
14466
  );
14420
14467
  if (!miniflare) {
14421
- const inputInspectorPort = await getInputInspectorPortOption(
14422
- pluginConfig,
14423
- viteDevServer
14424
- );
14425
14468
  miniflare = new Miniflare(
14426
14469
  getDevMiniflareOptions(
14427
14470
  resolvedPluginConfig,
@@ -14430,34 +14473,41 @@ function cloudflare2(pluginConfig = {}) {
14430
14473
  )
14431
14474
  );
14432
14475
  } else {
14433
- const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
14434
14476
  await miniflare.setOptions(
14435
14477
  getDevMiniflareOptions(
14436
14478
  resolvedPluginConfig,
14437
14479
  viteDevServer,
14438
- resolvedInspectorPort ?? false
14480
+ inputInspectorPort
14439
14481
  )
14440
14482
  );
14441
14483
  }
14442
14484
  await initRunners(resolvedPluginConfig, viteDevServer, miniflare);
14443
- const middleware = createMiddleware(
14444
- async ({ request }) => {
14485
+ if (viteDevServer.httpServer) {
14486
+ handleWebSocket(viteDevServer.httpServer, async () => {
14445
14487
  assert10(miniflare, `Miniflare not defined`);
14446
14488
  const routerWorker = await getRouterWorker(miniflare);
14447
- return routerWorker.fetch(toMiniflareRequest(request), {
14448
- redirect: "manual"
14449
- });
14450
- },
14451
- { alwaysCallNext: false }
14452
- );
14453
- handleWebSocket(viteDevServer.httpServer, async () => {
14454
- assert10(miniflare, `Miniflare not defined`);
14455
- const routerWorker = await getRouterWorker(miniflare);
14456
- return routerWorker.fetch;
14457
- });
14489
+ return routerWorker.fetch;
14490
+ });
14491
+ }
14458
14492
  return () => {
14459
- viteDevServer.middlewares.use((req, res, next) => {
14460
- middleware(req, res, next);
14493
+ viteDevServer.middlewares.use(async (req, res, next) => {
14494
+ try {
14495
+ assert10(miniflare, `Miniflare not defined`);
14496
+ const routerWorker = await getRouterWorker(miniflare);
14497
+ const request = createRequest(req, res);
14498
+ const response = await routerWorker.fetch(
14499
+ toMiniflareRequest(request),
14500
+ {
14501
+ redirect: "manual"
14502
+ }
14503
+ );
14504
+ if (req.httpVersionMajor === 2) {
14505
+ response.headers.delete("transfer-encoding");
14506
+ }
14507
+ await sendResponse(res, response);
14508
+ } catch (error) {
14509
+ next(error);
14510
+ }
14461
14511
  });
14462
14512
  };
14463
14513
  },
@@ -14475,20 +14525,24 @@ function cloudflare2(pluginConfig = {}) {
14475
14525
  inputInspectorPort
14476
14526
  )
14477
14527
  );
14478
- const middleware = createMiddleware(
14479
- ({ request }) => {
14480
- return miniflare2.dispatchFetch(toMiniflareRequest(request), {
14481
- redirect: "manual"
14482
- });
14483
- },
14484
- { alwaysCallNext: false }
14485
- );
14486
14528
  handleWebSocket(
14487
14529
  vitePreviewServer.httpServer,
14488
14530
  () => miniflare2.dispatchFetch
14489
14531
  );
14490
- vitePreviewServer.middlewares.use((req, res, next) => {
14491
- middleware(req, res, next);
14532
+ vitePreviewServer.middlewares.use(async (req, res, next) => {
14533
+ try {
14534
+ const request = createRequest(req, res);
14535
+ const response = await miniflare2.dispatchFetch(
14536
+ toMiniflareRequest(request),
14537
+ { redirect: "manual" }
14538
+ );
14539
+ if (req.httpVersionMajor === 2) {
14540
+ response.headers.delete("transfer-encoding");
14541
+ }
14542
+ await sendResponse(res, response);
14543
+ } catch (error) {
14544
+ next(error);
14545
+ }
14492
14546
  });
14493
14547
  }
14494
14548
  },
@@ -14681,6 +14735,25 @@ function cloudflare2(pluginConfig = {}) {
14681
14735
  );
14682
14736
  }
14683
14737
  },
14738
+ // Plugin that handles Node.js Async Local Storage (ALS) compatibility support for Vite Environments that are hosted in Cloudflare Workers.
14739
+ {
14740
+ name: "vite-plugin-cloudflare:nodejs-als",
14741
+ apply(_config, env2) {
14742
+ return !env2.isPreview;
14743
+ },
14744
+ configEnvironment(name, config) {
14745
+ if (isNodeAls(getWorkerConfig2(name))) {
14746
+ return {
14747
+ resolve: {
14748
+ builtins: ["async_hooks", "node:async_hooks"]
14749
+ },
14750
+ optimizeDeps: {
14751
+ exclude: ["async_hooks", "node:async_hooks"]
14752
+ }
14753
+ };
14754
+ }
14755
+ }
14756
+ },
14684
14757
  // Plugin that provides an __debug path for debugging the Cloudflare Workers.
14685
14758
  {
14686
14759
  name: "vite-plugin-cloudflare:debug",
@@ -14746,6 +14819,9 @@ function cloudflare2(pluginConfig = {}) {
14746
14819
  build.onResolve(
14747
14820
  { filter: NODEJS_MODULES_RE },
14748
14821
  ({ path: path10, importer }) => {
14822
+ if (isNodeAls(workerConfig) && isNodeAlsModule(path10)) {
14823
+ return;
14824
+ }
14749
14825
  const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
14750
14826
  nodeJsCompatWarnings?.registerImport(path10, importer);
14751
14827
  return { path: path10, external: true };
@@ -14775,6 +14851,9 @@ function cloudflare2(pluginConfig = {}) {
14775
14851
  async resolveId(source, importer) {
14776
14852
  const workerConfig = getWorkerConfig2(this.environment.name);
14777
14853
  if (workerConfig && !isNodeCompat(workerConfig)) {
14854
+ if (isNodeAls(workerConfig) && isNodeAlsModule(source)) {
14855
+ return;
14856
+ }
14778
14857
  const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
14779
14858
  if (nodejsBuiltins.has(source)) {
14780
14859
  nodeJsCompatWarnings?.registerImport(source, importer);
@@ -14793,10 +14872,16 @@ function cloudflare2(pluginConfig = {}) {
14793
14872
  }
14794
14873
  }
14795
14874
  async function getInputInspectorPortOption(pluginConfig, viteServer) {
14875
+ if (pluginConfig.inspectorPort === void 0 || pluginConfig.inspectorPort === 0) {
14876
+ const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
14877
+ if (resolvedInspectorPort !== null) {
14878
+ return resolvedInspectorPort;
14879
+ }
14880
+ }
14796
14881
  const inputInspectorPort = pluginConfig.inspectorPort ?? await getFirstAvailablePort(DEFAULT_INSPECTOR_PORT);
14797
14882
  if (pluginConfig.inspectorPort === void 0 && inputInspectorPort !== DEFAULT_INSPECTOR_PORT) {
14798
14883
  viteServer.config.logger.warn(
14799
- colors2.dim(
14884
+ colors3.dim(
14800
14885
  `Default inspector port ${DEFAULT_INSPECTOR_PORT} not available, using ${inputInspectorPort} instead
14801
14886
  `
14802
14887
  )
@@ -29,7 +29,7 @@ function stripInternalEnv(internalEnv) {
29
29
  return userEnv;
30
30
  }
31
31
 
32
- // ../../node_modules/.pnpm/vite@6.1.0_@types+node@18.19.76_jiti@2.4.2_lightningcss@1.29.2/node_modules/vite/dist/node/module-runner.js
32
+ // ../../node_modules/.pnpm/vite@6.1.0_@types+node@20.17.32_jiti@2.4.2_lightningcss@1.29.2/node_modules/vite/dist/node/module-runner.js
33
33
  var VALID_ID_PREFIX = "/@id/";
34
34
  var NULL_BYTE_PLACEHOLDER = "__x00__";
35
35
  var SOURCEMAPPING_URL = "sourceMa";
@@ -1435,20 +1435,21 @@ async function getWorkerEntryExport(path, entrypoint) {
1435
1435
  }
1436
1436
 
1437
1437
  // src/runner-worker/index.ts
1438
+ var IGNORED_KEYS = ["self", "tailStream"];
1438
1439
  var WORKER_ENTRYPOINT_KEYS = [
1439
1440
  "fetch",
1441
+ "queue",
1440
1442
  "tail",
1443
+ "test",
1441
1444
  "trace",
1442
- "scheduled",
1443
- "queue",
1444
- "test"
1445
+ "scheduled"
1445
1446
  ];
1446
1447
  var DURABLE_OBJECT_KEYS = [
1447
- "fetch",
1448
1448
  "alarm",
1449
- "webSocketMessage",
1449
+ "fetch",
1450
1450
  "webSocketClose",
1451
- "webSocketError"
1451
+ "webSocketError",
1452
+ "webSocketMessage"
1452
1453
  ];
1453
1454
  var WORKFLOW_ENTRYPOINT_KEYS = ["run"];
1454
1455
  var entryPath = "";
@@ -1513,7 +1514,7 @@ function createWorkerEntrypointWrapper(entrypoint) {
1513
1514
  if (value !== void 0) {
1514
1515
  return value;
1515
1516
  }
1516
- if (key === "self" || typeof key === "symbol" || DURABLE_OBJECT_KEYS.includes(key)) {
1517
+ if (typeof key === "symbol" || IGNORED_KEYS.includes(key) || DURABLE_OBJECT_KEYS.includes(key)) {
1517
1518
  return;
1518
1519
  }
1519
1520
  const property = getWorkerEntrypointRpcProperty.call(
@@ -1611,7 +1612,7 @@ function createDurableObjectWrapper(className) {
1611
1612
  if (value !== void 0) {
1612
1613
  return value;
1613
1614
  }
1614
- if (key === "self" || typeof key === "symbol" || WORKER_ENTRYPOINT_KEYS.includes(key)) {
1615
+ if (typeof key === "symbol" || IGNORED_KEYS.includes(key) || WORKER_ENTRYPOINT_KEYS.includes(key)) {
1615
1616
  return;
1616
1617
  }
1617
1618
  const property = getDurableObjectRpcProperty.call(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/vite-plugin",
3
- "version": "0.0.0-5388447d7",
3
+ "version": "0.0.0-53ba97df6",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -33,19 +33,19 @@
33
33
  "dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@hattip/adapter-node": "^0.0.49",
36
+ "@mjackson/node-fetch-server": "^0.6.1",
37
37
  "@rollup/plugin-replace": "^6.0.1",
38
38
  "get-port": "^7.1.0",
39
39
  "picocolors": "^1.1.1",
40
40
  "tinyglobby": "^0.2.12",
41
41
  "unenv": "2.0.0-rc.15",
42
42
  "ws": "8.18.0",
43
- "@cloudflare/unenv-preset": "0.0.0-5388447d7",
44
- "miniflare": "0.0.0-5388447d7",
45
- "wrangler": "0.0.0-5388447d7"
43
+ "@cloudflare/unenv-preset": "0.0.0-53ba97df6",
44
+ "miniflare": "0.0.0-53ba97df6",
45
+ "wrangler": "0.0.0-53ba97df6"
46
46
  },
47
47
  "devDependencies": {
48
- "@cloudflare/workers-types": "^4.20250409.0",
48
+ "@cloudflare/workers-types": "^4.20250508.0",
49
49
  "@types/node": "^22.10.1",
50
50
  "@types/ws": "^8.5.13",
51
51
  "magic-string": "^0.30.12",
@@ -54,9 +54,9 @@
54
54
  "typescript": "^5.7.2",
55
55
  "undici": "^5.28.5",
56
56
  "vite": "^6.1.0",
57
- "vitest": "~3.0.8",
57
+ "vitest": "~3.1.1",
58
+ "@cloudflare/workers-shared": "0.0.0-53ba97df6",
58
59
  "@cloudflare/mock-npm-registry": "0.0.0",
59
- "@cloudflare/workers-shared": "0.0.0-5388447d7",
60
60
  "@cloudflare/workers-tsconfig": "0.0.0"
61
61
  },
62
62
  "peerDependencies": {