@cloudflare/vite-plugin 1.12.3 → 1.13.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.
@@ -6005,6 +6005,9 @@ var ADDITIONAL_MODULE_TYPES = [
6005
6005
  "Data",
6006
6006
  "Text"
6007
6007
  ];
6008
+ var virtualPrefix = "virtual:cloudflare/";
6009
+ var VIRTUAL_USER_ENTRY = `${virtualPrefix}user-entry`;
6010
+ var VIRTUAL_NODEJS_COMPAT_ENTRY = `${virtualPrefix}nodejs-compat-entry`;
6008
6011
  var kRequestType = Symbol("kRequestType");
6009
6012
 
6010
6013
  // src/shared.ts
@@ -6015,6 +6018,7 @@ var additionalModuleGlobalRE = new RegExp(
6015
6018
  ADDITIONAL_MODULE_PATTERN,
6016
6019
  "g"
6017
6020
  );
6021
+ var VIRTUAL_WORKER_ENTRY = `${virtualPrefix}worker-entry`;
6018
6022
 
6019
6023
  // src/asset-workers/asset-worker.ts
6020
6024
  var CustomAssetWorker = class extends worker_default {
package/dist/index.js CHANGED
@@ -1748,6 +1748,14 @@ var ROUTER_WORKER_NAME = "__router-worker__";
1748
1748
  var ASSET_WORKER_NAME = "__asset-worker__";
1749
1749
  var VITE_PROXY_WORKER_NAME = "__vite_proxy_worker__";
1750
1750
  var ASSET_WORKERS_COMPATIBILITY_DATE = "2024-10-04";
1751
+ var ENTRY_MODULE_EXTENSIONS = [
1752
+ ".js",
1753
+ ".mjs",
1754
+ ".ts",
1755
+ ".mts",
1756
+ ".jsx",
1757
+ ".tsx"
1758
+ ];
1751
1759
  var ADDITIONAL_MODULE_TYPES = [
1752
1760
  "CompiledWasm",
1753
1761
  "Data",
@@ -1756,6 +1764,10 @@ var ADDITIONAL_MODULE_TYPES = [
1756
1764
  var PUBLIC_DIR_PREFIX = "/__vite_public_dir__";
1757
1765
  var DEFAULT_INSPECTOR_PORT = 9229;
1758
1766
  var DEBUG_PATH = "/__debug";
1767
+ var MAIN_ENTRY_NAME = "index";
1768
+ var virtualPrefix = "virtual:cloudflare/";
1769
+ var VIRTUAL_USER_ENTRY = `${virtualPrefix}user-entry`;
1770
+ var VIRTUAL_NODEJS_COMPAT_ENTRY = `${virtualPrefix}nodejs-compat-entry`;
1759
1771
  var kRequestType = Symbol("kRequestType");
1760
1772
 
1761
1773
  // src/additional-modules.ts
@@ -7434,6 +7446,7 @@ var additionalModuleGlobalRE = new RegExp(
7434
7446
  "g"
7435
7447
  );
7436
7448
  var WORKER_ENTRY_PATH_HEADER = "__VITE_WORKER_ENTRY_PATH__";
7449
+ var VIRTUAL_WORKER_ENTRY = `${virtualPrefix}worker-entry`;
7437
7450
 
7438
7451
  // src/utils.ts
7439
7452
  import * as path4 from "node:path";
@@ -7606,7 +7619,9 @@ function createCloudflareEnvironmentOptions({
7606
7619
  copyPublicDir: false,
7607
7620
  ssr: true,
7608
7621
  rollupOptions: {
7609
- input: workerConfig.main,
7622
+ input: {
7623
+ [MAIN_ENTRY_NAME]: VIRTUAL_WORKER_ENTRY
7624
+ },
7610
7625
  // workerd checks the types of the exports so we need to ensure that additional exports are not added to the entry module
7611
7626
  preserveEntrySignatures: "strict",
7612
7627
  // rolldown-only option
@@ -8956,7 +8971,7 @@ import colors3 from "picocolors";
8956
8971
  import { globSync } from "tinyglobby";
8957
8972
  import "vite";
8958
8973
  import {
8959
- experimental_maybeStartOrUpdateRemoteProxySession,
8974
+ maybeStartOrUpdateRemoteProxySession,
8960
8975
  unstable_convertConfigBindingsToStartWorkerBindings,
8961
8976
  unstable_getMiniflareWorkerOptions
8962
8977
  } from "wrangler";
@@ -9209,7 +9224,7 @@ async function getDevMiniflareOptions(config) {
9209
9224
  workerConfig
9210
9225
  );
9211
9226
  const preExistingRemoteProxySession = workerConfig.configPath ? remoteProxySessionsDataMap.get(workerConfig.configPath) : void 0;
9212
- const remoteProxySessionData = resolvedPluginConfig.experimental.remoteBindings ? await experimental_maybeStartOrUpdateRemoteProxySession(
9227
+ const remoteProxySessionData = resolvedPluginConfig.experimental.remoteBindings ?? true ? await maybeStartOrUpdateRemoteProxySession(
9213
9228
  {
9214
9229
  name: workerConfig.name,
9215
9230
  bindings: bindings ?? {}
@@ -9230,7 +9245,7 @@ async function getDevMiniflareOptions(config) {
9230
9245
  resolvedPluginConfig.cloudflareEnv,
9231
9246
  {
9232
9247
  remoteProxyConnectionString: remoteProxySessionData?.session?.remoteProxyConnectionString,
9233
- remoteBindingsEnabled: resolvedPluginConfig.experimental.remoteBindings,
9248
+ remoteBindingsEnabled: resolvedPluginConfig.experimental.remoteBindings ?? true,
9234
9249
  containerBuildId
9235
9250
  }
9236
9251
  );
@@ -9465,7 +9480,7 @@ async function getPreviewMiniflareOptions(config) {
9465
9480
  resolvedPluginConfig.workers.map(async (workerConfig, i) => {
9466
9481
  const bindings = unstable_convertConfigBindingsToStartWorkerBindings(workerConfig);
9467
9482
  const preExistingRemoteProxySessionData = workerConfig.configPath ? remoteProxySessionsDataMap.get(workerConfig.configPath) : void 0;
9468
- const remoteProxySessionData = resolvedPluginConfig.experimental.remoteBindings ? await experimental_maybeStartOrUpdateRemoteProxySession(
9483
+ const remoteProxySessionData = resolvedPluginConfig.experimental.remoteBindings ?? true ? await maybeStartOrUpdateRemoteProxySession(
9469
9484
  {
9470
9485
  name: workerConfig.name,
9471
9486
  bindings: bindings ?? {}
@@ -9483,7 +9498,7 @@ async function getPreviewMiniflareOptions(config) {
9483
9498
  void 0,
9484
9499
  {
9485
9500
  remoteProxyConnectionString: remoteProxySessionData?.session?.remoteProxyConnectionString,
9486
- remoteBindingsEnabled: resolvedPluginConfig.experimental.remoteBindings,
9501
+ remoteBindingsEnabled: resolvedPluginConfig.experimental.remoteBindings ?? true,
9487
9502
  containerBuildId
9488
9503
  }
9489
9504
  );
@@ -16605,17 +16620,10 @@ var NodeJsCompat = class {
16605
16620
  /**
16606
16621
  * Gets the necessary global polyfills to inject into the entry-point of the user's code.
16607
16622
  */
16608
- injectGlobalCode(id, code) {
16623
+ injectGlobalCode() {
16609
16624
  const injectedCode = Array.from(this.#virtualModulePathToSpecifier.keys()).map((moduleId) => `import "${moduleId}";`).join("\n");
16610
- const polyfillCode = this.#env.polyfill.map((polyfillPath) => `import "${polyfillPath}";
16611
- `).join("");
16612
- const modified = new MagicString(code);
16613
- modified.prepend(injectedCode);
16614
- modified.prepend(polyfillCode);
16615
- return {
16616
- code: modified.toString(),
16617
- map: modified.generateMap({ hires: "boundary", source: id })
16618
- };
16625
+ const polyfillCode = this.#env.polyfill.map((polyfillPath) => `import "${polyfillPath}";`).join("\n");
16626
+ return `${injectedCode}${polyfillCode}`;
16619
16627
  }
16620
16628
  /**
16621
16629
  * Resolves the `source` to a Node.js compat alias if possible.
@@ -16850,7 +16858,11 @@ function readWorkerConfig(configPath, env) {
16850
16858
  replacedByVite: /* @__PURE__ */ new Set(),
16851
16859
  notRelevant: /* @__PURE__ */ new Set()
16852
16860
  };
16853
- const config = unstable_readConfig2({ config: configPath, env });
16861
+ const config = unstable_readConfig2(
16862
+ { config: configPath, env },
16863
+ // Preserve the original `main` value so that Vite can resolve it
16864
+ { preserveOriginalMain: true }
16865
+ );
16854
16866
  const raw = structuredClone(config);
16855
16867
  nullableNonApplicable.forEach((prop) => {
16856
16868
  if (config[prop] !== void 0) {
@@ -16983,28 +16995,29 @@ function getWorkerConfig(configPath, env, opts) {
16983
16995
  if (!config.main) {
16984
16996
  throw new Error(missingFieldErrorMessage(`'main'`, configPath, env));
16985
16997
  }
16986
- const mainStat = fs5.statSync(config.main, { throwIfNoEntry: false });
16987
- if (!mainStat) {
16988
- throw new Error(
16989
- `The provided Wrangler config main field (${config.main}) doesn't point to an existing file`
16990
- );
16991
- }
16992
- if (mainStat.isDirectory()) {
16993
- throw new Error(
16994
- `The provided Wrangler config main field (${config.main}) points to a directory, it needs to point to a file instead`
16995
- );
16996
- }
16997
16998
  return {
16998
16999
  type: "worker",
16999
17000
  raw,
17000
17001
  config: {
17001
17002
  ...config,
17002
17003
  ...requiredFields,
17003
- main: config.main
17004
+ main: maybeResolveMain(config.main, configPath)
17004
17005
  },
17005
17006
  nonApplicable
17006
17007
  };
17007
17008
  }
17009
+ function maybeResolveMain(main, configPath) {
17010
+ if (!ENTRY_MODULE_EXTENSIONS.some((extension) => main.endsWith(extension))) {
17011
+ return main;
17012
+ }
17013
+ const resolvedMain = path11.resolve(path11.dirname(configPath), main);
17014
+ if (!fs5.existsSync(resolvedMain)) {
17015
+ throw new Error(
17016
+ `The provided Wrangler config main field (${resolvedMain}) doesn't point to an existing file`
17017
+ );
17018
+ }
17019
+ return resolvedMain;
17020
+ }
17008
17021
  function getValidatedWranglerConfigPath(root, requestedConfigPath, isForAuxiliaryWorker = false) {
17009
17022
  if (requestedConfigPath) {
17010
17023
  const configPath2 = path11.resolve(root, requestedConfigPath);
@@ -17332,9 +17345,6 @@ function cloudflare(pluginConfig = {}) {
17332
17345
  }
17333
17346
  };
17334
17347
  },
17335
- buildStart() {
17336
- workersConfigsWarningShown = false;
17337
- },
17338
17348
  // Vite `configResolved` Hook
17339
17349
  // see https://vite.dev/guide/api-plugin.html#configresolved
17340
17350
  configResolved(config) {
@@ -17346,24 +17356,35 @@ function cloudflare(pluginConfig = {}) {
17346
17356
  );
17347
17357
  }
17348
17358
  },
17349
- async transform(code, id) {
17359
+ buildStart() {
17360
+ workersConfigsWarningShown = false;
17361
+ },
17362
+ resolveId(source) {
17350
17363
  const workerConfig = getWorkerConfig2(this.environment.name);
17351
17364
  if (!workerConfig) {
17352
17365
  return;
17353
17366
  }
17354
- const resolvedWorkerEntry = await this.resolve(workerConfig.main);
17355
- if (id === resolvedWorkerEntry?.id) {
17356
- const modified = new MagicString(code);
17357
- const hmrCode = `
17367
+ if (source === VIRTUAL_WORKER_ENTRY) {
17368
+ return `\0${VIRTUAL_WORKER_ENTRY}`;
17369
+ }
17370
+ if (source === VIRTUAL_USER_ENTRY) {
17371
+ return this.resolve(workerConfig.main);
17372
+ }
17373
+ },
17374
+ load(id) {
17375
+ if (!getWorkerConfig2(this.environment.name)) {
17376
+ return;
17377
+ }
17378
+ if (id === `\0${VIRTUAL_WORKER_ENTRY}`) {
17379
+ const entryModule = getNodeJsCompat(this.environment.name) ? VIRTUAL_NODEJS_COMPAT_ENTRY : VIRTUAL_USER_ENTRY;
17380
+ return `
17381
+ import * as mod from "${entryModule}";
17382
+ export * from "${entryModule}";
17383
+ export default mod.default ?? {};
17358
17384
  if (import.meta.hot) {
17359
- import.meta.hot.accept();
17385
+ import.meta.hot.accept();
17360
17386
  }
17361
- `;
17362
- modified.append(hmrCode);
17363
- return {
17364
- code: modified.toString(),
17365
- map: modified.generateMap({ hires: "boundary", source: id })
17366
- };
17387
+ `;
17367
17388
  }
17368
17389
  },
17369
17390
  generateBundle(_, bundle) {
@@ -17371,13 +17392,17 @@ if (import.meta.hot) {
17371
17392
  let config;
17372
17393
  if (resolvedPluginConfig.type === "workers") {
17373
17394
  const workerConfig = resolvedPluginConfig.workers[this.environment.name];
17374
- const entryChunk = Object.entries(bundle).find(
17375
- ([_2, chunk]) => chunk.type === "chunk" && chunk.isEntry
17376
- );
17377
- if (!workerConfig || !entryChunk) {
17395
+ if (!workerConfig) {
17378
17396
  return;
17379
17397
  }
17380
- workerConfig.main = entryChunk[0];
17398
+ const entryChunk = Object.values(bundle).find(
17399
+ (chunk) => chunk.type === "chunk" && chunk.isEntry && chunk.name === MAIN_ENTRY_NAME
17400
+ );
17401
+ assert12(
17402
+ entryChunk,
17403
+ `Expected entry chunk with name "${MAIN_ENTRY_NAME}"`
17404
+ );
17405
+ workerConfig.main = entryChunk.fileName;
17381
17406
  workerConfig.no_bundle = true;
17382
17407
  workerConfig.rules = [
17383
17408
  { type: "ESModule", globs: ["**/*.js", "**/*.mjs"] }
@@ -17816,6 +17841,9 @@ if (import.meta.hot) {
17816
17841
  // rather than allowing the resolve hook here to alias them to polyfills.
17817
17842
  enforce: "pre",
17818
17843
  async resolveId(source, importer, options) {
17844
+ if (source === VIRTUAL_NODEJS_COMPAT_ENTRY) {
17845
+ return `\0${VIRTUAL_NODEJS_COMPAT_ENTRY}`;
17846
+ }
17819
17847
  const nodeJsCompat = getNodeJsCompat(this.environment.name);
17820
17848
  assertHasNodeJsCompat(nodeJsCompat);
17821
17849
  if (nodeJsCompat.isGlobalVirtualModule(source)) {
@@ -17841,19 +17869,15 @@ if (import.meta.hot) {
17841
17869
  load(id) {
17842
17870
  const nodeJsCompat = getNodeJsCompat(this.environment.name);
17843
17871
  assertHasNodeJsCompat(nodeJsCompat);
17844
- return nodeJsCompat.getGlobalVirtualModule(id);
17845
- },
17846
- async transform(code, id) {
17847
- const workerConfig = getWorkerConfig2(this.environment.name);
17848
- if (!workerConfig) {
17849
- return;
17850
- }
17851
- const resolvedId = await this.resolve(workerConfig.main);
17852
- if (id === resolvedId?.id) {
17853
- const nodeJsCompat = getNodeJsCompat(this.environment.name);
17854
- assertHasNodeJsCompat(nodeJsCompat);
17855
- return nodeJsCompat.injectGlobalCode(id, code);
17872
+ if (id === `\0${VIRTUAL_NODEJS_COMPAT_ENTRY}`) {
17873
+ return `
17874
+ ${nodeJsCompat.injectGlobalCode()}
17875
+ import * as mod from "${VIRTUAL_USER_ENTRY}";
17876
+ export * from "${VIRTUAL_USER_ENTRY}";
17877
+ export default mod.default ?? {};
17878
+ `;
17856
17879
  }
17880
+ return nodeJsCompat.getGlobalVirtualModule(id);
17857
17881
  },
17858
17882
  async configureServer(viteDevServer) {
17859
17883
  await Promise.all(
@@ -11,6 +11,9 @@ var ADDITIONAL_MODULE_TYPES = [
11
11
  "Data",
12
12
  "Text"
13
13
  ];
14
+ var virtualPrefix = "virtual:cloudflare/";
15
+ var VIRTUAL_USER_ENTRY = `${virtualPrefix}user-entry`;
16
+ var VIRTUAL_NODEJS_COMPAT_ENTRY = `${virtualPrefix}nodejs-compat-entry`;
14
17
  var kRequestType = Symbol("kRequestType");
15
18
 
16
19
  // src/shared.ts
@@ -23,6 +26,7 @@ var additionalModuleGlobalRE = new RegExp(
23
26
  "g"
24
27
  );
25
28
  var WORKER_ENTRY_PATH_HEADER = "__VITE_WORKER_ENTRY_PATH__";
29
+ var VIRTUAL_WORKER_ENTRY = `${virtualPrefix}worker-entry`;
26
30
 
27
31
  // src/runner-worker/env.ts
28
32
  function stripInternalEnv(internalEnv) {
@@ -1316,7 +1320,7 @@ async function createModuleRunner(env, webSocket) {
1316
1320
  Object.seal(context[ssrModuleExportsKey]);
1317
1321
  } catch (error) {
1318
1322
  if (error instanceof Error) {
1319
- error.message = `Error running module "${module.id}".
1323
+ error.message = `Error running module "${module.url}".
1320
1324
  ${error.message}.`;
1321
1325
  }
1322
1326
  throw error;
@@ -1342,7 +1346,7 @@ async function getWorkerEntryExport(workerEntryPath2, exportName) {
1342
1346
  if (!moduleRunner) {
1343
1347
  throw new Error(`Module runner not initialized`);
1344
1348
  }
1345
- const module = await moduleRunner.import(workerEntryPath2);
1349
+ const module = await moduleRunner.import(VIRTUAL_WORKER_ENTRY);
1346
1350
  const exportValue = typeof module === "object" && module !== null && exportName in module && module[exportName];
1347
1351
  if (!exportValue) {
1348
1352
  throw new Error(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/vite-plugin",
3
- "version": "1.12.3",
3
+ "version": "1.13.0",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -37,14 +37,14 @@
37
37
  "get-port": "^7.1.0",
38
38
  "picocolors": "^1.1.1",
39
39
  "tinyglobby": "^0.2.12",
40
- "unenv": "2.0.0-rc.20",
40
+ "unenv": "2.0.0-rc.21",
41
41
  "ws": "8.18.0",
42
- "@cloudflare/unenv-preset": "2.7.2",
43
- "miniflare": "4.20250902.0",
44
- "wrangler": "4.34.0"
42
+ "wrangler": "4.36.0",
43
+ "@cloudflare/unenv-preset": "2.7.3",
44
+ "miniflare": "4.20250906.1"
45
45
  },
46
46
  "devDependencies": {
47
- "@cloudflare/workers-types": "^4.20250902.0",
47
+ "@cloudflare/workers-types": "^4.20250906.0",
48
48
  "@types/node": "^22.10.1",
49
49
  "@types/ws": "^8.5.13",
50
50
  "magic-string": "^0.30.12",
@@ -53,14 +53,14 @@
53
53
  "typescript": "^5.8.3",
54
54
  "vite": "7.0.0",
55
55
  "vitest": "~3.2.0",
56
- "@cloudflare/containers-shared": "0.2.10",
57
56
  "@cloudflare/mock-npm-registry": "0.0.0",
57
+ "@cloudflare/containers-shared": "0.2.10",
58
58
  "@cloudflare/workers-shared": "0.18.8",
59
59
  "@cloudflare/workers-tsconfig": "0.0.0"
60
60
  },
61
61
  "peerDependencies": {
62
62
  "vite": "^6.1.0 || ^7.0.0",
63
- "wrangler": "^4.34.0"
63
+ "wrangler": "^4.36.0"
64
64
  },
65
65
  "publishConfig": {
66
66
  "access": "public"