@cloudflare/vite-plugin 1.3.1 → 1.5.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/dist/index.js CHANGED
@@ -1571,6 +1571,7 @@ var ADDITIONAL_MODULE_TYPES = [
1571
1571
  "Text"
1572
1572
  ];
1573
1573
  var DEFAULT_INSPECTOR_PORT = 9229;
1574
+ var kRequestType = Symbol("kRequestType");
1574
1575
 
1575
1576
  // src/additional-modules.ts
1576
1577
  var moduleRules = [
@@ -12648,8 +12649,8 @@ function packageResolve(specifier, base, conditions) {
12648
12649
  let packageJsonPath = fileURLToPath$1(packageJsonUrl);
12649
12650
  let lastPath;
12650
12651
  do {
12651
- const stat = tryStatSync(packageJsonPath.slice(0, -13));
12652
- if (!stat || !stat.isDirectory()) {
12652
+ const stat2 = tryStatSync(packageJsonPath.slice(0, -13));
12653
+ if (!stat2 || !stat2.isDirectory()) {
12653
12654
  lastPath = packageJsonPath;
12654
12655
  packageJsonUrl = new URL$1(
12655
12656
  (isScoped ? "../../../../node_modules/" : "../../../node_modules/") + packageName + "/package.json",
@@ -12779,8 +12780,8 @@ function _resolve(id, options = {}) {
12779
12780
  }
12780
12781
  if (isAbsolute(id)) {
12781
12782
  try {
12782
- const stat = statSync(id);
12783
- if (stat.isFile()) {
12783
+ const stat2 = statSync(id);
12784
+ if (stat2.isFile()) {
12784
12785
  return pathToFileURL(id);
12785
12786
  }
12786
12787
  } catch (error) {
@@ -13010,9 +13011,6 @@ function getOutputDirectory(userConfig, environmentName) {
13010
13011
  const rootOutputDirectory = userConfig.build?.outDir ?? "dist";
13011
13012
  return userConfig.environments?.[environmentName]?.build?.outDir ?? path4.join(rootOutputDirectory, environmentName);
13012
13013
  }
13013
- function getRouterWorker(miniflare2) {
13014
- return miniflare2.getWorker(ROUTER_WORKER_NAME);
13015
- }
13016
13014
  function toMiniflareRequest(request) {
13017
13015
  const host = request.headers.get("Host");
13018
13016
  if (host) {
@@ -13257,7 +13255,7 @@ import { unstable_readConfig } from "wrangler";
13257
13255
  function getDeployConfigPath(root) {
13258
13256
  return path5.resolve(root, ".wrangler", "deploy", "config.json");
13259
13257
  }
13260
- function getWorkerConfigs(root) {
13258
+ function getWorkerConfigs(root, mixedModeEnabled) {
13261
13259
  const deployConfigPath = getDeployConfigPath(root);
13262
13260
  const deployConfig = JSON.parse(
13263
13261
  fs2.readFileSync(deployConfigPath, "utf-8")
@@ -13270,7 +13268,10 @@ function getWorkerConfigs(root) {
13270
13268
  path5.dirname(deployConfigPath),
13271
13269
  configPath
13272
13270
  );
13273
- return unstable_readConfig({ config: resolvedConfigPath });
13271
+ return unstable_readConfig(
13272
+ { config: resolvedConfigPath },
13273
+ { experimental: { mixedModeEnabled } }
13274
+ );
13274
13275
  });
13275
13276
  }
13276
13277
  function getRelativePathToWorkerConfig(deployConfigDirectory, root, outputDirectory) {
@@ -13337,6 +13338,7 @@ import * as fsp from "node:fs/promises";
13337
13338
  import * as path6 from "node:path";
13338
13339
  import { fileURLToPath as fileURLToPath2 } from "node:url";
13339
13340
  import {
13341
+ getDefaultDevRegistryPath,
13340
13342
  kCurrentWorker,
13341
13343
  Log,
13342
13344
  LogLevel,
@@ -13445,28 +13447,27 @@ function getEntryWorkerConfig(resolvedPluginConfig) {
13445
13447
  }
13446
13448
  return resolvedPluginConfig.workers[resolvedPluginConfig.entryWorkerEnvironmentName];
13447
13449
  }
13448
- function filterTails(tails, userWorkers, log) {
13449
- return tails?.filter((tailService) => {
13450
+ function logUnknownTails(tails, userWorkers, log) {
13451
+ for (const tailService of tails ?? []) {
13450
13452
  let name;
13451
13453
  if (typeof tailService === "string") {
13452
13454
  name = tailService;
13453
13455
  } else if (typeof tailService === "object" && "name" in tailService && typeof tailService.name === "string") {
13454
13456
  name = tailService.name;
13455
13457
  } else {
13456
- return true;
13458
+ continue;
13457
13459
  }
13458
13460
  const found = userWorkers.some((w) => w.name === name);
13459
13461
  if (!found) {
13460
13462
  log(
13461
13463
  colors2.dim(
13462
13464
  colors2.yellow(
13463
- `Tail consumer "${name}" was not found in your config. Make sure you add it if you'd like to simulate receiving tail events locally.`
13465
+ `Tail consumer "${name}" was not found in your config. Make sure you add it to the config or run it in another dev session if you'd like to simulate receiving tail events locally.`
13464
13466
  )
13465
13467
  )
13466
13468
  );
13467
13469
  }
13468
- return found;
13469
- });
13470
+ }
13470
13471
  }
13471
13472
  async function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspectorPort) {
13472
13473
  const resolvedViteConfig = viteDevServer.config;
@@ -13519,12 +13520,14 @@ async function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspe
13519
13520
  serviceBindings: {
13520
13521
  __VITE_ASSET_EXISTS__: async (request) => {
13521
13522
  const { pathname } = new URL(request.url);
13522
- const filePath = path6.join(resolvedViteConfig.root, pathname);
13523
- let exists;
13524
- try {
13525
- exists = fs3.statSync(filePath).isFile();
13526
- } catch (error) {
13527
- exists = false;
13523
+ let exists = false;
13524
+ if (pathname.endsWith(".html")) {
13525
+ try {
13526
+ const filePath = path6.join(resolvedViteConfig.root, pathname);
13527
+ const stats = await fsp.stat(filePath);
13528
+ exists = stats.isFile();
13529
+ } catch (error) {
13530
+ }
13528
13531
  }
13529
13532
  return MiniflareResponse.json(exists);
13530
13533
  },
@@ -13538,7 +13541,7 @@ async function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspe
13538
13541
  headers: { "Content-Type": "text/html" }
13539
13542
  });
13540
13543
  } catch (error) {
13541
- throw new Error(`Unexpected error. Failed to load ${pathname}`);
13544
+ throw new Error(`Unexpected error. Failed to load "${pathname}".`);
13542
13545
  }
13543
13546
  }
13544
13547
  }
@@ -13555,7 +13558,8 @@ async function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspe
13555
13558
  },
13556
13559
  resolvedPluginConfig.cloudflareEnv,
13557
13560
  {
13558
- mixedModeConnectionString: mixedModeSession?.mixedModeConnectionString
13561
+ mixedModeConnectionString: mixedModeSession?.mixedModeConnectionString,
13562
+ mixedModeEnabled: resolvedPluginConfig.experimental.mixedMode
13559
13563
  }
13560
13564
  );
13561
13565
  const { externalWorkers: externalWorkers2 } = miniflareWorkerOptions;
@@ -13566,12 +13570,21 @@ async function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspe
13566
13570
  ...workerOptions,
13567
13571
  name: workerOptions.name ?? workerConfig.name,
13568
13572
  unsafeInspectorProxy: inspectorPort !== false,
13573
+ unsafeDirectSockets: environmentName === resolvedPluginConfig.entryWorkerEnvironmentName ? (
13574
+ // Expose the default entrypoint of the entry worker on the dev registry
13575
+ [{ entrypoint: void 0, proxy: true }]
13576
+ ) : [],
13569
13577
  modulesRoot: miniflareModulesRoot,
13570
13578
  unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
13571
13579
  serviceBindings: {
13572
13580
  ...workerOptions.serviceBindings,
13573
13581
  ...environmentName === resolvedPluginConfig.entryWorkerEnvironmentName && workerConfig.assets?.binding ? {
13574
- [workerConfig.assets.binding]: ASSET_WORKER_NAME
13582
+ [workerConfig.assets.binding]: {
13583
+ node: (req, res) => {
13584
+ req[kRequestType] = "asset";
13585
+ viteDevServer.middlewares(req, res);
13586
+ }
13587
+ }
13575
13588
  } : {},
13576
13589
  __VITE_INVOKE_MODULE__: async (request) => {
13577
13590
  const payload = await request.json();
@@ -13611,6 +13624,7 @@ async function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspe
13611
13624
  logRequests: false,
13612
13625
  inspectorPort: inspectorPort === false ? void 0 : inspectorPort,
13613
13626
  unsafeInspectorProxy: inspectorPort !== false,
13627
+ unsafeDevRegistryPath: getDefaultDevRegistryPath(),
13614
13628
  handleRuntimeStdio(stdout, stderr) {
13615
13629
  const decoder = new TextDecoder();
13616
13630
  stdout.forEach((data2) => logger.info(decoder.decode(data2)));
@@ -13664,13 +13678,13 @@ async function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspe
13664
13678
  `export const ${className} = createWorkflowEntrypointWrapper('${className}');`
13665
13679
  );
13666
13680
  }
13681
+ logUnknownTails(
13682
+ workerOptions.tails,
13683
+ userWorkers,
13684
+ viteDevServer.config.logger.warn
13685
+ );
13667
13686
  return {
13668
13687
  ...workerOptions,
13669
- tails: filterTails(
13670
- workerOptions.tails,
13671
- userWorkers,
13672
- viteDevServer.config.logger.warn
13673
- ),
13674
13688
  modules: [
13675
13689
  {
13676
13690
  type: "ESModule",
@@ -13755,27 +13769,33 @@ function getPreviewModules(main, modulesRules) {
13755
13769
  async function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistState, mixedModeEnabled, inspectorPort) {
13756
13770
  const resolvedViteConfig = vitePreviewServer.config;
13757
13771
  const workers = (await Promise.all(
13758
- workerConfigs.map(async (workerConfig) => {
13772
+ workerConfigs.map(async (workerConfig, i) => {
13759
13773
  const mixedModeSession = mixedModeEnabled ? await maybeStartOrUpdateMixedModeSession(workerConfig) : void 0;
13760
13774
  const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
13761
13775
  workerConfig,
13762
13776
  void 0,
13763
13777
  {
13764
- mixedModeConnectionString: mixedModeSession?.mixedModeConnectionString
13778
+ mixedModeConnectionString: mixedModeSession?.mixedModeConnectionString,
13779
+ mixedModeEnabled
13765
13780
  }
13766
13781
  );
13767
13782
  const { externalWorkers } = miniflareWorkerOptions;
13768
13783
  const { ratelimits, modulesRules, ...workerOptions } = miniflareWorkerOptions.workerOptions;
13784
+ logUnknownTails(
13785
+ workerOptions.tails,
13786
+ workerConfigs,
13787
+ vitePreviewServer.config.logger.warn
13788
+ );
13769
13789
  return [
13770
13790
  {
13771
13791
  ...workerOptions,
13772
- tails: filterTails(
13773
- workerOptions.tails,
13774
- workerConfigs,
13775
- vitePreviewServer.config.logger.warn
13776
- ),
13777
13792
  name: workerOptions.name ?? workerConfig.name,
13778
13793
  unsafeInspectorProxy: inspectorPort !== false,
13794
+ unsafeDirectSockets: (
13795
+ // This exposes the default entrypoint of the entry worker on the dev registry
13796
+ // Assuming that the first worker config to be the entry worker.
13797
+ i === 0 ? [{ entrypoint: void 0, proxy: true }] : []
13798
+ ),
13779
13799
  ...miniflareWorkerOptions.main ? getPreviewModules(miniflareWorkerOptions.main, modulesRules) : { modules: true, script: "" }
13780
13800
  },
13781
13801
  ...externalWorkers
@@ -13787,6 +13807,7 @@ async function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, pers
13787
13807
  log: logger,
13788
13808
  inspectorPort: inspectorPort === false ? void 0 : inspectorPort,
13789
13809
  unsafeInspectorProxy: inspectorPort !== false,
13810
+ unsafeDevRegistryPath: getDefaultDevRegistryPath(),
13790
13811
  handleRuntimeStdio(stdout, stderr) {
13791
13812
  const decoder = new TextDecoder();
13792
13813
  stdout.forEach((data2) => logger.info(decoder.decode(data2)));
@@ -13904,12 +13925,15 @@ var nullableNonApplicable = [
13904
13925
  "site",
13905
13926
  "tsconfig"
13906
13927
  ];
13907
- function readWorkerConfig(configPath, env2) {
13928
+ function readWorkerConfig(configPath, env2, mixedModeEnabled) {
13908
13929
  const nonApplicable = {
13909
13930
  replacedByVite: /* @__PURE__ */ new Set(),
13910
13931
  notRelevant: /* @__PURE__ */ new Set()
13911
13932
  };
13912
- const config = unstable_readConfig2({ config: configPath, env: env2 }, {});
13933
+ const config = unstable_readConfig2(
13934
+ { config: configPath, env: env2 },
13935
+ { experimental: { mixedModeEnabled } }
13936
+ );
13913
13937
  const raw = structuredClone(config);
13914
13938
  nullableNonApplicable.forEach((prop) => {
13915
13939
  if (config[prop] !== void 0) {
@@ -14004,11 +14028,15 @@ function isNotRelevant(configName) {
14004
14028
  function missingFieldErrorMessage(field, configPath, env2) {
14005
14029
  return `No ${field} field provided in '${configPath}'${env2 ? ` for '${env2}' environment` : ""}`;
14006
14030
  }
14007
- function getWorkerConfig(configPath, env2, opts) {
14031
+ function getWorkerConfig(configPath, env2, mixedModeEnabled, opts) {
14008
14032
  if (opts?.visitedConfigPaths?.has(configPath)) {
14009
14033
  throw new Error(`Duplicate Wrangler config path found: ${configPath}`);
14010
14034
  }
14011
- const { raw, config, nonApplicable } = readWorkerConfig(configPath, env2);
14035
+ const { raw, config, nonApplicable } = readWorkerConfig(
14036
+ configPath,
14037
+ env2,
14038
+ mixedModeEnabled
14039
+ );
14012
14040
  opts?.visitedConfigPaths?.add(configPath);
14013
14041
  if (!config.name) {
14014
14042
  throw new Error(missingFieldErrorMessage(`'name'`, configPath, env2));
@@ -14133,6 +14161,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
14133
14161
  const entryWorkerResolvedConfig = getWorkerConfig(
14134
14162
  entryWorkerConfigPath,
14135
14163
  cloudflareEnv,
14164
+ pluginConfig.experimental?.mixedMode ?? false,
14136
14165
  {
14137
14166
  visitedConfigPaths: configPaths,
14138
14167
  isEntryWorker: true
@@ -14166,6 +14195,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
14166
14195
  const workerResolvedConfig = getWorkerConfig(
14167
14196
  workerConfigPath,
14168
14197
  cloudflareEnv,
14198
+ pluginConfig.experimental?.mixedMode ?? false,
14169
14199
  {
14170
14200
  visitedConfigPaths: configPaths
14171
14201
  }
@@ -14497,7 +14527,7 @@ function cloudflare2(pluginConfig = {}) {
14497
14527
  if (viteDevServer.httpServer) {
14498
14528
  handleWebSocket(viteDevServer.httpServer, async () => {
14499
14529
  assert10(miniflare, `Miniflare not defined`);
14500
- const routerWorker = await getRouterWorker(miniflare);
14530
+ const routerWorker = await miniflare.getWorker(ROUTER_WORKER_NAME);
14501
14531
  return routerWorker.fetch;
14502
14532
  });
14503
14533
  }
@@ -14505,14 +14535,21 @@ function cloudflare2(pluginConfig = {}) {
14505
14535
  viteDevServer.middlewares.use(async (req, res, next) => {
14506
14536
  try {
14507
14537
  assert10(miniflare, `Miniflare not defined`);
14508
- const routerWorker = await getRouterWorker(miniflare);
14509
14538
  const request = createRequest(req, res);
14510
- const response = await routerWorker.fetch(
14511
- toMiniflareRequest(request),
14512
- {
14513
- redirect: "manual"
14514
- }
14515
- );
14539
+ let response;
14540
+ if (req[kRequestType] === "asset") {
14541
+ const assetWorker = await miniflare.getWorker(ASSET_WORKER_NAME);
14542
+ response = await assetWorker.fetch(
14543
+ toMiniflareRequest(request),
14544
+ { redirect: "manual" }
14545
+ );
14546
+ } else {
14547
+ const routerWorker = await miniflare.getWorker(ROUTER_WORKER_NAME);
14548
+ response = await routerWorker.fetch(
14549
+ toMiniflareRequest(request),
14550
+ { redirect: "manual" }
14551
+ );
14552
+ }
14516
14553
  if (req.httpVersionMajor === 2) {
14517
14554
  response.headers.delete("transfer-encoding");
14518
14555
  }
@@ -14524,7 +14561,10 @@ function cloudflare2(pluginConfig = {}) {
14524
14561
  };
14525
14562
  },
14526
14563
  async configurePreviewServer(vitePreviewServer) {
14527
- const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
14564
+ const workerConfigs = getWorkerConfigs(
14565
+ vitePreviewServer.config.root,
14566
+ pluginConfig.experimental?.mixedMode ?? false
14567
+ );
14528
14568
  const inputInspectorPort = await getInputInspectorPortOption(
14529
14569
  pluginConfig,
14530
14570
  vitePreviewServer
@@ -14791,7 +14831,10 @@ function cloudflare2(pluginConfig = {}) {
14791
14831
  });
14792
14832
  },
14793
14833
  async configurePreviewServer(vitePreviewServer) {
14794
- const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
14834
+ const workerConfigs = getWorkerConfigs(
14835
+ vitePreviewServer.config.root,
14836
+ pluginConfig.experimental?.mixedMode ?? false
14837
+ );
14795
14838
  if (workerConfigs.length >= 1 && pluginConfig.inspectorPort !== false) {
14796
14839
  addDebugToVitePrintUrls(vitePreviewServer);
14797
14840
  }
@@ -11,6 +11,7 @@ var ADDITIONAL_MODULE_TYPES = [
11
11
  "Data",
12
12
  "Text"
13
13
  ];
14
+ var kRequestType = Symbol("kRequestType");
14
15
 
15
16
  // src/shared.ts
16
17
  var UNKNOWN_HOST = "http://localhost";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/vite-plugin",
3
- "version": "1.3.1",
3
+ "version": "1.5.0",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -41,8 +41,8 @@
41
41
  "tinyglobby": "^0.2.12",
42
42
  "unenv": "2.0.0-rc.17",
43
43
  "ws": "8.18.0",
44
- "miniflare": "4.20250525.0",
45
- "wrangler": "4.18.0"
44
+ "miniflare": "4.20250525.1",
45
+ "wrangler": "4.19.1"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@cloudflare/workers-types": "^4.20250525.0",
@@ -52,7 +52,6 @@
52
52
  "mlly": "^1.7.4",
53
53
  "tsup": "8.3.0",
54
54
  "typescript": "^5.7.2",
55
- "undici": "^5.28.5",
56
55
  "vite": "^6.1.0",
57
56
  "vitest": "~3.1.1",
58
57
  "@cloudflare/mock-npm-registry": "0.0.0",