@cloudflare/vite-plugin 0.0.0-a45c58bcf → 0.0.0-a4909cbe5

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
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  import assert9 from "node:assert";
3
3
  import * as fs5 from "node:fs";
4
+ import { builtinModules as builtinModules2 } from "node:module";
4
5
  import * as path7 from "node:path";
5
6
  import { createMiddleware } from "@hattip/adapter-node";
6
7
 
@@ -1074,7 +1075,6 @@ import * as vite6 from "vite";
1074
1075
 
1075
1076
  // src/cloudflare-environment.ts
1076
1077
  import assert from "node:assert";
1077
- import { builtinModules } from "node:module";
1078
1078
  import * as vite2 from "vite";
1079
1079
 
1080
1080
  // src/constants.ts
@@ -1207,7 +1207,9 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
1207
1207
  // dependencies as not external
1208
1208
  noExternal: true,
1209
1209
  // We want to use `workerd` package exports if available (e.g. for postgres).
1210
- conditions: [...defaultConditions, "development|production"]
1210
+ conditions: [...defaultConditions, "development|production"],
1211
+ // The Cloudflare ones are proper builtins in the environment
1212
+ builtins: [...cloudflareBuiltInModules]
1211
1213
  },
1212
1214
  dev: {
1213
1215
  createEnvironment(name2, config) {
@@ -1229,19 +1231,14 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
1229
1231
  // so the input value here serves both as the build input as well as the starting point for
1230
1232
  // dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set
1231
1233
  // optimizeDeps.entries in the dev config)
1232
- input: workerConfig.main,
1233
- external: [...cloudflareBuiltInModules]
1234
+ input: workerConfig.main
1234
1235
  }
1235
1236
  },
1236
1237
  optimizeDeps: {
1237
1238
  // Note: ssr pre-bundling is opt-in and we need to enable it by setting `noDiscovery` to false
1238
1239
  noDiscovery: false,
1239
1240
  entries: workerConfig.main,
1240
- exclude: [
1241
- ...cloudflareBuiltInModules,
1242
- // we have to exclude all node modules to work in dev-mode not just the unenv externals...
1243
- ...builtinModules.concat(builtinModules.map((m) => `node:${m}`))
1244
- ],
1241
+ exclude: [...cloudflareBuiltInModules],
1245
1242
  esbuildOptions: {
1246
1243
  platform: "neutral",
1247
1244
  conditions: [...defaultConditions, "development"],
@@ -1367,7 +1364,12 @@ import * as fs2 from "node:fs";
1367
1364
  import * as fsp from "node:fs/promises";
1368
1365
  import * as path3 from "node:path";
1369
1366
  import { fileURLToPath } from "node:url";
1370
- import { Log, LogLevel, Response as MiniflareResponse } from "miniflare";
1367
+ import {
1368
+ kCurrentWorker,
1369
+ Log,
1370
+ LogLevel,
1371
+ Response as MiniflareResponse
1372
+ } from "miniflare";
1371
1373
  import "vite";
1372
1374
  import {
1373
1375
  unstable_getMiniflareWorkerOptions,
@@ -1401,11 +1403,10 @@ function getWorkerToWorkerEntrypointNamesMap(workers) {
1401
1403
  );
1402
1404
  for (const worker of workers) {
1403
1405
  for (const value of Object.values(worker.serviceBindings ?? {})) {
1404
- if (typeof value === "object" && "name" in value && typeof value.name === "string" && value.entrypoint !== void 0 && value.entrypoint !== "default") {
1405
- const entrypointNames = workerToWorkerEntrypointNamesMap.get(
1406
- value.name
1407
- );
1408
- assert4(entrypointNames, missingWorkerErrorMessage(value.name));
1406
+ if (typeof value === "object" && "name" in value && value.entrypoint !== void 0 && value.entrypoint !== "default") {
1407
+ const targetWorkerName = value.name === kCurrentWorker ? worker.name : value.name;
1408
+ const entrypointNames = workerToWorkerEntrypointNamesMap.get(targetWorkerName);
1409
+ assert4(entrypointNames, missingWorkerErrorMessage(targetWorkerName));
1409
1410
  entrypointNames.add(value.entrypoint);
1410
1411
  }
1411
1412
  }
@@ -1585,20 +1586,17 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1585
1586
  );
1586
1587
  const [moduleId] = invokePayloadData.data;
1587
1588
  const moduleRE = new RegExp(MODULE_PATTERN);
1588
- if (moduleRE.test(moduleId)) {
1589
+ const shouldExternalize = (
1590
+ // Worker modules (CompiledWasm, Text, Data)
1591
+ moduleRE.test(moduleId)
1592
+ );
1593
+ if (shouldExternalize) {
1589
1594
  const result2 = {
1590
1595
  externalize: moduleId,
1591
1596
  type: "module"
1592
1597
  };
1593
1598
  return MiniflareResponse.json({ result: result2 });
1594
1599
  }
1595
- if (moduleId.startsWith("cloudflare:")) {
1596
- const result2 = {
1597
- externalize: moduleId,
1598
- type: "builtin"
1599
- };
1600
- return MiniflareResponse.json({ result: result2 });
1601
- }
1602
1600
  const devEnvironment = viteDevServer.environments[environmentName];
1603
1601
  const result = await devEnvironment.hot.handleInvoke(payload);
1604
1602
  return MiniflareResponse.json(result);
@@ -7310,7 +7308,7 @@ Parser.acorn = {
7310
7308
  };
7311
7309
 
7312
7310
  // ../../node_modules/.pnpm/mlly@1.7.4/node_modules/mlly/dist/index.mjs
7313
- import { builtinModules as builtinModules2, createRequire } from "node:module";
7311
+ import { builtinModules, createRequire } from "node:module";
7314
7312
  import fs3, { realpathSync, statSync as statSync2, promises } from "node:fs";
7315
7313
 
7316
7314
  // ../../node_modules/.pnpm/ufo@1.5.4/node_modules/ufo/dist/index.mjs
@@ -7373,7 +7371,7 @@ import process$1 from "node:process";
7373
7371
  import path4, { dirname as dirname3 } from "node:path";
7374
7372
  import v8 from "node:v8";
7375
7373
  import { format as format2, inspect } from "node:util";
7376
- var BUILTIN_MODULES = new Set(builtinModules2);
7374
+ var BUILTIN_MODULES = new Set(builtinModules);
7377
7375
  function normalizeSlash(path8) {
7378
7376
  return path8.replace(/\\/g, "/");
7379
7377
  }
@@ -8487,7 +8485,7 @@ function parsePackageName(specifier, base) {
8487
8485
  return { packageName, packageSubpath, isScoped };
8488
8486
  }
8489
8487
  function packageResolve(specifier, base, conditions) {
8490
- if (builtinModules2.includes(specifier)) {
8488
+ if (builtinModules.includes(specifier)) {
8491
8489
  return new URL$1("node:" + specifier);
8492
8490
  }
8493
8491
  const { packageName, packageSubpath, isScoped } = parsePackageName(
@@ -8574,7 +8572,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
8574
8572
  try {
8575
8573
  resolved = new URL$1(specifier);
8576
8574
  } catch (error_) {
8577
- if (isRemote && !builtinModules2.includes(specifier)) {
8575
+ if (isRemote && !builtinModules.includes(specifier)) {
8578
8576
  const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
8579
8577
  error.cause = error_;
8580
8578
  throw error;
@@ -8719,7 +8717,8 @@ var { env } = defineEnv({
8719
8717
  nodeCompat: true,
8720
8718
  presets: [cloudflare]
8721
8719
  });
8722
- var CLOUDFLARE_VIRTUAL_PREFIX = "\0__CLOUDFLARE_NODEJS_COMPAT__";
8720
+ var nodeCompatExternals = new Set(env.external);
8721
+ var nodeCompatEntries = getNodeCompatEntries();
8723
8722
  function isNodeCompat(workerConfig) {
8724
8723
  if (workerConfig === void 0) {
8725
8724
  return false;
@@ -8747,12 +8746,17 @@ function injectGlobalCode(id, code) {
8747
8746
  const injectedCode = Object.entries(env.inject).map(([globalName, globalInject]) => {
8748
8747
  if (typeof globalInject === "string") {
8749
8748
  const moduleSpecifier2 = globalInject;
8750
- return `import var_${globalName} from "${CLOUDFLARE_VIRTUAL_PREFIX}${moduleSpecifier2}";
8749
+ return `import var_${globalName} from "${moduleSpecifier2}";
8751
8750
  globalThis.${globalName} = var_${globalName};
8752
8751
  `;
8753
8752
  }
8754
8753
  const [moduleSpecifier, exportName] = globalInject;
8755
- return `import var_${globalName} from "${CLOUDFLARE_VIRTUAL_PREFIX}${moduleSpecifier}";
8754
+ assert6(
8755
+ moduleSpecifier !== void 0,
8756
+ "Expected moduleSpecifier to be defined"
8757
+ );
8758
+ assert6(exportName !== void 0, "Expected exportName to be defined");
8759
+ return `import var_${globalName} from "${moduleSpecifier}";
8756
8760
  globalThis.${globalName} = var_${globalName}.${exportName};
8757
8761
  `;
8758
8762
  }).join("\n");
@@ -8763,37 +8767,36 @@ globalThis.${globalName} = var_${globalName}.${exportName};
8763
8767
  map: modified.generateMap({ hires: "boundary", source: id })
8764
8768
  };
8765
8769
  }
8766
- function getNodeCompatAliases() {
8767
- const aliases = {};
8768
- Object.keys(env.alias).forEach((key) => {
8769
- if (!env.external.includes(key)) {
8770
- aliases[key] = CLOUDFLARE_VIRTUAL_PREFIX + key;
8771
- }
8772
- });
8773
- return aliases;
8774
- }
8775
- function getNodeCompatExternals() {
8776
- return env.external;
8777
- }
8778
- function maybeStripNodeJsVirtualPrefix(source) {
8779
- return source.startsWith(CLOUDFLARE_VIRTUAL_PREFIX) ? source.slice(CLOUDFLARE_VIRTUAL_PREFIX.length) : void 0;
8780
- }
8781
8770
  function resolveNodeJSImport(source) {
8782
8771
  const alias = env.alias[source];
8783
8772
  if (alias) {
8784
- assert6(
8785
- !env.external.includes(alias),
8786
- `Unexpected unenv alias to external module: ${source} -> ${alias}`
8787
- );
8788
- source = alias;
8773
+ return {
8774
+ unresolved: alias,
8775
+ resolved: resolvePathSync(alias, { url: import.meta.url })
8776
+ };
8789
8777
  }
8790
- const resolved = resolvePathSync(source, {
8791
- url: import.meta.url
8792
- });
8793
- return {
8794
- unresolved: source,
8795
- resolved
8796
- };
8778
+ if (nodeCompatEntries.has(source)) {
8779
+ return {
8780
+ unresolved: source,
8781
+ resolved: resolvePathSync(source, { url: import.meta.url })
8782
+ };
8783
+ }
8784
+ }
8785
+ function getNodeCompatEntries() {
8786
+ const entries = new Set(Object.values(env.alias));
8787
+ for (const globalInject of Object.values(env.inject)) {
8788
+ if (typeof globalInject === "string") {
8789
+ entries.add(globalInject);
8790
+ } else {
8791
+ assert6(
8792
+ globalInject[0] !== void 0,
8793
+ "Expected first element of globalInject to be defined"
8794
+ );
8795
+ entries.add(globalInject[0]);
8796
+ }
8797
+ }
8798
+ nodeCompatExternals.forEach((external) => entries.delete(external));
8799
+ return entries;
8797
8800
  }
8798
8801
 
8799
8802
  // src/plugin-config.ts
@@ -9106,9 +9109,9 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
9106
9109
  }
9107
9110
 
9108
9111
  // src/websockets.ts
9109
- import ws from "ws";
9112
+ import { WebSocketServer } from "ws";
9110
9113
  function handleWebSocket(httpServer, fetcher, logger) {
9111
- const nodeWebSocket = new ws.Server({ noServer: true });
9114
+ const nodeWebSocket = new WebSocketServer({ noServer: true });
9112
9115
  httpServer.on(
9113
9116
  "upgrade",
9114
9117
  async (request, socket, head) => {
@@ -9217,7 +9220,7 @@ function cloudflare2(pluginConfig = {}) {
9217
9220
  }
9218
9221
  } : void 0,
9219
9222
  builder: {
9220
- async buildApp(builder) {
9223
+ buildApp: userConfig.builder?.buildApp ?? (async (builder) => {
9221
9224
  const clientEnvironment = builder.environments.client;
9222
9225
  const defaultHtmlPath = path7.resolve(
9223
9226
  builder.config.root,
@@ -9243,7 +9246,7 @@ function cloudflare2(pluginConfig = {}) {
9243
9246
  )
9244
9247
  );
9245
9248
  }
9246
- }
9249
+ })
9247
9250
  }
9248
9251
  };
9249
9252
  },
@@ -9462,49 +9465,61 @@ function cloudflare2(pluginConfig = {}) {
9462
9465
  apply(_config, env2) {
9463
9466
  return !env2.isPreview;
9464
9467
  },
9465
- config() {
9466
- return {
9467
- resolve: {
9468
- alias: getNodeCompatAliases()
9469
- }
9470
- };
9471
- },
9472
- configEnvironment(environmentName) {
9473
- const workerConfig = getWorkerConfig2(environmentName);
9474
- if (isNodeCompat(workerConfig)) {
9468
+ configEnvironment(name2) {
9469
+ if (isNodeCompat(getWorkerConfig2(name2))) {
9475
9470
  return {
9476
- build: {
9477
- rollupOptions: {
9478
- external: getNodeCompatExternals()
9479
- }
9471
+ resolve: {
9472
+ builtins: [...nodeCompatExternals]
9473
+ },
9474
+ optimizeDeps: {
9475
+ // This is a list of dependency entry-points that should be pre-bundled.
9476
+ // In this case we provide a list of all the possible polyfills so that they are pre-bundled,
9477
+ // ready ahead the first request to the dev server.
9478
+ // Without this the dependency optimizer will try to bundle them on-the-fly in the middle of the first request,
9479
+ // which can potentially cause problems if it leads to previous pre-bundling to become stale and needing to be reloaded.
9480
+ // TODO: work out how to re-enable pre-bundling of these
9481
+ // include: [...getNodeCompatEntries()],
9482
+ // This is a list of module specifiers that the dependency optimizer should not follow when doing import analysis.
9483
+ // 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.
9484
+ // Obviously we don't want/need the optimizer to try to process modules that are built-in;
9485
+ // But also we want to avoid following the ones that are polyfilled since the dependency-optimizer import analyzer does not
9486
+ // resolve these imports using our `resolveId()` hook causing the optimization step to fail.
9487
+ exclude: [
9488
+ ...builtinModules2,
9489
+ ...builtinModules2.map((m) => `node:${m}`)
9490
+ ]
9480
9491
  }
9481
9492
  };
9482
9493
  }
9483
9494
  },
9495
+ applyToEnvironment(environment) {
9496
+ return isNodeCompat(getWorkerConfig2(environment.name));
9497
+ },
9498
+ // We need the resolver from this plugin to run before built-in ones, otherwise Vite's built-in
9499
+ // resolver will try to externalize the Node.js module imports (e.g. `perf_hooks` and `node:tty`)
9500
+ // rather than allowing the resolve hook here to alias then to polyfills.
9501
+ enforce: "pre",
9484
9502
  async resolveId(source, importer, options) {
9485
- const strippedSource = maybeStripNodeJsVirtualPrefix(source);
9486
- if (!strippedSource) {
9487
- return;
9488
- }
9489
- const workerConfig = getWorkerConfig2(this.environment.name);
9490
- if (!isNodeCompat(workerConfig)) {
9491
- return this.resolve(strippedSource, importer, options);
9503
+ const result = resolveNodeJSImport(source);
9504
+ if (!result) {
9505
+ return this.resolve(source, importer, options);
9492
9506
  }
9493
- const { unresolved, resolved } = resolveNodeJSImport(strippedSource);
9494
- if (this.environment.mode === "dev" && this.environment.depsOptimizer) {
9495
- const optimized = this.environment.depsOptimizer.registerMissingImport(
9496
- unresolved,
9497
- resolved
9498
- ).id;
9499
- return this.resolve(optimized, importer, options);
9507
+ if (this.environment.mode === "dev") {
9508
+ assert9(
9509
+ this.environment.depsOptimizer,
9510
+ "depsOptimizer is required in dev mode"
9511
+ );
9512
+ const { id } = this.environment.depsOptimizer.registerMissingImport(
9513
+ result.unresolved,
9514
+ result.resolved
9515
+ );
9516
+ return this.resolve(id, importer, options);
9500
9517
  }
9501
- return this.resolve(resolved, importer, options);
9518
+ return this.resolve(result.resolved, importer, options);
9502
9519
  },
9503
9520
  async transform(code, id) {
9504
9521
  const workerConfig = getWorkerConfig2(this.environment.name);
9505
- if (!isNodeCompat(workerConfig)) {
9506
- return;
9507
- }
9522
+ assert9(workerConfig, "Expected a worker config");
9508
9523
  const resolvedId = await this.resolve(workerConfig.main);
9509
9524
  if (id === resolvedId?.id) {
9510
9525
  return injectGlobalCode(id, code);
@@ -25,7 +25,7 @@ function stripInternalEnv(internalEnv) {
25
25
  return userEnv;
26
26
  }
27
27
 
28
- // ../../node_modules/.pnpm/vite@6.1.0_@types+node@18.19.74_jiti@2.4.2/node_modules/vite/dist/node/module-runner.js
28
+ // ../../node_modules/.pnpm/vite@6.1.0_@types+node@18.19.76_jiti@2.4.2/node_modules/vite/dist/node/module-runner.js
29
29
  var VALID_ID_PREFIX = "/@id/";
30
30
  var NULL_BYTE_PLACEHOLDER = "__x00__";
31
31
  var SOURCEMAPPING_URL = "sourceMa";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/vite-plugin",
3
- "version": "0.0.0-a45c58bcf",
3
+ "version": "0.0.0-a4909cbe5",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -36,24 +36,24 @@
36
36
  "@cloudflare/unenv-preset": "1.1.1",
37
37
  "@hattip/adapter-node": "^0.0.49",
38
38
  "unenv": "2.0.0-rc.1",
39
- "ws": "^8.18.0",
40
- "miniflare": "0.0.0-a45c58bcf"
39
+ "ws": "8.18.0",
40
+ "miniflare": "0.0.0-a4909cbe5"
41
41
  },
42
42
  "devDependencies": {
43
- "@cloudflare/workers-types": "^4.20250204.0",
43
+ "@cloudflare/workers-types": "^4.20250214.0",
44
44
  "@types/node": "^22.10.1",
45
45
  "@types/ws": "^8.5.13",
46
46
  "magic-string": "^0.30.12",
47
47
  "mlly": "^1.7.4",
48
48
  "tsup": "8.3.0",
49
49
  "typescript": "^5.7.2",
50
- "undici": "^5.28.4",
50
+ "undici": "^5.28.5",
51
51
  "vite": "^6.1.0",
52
- "vitest": "~2.1.9",
52
+ "vitest": "~3.0.5",
53
53
  "@cloudflare/mock-npm-registry": "0.0.0",
54
+ "@cloudflare/workers-shared": "0.0.0-a4909cbe5",
54
55
  "@cloudflare/workers-tsconfig": "0.0.0",
55
- "wrangler": "0.0.0-a45c58bcf",
56
- "@cloudflare/workers-shared": "0.0.0-a45c58bcf"
56
+ "wrangler": "0.0.0-a4909cbe5"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "vite": "^6.1.0",