@cloudflare/vite-plugin 0.0.0-b24497daf → 0.0.0-b48741547

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
@@ -1200,6 +1200,7 @@ var cloudflareBuiltInModules = [
1200
1200
  "cloudflare:workflows"
1201
1201
  ];
1202
1202
  var defaultConditions = ["workerd", "module", "browser"];
1203
+ var target = "es2022";
1203
1204
  function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmentName) {
1204
1205
  return {
1205
1206
  resolve: {
@@ -1220,7 +1221,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
1220
1221
  createEnvironment(name2, config) {
1221
1222
  return new vite2.BuildEnvironment(name2, config);
1222
1223
  },
1223
- target: "es2022",
1224
+ target,
1224
1225
  // We need to enable `emitAssets` in order to support additional modules defined by `rules`
1225
1226
  emitAssets: true,
1226
1227
  outDir: getOutputDirectory(userConfig, environmentName),
@@ -1241,6 +1242,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
1241
1242
  exclude: [...cloudflareBuiltInModules],
1242
1243
  esbuildOptions: {
1243
1244
  platform: "neutral",
1245
+ target,
1244
1246
  conditions: [...defaultConditions, "development"],
1245
1247
  resolveExtensions: [
1246
1248
  ".mjs",
@@ -1552,7 +1554,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1552
1554
  }
1553
1555
  }
1554
1556
  ];
1555
- const userWorkers = resolvedPluginConfig.type === "workers" ? Object.entries(resolvedPluginConfig.workers).map(
1557
+ const workersFromConfig = resolvedPluginConfig.type === "workers" ? Object.entries(resolvedPluginConfig.workers).map(
1556
1558
  ([environmentName, workerConfig]) => {
1557
1559
  const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
1558
1560
  {
@@ -1561,51 +1563,58 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1561
1563
  },
1562
1564
  resolvedPluginConfig.cloudflareEnv
1563
1565
  );
1566
+ const { externalWorkers: externalWorkers2 } = miniflareWorkerOptions;
1564
1567
  const { ratelimits, ...workerOptions } = miniflareWorkerOptions.workerOptions;
1565
1568
  return {
1566
- ...workerOptions,
1567
- // We have to add the name again because `unstable_getMiniflareWorkerOptions` sets it to `undefined`
1568
- name: workerConfig.name,
1569
- modulesRoot: miniflareModulesRoot,
1570
- unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
1571
- bindings: {
1572
- ...workerOptions.bindings,
1573
- __VITE_ROOT__: resolvedViteConfig.root,
1574
- __VITE_ENTRY_PATH__: workerConfig.main
1575
- },
1576
- serviceBindings: {
1577
- ...workerOptions.serviceBindings,
1578
- ...environmentName === resolvedPluginConfig.entryWorkerEnvironmentName && workerConfig.assets?.binding ? {
1579
- [workerConfig.assets.binding]: ASSET_WORKER_NAME
1580
- } : {},
1581
- __VITE_INVOKE_MODULE__: async (request) => {
1582
- const payload = await request.json();
1583
- const invokePayloadData = payload.data;
1584
- assert4(
1585
- invokePayloadData.name === "fetchModule",
1586
- `Invalid invoke event: ${invokePayloadData.name}`
1587
- );
1588
- const [moduleId] = invokePayloadData.data;
1589
- const moduleRE = new RegExp(MODULE_PATTERN);
1590
- const shouldExternalize = (
1591
- // Worker modules (CompiledWasm, Text, Data)
1592
- moduleRE.test(moduleId)
1593
- );
1594
- if (shouldExternalize) {
1595
- const result2 = {
1596
- externalize: moduleId,
1597
- type: "module"
1598
- };
1599
- return MiniflareResponse.json({ result: result2 });
1569
+ externalWorkers: externalWorkers2,
1570
+ worker: {
1571
+ ...workerOptions,
1572
+ name: workerOptions.name ?? workerConfig.name,
1573
+ modulesRoot: miniflareModulesRoot,
1574
+ unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
1575
+ bindings: {
1576
+ ...workerOptions.bindings,
1577
+ __VITE_ROOT__: resolvedViteConfig.root,
1578
+ __VITE_ENTRY_PATH__: workerConfig.main
1579
+ },
1580
+ serviceBindings: {
1581
+ ...workerOptions.serviceBindings,
1582
+ ...environmentName === resolvedPluginConfig.entryWorkerEnvironmentName && workerConfig.assets?.binding ? {
1583
+ [workerConfig.assets.binding]: ASSET_WORKER_NAME
1584
+ } : {},
1585
+ __VITE_INVOKE_MODULE__: async (request) => {
1586
+ const payload = await request.json();
1587
+ const invokePayloadData = payload.data;
1588
+ assert4(
1589
+ invokePayloadData.name === "fetchModule",
1590
+ `Invalid invoke event: ${invokePayloadData.name}`
1591
+ );
1592
+ const [moduleId] = invokePayloadData.data;
1593
+ const moduleRE = new RegExp(MODULE_PATTERN);
1594
+ const shouldExternalize = (
1595
+ // Worker modules (CompiledWasm, Text, Data)
1596
+ moduleRE.test(moduleId)
1597
+ );
1598
+ if (shouldExternalize) {
1599
+ const result2 = {
1600
+ externalize: moduleId,
1601
+ type: "module"
1602
+ };
1603
+ return MiniflareResponse.json({ result: result2 });
1604
+ }
1605
+ const devEnvironment = viteDevServer.environments[environmentName];
1606
+ const result = await devEnvironment.hot.handleInvoke(payload);
1607
+ return MiniflareResponse.json(result);
1600
1608
  }
1601
- const devEnvironment = viteDevServer.environments[environmentName];
1602
- const result = await devEnvironment.hot.handleInvoke(payload);
1603
- return MiniflareResponse.json(result);
1604
1609
  }
1605
1610
  }
1606
1611
  };
1607
1612
  }
1608
1613
  ) : [];
1614
+ const userWorkers = workersFromConfig.map((options) => options.worker);
1615
+ const externalWorkers = workersFromConfig.flatMap(
1616
+ (options) => options.externalWorkers
1617
+ );
1609
1618
  const workerToWorkerEntrypointNamesMap = getWorkerToWorkerEntrypointNamesMap(userWorkers);
1610
1619
  const workerToDurableObjectClassNamesMap = getWorkerToDurableObjectClassNamesMap(userWorkers);
1611
1620
  const workerToWorkflowEntrypointClassNamesMap = getWorkerToWorkflowEntrypointClassNamesMap(userWorkers);
@@ -1625,6 +1634,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1625
1634
  ),
1626
1635
  workers: [
1627
1636
  ...assetWorkers,
1637
+ ...externalWorkers,
1628
1638
  ...userWorkers.map((workerOptions) => {
1629
1639
  const wrappers = [
1630
1640
  `import { createWorkerEntrypointWrapper, createDurableObjectWrapper, createWorkflowEntrypointWrapper } from '${RUNNER_PATH}';`,
@@ -1740,15 +1750,18 @@ function getPreviewMiniflareOptions(vitePreviewServer, persistState) {
1740
1750
  const workerConfigs = configPaths.map(
1741
1751
  (configPath) => unstable_readConfig({ config: configPath })
1742
1752
  );
1743
- const workers = workerConfigs.map((config) => {
1753
+ const workers = workerConfigs.flatMap((config) => {
1744
1754
  const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(config);
1755
+ const { externalWorkers } = miniflareWorkerOptions;
1745
1756
  const { ratelimits, modulesRules, ...workerOptions } = miniflareWorkerOptions.workerOptions;
1746
- return {
1747
- ...workerOptions,
1748
- // We have to add the name again because `unstable_getMiniflareWorkerOptions` sets it to `undefined`
1749
- name: config.name,
1750
- ...miniflareWorkerOptions.main ? getPreviewModules(miniflareWorkerOptions.main, modulesRules) : { modules: true, script: "" }
1751
- };
1757
+ return [
1758
+ {
1759
+ ...workerOptions,
1760
+ name: workerOptions.name ?? config.name,
1761
+ ...miniflareWorkerOptions.main ? getPreviewModules(miniflareWorkerOptions.main, modulesRules) : { modules: true, script: "" }
1762
+ },
1763
+ ...externalWorkers
1764
+ ];
1752
1765
  });
1753
1766
  const logger = new ViteMiniflareLogger(resolvedViteConfig);
1754
1767
  return {
@@ -7522,14 +7535,14 @@ codes.ERR_INVALID_PACKAGE_TARGET = createError(
7522
7535
  * @param {boolean} [isImport=false]
7523
7536
  * @param {string} [base]
7524
7537
  */
7525
- (packagePath, key, target, isImport = false, base = void 0) => {
7526
- const relatedError = typeof target === "string" && !isImport && target.length > 0 && !target.startsWith("./");
7538
+ (packagePath, key, target2, isImport = false, base = void 0) => {
7539
+ const relatedError = typeof target2 === "string" && !isImport && target2.length > 0 && !target2.startsWith("./");
7527
7540
  if (key === ".") {
7528
7541
  assert5(isImport === false);
7529
- return `Invalid "exports" main target ${JSON.stringify(target)} defined in the package config ${packagePath}package.json${base ? ` imported from ${base}` : ""}${relatedError ? '; targets must start with "./"' : ""}`;
7542
+ return `Invalid "exports" main target ${JSON.stringify(target2)} defined in the package config ${packagePath}package.json${base ? ` imported from ${base}` : ""}${relatedError ? '; targets must start with "./"' : ""}`;
7530
7543
  }
7531
7544
  return `Invalid "${isImport ? "imports" : "exports"}" target ${JSON.stringify(
7532
- target
7545
+ target2
7533
7546
  )} defined for '${key}' in the package config ${packagePath}package.json${base ? ` imported from ${base}` : ""}${relatedError ? '; targets must start with "./"' : ""}`;
7534
7547
  },
7535
7548
  Error
@@ -7925,14 +7938,14 @@ var patternRegEx = /\*/g;
7925
7938
  var encodedSeparatorRegEx = /%2f|%5c/i;
7926
7939
  var emittedPackageWarnings = /* @__PURE__ */ new Set();
7927
7940
  var doubleSlashRegEx = /[/\\]{2}/;
7928
- function emitInvalidSegmentDeprecation(target, request, match, packageJsonUrl, internal, base, isTarget) {
7941
+ function emitInvalidSegmentDeprecation(target2, request, match, packageJsonUrl, internal, base, isTarget) {
7929
7942
  if (process$1.noDeprecation) {
7930
7943
  return;
7931
7944
  }
7932
7945
  const pjsonPath = fileURLToPath$1(packageJsonUrl);
7933
- const double = doubleSlashRegEx.exec(isTarget ? target : request) !== null;
7946
+ const double = doubleSlashRegEx.exec(isTarget ? target2 : request) !== null;
7934
7947
  process$1.emitWarning(
7935
- `Use of deprecated ${double ? "double slash" : "leading or trailing slash matching"} resolving "${target}" for module request "${request}" ${request === match ? "" : `matched to "${match}" `}in the "${internal ? "imports" : "exports"}" field module resolution of the package at ${pjsonPath}${base ? ` imported from ${fileURLToPath$1(base)}` : ""}.`,
7948
+ `Use of deprecated ${double ? "double slash" : "leading or trailing slash matching"} resolving "${target2}" for module request "${request}" ${request === match ? "" : `matched to "${match}" `}in the "${internal ? "imports" : "exports"}" field module resolution of the package at ${pjsonPath}${base ? ` imported from ${fileURLToPath$1(base)}` : ""}.`,
7936
7949
  "DeprecationWarning",
7937
7950
  "DEP0166"
7938
7951
  );
@@ -8090,47 +8103,47 @@ function throwInvalidSubpath(request, match, packageJsonUrl, internal, base) {
8090
8103
  base && fileURLToPath$1(base)
8091
8104
  );
8092
8105
  }
8093
- function invalidPackageTarget(subpath, target, packageJsonUrl, internal, base) {
8094
- target = typeof target === "object" && target !== null ? JSON.stringify(target, null, "") : `${target}`;
8106
+ function invalidPackageTarget(subpath, target2, packageJsonUrl, internal, base) {
8107
+ target2 = typeof target2 === "object" && target2 !== null ? JSON.stringify(target2, null, "") : `${target2}`;
8095
8108
  return new ERR_INVALID_PACKAGE_TARGET(
8096
8109
  fileURLToPath$1(new URL$1(".", packageJsonUrl)),
8097
8110
  subpath,
8098
- target,
8111
+ target2,
8099
8112
  internal,
8100
8113
  base && fileURLToPath$1(base)
8101
8114
  );
8102
8115
  }
8103
- function resolvePackageTargetString(target, subpath, match, packageJsonUrl, base, pattern, internal, isPathMap, conditions) {
8104
- if (subpath !== "" && !pattern && target[target.length - 1] !== "/")
8105
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8106
- if (!target.startsWith("./")) {
8107
- if (internal && !target.startsWith("../") && !target.startsWith("/")) {
8116
+ function resolvePackageTargetString(target2, subpath, match, packageJsonUrl, base, pattern, internal, isPathMap, conditions) {
8117
+ if (subpath !== "" && !pattern && target2[target2.length - 1] !== "/")
8118
+ throw invalidPackageTarget(match, target2, packageJsonUrl, internal, base);
8119
+ if (!target2.startsWith("./")) {
8120
+ if (internal && !target2.startsWith("../") && !target2.startsWith("/")) {
8108
8121
  let isURL = false;
8109
8122
  try {
8110
- new URL$1(target);
8123
+ new URL$1(target2);
8111
8124
  isURL = true;
8112
8125
  } catch {
8113
8126
  }
8114
8127
  if (!isURL) {
8115
8128
  const exportTarget = pattern ? RegExpPrototypeSymbolReplace.call(
8116
8129
  patternRegEx,
8117
- target,
8130
+ target2,
8118
8131
  () => subpath
8119
- ) : target + subpath;
8132
+ ) : target2 + subpath;
8120
8133
  return packageResolve(exportTarget, packageJsonUrl, conditions);
8121
8134
  }
8122
8135
  }
8123
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8136
+ throw invalidPackageTarget(match, target2, packageJsonUrl, internal, base);
8124
8137
  }
8125
- if (invalidSegmentRegEx.exec(target.slice(2)) !== null) {
8126
- if (deprecatedInvalidSegmentRegEx.exec(target.slice(2)) === null) {
8138
+ if (invalidSegmentRegEx.exec(target2.slice(2)) !== null) {
8139
+ if (deprecatedInvalidSegmentRegEx.exec(target2.slice(2)) === null) {
8127
8140
  if (!isPathMap) {
8128
8141
  const request = pattern ? match.replace("*", () => subpath) : match + subpath;
8129
8142
  const resolvedTarget = pattern ? RegExpPrototypeSymbolReplace.call(
8130
8143
  patternRegEx,
8131
- target,
8144
+ target2,
8132
8145
  () => subpath
8133
- ) : target;
8146
+ ) : target2;
8134
8147
  emitInvalidSegmentDeprecation(
8135
8148
  resolvedTarget,
8136
8149
  request,
@@ -8142,14 +8155,14 @@ function resolvePackageTargetString(target, subpath, match, packageJsonUrl, base
8142
8155
  );
8143
8156
  }
8144
8157
  } else {
8145
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8158
+ throw invalidPackageTarget(match, target2, packageJsonUrl, internal, base);
8146
8159
  }
8147
8160
  }
8148
- const resolved = new URL$1(target, packageJsonUrl);
8161
+ const resolved = new URL$1(target2, packageJsonUrl);
8149
8162
  const resolvedPath = resolved.pathname;
8150
8163
  const packagePath = new URL$1(".", packageJsonUrl).pathname;
8151
8164
  if (!resolvedPath.startsWith(packagePath))
8152
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8165
+ throw invalidPackageTarget(match, target2, packageJsonUrl, internal, base);
8153
8166
  if (subpath === "") return resolved;
8154
8167
  if (invalidSegmentRegEx.exec(subpath) !== null) {
8155
8168
  const request = pattern ? match.replace("*", () => subpath) : match + subpath;
@@ -8157,9 +8170,9 @@ function resolvePackageTargetString(target, subpath, match, packageJsonUrl, base
8157
8170
  if (!isPathMap) {
8158
8171
  const resolvedTarget = pattern ? RegExpPrototypeSymbolReplace.call(
8159
8172
  patternRegEx,
8160
- target,
8173
+ target2,
8161
8174
  () => subpath
8162
- ) : target;
8175
+ ) : target2;
8163
8176
  emitInvalidSegmentDeprecation(
8164
8177
  resolvedTarget,
8165
8178
  request,
@@ -8190,10 +8203,10 @@ function isArrayIndex(key) {
8190
8203
  if (`${keyNumber}` !== key) return false;
8191
8204
  return keyNumber >= 0 && keyNumber < 4294967295;
8192
8205
  }
8193
- function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, base, pattern, internal, isPathMap, conditions) {
8194
- if (typeof target === "string") {
8206
+ function resolvePackageTarget(packageJsonUrl, target2, subpath, packageSubpath, base, pattern, internal, isPathMap, conditions) {
8207
+ if (typeof target2 === "string") {
8195
8208
  return resolvePackageTargetString(
8196
- target,
8209
+ target2,
8197
8210
  subpath,
8198
8211
  packageSubpath,
8199
8212
  packageJsonUrl,
@@ -8204,8 +8217,8 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8204
8217
  conditions
8205
8218
  );
8206
8219
  }
8207
- if (Array.isArray(target)) {
8208
- const targetList = target;
8220
+ if (Array.isArray(target2)) {
8221
+ const targetList = target2;
8209
8222
  if (targetList.length === 0) return null;
8210
8223
  let lastException;
8211
8224
  let i = -1;
@@ -8245,8 +8258,8 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8245
8258
  }
8246
8259
  throw lastException;
8247
8260
  }
8248
- if (typeof target === "object" && target !== null) {
8249
- const keys = Object.getOwnPropertyNames(target);
8261
+ if (typeof target2 === "object" && target2 !== null) {
8262
+ const keys = Object.getOwnPropertyNames(target2);
8250
8263
  let i = -1;
8251
8264
  while (++i < keys.length) {
8252
8265
  const key = keys[i];
@@ -8264,7 +8277,7 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8264
8277
  if (key === "default" || conditions && conditions.has(key)) {
8265
8278
  const conditionalTarget = (
8266
8279
  /** @type {unknown} */
8267
- target[key]
8280
+ target2[key]
8268
8281
  );
8269
8282
  const resolveResult = resolvePackageTarget(
8270
8283
  packageJsonUrl,
@@ -8283,12 +8296,12 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8283
8296
  }
8284
8297
  return null;
8285
8298
  }
8286
- if (target === null) {
8299
+ if (target2 === null) {
8287
8300
  return null;
8288
8301
  }
8289
8302
  throw invalidPackageTarget(
8290
8303
  packageSubpath,
8291
- target,
8304
+ target2,
8292
8305
  packageJsonUrl,
8293
8306
  internal,
8294
8307
  base
@@ -8335,10 +8348,10 @@ function packageExportsResolve(packageJsonUrl, packageSubpath, packageConfig, ba
8335
8348
  exports = { ".": exports };
8336
8349
  }
8337
8350
  if (own.call(exports, packageSubpath) && !packageSubpath.includes("*") && !packageSubpath.endsWith("/")) {
8338
- const target = exports[packageSubpath];
8351
+ const target2 = exports[packageSubpath];
8339
8352
  const resolveResult = resolvePackageTarget(
8340
8353
  packageJsonUrl,
8341
- target,
8354
+ target2,
8342
8355
  "",
8343
8356
  packageSubpath,
8344
8357
  base,
@@ -8378,13 +8391,13 @@ function packageExportsResolve(packageJsonUrl, packageSubpath, packageConfig, ba
8378
8391
  }
8379
8392
  }
8380
8393
  if (bestMatch) {
8381
- const target = (
8394
+ const target2 = (
8382
8395
  /** @type {unknown} */
8383
8396
  exports[bestMatch]
8384
8397
  );
8385
8398
  const resolveResult = resolvePackageTarget(
8386
8399
  packageJsonUrl,
8387
- target,
8400
+ target2,
8388
8401
  bestMatchSubpath,
8389
8402
  bestMatch,
8390
8403
  base,
@@ -8459,10 +8472,10 @@ function packageImportsResolve(name2, base, conditions) {
8459
8472
  }
8460
8473
  }
8461
8474
  if (bestMatch) {
8462
- const target = imports[bestMatch];
8475
+ const target2 = imports[bestMatch];
8463
8476
  const resolveResult = resolvePackageTarget(
8464
8477
  packageJsonUrl,
8465
- target,
8478
+ target2,
8466
8479
  bestMatchSubpath,
8467
8480
  bestMatch,
8468
8481
  base,
@@ -8752,11 +8765,6 @@ function isNodeCompat(workerConfig) {
8752
8765
  if (nodeCompatMode === "v2") {
8753
8766
  return true;
8754
8767
  }
8755
- if (nodeCompatMode === "legacy") {
8756
- throw new Error(
8757
- "Unsupported Node.js compat mode (legacy). Remove the `node_compat` setting and add the `nodejs_compat` flag instead."
8758
- );
8759
- }
8760
8768
  if (nodeCompatMode === "v1") {
8761
8769
  throw new Error(
8762
8770
  `Unsupported Node.js compat mode (v1). Only the v2 mode is supported, either change your compat date to "2024-09-23" or later, or set the "nodejs_compat_v2" compatibility flag`
@@ -8863,7 +8871,6 @@ var nonApplicableWorkerConfigs = {
8863
8871
  "build",
8864
8872
  "find_additional_modules",
8865
8873
  "no_bundle",
8866
- "node_compat",
8867
8874
  "preserve_file_names",
8868
8875
  "site",
8869
8876
  "tsconfig",
@@ -8880,7 +8887,6 @@ var nullableNonApplicable = [
8880
8887
  "find_additional_modules",
8881
8888
  "minify",
8882
8889
  "no_bundle",
8883
- "node_compat",
8884
8890
  "preserve_file_names",
8885
8891
  "site",
8886
8892
  "tsconfig",
@@ -9031,6 +9037,17 @@ function getWorkerConfig(configPath, env2, opts) {
9031
9037
  };
9032
9038
  }
9033
9039
  assert7(config.main, missingFieldErrorMessage(`'main'`, configPath, env2));
9040
+ const mainStat = fs4.statSync(config.main, { throwIfNoEntry: false });
9041
+ if (!mainStat) {
9042
+ throw new Error(
9043
+ `The provided Wrangler config main field (${config.main}) doesn't point to an existing file`
9044
+ );
9045
+ }
9046
+ if (mainStat.isDirectory()) {
9047
+ throw new Error(
9048
+ `The provided Wrangler config main field (${config.main}) points to a directory, it needs to point to a file instead`
9049
+ );
9050
+ }
9034
9051
  return {
9035
9052
  type: "worker",
9036
9053
  raw,
@@ -9131,8 +9148,9 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
9131
9148
  }
9132
9149
 
9133
9150
  // src/websockets.ts
9151
+ import { coupleWebSocket } from "miniflare";
9134
9152
  import { WebSocketServer } from "ws";
9135
- function handleWebSocket(httpServer, fetcher, logger) {
9153
+ function handleWebSocket(httpServer, fetcher) {
9136
9154
  const nodeWebSocket = new WebSocketServer({ noServer: true });
9137
9155
  httpServer.on(
9138
9156
  "upgrade",
@@ -9156,34 +9174,7 @@ function handleWebSocket(httpServer, fetcher, logger) {
9156
9174
  socket,
9157
9175
  head,
9158
9176
  async (clientWebSocket) => {
9159
- workerWebSocket.accept();
9160
- workerWebSocket.addEventListener("message", (event) => {
9161
- clientWebSocket.send(event.data);
9162
- });
9163
- workerWebSocket.addEventListener("error", (event) => {
9164
- logger.error(
9165
- `WebSocket error:
9166
- ${event.error?.stack || event.error?.message}`,
9167
- { error: event.error }
9168
- );
9169
- });
9170
- workerWebSocket.addEventListener("close", () => {
9171
- clientWebSocket.close();
9172
- });
9173
- clientWebSocket.on("message", (data2, isBinary) => {
9174
- workerWebSocket.send(
9175
- isBinary ? Array.isArray(data2) ? Buffer.concat(data2) : data2 : data2.toString()
9176
- );
9177
- });
9178
- clientWebSocket.on("error", (error) => {
9179
- logger.error(`WebSocket error:
9180
- ${error.stack || error.message}`, {
9181
- error
9182
- });
9183
- });
9184
- clientWebSocket.on("close", () => {
9185
- workerWebSocket.close();
9186
- });
9177
+ coupleWebSocket(clientWebSocket, workerWebSocket);
9187
9178
  nodeWebSocket.emit("connection", clientWebSocket, request);
9188
9179
  }
9189
9180
  );
@@ -9376,11 +9367,7 @@ function cloudflare2(pluginConfig = {}) {
9376
9367
  },
9377
9368
  { alwaysCallNext: false }
9378
9369
  );
9379
- handleWebSocket(
9380
- viteDevServer.httpServer,
9381
- entryWorker.fetch,
9382
- viteDevServer.config.logger
9383
- );
9370
+ handleWebSocket(viteDevServer.httpServer, entryWorker.fetch);
9384
9371
  return () => {
9385
9372
  viteDevServer.middlewares.use((req, res, next) => {
9386
9373
  middleware(req, res, next);
@@ -9402,16 +9389,10 @@ function cloudflare2(pluginConfig = {}) {
9402
9389
  },
9403
9390
  { alwaysCallNext: false }
9404
9391
  );
9405
- handleWebSocket(
9406
- vitePreviewServer.httpServer,
9407
- miniflare2.dispatchFetch,
9408
- vitePreviewServer.config.logger
9409
- );
9410
- return () => {
9411
- vitePreviewServer.middlewares.use((req, res, next) => {
9412
- middleware(req, res, next);
9413
- });
9414
- };
9392
+ handleWebSocket(vitePreviewServer.httpServer, miniflare2.dispatchFetch);
9393
+ vitePreviewServer.middlewares.use((req, res, next) => {
9394
+ middleware(req, res, next);
9395
+ });
9415
9396
  }
9416
9397
  },
9417
9398
  // Plugin to support `CompiledWasm` modules
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/vite-plugin",
3
- "version": "0.0.0-b24497daf",
3
+ "version": "0.0.0-b48741547",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -38,10 +38,11 @@
38
38
  "tinyglobby": "^0.2.12",
39
39
  "unenv": "2.0.0-rc.1",
40
40
  "ws": "8.18.0",
41
- "miniflare": "0.0.0-b24497daf"
41
+ "miniflare": "0.0.0-b48741547",
42
+ "wrangler": "0.0.0-b48741547"
42
43
  },
43
44
  "devDependencies": {
44
- "@cloudflare/workers-types": "^4.20250224.0",
45
+ "@cloudflare/workers-types": "^4.20250317.0",
45
46
  "@types/node": "^22.10.1",
46
47
  "@types/ws": "^8.5.13",
47
48
  "magic-string": "^0.30.12",
@@ -51,14 +52,13 @@
51
52
  "undici": "^5.28.5",
52
53
  "vite": "^6.1.0",
53
54
  "vitest": "~3.0.5",
54
- "@cloudflare/workers-shared": "0.0.0-b24497daf",
55
- "@cloudflare/workers-tsconfig": "0.0.0",
56
55
  "@cloudflare/mock-npm-registry": "0.0.0",
57
- "wrangler": "0.0.0-b24497daf"
56
+ "@cloudflare/workers-tsconfig": "0.0.0",
57
+ "@cloudflare/workers-shared": "0.0.0-b48741547"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "vite": "^6.1.0",
61
- "wrangler": "^3.101.0"
61
+ "wrangler": "^3.101.0 || ^4.0.0"
62
62
  },
63
63
  "publishConfig": {
64
64
  "access": "public"
@@ -71,7 +71,7 @@
71
71
  "check:type": "tsc --build",
72
72
  "dev": "tsup --watch",
73
73
  "test": "vitest run",
74
- "test:ci": "pnpm test && pnpm test:e2e",
74
+ "test:ci": "pnpm test",
75
75
  "test:e2e": "vitest run -c e2e/vitest.config.ts",
76
76
  "test:watch": "vitest"
77
77
  }