@cloudflare/vite-plugin 0.0.0-ab4dcff48 → 0.0.0-aba0e9cad

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
@@ -1238,6 +1238,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
1238
1238
  // Note: ssr pre-bundling is opt-in and we need to enable it by setting `noDiscovery` to false
1239
1239
  noDiscovery: false,
1240
1240
  entries: workerConfig.main,
1241
+ exclude: [...cloudflareBuiltInModules],
1241
1242
  esbuildOptions: {
1242
1243
  platform: "neutral",
1243
1244
  conditions: [...defaultConditions, "development"],
@@ -1363,7 +1364,13 @@ import * as fs2 from "node:fs";
1363
1364
  import * as fsp from "node:fs/promises";
1364
1365
  import * as path3 from "node:path";
1365
1366
  import { fileURLToPath } from "node:url";
1366
- import { Log, LogLevel, Response as MiniflareResponse } from "miniflare";
1367
+ import {
1368
+ kCurrentWorker,
1369
+ Log,
1370
+ LogLevel,
1371
+ Response as MiniflareResponse
1372
+ } from "miniflare";
1373
+ import { globSync } from "tinyglobby";
1367
1374
  import "vite";
1368
1375
  import {
1369
1376
  unstable_getMiniflareWorkerOptions,
@@ -1397,11 +1404,10 @@ function getWorkerToWorkerEntrypointNamesMap(workers) {
1397
1404
  );
1398
1405
  for (const worker of workers) {
1399
1406
  for (const value of Object.values(worker.serviceBindings ?? {})) {
1400
- if (typeof value === "object" && "name" in value && typeof value.name === "string" && value.entrypoint !== void 0 && value.entrypoint !== "default") {
1401
- const entrypointNames = workerToWorkerEntrypointNamesMap.get(
1402
- value.name
1403
- );
1404
- assert4(entrypointNames, missingWorkerErrorMessage(value.name));
1407
+ if (typeof value === "object" && "name" in value && value.entrypoint !== void 0 && value.entrypoint !== "default") {
1408
+ const targetWorkerName = value.name === kCurrentWorker ? worker.name : value.name;
1409
+ const entrypointNames = workerToWorkerEntrypointNamesMap.get(targetWorkerName);
1410
+ assert4(entrypointNames, missingWorkerErrorMessage(targetWorkerName));
1405
1411
  entrypointNames.add(value.entrypoint);
1406
1412
  }
1407
1413
  }
@@ -1546,7 +1552,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1546
1552
  }
1547
1553
  }
1548
1554
  ];
1549
- const userWorkers = resolvedPluginConfig.type === "workers" ? Object.entries(resolvedPluginConfig.workers).map(
1555
+ const workersFromConfig = resolvedPluginConfig.type === "workers" ? Object.entries(resolvedPluginConfig.workers).map(
1550
1556
  ([environmentName, workerConfig]) => {
1551
1557
  const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
1552
1558
  {
@@ -1555,51 +1561,58 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1555
1561
  },
1556
1562
  resolvedPluginConfig.cloudflareEnv
1557
1563
  );
1564
+ const { externalWorkers: externalWorkers2 } = miniflareWorkerOptions;
1558
1565
  const { ratelimits, ...workerOptions } = miniflareWorkerOptions.workerOptions;
1559
1566
  return {
1560
- ...workerOptions,
1561
- // We have to add the name again because `unstable_getMiniflareWorkerOptions` sets it to `undefined`
1562
- name: workerConfig.name,
1563
- modulesRoot: miniflareModulesRoot,
1564
- unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
1565
- bindings: {
1566
- ...workerOptions.bindings,
1567
- __VITE_ROOT__: resolvedViteConfig.root,
1568
- __VITE_ENTRY_PATH__: workerConfig.main
1569
- },
1570
- serviceBindings: {
1571
- ...workerOptions.serviceBindings,
1572
- ...environmentName === resolvedPluginConfig.entryWorkerEnvironmentName && workerConfig.assets?.binding ? {
1573
- [workerConfig.assets.binding]: ASSET_WORKER_NAME
1574
- } : {},
1575
- __VITE_INVOKE_MODULE__: async (request) => {
1576
- const payload = await request.json();
1577
- const invokePayloadData = payload.data;
1578
- assert4(
1579
- invokePayloadData.name === "fetchModule",
1580
- `Invalid invoke event: ${invokePayloadData.name}`
1581
- );
1582
- const [moduleId] = invokePayloadData.data;
1583
- const moduleRE = new RegExp(MODULE_PATTERN);
1584
- const shouldExternalize = (
1585
- // Worker modules (CompiledWasm, Text, Data)
1586
- moduleRE.test(moduleId)
1587
- );
1588
- if (shouldExternalize) {
1589
- const result2 = {
1590
- externalize: moduleId,
1591
- type: "module"
1592
- };
1593
- return MiniflareResponse.json({ result: result2 });
1567
+ externalWorkers: externalWorkers2,
1568
+ worker: {
1569
+ ...workerOptions,
1570
+ name: workerOptions.name ?? workerConfig.name,
1571
+ modulesRoot: miniflareModulesRoot,
1572
+ unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
1573
+ bindings: {
1574
+ ...workerOptions.bindings,
1575
+ __VITE_ROOT__: resolvedViteConfig.root,
1576
+ __VITE_ENTRY_PATH__: workerConfig.main
1577
+ },
1578
+ serviceBindings: {
1579
+ ...workerOptions.serviceBindings,
1580
+ ...environmentName === resolvedPluginConfig.entryWorkerEnvironmentName && workerConfig.assets?.binding ? {
1581
+ [workerConfig.assets.binding]: ASSET_WORKER_NAME
1582
+ } : {},
1583
+ __VITE_INVOKE_MODULE__: async (request) => {
1584
+ const payload = await request.json();
1585
+ const invokePayloadData = payload.data;
1586
+ assert4(
1587
+ invokePayloadData.name === "fetchModule",
1588
+ `Invalid invoke event: ${invokePayloadData.name}`
1589
+ );
1590
+ const [moduleId] = invokePayloadData.data;
1591
+ const moduleRE = new RegExp(MODULE_PATTERN);
1592
+ const shouldExternalize = (
1593
+ // Worker modules (CompiledWasm, Text, Data)
1594
+ moduleRE.test(moduleId)
1595
+ );
1596
+ if (shouldExternalize) {
1597
+ const result2 = {
1598
+ externalize: moduleId,
1599
+ type: "module"
1600
+ };
1601
+ return MiniflareResponse.json({ result: result2 });
1602
+ }
1603
+ const devEnvironment = viteDevServer.environments[environmentName];
1604
+ const result = await devEnvironment.hot.handleInvoke(payload);
1605
+ return MiniflareResponse.json(result);
1594
1606
  }
1595
- const devEnvironment = viteDevServer.environments[environmentName];
1596
- const result = await devEnvironment.hot.handleInvoke(payload);
1597
- return MiniflareResponse.json(result);
1598
1607
  }
1599
1608
  }
1600
1609
  };
1601
1610
  }
1602
1611
  ) : [];
1612
+ const userWorkers = workersFromConfig.map((options) => options.worker);
1613
+ const externalWorkers = workersFromConfig.flatMap(
1614
+ (options) => options.externalWorkers
1615
+ );
1603
1616
  const workerToWorkerEntrypointNamesMap = getWorkerToWorkerEntrypointNamesMap(userWorkers);
1604
1617
  const workerToDurableObjectClassNamesMap = getWorkerToDurableObjectClassNamesMap(userWorkers);
1605
1618
  const workerToWorkflowEntrypointClassNamesMap = getWorkerToWorkflowEntrypointClassNamesMap(userWorkers);
@@ -1619,6 +1632,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1619
1632
  ),
1620
1633
  workers: [
1621
1634
  ...assetWorkers,
1635
+ ...externalWorkers,
1622
1636
  ...userWorkers.map((workerOptions) => {
1623
1637
  const wrappers = [
1624
1638
  `import { createWorkerEntrypointWrapper, createDurableObjectWrapper, createWorkflowEntrypointWrapper } from '${RUNNER_PATH}';`,
@@ -1651,7 +1665,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1651
1665
  const workflowEntrypointClassNames = workerToWorkflowEntrypointClassNamesMap.get(workerOptions.name);
1652
1666
  assert4(
1653
1667
  workflowEntrypointClassNames,
1654
- `WorkflowEntrypoint class names not found for worker ${workerOptions.name}`
1668
+ `WorkflowEntrypoint class names not found for worker: ${workerOptions.name}`
1655
1669
  );
1656
1670
  for (const className of [...workflowEntrypointClassNames].sort()) {
1657
1671
  wrappers.push(
@@ -1687,17 +1701,19 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1687
1701
  );
1688
1702
  const moduleRE = new RegExp(MODULE_PATTERN);
1689
1703
  const match = moduleRE.exec(rawSpecifier);
1690
- assert4(match, `Unexpected error: no match for module ${rawSpecifier}.`);
1704
+ assert4(match, `Unexpected error: no match for module: ${rawSpecifier}.`);
1691
1705
  const [full, moduleType, modulePath] = match;
1692
1706
  assert4(
1693
1707
  modulePath,
1694
- `Unexpected error: module path not found in reference ${full}.`
1708
+ `Unexpected error: module path not found in reference: ${full}.`
1695
1709
  );
1696
1710
  let source;
1697
1711
  try {
1698
1712
  source = fs2.readFileSync(modulePath);
1699
1713
  } catch (error) {
1700
- throw new Error(`Import ${modulePath} not found. Does the file exist?`);
1714
+ throw new Error(
1715
+ `Import "${modulePath}" not found. Does the file exist?`
1716
+ );
1701
1717
  }
1702
1718
  return MiniflareResponse.json({
1703
1719
  // Cap'n Proto expects byte arrays for `:Data` typed fields from JSON
@@ -1706,22 +1722,44 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
1706
1722
  }
1707
1723
  };
1708
1724
  }
1725
+ function getPreviewModules(main, modulesRules) {
1726
+ assert4(modulesRules, `Unexpected error: 'modulesRules' is undefined`);
1727
+ const rootPath = path3.dirname(main);
1728
+ const entryPath = path3.basename(main);
1729
+ return {
1730
+ rootPath,
1731
+ modules: [
1732
+ {
1733
+ type: "ESModule",
1734
+ path: entryPath
1735
+ },
1736
+ ...modulesRules.flatMap(
1737
+ ({ type, include }) => globSync(include, { cwd: rootPath, ignore: entryPath }).map((path8) => ({
1738
+ type,
1739
+ path: path8
1740
+ }))
1741
+ )
1742
+ ]
1743
+ };
1744
+ }
1709
1745
  function getPreviewMiniflareOptions(vitePreviewServer, persistState) {
1710
1746
  const resolvedViteConfig = vitePreviewServer.config;
1711
1747
  const configPaths = getWorkerConfigPaths(resolvedViteConfig.root);
1712
1748
  const workerConfigs = configPaths.map(
1713
1749
  (configPath) => unstable_readConfig({ config: configPath })
1714
1750
  );
1715
- const workers = workerConfigs.map((config) => {
1751
+ const workers = workerConfigs.flatMap((config) => {
1716
1752
  const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(config);
1717
- const { ratelimits, ...workerOptions } = miniflareWorkerOptions.workerOptions;
1718
- return {
1719
- ...workerOptions,
1720
- // We have to add the name again because `unstable_getMiniflareWorkerOptions` sets it to `undefined`
1721
- name: config.name,
1722
- modules: true,
1723
- ...miniflareWorkerOptions.main ? { scriptPath: miniflareWorkerOptions.main } : { script: "" }
1724
- };
1753
+ const { externalWorkers } = miniflareWorkerOptions;
1754
+ const { ratelimits, modulesRules, ...workerOptions } = miniflareWorkerOptions.workerOptions;
1755
+ return [
1756
+ {
1757
+ ...workerOptions,
1758
+ name: workerOptions.name ?? config.name,
1759
+ ...miniflareWorkerOptions.main ? getPreviewModules(miniflareWorkerOptions.main, modulesRules) : { modules: true, script: "" }
1760
+ },
1761
+ ...externalWorkers
1762
+ ];
1725
1763
  });
1726
1764
  const logger = new ViteMiniflareLogger(resolvedViteConfig);
1727
1765
  return {
@@ -7363,7 +7401,7 @@ var isAbsolute = function(p) {
7363
7401
  import { fileURLToPath as fileURLToPath$1, URL as URL$1, pathToFileURL as pathToFileURL$1 } from "node:url";
7364
7402
  import assert5 from "node:assert";
7365
7403
  import process$1 from "node:process";
7366
- import path4, { dirname as dirname3 } from "node:path";
7404
+ import path4, { dirname as dirname4 } from "node:path";
7367
7405
  import v8 from "node:v8";
7368
7406
  import { format as format2, inspect } from "node:util";
7369
7407
  var BUILTIN_MODULES = new Set(builtinModules);
@@ -8712,6 +8750,8 @@ var { env } = defineEnv({
8712
8750
  nodeCompat: true,
8713
8751
  presets: [cloudflare]
8714
8752
  });
8753
+ var nodeCompatExternals = new Set(env.external);
8754
+ var nodeCompatEntries = getNodeCompatEntries();
8715
8755
  function isNodeCompat(workerConfig) {
8716
8756
  if (workerConfig === void 0) {
8717
8757
  return false;
@@ -8735,24 +8775,6 @@ function isNodeCompat(workerConfig) {
8735
8775
  }
8736
8776
  return false;
8737
8777
  }
8738
- function getNodeCompatEntries() {
8739
- const entries = new Set(Object.values(env.alias));
8740
- for (const globalInject of Object.values(env.inject)) {
8741
- if (typeof globalInject === "string") {
8742
- entries.add(globalInject);
8743
- } else {
8744
- assert6(
8745
- globalInject[0] !== void 0,
8746
- "Expected first element of globalInject to be defined"
8747
- );
8748
- entries.add(globalInject[0]);
8749
- }
8750
- }
8751
- for (const external of env.external) {
8752
- entries.delete(external);
8753
- }
8754
- return entries;
8755
- }
8756
8778
  function injectGlobalCode(id, code) {
8757
8779
  const injectedCode = Object.entries(env.inject).map(([globalName, globalInject]) => {
8758
8780
  if (typeof globalInject === "string") {
@@ -8778,9 +8800,6 @@ globalThis.${globalName} = var_${globalName}.${exportName};
8778
8800
  map: modified.generateMap({ hires: "boundary", source: id })
8779
8801
  };
8780
8802
  }
8781
- function getNodeCompatExternals() {
8782
- return env.external;
8783
- }
8784
8803
  function resolveNodeJSImport(source) {
8785
8804
  const alias = env.alias[source];
8786
8805
  if (alias) {
@@ -8789,6 +8808,28 @@ function resolveNodeJSImport(source) {
8789
8808
  resolved: resolvePathSync(alias, { url: import.meta.url })
8790
8809
  };
8791
8810
  }
8811
+ if (nodeCompatEntries.has(source)) {
8812
+ return {
8813
+ unresolved: source,
8814
+ resolved: resolvePathSync(source, { url: import.meta.url })
8815
+ };
8816
+ }
8817
+ }
8818
+ function getNodeCompatEntries() {
8819
+ const entries = new Set(Object.values(env.alias));
8820
+ for (const globalInject of Object.values(env.inject)) {
8821
+ if (typeof globalInject === "string") {
8822
+ entries.add(globalInject);
8823
+ } else {
8824
+ assert6(
8825
+ globalInject[0] !== void 0,
8826
+ "Expected first element of globalInject to be defined"
8827
+ );
8828
+ entries.add(globalInject[0]);
8829
+ }
8830
+ }
8831
+ nodeCompatExternals.forEach((external) => entries.delete(external));
8832
+ return entries;
8792
8833
  }
8793
8834
 
8794
8835
  // src/plugin-config.ts
@@ -9101,9 +9142,9 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
9101
9142
  }
9102
9143
 
9103
9144
  // src/websockets.ts
9104
- import ws from "ws";
9145
+ import { WebSocketServer } from "ws";
9105
9146
  function handleWebSocket(httpServer, fetcher, logger) {
9106
- const nodeWebSocket = new ws.Server({ noServer: true });
9147
+ const nodeWebSocket = new WebSocketServer({ noServer: true });
9107
9148
  httpServer.on(
9108
9149
  "upgrade",
9109
9150
  async (request, socket, head) => {
@@ -9212,7 +9253,7 @@ function cloudflare2(pluginConfig = {}) {
9212
9253
  }
9213
9254
  } : void 0,
9214
9255
  builder: {
9215
- async buildApp(builder) {
9256
+ buildApp: userConfig.builder?.buildApp ?? (async (builder) => {
9216
9257
  const clientEnvironment = builder.environments.client;
9217
9258
  const defaultHtmlPath = path7.resolve(
9218
9259
  builder.config.root,
@@ -9238,7 +9279,7 @@ function cloudflare2(pluginConfig = {}) {
9238
9279
  )
9239
9280
  );
9240
9281
  }
9241
- }
9282
+ })
9242
9283
  }
9243
9284
  };
9244
9285
  },
@@ -9299,7 +9340,7 @@ function cloudflare2(pluginConfig = {}) {
9299
9340
  return;
9300
9341
  }
9301
9342
  config.no_bundle = true;
9302
- config.rules = [{ type: "ESModule", globs: ["**/*.js"] }];
9343
+ config.rules = [{ type: "ESModule", globs: ["**/*.js", "**/*.mjs"] }];
9303
9344
  if (config.unsafe && Object.keys(config.unsafe).length === 0) {
9304
9345
  config.unsafe = void 0;
9305
9346
  }
@@ -9461,7 +9502,7 @@ function cloudflare2(pluginConfig = {}) {
9461
9502
  if (isNodeCompat(getWorkerConfig2(name2))) {
9462
9503
  return {
9463
9504
  resolve: {
9464
- builtins: getNodeCompatExternals()
9505
+ builtins: [...nodeCompatExternals]
9465
9506
  },
9466
9507
  optimizeDeps: {
9467
9508
  // This is a list of dependency entry-points that should be pre-bundled.
@@ -9469,7 +9510,8 @@ function cloudflare2(pluginConfig = {}) {
9469
9510
  // ready ahead the first request to the dev server.
9470
9511
  // Without this the dependency optimizer will try to bundle them on-the-fly in the middle of the first request,
9471
9512
  // which can potentially cause problems if it leads to previous pre-bundling to become stale and needing to be reloaded.
9472
- include: [...getNodeCompatEntries()],
9513
+ // TODO: work out how to re-enable pre-bundling of these
9514
+ // include: [...getNodeCompatEntries()],
9473
9515
  // This is a list of module specifiers that the dependency optimizer should not follow when doing import analysis.
9474
9516
  // 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.
9475
9517
  // Obviously we don't want/need the optimizer to try to process modules that are built-in;
@@ -9500,7 +9542,11 @@ function cloudflare2(pluginConfig = {}) {
9500
9542
  this.environment.depsOptimizer,
9501
9543
  "depsOptimizer is required in dev mode"
9502
9544
  );
9503
- return this.resolve(result.unresolved, importer, options);
9545
+ const { id } = this.environment.depsOptimizer.registerMissingImport(
9546
+ result.unresolved,
9547
+ result.resolved
9548
+ );
9549
+ return this.resolve(id, importer, options);
9504
9550
  }
9505
9551
  return this.resolve(result.resolved, importer, options);
9506
9552
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/vite-plugin",
3
- "version": "0.0.0-ab4dcff48",
3
+ "version": "0.0.0-aba0e9cad",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -35,12 +35,14 @@
35
35
  "dependencies": {
36
36
  "@cloudflare/unenv-preset": "1.1.1",
37
37
  "@hattip/adapter-node": "^0.0.49",
38
+ "tinyglobby": "^0.2.12",
38
39
  "unenv": "2.0.0-rc.1",
39
- "ws": "^8.18.0",
40
- "miniflare": "0.0.0-ab4dcff48"
40
+ "ws": "8.18.0",
41
+ "miniflare": "0.0.0-aba0e9cad",
42
+ "wrangler": "0.0.0-aba0e9cad"
41
43
  },
42
44
  "devDependencies": {
43
- "@cloudflare/workers-types": "^4.20250214.0",
45
+ "@cloudflare/workers-types": "^4.20250224.0",
44
46
  "@types/node": "^22.10.1",
45
47
  "@types/ws": "^8.5.13",
46
48
  "magic-string": "^0.30.12",
@@ -51,13 +53,12 @@
51
53
  "vite": "^6.1.0",
52
54
  "vitest": "~3.0.5",
53
55
  "@cloudflare/mock-npm-registry": "0.0.0",
54
- "@cloudflare/workers-shared": "0.0.0-ab4dcff48",
55
- "@cloudflare/workers-tsconfig": "0.0.0",
56
- "wrangler": "0.0.0-ab4dcff48"
56
+ "@cloudflare/workers-shared": "0.0.0-aba0e9cad",
57
+ "@cloudflare/workers-tsconfig": "0.0.0"
57
58
  },
58
59
  "peerDependencies": {
59
60
  "vite": "^6.1.0",
60
- "wrangler": "^3.101.0"
61
+ "wrangler": "0.0.0-aba0e9cad"
61
62
  },
62
63
  "publishConfig": {
63
64
  "access": "public"
@@ -70,7 +71,7 @@
70
71
  "check:type": "tsc --build",
71
72
  "dev": "tsup --watch",
72
73
  "test": "vitest run",
73
- "test:ci": "pnpm test && pnpm test:e2e",
74
+ "test:ci": "pnpm test",
74
75
  "test:e2e": "vitest run -c e2e/vitest.config.ts",
75
76
  "test:watch": "vitest"
76
77
  }