@cloudflare/vite-plugin 0.1.12 → 0.1.13

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.
Files changed (2) hide show
  1. package/dist/index.js +66 -89
  2. package/package.json +4 -4
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",
@@ -7533,14 +7535,14 @@ codes.ERR_INVALID_PACKAGE_TARGET = createError(
7533
7535
  * @param {boolean} [isImport=false]
7534
7536
  * @param {string} [base]
7535
7537
  */
7536
- (packagePath, key, target, isImport = false, base = void 0) => {
7537
- 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("./");
7538
7540
  if (key === ".") {
7539
7541
  assert5(isImport === false);
7540
- 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 "./"' : ""}`;
7541
7543
  }
7542
7544
  return `Invalid "${isImport ? "imports" : "exports"}" target ${JSON.stringify(
7543
- target
7545
+ target2
7544
7546
  )} defined for '${key}' in the package config ${packagePath}package.json${base ? ` imported from ${base}` : ""}${relatedError ? '; targets must start with "./"' : ""}`;
7545
7547
  },
7546
7548
  Error
@@ -7936,14 +7938,14 @@ var patternRegEx = /\*/g;
7936
7938
  var encodedSeparatorRegEx = /%2f|%5c/i;
7937
7939
  var emittedPackageWarnings = /* @__PURE__ */ new Set();
7938
7940
  var doubleSlashRegEx = /[/\\]{2}/;
7939
- function emitInvalidSegmentDeprecation(target, request, match, packageJsonUrl, internal, base, isTarget) {
7941
+ function emitInvalidSegmentDeprecation(target2, request, match, packageJsonUrl, internal, base, isTarget) {
7940
7942
  if (process$1.noDeprecation) {
7941
7943
  return;
7942
7944
  }
7943
7945
  const pjsonPath = fileURLToPath$1(packageJsonUrl);
7944
- const double = doubleSlashRegEx.exec(isTarget ? target : request) !== null;
7946
+ const double = doubleSlashRegEx.exec(isTarget ? target2 : request) !== null;
7945
7947
  process$1.emitWarning(
7946
- `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)}` : ""}.`,
7947
7949
  "DeprecationWarning",
7948
7950
  "DEP0166"
7949
7951
  );
@@ -8101,47 +8103,47 @@ function throwInvalidSubpath(request, match, packageJsonUrl, internal, base) {
8101
8103
  base && fileURLToPath$1(base)
8102
8104
  );
8103
8105
  }
8104
- function invalidPackageTarget(subpath, target, packageJsonUrl, internal, base) {
8105
- 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}`;
8106
8108
  return new ERR_INVALID_PACKAGE_TARGET(
8107
8109
  fileURLToPath$1(new URL$1(".", packageJsonUrl)),
8108
8110
  subpath,
8109
- target,
8111
+ target2,
8110
8112
  internal,
8111
8113
  base && fileURLToPath$1(base)
8112
8114
  );
8113
8115
  }
8114
- function resolvePackageTargetString(target, subpath, match, packageJsonUrl, base, pattern, internal, isPathMap, conditions) {
8115
- if (subpath !== "" && !pattern && target[target.length - 1] !== "/")
8116
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8117
- if (!target.startsWith("./")) {
8118
- 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("/")) {
8119
8121
  let isURL = false;
8120
8122
  try {
8121
- new URL$1(target);
8123
+ new URL$1(target2);
8122
8124
  isURL = true;
8123
8125
  } catch {
8124
8126
  }
8125
8127
  if (!isURL) {
8126
8128
  const exportTarget = pattern ? RegExpPrototypeSymbolReplace.call(
8127
8129
  patternRegEx,
8128
- target,
8130
+ target2,
8129
8131
  () => subpath
8130
- ) : target + subpath;
8132
+ ) : target2 + subpath;
8131
8133
  return packageResolve(exportTarget, packageJsonUrl, conditions);
8132
8134
  }
8133
8135
  }
8134
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8136
+ throw invalidPackageTarget(match, target2, packageJsonUrl, internal, base);
8135
8137
  }
8136
- if (invalidSegmentRegEx.exec(target.slice(2)) !== null) {
8137
- if (deprecatedInvalidSegmentRegEx.exec(target.slice(2)) === null) {
8138
+ if (invalidSegmentRegEx.exec(target2.slice(2)) !== null) {
8139
+ if (deprecatedInvalidSegmentRegEx.exec(target2.slice(2)) === null) {
8138
8140
  if (!isPathMap) {
8139
8141
  const request = pattern ? match.replace("*", () => subpath) : match + subpath;
8140
8142
  const resolvedTarget = pattern ? RegExpPrototypeSymbolReplace.call(
8141
8143
  patternRegEx,
8142
- target,
8144
+ target2,
8143
8145
  () => subpath
8144
- ) : target;
8146
+ ) : target2;
8145
8147
  emitInvalidSegmentDeprecation(
8146
8148
  resolvedTarget,
8147
8149
  request,
@@ -8153,14 +8155,14 @@ function resolvePackageTargetString(target, subpath, match, packageJsonUrl, base
8153
8155
  );
8154
8156
  }
8155
8157
  } else {
8156
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8158
+ throw invalidPackageTarget(match, target2, packageJsonUrl, internal, base);
8157
8159
  }
8158
8160
  }
8159
- const resolved = new URL$1(target, packageJsonUrl);
8161
+ const resolved = new URL$1(target2, packageJsonUrl);
8160
8162
  const resolvedPath = resolved.pathname;
8161
8163
  const packagePath = new URL$1(".", packageJsonUrl).pathname;
8162
8164
  if (!resolvedPath.startsWith(packagePath))
8163
- throw invalidPackageTarget(match, target, packageJsonUrl, internal, base);
8165
+ throw invalidPackageTarget(match, target2, packageJsonUrl, internal, base);
8164
8166
  if (subpath === "") return resolved;
8165
8167
  if (invalidSegmentRegEx.exec(subpath) !== null) {
8166
8168
  const request = pattern ? match.replace("*", () => subpath) : match + subpath;
@@ -8168,9 +8170,9 @@ function resolvePackageTargetString(target, subpath, match, packageJsonUrl, base
8168
8170
  if (!isPathMap) {
8169
8171
  const resolvedTarget = pattern ? RegExpPrototypeSymbolReplace.call(
8170
8172
  patternRegEx,
8171
- target,
8173
+ target2,
8172
8174
  () => subpath
8173
- ) : target;
8175
+ ) : target2;
8174
8176
  emitInvalidSegmentDeprecation(
8175
8177
  resolvedTarget,
8176
8178
  request,
@@ -8201,10 +8203,10 @@ function isArrayIndex(key) {
8201
8203
  if (`${keyNumber}` !== key) return false;
8202
8204
  return keyNumber >= 0 && keyNumber < 4294967295;
8203
8205
  }
8204
- function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, base, pattern, internal, isPathMap, conditions) {
8205
- if (typeof target === "string") {
8206
+ function resolvePackageTarget(packageJsonUrl, target2, subpath, packageSubpath, base, pattern, internal, isPathMap, conditions) {
8207
+ if (typeof target2 === "string") {
8206
8208
  return resolvePackageTargetString(
8207
- target,
8209
+ target2,
8208
8210
  subpath,
8209
8211
  packageSubpath,
8210
8212
  packageJsonUrl,
@@ -8215,8 +8217,8 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8215
8217
  conditions
8216
8218
  );
8217
8219
  }
8218
- if (Array.isArray(target)) {
8219
- const targetList = target;
8220
+ if (Array.isArray(target2)) {
8221
+ const targetList = target2;
8220
8222
  if (targetList.length === 0) return null;
8221
8223
  let lastException;
8222
8224
  let i = -1;
@@ -8256,8 +8258,8 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8256
8258
  }
8257
8259
  throw lastException;
8258
8260
  }
8259
- if (typeof target === "object" && target !== null) {
8260
- const keys = Object.getOwnPropertyNames(target);
8261
+ if (typeof target2 === "object" && target2 !== null) {
8262
+ const keys = Object.getOwnPropertyNames(target2);
8261
8263
  let i = -1;
8262
8264
  while (++i < keys.length) {
8263
8265
  const key = keys[i];
@@ -8275,7 +8277,7 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8275
8277
  if (key === "default" || conditions && conditions.has(key)) {
8276
8278
  const conditionalTarget = (
8277
8279
  /** @type {unknown} */
8278
- target[key]
8280
+ target2[key]
8279
8281
  );
8280
8282
  const resolveResult = resolvePackageTarget(
8281
8283
  packageJsonUrl,
@@ -8294,12 +8296,12 @@ function resolvePackageTarget(packageJsonUrl, target, subpath, packageSubpath, b
8294
8296
  }
8295
8297
  return null;
8296
8298
  }
8297
- if (target === null) {
8299
+ if (target2 === null) {
8298
8300
  return null;
8299
8301
  }
8300
8302
  throw invalidPackageTarget(
8301
8303
  packageSubpath,
8302
- target,
8304
+ target2,
8303
8305
  packageJsonUrl,
8304
8306
  internal,
8305
8307
  base
@@ -8346,10 +8348,10 @@ function packageExportsResolve(packageJsonUrl, packageSubpath, packageConfig, ba
8346
8348
  exports = { ".": exports };
8347
8349
  }
8348
8350
  if (own.call(exports, packageSubpath) && !packageSubpath.includes("*") && !packageSubpath.endsWith("/")) {
8349
- const target = exports[packageSubpath];
8351
+ const target2 = exports[packageSubpath];
8350
8352
  const resolveResult = resolvePackageTarget(
8351
8353
  packageJsonUrl,
8352
- target,
8354
+ target2,
8353
8355
  "",
8354
8356
  packageSubpath,
8355
8357
  base,
@@ -8389,13 +8391,13 @@ function packageExportsResolve(packageJsonUrl, packageSubpath, packageConfig, ba
8389
8391
  }
8390
8392
  }
8391
8393
  if (bestMatch) {
8392
- const target = (
8394
+ const target2 = (
8393
8395
  /** @type {unknown} */
8394
8396
  exports[bestMatch]
8395
8397
  );
8396
8398
  const resolveResult = resolvePackageTarget(
8397
8399
  packageJsonUrl,
8398
- target,
8400
+ target2,
8399
8401
  bestMatchSubpath,
8400
8402
  bestMatch,
8401
8403
  base,
@@ -8470,10 +8472,10 @@ function packageImportsResolve(name2, base, conditions) {
8470
8472
  }
8471
8473
  }
8472
8474
  if (bestMatch) {
8473
- const target = imports[bestMatch];
8475
+ const target2 = imports[bestMatch];
8474
8476
  const resolveResult = resolvePackageTarget(
8475
8477
  packageJsonUrl,
8476
- target,
8478
+ target2,
8477
8479
  bestMatchSubpath,
8478
8480
  bestMatch,
8479
8481
  base,
@@ -9035,6 +9037,17 @@ function getWorkerConfig(configPath, env2, opts) {
9035
9037
  };
9036
9038
  }
9037
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
+ }
9038
9051
  return {
9039
9052
  type: "worker",
9040
9053
  raw,
@@ -9135,8 +9148,9 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
9135
9148
  }
9136
9149
 
9137
9150
  // src/websockets.ts
9151
+ import { coupleWebSocket } from "miniflare";
9138
9152
  import { WebSocketServer } from "ws";
9139
- function handleWebSocket(httpServer, fetcher, logger) {
9153
+ function handleWebSocket(httpServer, fetcher) {
9140
9154
  const nodeWebSocket = new WebSocketServer({ noServer: true });
9141
9155
  httpServer.on(
9142
9156
  "upgrade",
@@ -9160,34 +9174,7 @@ function handleWebSocket(httpServer, fetcher, logger) {
9160
9174
  socket,
9161
9175
  head,
9162
9176
  async (clientWebSocket) => {
9163
- workerWebSocket.accept();
9164
- workerWebSocket.addEventListener("message", (event) => {
9165
- clientWebSocket.send(event.data);
9166
- });
9167
- workerWebSocket.addEventListener("error", (event) => {
9168
- logger.error(
9169
- `WebSocket error:
9170
- ${event.error?.stack || event.error?.message}`,
9171
- { error: event.error }
9172
- );
9173
- });
9174
- workerWebSocket.addEventListener("close", () => {
9175
- clientWebSocket.close();
9176
- });
9177
- clientWebSocket.on("message", (data2, isBinary) => {
9178
- workerWebSocket.send(
9179
- isBinary ? Array.isArray(data2) ? Buffer.concat(data2) : data2 : data2.toString()
9180
- );
9181
- });
9182
- clientWebSocket.on("error", (error) => {
9183
- logger.error(`WebSocket error:
9184
- ${error.stack || error.message}`, {
9185
- error
9186
- });
9187
- });
9188
- clientWebSocket.on("close", () => {
9189
- workerWebSocket.close();
9190
- });
9177
+ coupleWebSocket(clientWebSocket, workerWebSocket);
9191
9178
  nodeWebSocket.emit("connection", clientWebSocket, request);
9192
9179
  }
9193
9180
  );
@@ -9380,11 +9367,7 @@ function cloudflare2(pluginConfig = {}) {
9380
9367
  },
9381
9368
  { alwaysCallNext: false }
9382
9369
  );
9383
- handleWebSocket(
9384
- viteDevServer.httpServer,
9385
- entryWorker.fetch,
9386
- viteDevServer.config.logger
9387
- );
9370
+ handleWebSocket(viteDevServer.httpServer, entryWorker.fetch);
9388
9371
  return () => {
9389
9372
  viteDevServer.middlewares.use((req, res, next) => {
9390
9373
  middleware(req, res, next);
@@ -9406,16 +9389,10 @@ function cloudflare2(pluginConfig = {}) {
9406
9389
  },
9407
9390
  { alwaysCallNext: false }
9408
9391
  );
9409
- handleWebSocket(
9410
- vitePreviewServer.httpServer,
9411
- miniflare2.dispatchFetch,
9412
- vitePreviewServer.config.logger
9413
- );
9414
- return () => {
9415
- vitePreviewServer.middlewares.use((req, res, next) => {
9416
- middleware(req, res, next);
9417
- });
9418
- };
9392
+ handleWebSocket(vitePreviewServer.httpServer, miniflare2.dispatchFetch);
9393
+ vitePreviewServer.middlewares.use((req, res, next) => {
9394
+ middleware(req, res, next);
9395
+ });
9419
9396
  }
9420
9397
  },
9421
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.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -38,8 +38,8 @@
38
38
  "tinyglobby": "^0.2.12",
39
39
  "unenv": "2.0.0-rc.1",
40
40
  "ws": "8.18.0",
41
- "miniflare": "4.20250317.0",
42
- "wrangler": "4.1.0"
41
+ "miniflare": "4.20250317.1",
42
+ "wrangler": "4.2.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@cloudflare/workers-types": "^4.20250317.0",
@@ -58,7 +58,7 @@
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"