@cloudflare/vite-plugin 0.0.0-e6fea1318 → 0.0.0-e7fce2523

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
@@ -485,11 +485,12 @@ var require_mime = __commonJS({
485
485
  });
486
486
 
487
487
  // src/index.ts
488
- import assert9 from "node:assert";
488
+ import assert10 from "node:assert";
489
489
  import * as fs5 from "node:fs";
490
490
  import * as fsp2 from "node:fs/promises";
491
491
  import * as path9 from "node:path";
492
- import { createMiddleware } from "@hattip/adapter-node";
492
+ import { createRequest, sendResponse } from "@mjackson/node-fetch-server";
493
+ import replace from "@rollup/plugin-replace";
493
494
 
494
495
  // ../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
495
496
  var comma = ",".charCodeAt(0);
@@ -1557,6 +1558,7 @@ var MagicString = class _MagicString {
1557
1558
 
1558
1559
  // src/index.ts
1559
1560
  import { Miniflare } from "miniflare";
1561
+ import colors3 from "picocolors";
1560
1562
  import * as vite7 from "vite";
1561
1563
 
1562
1564
  // src/constants.ts
@@ -1587,7 +1589,7 @@ function matchAdditionalModule(source) {
1587
1589
  return null;
1588
1590
  }
1589
1591
  function createModuleReference(type, id) {
1590
- return `__CLOUDFLARE_MODULE__${type}__${id}__`;
1592
+ return `__CLOUDFLARE_MODULE__${type}__${id}__CLOUDFLARE_MODULE__`;
1591
1593
  }
1592
1594
 
1593
1595
  // src/asset-config.ts
@@ -5782,10 +5784,17 @@ var z = /* @__PURE__ */ Object.freeze({
5782
5784
  // ../workers-shared/utils/types.ts
5783
5785
  var InternalConfigSchema = z.object({
5784
5786
  account_id: z.number().optional(),
5785
- script_id: z.number().optional()
5787
+ script_id: z.number().optional(),
5788
+ debug: z.boolean().optional()
5789
+ });
5790
+ var StaticRoutingSchema = z.object({
5791
+ version: z.literal(1),
5792
+ include: z.array(z.string()),
5793
+ exclude: z.array(z.string()).optional()
5786
5794
  });
5787
5795
  var RouterConfigSchema = z.object({
5788
5796
  invoke_user_worker_ahead_of_assets: z.boolean().optional(),
5797
+ static_routing: StaticRoutingSchema.optional(),
5789
5798
  has_user_worker: z.boolean().optional(),
5790
5799
  ...InternalConfigSchema.shape
5791
5800
  });
@@ -5826,18 +5835,19 @@ var AssetConfigSchema = z.object({
5826
5835
  not_found_handling: z.enum(["single-page-application", "404-page", "none"]).optional(),
5827
5836
  redirects: RedirectsSchema,
5828
5837
  headers: HeadersSchema,
5838
+ has_static_routing: z.boolean().optional(),
5829
5839
  ...InternalConfigSchema.shape
5830
5840
  });
5831
5841
 
5832
5842
  // src/asset-config.ts
5833
- function hasAssetsConfigChanged(resolvedPluginConfig, resolvedViteConfig, changedFile) {
5843
+ function hasAssetsConfigChanged(resolvedPluginConfig, resolvedViteConfig, changedFilePath) {
5834
5844
  if (!resolvedPluginConfig.experimental?.headersAndRedirectsDevModeSupport) {
5835
5845
  return false;
5836
5846
  }
5837
5847
  return [
5838
5848
  getRedirectsConfigPath(resolvedViteConfig),
5839
5849
  getHeadersConfigPath(resolvedViteConfig)
5840
- ].includes(path.resolve(changedFile));
5850
+ ].includes(changedFilePath);
5841
5851
  }
5842
5852
  function getAssetsConfig(resolvedPluginConfig, entryWorkerConfig, resolvedConfig) {
5843
5853
  const assetsConfig = resolvedPluginConfig.type === "assets-only" ? resolvedPluginConfig.config.assets : entryWorkerConfig?.assets;
@@ -12878,6 +12888,15 @@ function isNodeCompat(workerConfig) {
12878
12888
  }
12879
12889
  return false;
12880
12890
  }
12891
+ function isNodeAls(workerConfig) {
12892
+ return workerConfig !== void 0 && getNodeCompat(
12893
+ workerConfig.compatibility_date,
12894
+ workerConfig.compatibility_flags ?? []
12895
+ ).mode === "als";
12896
+ }
12897
+ function isNodeAlsModule(path10) {
12898
+ return /^(node:)?async_hooks$/.test(path10);
12899
+ }
12881
12900
  function injectGlobalCode(id, code) {
12882
12901
  const injectedCode = Object.entries(env.inject).map(([globalName, globalInject]) => {
12883
12902
  if (typeof globalInject === "string") {
@@ -12908,7 +12927,7 @@ globalThis.${globalName} = var_${globalName}.${exportName};
12908
12927
  }
12909
12928
  function resolveNodeJSImport(source) {
12910
12929
  const alias = env.alias[source];
12911
- if (alias) {
12930
+ if (alias && !nodeCompatExternals.has(alias)) {
12912
12931
  return {
12913
12932
  unresolved: alias,
12914
12933
  resolved: resolvePathSync(alias, { url: import.meta.url })
@@ -12939,8 +12958,9 @@ function getNodeCompatEntries() {
12939
12958
  return entries;
12940
12959
  }
12941
12960
  var NodeJsCompatWarnings = class {
12942
- constructor(environment) {
12943
- this.environment = environment;
12961
+ constructor(environmentName, resolvedViteConfig) {
12962
+ this.environmentName = environmentName;
12963
+ this.resolvedViteConfig = resolvedViteConfig;
12944
12964
  }
12945
12965
  sources = /* @__PURE__ */ new Map();
12946
12966
  timer;
@@ -12948,6 +12968,7 @@ var NodeJsCompatWarnings = class {
12948
12968
  const importers = this.sources.get(source) ?? /* @__PURE__ */ new Set();
12949
12969
  this.sources.set(source, importers);
12950
12970
  importers.add(importer);
12971
+ this.renderWarningsOnIdle();
12951
12972
  }
12952
12973
  renderWarningsOnIdle() {
12953
12974
  if (this.timer) {
@@ -12960,19 +12981,16 @@ var NodeJsCompatWarnings = class {
12960
12981
  }
12961
12982
  renderWarnings() {
12962
12983
  if (this.sources.size > 0) {
12963
- let message = `
12964
-
12965
- Unexpected Node.js imports for environment "${this.environment.name}". Do you need to enable the "nodejs_compat" compatibility flag?
12966
- Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details.
12984
+ let message = `Unexpected Node.js imports for environment "${this.environmentName}". Do you need to enable the "nodejs_compat" compatibility flag? Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details.
12967
12985
  `;
12968
12986
  this.sources.forEach((importers, source) => {
12969
12987
  importers.forEach((importer) => {
12970
- message += ` - "${source}" imported from "${path3.relative(this.environment.config.root, importer)}"
12988
+ message += ` - "${source}" imported from "${path3.relative(this.resolvedViteConfig.root, importer)}"
12971
12989
  `;
12972
12990
  });
12973
12991
  });
12974
- this.environment.logger.warn(message, {
12975
- environment: this.environment.name
12992
+ this.resolvedViteConfig.logger.warn(message, {
12993
+ timestamp: true
12976
12994
  });
12977
12995
  this.sources.clear();
12978
12996
  }
@@ -12982,7 +13000,7 @@ Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more
12982
13000
  // src/shared.ts
12983
13001
  var UNKNOWN_HOST = "http://localhost";
12984
13002
  var INIT_PATH = "/__vite_plugin_cloudflare_init__";
12985
- var ADDITIONAL_MODULE_PATTERN = `__CLOUDFLARE_MODULE__(${ADDITIONAL_MODULE_TYPES.join("|")})__(.*?)__`;
13003
+ var ADDITIONAL_MODULE_PATTERN = `__CLOUDFLARE_MODULE__(${ADDITIONAL_MODULE_TYPES.join("|")})__(.*?)__CLOUDFLARE_MODULE__`;
12986
13004
  var additionalModuleRE = new RegExp(ADDITIONAL_MODULE_PATTERN);
12987
13005
  var additionalModuleGlobalRE = new RegExp(
12988
13006
  ADDITIONAL_MODULE_PATTERN,
@@ -12992,6 +13010,7 @@ var VITE_DEV_METADATA_HEADER = "__VITE_DEV_METADATA__";
12992
13010
 
12993
13011
  // src/utils.ts
12994
13012
  import * as path4 from "node:path";
13013
+ import getPort, { portNumbers } from "get-port";
12995
13014
  import { Request as MiniflareRequest } from "miniflare";
12996
13015
  import "vite";
12997
13016
  function getOutputDirectory(userConfig, environmentName) {
@@ -13017,23 +13036,13 @@ function toMiniflareRequest(request) {
13017
13036
  duplex: "half"
13018
13037
  });
13019
13038
  }
13020
- function nodeHeadersToWebHeaders(nodeHeaders) {
13021
- const headers = new Headers();
13022
- for (const [key, value] of Object.entries(nodeHeaders)) {
13023
- if (typeof value === "string") {
13024
- headers.append(key, value);
13025
- } else if (Array.isArray(value)) {
13026
- for (const item of value) {
13027
- headers.append(key, item);
13028
- }
13029
- }
13030
- }
13031
- return headers;
13032
- }
13033
13039
  var postfixRE = /[?#].*$/;
13034
13040
  function cleanUrl(url) {
13035
13041
  return url.replace(postfixRE, "");
13036
13042
  }
13043
+ function getFirstAvailablePort(start) {
13044
+ return getPort({ port: portNumbers(start, 65535) });
13045
+ }
13037
13046
 
13038
13047
  // src/cloudflare-environment.ts
13039
13048
  var webSocketUndefinedError = "The WebSocket is undefined";
@@ -13340,6 +13349,7 @@ import {
13340
13349
  LogLevel,
13341
13350
  Response as MiniflareResponse
13342
13351
  } from "miniflare";
13352
+ import colors2 from "picocolors";
13343
13353
  import { globSync } from "tinyglobby";
13344
13354
  import "vite";
13345
13355
  import { unstable_getMiniflareWorkerOptions } from "wrangler";
@@ -13444,7 +13454,30 @@ function getEntryWorkerConfig(resolvedPluginConfig) {
13444
13454
  }
13445
13455
  return resolvedPluginConfig.workers[resolvedPluginConfig.entryWorkerEnvironmentName];
13446
13456
  }
13447
- function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
13457
+ function filterTails(tails, userWorkers, log) {
13458
+ return tails?.filter((tailService) => {
13459
+ let name;
13460
+ if (typeof tailService === "string") {
13461
+ name = tailService;
13462
+ } else if (typeof tailService === "object" && "name" in tailService && typeof tailService.name === "string") {
13463
+ name = tailService.name;
13464
+ } else {
13465
+ return true;
13466
+ }
13467
+ const found = userWorkers.some((w) => w.name === name);
13468
+ if (!found) {
13469
+ log(
13470
+ colors2.dim(
13471
+ colors2.yellow(
13472
+ `Tail consumer "${name}" was not found in your config. Make sure you add it if you'd like to simulate receiving tail events locally.`
13473
+ )
13474
+ )
13475
+ );
13476
+ }
13477
+ return found;
13478
+ });
13479
+ }
13480
+ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspectorPort) {
13448
13481
  const resolvedViteConfig = viteDevServer.config;
13449
13482
  const entryWorkerConfig = getEntryWorkerConfig(resolvedPluginConfig);
13450
13483
  const assetsConfig = getAssetsConfig(
@@ -13536,7 +13569,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
13536
13569
  worker: {
13537
13570
  ...workerOptions,
13538
13571
  name: workerOptions.name ?? workerConfig.name,
13539
- unsafeInspectorProxy: resolvedPluginConfig.inspectorPort !== false,
13572
+ unsafeInspectorProxy: inspectorPort !== false,
13540
13573
  modulesRoot: miniflareModulesRoot,
13541
13574
  unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
13542
13575
  serviceBindings: {
@@ -13578,8 +13611,8 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
13578
13611
  const logger = new ViteMiniflareLogger(resolvedViteConfig);
13579
13612
  return {
13580
13613
  log: logger,
13581
- inspectorPort: resolvedPluginConfig.inspectorPort || void 0,
13582
- unsafeInspectorProxy: resolvedPluginConfig.inspectorPort !== false,
13614
+ inspectorPort: inspectorPort === false ? void 0 : inspectorPort,
13615
+ unsafeInspectorProxy: inspectorPort !== false,
13583
13616
  handleRuntimeStdio(stdout, stderr) {
13584
13617
  const decoder = new TextDecoder();
13585
13618
  stdout.forEach((data2) => logger.info(decoder.decode(data2)));
@@ -13635,6 +13668,11 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
13635
13668
  }
13636
13669
  return {
13637
13670
  ...workerOptions,
13671
+ tails: filterTails(
13672
+ workerOptions.tails,
13673
+ userWorkers,
13674
+ viteDevServer.config.logger.warn
13675
+ ),
13638
13676
  modules: [
13639
13677
  {
13640
13678
  type: "ESModule",
@@ -13716,7 +13754,7 @@ function getPreviewModules(main, modulesRules) {
13716
13754
  ]
13717
13755
  };
13718
13756
  }
13719
- function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistState, inspectorPort = DEFAULT_INSPECTOR_PORT) {
13757
+ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistState, inspectorPort) {
13720
13758
  const resolvedViteConfig = vitePreviewServer.config;
13721
13759
  const workers = workerConfigs.flatMap((config) => {
13722
13760
  const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(config);
@@ -13725,6 +13763,11 @@ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistSta
13725
13763
  return [
13726
13764
  {
13727
13765
  ...workerOptions,
13766
+ tails: filterTails(
13767
+ workerOptions.tails,
13768
+ workerConfigs,
13769
+ vitePreviewServer.config.logger.warn
13770
+ ),
13728
13771
  name: workerOptions.name ?? config.name,
13729
13772
  unsafeInspectorProxy: inspectorPort !== false,
13730
13773
  ...miniflareWorkerOptions.main ? getPreviewModules(miniflareWorkerOptions.main, modulesRules) : { modules: true, script: "" }
@@ -13735,7 +13778,7 @@ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistSta
13735
13778
  const logger = new ViteMiniflareLogger(resolvedViteConfig);
13736
13779
  return {
13737
13780
  log: logger,
13738
- inspectorPort: inspectorPort || void 0,
13781
+ inspectorPort: inspectorPort === false ? void 0 : inspectorPort,
13739
13782
  unsafeInspectorProxy: inspectorPort !== false,
13740
13783
  handleRuntimeStdio(stdout, stderr) {
13741
13784
  const decoder = new TextDecoder();
@@ -13824,8 +13867,7 @@ var nonApplicableWorkerConfigs = {
13824
13867
  "preserve_file_names",
13825
13868
  "rules",
13826
13869
  "site",
13827
- "tsconfig",
13828
- "upload_source_maps"
13870
+ "tsconfig"
13829
13871
  ]
13830
13872
  };
13831
13873
  var nullableNonApplicable = [
@@ -13836,8 +13878,7 @@ var nullableNonApplicable = [
13836
13878
  "no_bundle",
13837
13879
  "preserve_file_names",
13838
13880
  "site",
13839
- "tsconfig",
13840
- "upload_source_maps"
13881
+ "tsconfig"
13841
13882
  ];
13842
13883
  function readWorkerConfig(configPath, env2) {
13843
13884
  const nonApplicable = {
@@ -14053,7 +14094,6 @@ function workerNameToEnvironmentName(workerName) {
14053
14094
  function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
14054
14095
  const configPaths = /* @__PURE__ */ new Set();
14055
14096
  const persistState = pluginConfig.persistState ?? true;
14056
- const inspectorPort = pluginConfig.inspectorPort ?? DEFAULT_INSPECTOR_PORT;
14057
14097
  const experimental = pluginConfig.experimental ?? {};
14058
14098
  const root = userConfig.root ? path8.resolve(userConfig.root) : process.cwd();
14059
14099
  const { CLOUDFLARE_ENV: cloudflareEnv } = vite6.loadEnv(
@@ -14079,7 +14119,6 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
14079
14119
  type: "assets-only",
14080
14120
  config: entryWorkerResolvedConfig.config,
14081
14121
  configPaths,
14082
- inspectorPort,
14083
14122
  persistState,
14084
14123
  rawConfigs: {
14085
14124
  entryWorker: entryWorkerResolvedConfig
@@ -14125,7 +14164,6 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
14125
14164
  type: "workers",
14126
14165
  configPaths,
14127
14166
  persistState,
14128
- inspectorPort,
14129
14167
  workers,
14130
14168
  entryWorkerEnvironmentName,
14131
14169
  rawConfigs: {
@@ -14138,6 +14176,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
14138
14176
  }
14139
14177
 
14140
14178
  // src/websockets.ts
14179
+ import { createHeaders } from "@mjackson/node-fetch-server";
14141
14180
  import { coupleWebSocket } from "miniflare";
14142
14181
  import { WebSocketServer } from "ws";
14143
14182
  function handleWebSocket(httpServer, getFetcher) {
@@ -14149,7 +14188,7 @@ function handleWebSocket(httpServer, getFetcher) {
14149
14188
  if (request.headers["sec-websocket-protocol"]?.startsWith("vite")) {
14150
14189
  return;
14151
14190
  }
14152
- const headers = nodeHeadersToWebHeaders(request.headers);
14191
+ const headers = createHeaders(request);
14153
14192
  const fetcher = await getFetcher();
14154
14193
  const response = await fetcher(url, {
14155
14194
  headers,
@@ -14173,6 +14212,57 @@ function handleWebSocket(httpServer, getFetcher) {
14173
14212
  );
14174
14213
  }
14175
14214
 
14215
+ // src/worker-environments-validation.ts
14216
+ import assert9 from "node:assert";
14217
+ function validateWorkerEnvironmentsResolvedConfigs(resolvedPluginConfig, resolvedViteConfig) {
14218
+ const workersEnvironmentNames = Object.keys(resolvedPluginConfig.workers);
14219
+ const disallowedEnvsConfigs = /* @__PURE__ */ new Map();
14220
+ for (const envName of workersEnvironmentNames) {
14221
+ const workerEnvConfig = resolvedViteConfig.environments[envName];
14222
+ assert9(workerEnvConfig, `Missing environment config for "${envName}"`);
14223
+ const { optimizeDeps, resolve: resolve7 } = workerEnvConfig;
14224
+ const disallowedConfig = {};
14225
+ const disallowedOptimizeDepsExcludeEntries = (optimizeDeps.exclude ?? []).filter((entry) => {
14226
+ if (cloudflareBuiltInModules.includes(entry)) {
14227
+ return false;
14228
+ }
14229
+ if (isNodeAlsModule(entry) && isNodeAls(resolvedPluginConfig.workers[envName])) {
14230
+ return false;
14231
+ }
14232
+ if (NODEJS_MODULES_RE.test(entry) && isNodeCompat(resolvedPluginConfig.workers[envName])) {
14233
+ return false;
14234
+ }
14235
+ return true;
14236
+ });
14237
+ if (disallowedOptimizeDepsExcludeEntries.length > 0) {
14238
+ disallowedConfig.optimizeDepsExclude = disallowedOptimizeDepsExcludeEntries;
14239
+ }
14240
+ if (resolve7.external === true || resolve7.external.length > 0) {
14241
+ disallowedConfig.resolveExternal = resolve7.external;
14242
+ }
14243
+ if (Object.keys(disallowedConfig).length > 0) {
14244
+ disallowedEnvsConfigs.set(envName, disallowedConfig);
14245
+ }
14246
+ }
14247
+ if (disallowedEnvsConfigs.size > 0) {
14248
+ const errorMessage = `The following environment configurations are incompatible with the Cloudflare Vite plugin:
14249
+ ${[
14250
+ ...disallowedEnvsConfigs
14251
+ ].map(
14252
+ ([envName, disallowedConfig]) => [
14253
+ disallowedConfig.optimizeDepsExclude ? ` - "${envName}" environment: \`optimizeDeps.exclude\`: ${JSON.stringify(disallowedConfig.optimizeDepsExclude)}
14254
+ ` : null,
14255
+ disallowedConfig.resolveExternal ? ` - "${envName}" environment: \`resolve.external\`: ${JSON.stringify(disallowedConfig.resolveExternal)}
14256
+ ` : null
14257
+ ].join("")
14258
+ ).join(
14259
+ ""
14260
+ )}To resolve this issue, avoid setting \`optimizeDeps.exclude\` and \`resolve.external\` in your Cloudflare Worker environments.
14261
+ `;
14262
+ throw new Error(errorMessage);
14263
+ }
14264
+ }
14265
+
14176
14266
  // src/index.ts
14177
14267
  var workersConfigsWarningShown = false;
14178
14268
  var miniflare;
@@ -14243,7 +14333,7 @@ function cloudflare2(pluginConfig = {}) {
14243
14333
  resolvedPluginConfig.workers
14244
14334
  ).map((environmentName) => {
14245
14335
  const environment = builder.environments[environmentName];
14246
- assert9(
14336
+ assert10(
14247
14337
  environment,
14248
14338
  `${environmentName} environment not found`
14249
14339
  );
@@ -14264,6 +14354,12 @@ function cloudflare2(pluginConfig = {}) {
14264
14354
  },
14265
14355
  configResolved(config) {
14266
14356
  resolvedViteConfig = config;
14357
+ if (resolvedPluginConfig?.type === "workers") {
14358
+ validateWorkerEnvironmentsResolvedConfigs(
14359
+ resolvedPluginConfig,
14360
+ resolvedViteConfig
14361
+ );
14362
+ }
14267
14363
  },
14268
14364
  generateBundle(_, bundle) {
14269
14365
  let config;
@@ -14284,7 +14380,7 @@ function cloudflare2(pluginConfig = {}) {
14284
14380
  if (isEntryWorker && hasClientBuild) {
14285
14381
  const workerOutputDirectory = this.environment.config.build.outDir;
14286
14382
  const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
14287
- assert9(
14383
+ assert10(
14288
14384
  clientOutputDirectory,
14289
14385
  "Unexpected error: client output directory is undefined"
14290
14386
  );
@@ -14348,78 +14444,100 @@ function cloudflare2(pluginConfig = {}) {
14348
14444
  }
14349
14445
  },
14350
14446
  hotUpdate(options) {
14351
- if (
14352
- // Vite normalizes `options.file` so we use `path.resolve` for Windows compatibility
14353
- resolvedPluginConfig.configPaths.has(path9.resolve(options.file)) || hasAssetsConfigChanged(
14354
- resolvedPluginConfig,
14355
- resolvedViteConfig,
14356
- options.file
14357
- )
14358
- ) {
14447
+ const changedFilePath = path9.resolve(options.file);
14448
+ if (resolvedPluginConfig.configPaths.has(changedFilePath) || hasDotDevDotVarsFileChanged(resolvedPluginConfig, changedFilePath) || hasAssetsConfigChanged(
14449
+ resolvedPluginConfig,
14450
+ resolvedViteConfig,
14451
+ changedFilePath
14452
+ )) {
14359
14453
  options.server.restart();
14360
14454
  return [];
14361
14455
  }
14362
14456
  },
14363
14457
  async configureServer(viteDevServer) {
14364
- assert9(
14365
- viteDevServer.httpServer,
14366
- "Unexpected error: No Vite HTTP server"
14458
+ const inputInspectorPort = await getInputInspectorPortOption(
14459
+ pluginConfig,
14460
+ viteDevServer
14367
14461
  );
14368
- if (miniflare) {
14369
- await miniflare.setOptions(
14370
- getDevMiniflareOptions(resolvedPluginConfig, viteDevServer)
14462
+ if (!miniflare) {
14463
+ miniflare = new Miniflare(
14464
+ getDevMiniflareOptions(
14465
+ resolvedPluginConfig,
14466
+ viteDevServer,
14467
+ inputInspectorPort
14468
+ )
14371
14469
  );
14372
14470
  } else {
14373
- miniflare = new Miniflare(
14374
- getDevMiniflareOptions(resolvedPluginConfig, viteDevServer)
14471
+ await miniflare.setOptions(
14472
+ getDevMiniflareOptions(
14473
+ resolvedPluginConfig,
14474
+ viteDevServer,
14475
+ inputInspectorPort
14476
+ )
14375
14477
  );
14376
14478
  }
14377
14479
  await initRunners(resolvedPluginConfig, viteDevServer, miniflare);
14378
- const middleware = createMiddleware(
14379
- async ({ request }) => {
14380
- assert9(miniflare, `Miniflare not defined`);
14480
+ if (viteDevServer.httpServer) {
14481
+ handleWebSocket(viteDevServer.httpServer, async () => {
14482
+ assert10(miniflare, `Miniflare not defined`);
14381
14483
  const routerWorker = await getRouterWorker(miniflare);
14382
- return routerWorker.fetch(toMiniflareRequest(request), {
14383
- redirect: "manual"
14384
- });
14385
- },
14386
- { alwaysCallNext: false }
14387
- );
14388
- handleWebSocket(viteDevServer.httpServer, async () => {
14389
- assert9(miniflare, `Miniflare not defined`);
14390
- const routerWorker = await getRouterWorker(miniflare);
14391
- return routerWorker.fetch;
14392
- });
14484
+ return routerWorker.fetch;
14485
+ });
14486
+ }
14393
14487
  return () => {
14394
- viteDevServer.middlewares.use((req, res, next) => {
14395
- middleware(req, res, next);
14488
+ viteDevServer.middlewares.use(async (req, res, next) => {
14489
+ try {
14490
+ assert10(miniflare, `Miniflare not defined`);
14491
+ const routerWorker = await getRouterWorker(miniflare);
14492
+ const request = createRequest(req, res);
14493
+ const response = await routerWorker.fetch(
14494
+ toMiniflareRequest(request),
14495
+ {
14496
+ redirect: "manual"
14497
+ }
14498
+ );
14499
+ if (req.httpVersionMajor === 2) {
14500
+ response.headers.delete("transfer-encoding");
14501
+ }
14502
+ await sendResponse(res, response);
14503
+ } catch (error) {
14504
+ next(error);
14505
+ }
14396
14506
  });
14397
14507
  };
14398
14508
  },
14399
- configurePreviewServer(vitePreviewServer) {
14509
+ async configurePreviewServer(vitePreviewServer) {
14400
14510
  const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
14511
+ const inputInspectorPort = await getInputInspectorPortOption(
14512
+ pluginConfig,
14513
+ vitePreviewServer
14514
+ );
14401
14515
  const miniflare2 = new Miniflare(
14402
14516
  getPreviewMiniflareOptions(
14403
14517
  vitePreviewServer,
14404
14518
  workerConfigs,
14405
14519
  pluginConfig.persistState ?? true,
14406
- pluginConfig.inspectorPort
14520
+ inputInspectorPort
14407
14521
  )
14408
14522
  );
14409
- const middleware = createMiddleware(
14410
- ({ request }) => {
14411
- return miniflare2.dispatchFetch(toMiniflareRequest(request), {
14412
- redirect: "manual"
14413
- });
14414
- },
14415
- { alwaysCallNext: false }
14416
- );
14417
14523
  handleWebSocket(
14418
14524
  vitePreviewServer.httpServer,
14419
14525
  () => miniflare2.dispatchFetch
14420
14526
  );
14421
- vitePreviewServer.middlewares.use((req, res, next) => {
14422
- middleware(req, res, next);
14527
+ vitePreviewServer.middlewares.use(async (req, res, next) => {
14528
+ try {
14529
+ const request = createRequest(req, res);
14530
+ const response = await miniflare2.dispatchFetch(
14531
+ toMiniflareRequest(request),
14532
+ { redirect: "manual" }
14533
+ );
14534
+ if (req.httpVersionMajor === 2) {
14535
+ response.headers.delete("transfer-encoding");
14536
+ }
14537
+ await sendResponse(res, response);
14538
+ } catch (error) {
14539
+ next(error);
14540
+ }
14423
14541
  });
14424
14542
  }
14425
14543
  },
@@ -14482,7 +14600,7 @@ function cloudflare2(pluginConfig = {}) {
14482
14600
  for (const match of matches) {
14483
14601
  magicString ??= new MagicString(code);
14484
14602
  const [full, _, modulePath] = match;
14485
- assert9(
14603
+ assert10(
14486
14604
  modulePath,
14487
14605
  `Unexpected error: module path not found in reference ${full}.`
14488
14606
  );
@@ -14528,17 +14646,22 @@ function cloudflare2(pluginConfig = {}) {
14528
14646
  configEnvironment(name) {
14529
14647
  if (isNodeCompat(getWorkerConfig2(name))) {
14530
14648
  return {
14649
+ build: {
14650
+ rollupOptions: {
14651
+ plugins: [
14652
+ replace({
14653
+ "process.env.NODE_ENV": JSON.stringify(
14654
+ process.env.NODE_ENV ?? "production"
14655
+ ),
14656
+ preventAssignment: true
14657
+ })
14658
+ ]
14659
+ }
14660
+ },
14531
14661
  resolve: {
14532
14662
  builtins: [...nodeCompatExternals]
14533
14663
  },
14534
14664
  optimizeDeps: {
14535
- // This is a list of dependency entry-points that should be pre-bundled.
14536
- // In this case we provide a list of all the possible polyfills so that they are pre-bundled,
14537
- // ready ahead the first request to the dev server.
14538
- // Without this the dependency optimizer will try to bundle them on-the-fly in the middle of the first request,
14539
- // which can potentially cause problems if it leads to previous pre-bundling to become stale and needing to be reloaded.
14540
- // TODO: work out how to re-enable pre-bundling of these
14541
- // include: [...getNodeCompatEntries()],
14542
14665
  // This is a list of module specifiers that the dependency optimizer should not follow when doing import analysis.
14543
14666
  // In this case we provide a list of all the Node.js modules, both those built-in to workerd and those that will be polyfilled.
14544
14667
  // Obviously we don't want/need the optimizer to try to process modules that are built-in;
@@ -14562,7 +14685,7 @@ function cloudflare2(pluginConfig = {}) {
14562
14685
  return this.resolve(source, importer, options);
14563
14686
  }
14564
14687
  if (this.environment.mode === "dev") {
14565
- assert9(
14688
+ assert10(
14566
14689
  this.environment.depsOptimizer,
14567
14690
  "depsOptimizer is required in dev mode"
14568
14691
  );
@@ -14576,11 +14699,54 @@ function cloudflare2(pluginConfig = {}) {
14576
14699
  },
14577
14700
  async transform(code, id) {
14578
14701
  const workerConfig = getWorkerConfig2(this.environment.name);
14579
- assert9(workerConfig, "Expected a worker config");
14702
+ if (!workerConfig) {
14703
+ return;
14704
+ }
14580
14705
  const resolvedId = await this.resolve(workerConfig.main);
14581
14706
  if (id === resolvedId?.id) {
14582
14707
  return injectGlobalCode(id, code);
14583
14708
  }
14709
+ },
14710
+ async configureServer(viteDevServer) {
14711
+ await Promise.all(
14712
+ Object.values(viteDevServer.environments).flatMap(
14713
+ async (environment) => {
14714
+ const workerConfig = getWorkerConfig2(environment.name);
14715
+ if (isNodeCompat(workerConfig)) {
14716
+ await environment.depsOptimizer?.init();
14717
+ return Array.from(nodeCompatEntries).map((entry) => {
14718
+ const result = resolveNodeJSImport(entry);
14719
+ if (result) {
14720
+ const registration = environment.depsOptimizer?.registerMissingImport(
14721
+ result.unresolved,
14722
+ result.resolved
14723
+ );
14724
+ return registration?.processing;
14725
+ }
14726
+ });
14727
+ }
14728
+ }
14729
+ )
14730
+ );
14731
+ }
14732
+ },
14733
+ // Plugin that handles Node.js Async Local Storage (ALS) compatibility support for Vite Environments that are hosted in Cloudflare Workers.
14734
+ {
14735
+ name: "vite-plugin-cloudflare:nodejs-als",
14736
+ apply(_config, env2) {
14737
+ return !env2.isPreview;
14738
+ },
14739
+ configEnvironment(name, config) {
14740
+ if (isNodeAls(getWorkerConfig2(name))) {
14741
+ return {
14742
+ resolve: {
14743
+ builtins: ["async_hooks", "node:async_hooks"]
14744
+ },
14745
+ optimizeDeps: {
14746
+ exclude: ["async_hooks", "node:async_hooks"]
14747
+ }
14748
+ };
14749
+ }
14584
14750
  }
14585
14751
  },
14586
14752
  // Plugin that provides an __debug path for debugging the Cloudflare Workers.
@@ -14590,39 +14756,35 @@ function cloudflare2(pluginConfig = {}) {
14590
14756
  // the preview middleware here can take precedence
14591
14757
  enforce: "pre",
14592
14758
  configureServer(viteDevServer) {
14593
- if (resolvedPluginConfig.type === "workers" && resolvedPluginConfig.inspectorPort !== false) {
14759
+ if (resolvedPluginConfig.type === "workers" && pluginConfig.inspectorPort !== false) {
14594
14760
  addDebugToVitePrintUrls(viteDevServer);
14595
14761
  }
14596
14762
  const workerNames = resolvedPluginConfig.type === "assets-only" ? [] : Object.values(resolvedPluginConfig.workers).map(
14597
14763
  (worker) => worker.name
14598
14764
  );
14599
- viteDevServer.middlewares.use((req, res, next) => {
14600
- if (req.url === debuggingPath && resolvedPluginConfig.inspectorPort !== false) {
14601
- const html = getDebugPathHtml(
14602
- workerNames,
14603
- resolvedPluginConfig.inspectorPort
14604
- );
14765
+ viteDevServer.middlewares.use(async (req, res, next) => {
14766
+ const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
14767
+ if (req.url === debuggingPath && resolvedInspectorPort) {
14768
+ const html = getDebugPathHtml(workerNames, resolvedInspectorPort);
14605
14769
  res.setHeader("Content-Type", "text/html");
14606
14770
  return res.end(html);
14607
14771
  }
14608
14772
  next();
14609
14773
  });
14610
14774
  },
14611
- configurePreviewServer(vitePreviewServer) {
14775
+ async configurePreviewServer(vitePreviewServer) {
14612
14776
  const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
14613
14777
  if (workerConfigs.length >= 1 && pluginConfig.inspectorPort !== false) {
14614
14778
  addDebugToVitePrintUrls(vitePreviewServer);
14615
14779
  }
14616
14780
  const workerNames = workerConfigs.map((worker) => {
14617
- assert9(worker.name, "Expected the Worker to have a name");
14781
+ assert10(worker.name, "Expected the Worker to have a name");
14618
14782
  return worker.name;
14619
14783
  });
14620
- vitePreviewServer.middlewares.use((req, res, next) => {
14621
- if (req.url === debuggingPath && pluginConfig.inspectorPort !== false) {
14622
- const html = getDebugPathHtml(
14623
- workerNames,
14624
- pluginConfig.inspectorPort ?? DEFAULT_INSPECTOR_PORT
14625
- );
14784
+ vitePreviewServer.middlewares.use(async (req, res, next) => {
14785
+ const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
14786
+ if (req.url === debuggingPath && resolvedInspectorPort) {
14787
+ const html = getDebugPathHtml(workerNames, resolvedInspectorPort);
14626
14788
  res.setHeader("Content-Type", "text/html");
14627
14789
  return res.end(html);
14628
14790
  }
@@ -14636,6 +14798,9 @@ function cloudflare2(pluginConfig = {}) {
14636
14798
  apply(_config, env2) {
14637
14799
  return !env2.isPreview;
14638
14800
  },
14801
+ // We must ensure that the `resolveId` hook runs before the built-in ones.
14802
+ // Otherwise we never see the Node.js built-in imports since they get handled by default Vite behavior.
14803
+ enforce: "pre",
14639
14804
  configEnvironment(environmentName) {
14640
14805
  const workerConfig = getWorkerConfig2(environmentName);
14641
14806
  if (workerConfig && !isNodeCompat(workerConfig)) {
@@ -14649,21 +14814,14 @@ function cloudflare2(pluginConfig = {}) {
14649
14814
  build.onResolve(
14650
14815
  { filter: NODEJS_MODULES_RE },
14651
14816
  ({ path: path10, importer }) => {
14817
+ if (isNodeAls(workerConfig) && isNodeAlsModule(path10)) {
14818
+ return;
14819
+ }
14652
14820
  const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
14653
- assert9(
14654
- nodeJsCompatWarnings,
14655
- `expected nodeJsCompatWarnings to be defined for Worker "${workerConfig.name}"`
14656
- );
14657
- nodeJsCompatWarnings.registerImport(path10, importer);
14821
+ nodeJsCompatWarnings?.registerImport(path10, importer);
14658
14822
  return { path: path10, external: true };
14659
14823
  }
14660
14824
  );
14661
- build.onEnd(() => {
14662
- const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
14663
- if (nodeJsCompatWarnings) {
14664
- nodeJsCompatWarnings.renderWarnings();
14665
- }
14666
- });
14667
14825
  }
14668
14826
  }
14669
14827
  ]
@@ -14672,47 +14830,28 @@ function cloudflare2(pluginConfig = {}) {
14672
14830
  };
14673
14831
  }
14674
14832
  },
14675
- configureServer(viteDevServer) {
14676
- for (const environment of Object.values(viteDevServer.environments)) {
14677
- const workerConfig = getWorkerConfig2(environment.name);
14833
+ configResolved(resolvedViteConfig2) {
14834
+ for (const environmentName of Object.keys(
14835
+ resolvedViteConfig2.environments
14836
+ )) {
14837
+ const workerConfig = getWorkerConfig2(environmentName);
14678
14838
  if (workerConfig && !isNodeCompat(workerConfig)) {
14679
14839
  nodeJsCompatWarningsMap.set(
14680
14840
  workerConfig,
14681
- new NodeJsCompatWarnings(environment)
14841
+ new NodeJsCompatWarnings(environmentName, resolvedViteConfig2)
14682
14842
  );
14683
14843
  }
14684
14844
  }
14685
14845
  },
14686
- buildStart() {
14687
- const workerConfig = getWorkerConfig2(this.environment.name);
14688
- if (workerConfig && !isNodeCompat(workerConfig)) {
14689
- nodeJsCompatWarningsMap.set(
14690
- workerConfig,
14691
- new NodeJsCompatWarnings(this.environment)
14692
- );
14693
- }
14694
- },
14695
- buildEnd() {
14696
- const workerConfig = getWorkerConfig2(this.environment.name);
14697
- if (workerConfig && !isNodeCompat(workerConfig)) {
14698
- const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
14699
- assert9(
14700
- nodeJsCompatWarnings,
14701
- `expected nodeJsCompatWarnings to be defined for Worker "${workerConfig.name}"`
14702
- );
14703
- nodeJsCompatWarnings.renderWarnings();
14704
- }
14705
- },
14706
- // We must ensure that the `resolveId` hook runs before the built-in ones otherwise we
14707
- // never see the Node.js built-in imports since they get handled by default Vite behavior.
14708
- enforce: "pre",
14709
14846
  async resolveId(source, importer) {
14710
14847
  const workerConfig = getWorkerConfig2(this.environment.name);
14711
14848
  if (workerConfig && !isNodeCompat(workerConfig)) {
14849
+ if (isNodeAls(workerConfig) && isNodeAlsModule(source)) {
14850
+ return;
14851
+ }
14712
14852
  const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
14713
14853
  if (nodejsBuiltins.has(source)) {
14714
14854
  nodeJsCompatWarnings?.registerImport(source, importer);
14715
- nodeJsCompatWarnings?.renderWarningsOnIdle();
14716
14855
  return {
14717
14856
  id: source,
14718
14857
  external: true
@@ -14723,10 +14862,35 @@ function cloudflare2(pluginConfig = {}) {
14723
14862
  }
14724
14863
  ];
14725
14864
  function getWorkerConfig2(environmentName) {
14726
- assert9(resolvedPluginConfig, "Expected resolvedPluginConfig to be defined");
14865
+ assert10(resolvedPluginConfig, "Expected resolvedPluginConfig to be defined");
14727
14866
  return resolvedPluginConfig.type !== "assets-only" ? resolvedPluginConfig.workers[environmentName] : void 0;
14728
14867
  }
14729
14868
  }
14869
+ async function getInputInspectorPortOption(pluginConfig, viteServer) {
14870
+ if (pluginConfig.inspectorPort === void 0 || pluginConfig.inspectorPort === 0) {
14871
+ const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
14872
+ if (resolvedInspectorPort !== null) {
14873
+ return resolvedInspectorPort;
14874
+ }
14875
+ }
14876
+ const inputInspectorPort = pluginConfig.inspectorPort ?? await getFirstAvailablePort(DEFAULT_INSPECTOR_PORT);
14877
+ if (pluginConfig.inspectorPort === void 0 && inputInspectorPort !== DEFAULT_INSPECTOR_PORT) {
14878
+ viteServer.config.logger.warn(
14879
+ colors3.dim(
14880
+ `Default inspector port ${DEFAULT_INSPECTOR_PORT} not available, using ${inputInspectorPort} instead
14881
+ `
14882
+ )
14883
+ );
14884
+ }
14885
+ return inputInspectorPort;
14886
+ }
14887
+ async function getResolvedInspectorPort(pluginConfig) {
14888
+ if (miniflare && pluginConfig.inspectorPort !== false) {
14889
+ const miniflareInspectorUrl = await miniflare.getInspectorURL();
14890
+ return Number.parseInt(miniflareInspectorUrl.port);
14891
+ }
14892
+ return null;
14893
+ }
14730
14894
  function getDotDevDotVarsContent(configPath, cloudflareEnv) {
14731
14895
  const configDir = path9.dirname(configPath);
14732
14896
  const defaultDotDevDotVarsPath = `${configDir}/.dev.vars`;
@@ -14738,6 +14902,19 @@ function getDotDevDotVarsContent(configPath, cloudflareEnv) {
14738
14902
  }
14739
14903
  return null;
14740
14904
  }
14905
+ function hasDotDevDotVarsFileChanged(resolvedPluginConfig, changedFilePath) {
14906
+ return [...resolvedPluginConfig.configPaths].some((configPath) => {
14907
+ const dotDevDotVars = path9.join(path9.dirname(configPath), ".dev.vars");
14908
+ if (dotDevDotVars === changedFilePath) {
14909
+ return true;
14910
+ }
14911
+ if (resolvedPluginConfig.cloudflareEnv) {
14912
+ const dotDevDotVarsForEnv = `${dotDevDotVars}.${resolvedPluginConfig.cloudflareEnv}`;
14913
+ return dotDevDotVarsForEnv === changedFilePath;
14914
+ }
14915
+ return false;
14916
+ });
14917
+ }
14741
14918
  export {
14742
14919
  cloudflare2 as cloudflare
14743
14920
  };