@cloudflare/vite-plugin 0.0.0-e2f5756c2 → 0.0.0-e62b097bb

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.d.ts CHANGED
@@ -19,6 +19,13 @@ interface PluginConfig extends EntryWorkerConfig {
19
19
  persistState?: PersistState;
20
20
  }
21
21
 
22
+ /**
23
+ * Vite plugin that enables a full-featured integration between Vite and the Cloudflare Workers runtime.
24
+ *
25
+ * See the [README](https://github.com/cloudflare/workers-sdk/tree/main/packages/vite-plugin-cloudflare#readme) for more details.
26
+ *
27
+ * @param pluginConfig An optional {@link PluginConfig} object.
28
+ */
22
29
  declare function cloudflare(pluginConfig?: PluginConfig): vite.Plugin;
23
30
 
24
31
  export { cloudflare };
package/dist/index.js CHANGED
@@ -1268,7 +1268,10 @@ var CloudflareDevEnvironment = class extends vite2.DevEnvironment {
1268
1268
  }
1269
1269
  }
1270
1270
  );
1271
- assert(response.ok, "Failed to initialize module runner");
1271
+ assert(
1272
+ response.ok,
1273
+ `Failed to initialize module runner, error: ${await response.text()}`
1274
+ );
1272
1275
  const webSocket = response.webSocket;
1273
1276
  assert(webSocket, "Failed to establish WebSocket");
1274
1277
  webSocket.accept();
@@ -1634,10 +1637,13 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1634
1637
  ];
1635
1638
  const userWorkers = resolvedPluginConfig.type === "workers" ? Object.entries(resolvedPluginConfig.workers).map(
1636
1639
  ([environmentName, workerConfig]) => {
1637
- const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions({
1638
- ...workerConfig,
1639
- assets: void 0
1640
- });
1640
+ const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
1641
+ {
1642
+ ...workerConfig,
1643
+ assets: void 0
1644
+ },
1645
+ resolvedPluginConfig.cloudflareEnv
1646
+ );
1641
1647
  const { ratelimits, ...workerOptions } = miniflareWorkerOptions.workerOptions;
1642
1648
  return {
1643
1649
  ...workerOptions,
@@ -2061,20 +2067,21 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
2061
2067
  const configPaths = /* @__PURE__ */ new Set();
2062
2068
  const persistState = pluginConfig.persistState ?? true;
2063
2069
  const root = userConfig.root ? path5.resolve(userConfig.root) : process.cwd();
2064
- const { CLOUDFLARE_ENV } = vite5.loadEnv(viteEnv.mode, root, "");
2070
+ const { CLOUDFLARE_ENV: cloudflareEnv } = vite5.loadEnv(
2071
+ viteEnv.mode,
2072
+ root,
2073
+ /* prefixes */
2074
+ ""
2075
+ );
2065
2076
  const configPath = pluginConfig.configPath ? path5.resolve(root, pluginConfig.configPath) : findWranglerConfig(root);
2066
2077
  assert6(
2067
2078
  configPath,
2068
2079
  `Config not found. Have you created a wrangler.json(c) or wrangler.toml file?`
2069
2080
  );
2070
- const entryWorkerResolvedConfig = getWorkerConfig(
2071
- configPath,
2072
- CLOUDFLARE_ENV,
2073
- {
2074
- visitedConfigPaths: configPaths,
2075
- isEntryWorker: true
2076
- }
2077
- );
2081
+ const entryWorkerResolvedConfig = getWorkerConfig(configPath, cloudflareEnv, {
2082
+ visitedConfigPaths: configPaths,
2083
+ isEntryWorker: true
2084
+ });
2078
2085
  if (entryWorkerResolvedConfig.type === "assets-only") {
2079
2086
  return {
2080
2087
  type: "assets-only",
@@ -2083,7 +2090,8 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
2083
2090
  persistState,
2084
2091
  rawConfigs: {
2085
2092
  entryWorker: entryWorkerResolvedConfig
2086
- }
2093
+ },
2094
+ cloudflareEnv
2087
2095
  };
2088
2096
  }
2089
2097
  const entryWorkerConfig = entryWorkerResolvedConfig.config;
@@ -2095,7 +2103,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
2095
2103
  for (const auxiliaryWorker of pluginConfig.auxiliaryWorkers ?? []) {
2096
2104
  const workerResolvedConfig = getWorkerConfig(
2097
2105
  path5.resolve(root, auxiliaryWorker.configPath),
2098
- CLOUDFLARE_ENV,
2106
+ cloudflareEnv,
2099
2107
  {
2100
2108
  visitedConfigPaths: configPaths
2101
2109
  }
@@ -2123,7 +2131,8 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
2123
2131
  rawConfigs: {
2124
2132
  entryWorker: entryWorkerResolvedConfig,
2125
2133
  auxiliaryWorkers: auxiliaryWorkersResolvedConfigs
2126
- }
2134
+ },
2135
+ cloudflareEnv
2127
2136
  };
2128
2137
  }
2129
2138
 
@@ -2314,13 +2323,28 @@ function cloudflare2(pluginConfig = {}) {
2314
2323
  );
2315
2324
  }
2316
2325
  config = workerConfig;
2326
+ if (workerConfig.configPath) {
2327
+ const dotDevDotVarsContent = getDotDevDotVarsContent(
2328
+ workerConfig.configPath,
2329
+ resolvedPluginConfig.cloudflareEnv
2330
+ );
2331
+ if (dotDevDotVarsContent) {
2332
+ this.emitFile({
2333
+ type: "asset",
2334
+ fileName: ".dev.vars",
2335
+ source: dotDevDotVarsContent
2336
+ });
2337
+ }
2338
+ }
2317
2339
  } else if (this.environment.name === "client") {
2318
2340
  const assetsOnlyConfig = resolvedPluginConfig.config;
2319
2341
  assetsOnlyConfig.assets.directory = ".";
2342
+ const filesToAssetsIgnore = ["wrangler.json", ".dev.vars"];
2320
2343
  this.emitFile({
2321
2344
  type: "asset",
2322
2345
  fileName: ".assetsignore",
2323
- source: "wrangler.json"
2346
+ source: `${filesToAssetsIgnore.join("\n")}
2347
+ `
2324
2348
  });
2325
2349
  config = assetsOnlyConfig;
2326
2350
  }
@@ -2329,7 +2353,9 @@ function cloudflare2(pluginConfig = {}) {
2329
2353
  }
2330
2354
  config.no_bundle = true;
2331
2355
  config.rules = [{ type: "ESModule", globs: ["**/*.js"] }];
2332
- config.unsafe = void 0;
2356
+ if (config.unsafe && Object.keys(config.unsafe).length === 0) {
2357
+ config.unsafe = void 0;
2358
+ }
2333
2359
  this.emitFile({
2334
2360
  type: "asset",
2335
2361
  fileName: "wrangler.json",
@@ -2398,6 +2424,17 @@ function cloudflare2(pluginConfig = {}) {
2398
2424
  }
2399
2425
  };
2400
2426
  }
2427
+ function getDotDevDotVarsContent(configPath, cloudflareEnv) {
2428
+ const configDir = path6.dirname(configPath);
2429
+ const defaultDotDevDotVarsPath = `${configDir}/.dev.vars`;
2430
+ const inputDotDevDotVarsPath = `${defaultDotDevDotVarsPath}${cloudflareEnv ? `.${cloudflareEnv}` : ""}`;
2431
+ const targetPath = fs4.existsSync(inputDotDevDotVarsPath) ? inputDotDevDotVarsPath : fs4.existsSync(defaultDotDevDotVarsPath) ? defaultDotDevDotVarsPath : null;
2432
+ if (targetPath) {
2433
+ const dotDevDotVarsContent = fs4.readFileSync(targetPath);
2434
+ return dotDevDotVarsContent;
2435
+ }
2436
+ return null;
2437
+ }
2401
2438
  export {
2402
2439
  cloudflare2 as cloudflare
2403
2440
  };
@@ -21,7 +21,7 @@ function stripInternalEnv(internalEnv) {
21
21
  return userEnv;
22
22
  }
23
23
 
24
- // ../../node_modules/.pnpm/vite@6.0.7_@types+node@18.19.59_jiti@2.4.2/node_modules/vite/dist/node/module-runner.js
24
+ // ../../node_modules/.pnpm/vite@6.0.7_@types+node@18.19.74_jiti@2.4.2/node_modules/vite/dist/node/module-runner.js
25
25
  var VALID_ID_PREFIX = "/@id/";
26
26
  var NULL_BYTE_PLACEHOLDER = "__x00__";
27
27
  var SOURCEMAPPING_URL = "sourceMa";
@@ -1531,7 +1531,14 @@ function createWorkerEntrypointWrapper(entrypoint) {
1531
1531
  const url = new URL(request.url);
1532
1532
  if (url.pathname === INIT_PATH) {
1533
1533
  const { 0: client, 1: server } = new WebSocketPair();
1534
- createModuleRunner(this.env, server);
1534
+ try {
1535
+ await createModuleRunner(this.env, server);
1536
+ } catch (e) {
1537
+ return new Response(
1538
+ e instanceof Error ? e.message : JSON.stringify(e),
1539
+ { status: 500 }
1540
+ );
1541
+ }
1535
1542
  return new Response(null, { status: 101, webSocket: client });
1536
1543
  }
1537
1544
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/vite-plugin",
3
- "version": "0.0.0-e2f5756c2",
3
+ "version": "0.0.0-e62b097bb",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -36,19 +36,19 @@
36
36
  "@hattip/adapter-node": "^0.0.49",
37
37
  "unenv": "npm:unenv-nightly@2.0.0-20241218-183400-5d6aec3",
38
38
  "ws": "^8.18.0",
39
- "miniflare": "0.0.0-e2f5756c2"
39
+ "miniflare": "0.0.0-e62b097bb"
40
40
  },
41
41
  "devDependencies": {
42
- "@cloudflare/workers-types": "^4.20241230.0",
42
+ "@cloudflare/workers-types": "^4.20250204.0",
43
43
  "@types/node": "^22.10.1",
44
44
  "@types/ws": "^8.5.13",
45
45
  "magic-string": "^0.30.12",
46
- "tsup": "^8.3.0",
46
+ "tsup": "8.3.0",
47
47
  "typescript": "^5.7.2",
48
48
  "vite": "^6.0.7",
49
- "wrangler": "0.0.0-e2f5756c2",
50
49
  "@cloudflare/workers-tsconfig": "0.0.0",
51
- "@cloudflare/workers-shared": "0.0.0-e2f5756c2"
50
+ "@cloudflare/workers-shared": "0.0.0-e62b097bb",
51
+ "wrangler": "0.0.0-e62b097bb"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "vite": "^6.0.7",
@@ -63,8 +63,8 @@
63
63
  "scripts": {
64
64
  "build": "tsup",
65
65
  "check:type": "tsc --build",
66
+ "dev": "tsup --watch",
66
67
  "test": "vitest",
67
- "test:ci": "vitest run",
68
- "watch": "tsup --watch"
68
+ "test:ci": "vitest run"
69
69
  }
70
70
  }