@cloudflare/vite-plugin 0.0.0-027698c05 → 0.0.0-060a4db04

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,5 +1,5 @@
1
1
  // src/index.ts
2
- import assert7 from "node:assert";
2
+ import assert8 from "node:assert";
3
3
  import * as fs4 from "node:fs";
4
4
  import * as path6 from "node:path";
5
5
  import { createMiddleware } from "@hattip/adapter-node";
@@ -1077,98 +1077,6 @@ import assert from "node:assert";
1077
1077
  import { builtinModules } from "node:module";
1078
1078
  import * as vite2 from "vite";
1079
1079
 
1080
- // src/node-js-compat.ts
1081
- import { createRequire } from "node:module";
1082
- import { getNodeCompat } from "miniflare";
1083
- import * as unenv from "unenv";
1084
- var require2 = createRequire(import.meta.url);
1085
- var preset = unenv.env(unenv.nodeless, unenv.cloudflare);
1086
- var CLOUDFLARE_VIRTUAL_PREFIX = "\0cloudflare-";
1087
- function isNodeCompat({
1088
- compatibility_date,
1089
- compatibility_flags
1090
- }) {
1091
- const nodeCompatMode = getNodeCompat(
1092
- compatibility_date,
1093
- compatibility_flags ?? []
1094
- ).mode;
1095
- if (nodeCompatMode === "v2") {
1096
- return true;
1097
- }
1098
- if (nodeCompatMode === "legacy") {
1099
- throw new Error(
1100
- "Unsupported Node.js compat mode (legacy). Remove the `node_compat` setting and add the `nodejs_compat` flag instead."
1101
- );
1102
- }
1103
- if (nodeCompatMode === "v1") {
1104
- throw new Error(
1105
- `Unsupported Node.js compat mode (v1). Only the v2 mode is supported, either change your compat date to "2024-09-23" or later, or set the "nodejs_compat_v2" compatibility flag`
1106
- );
1107
- }
1108
- return false;
1109
- }
1110
- function injectGlobalCode(id, code, workerConfig) {
1111
- if (!isNodeCompat(workerConfig)) {
1112
- return;
1113
- }
1114
- const injectedCode = Object.entries(preset.inject).map(([globalName, globalInject]) => {
1115
- if (typeof globalInject === "string") {
1116
- const moduleSpecifier2 = globalInject;
1117
- return `import var_${globalName} from "${moduleSpecifier2}";
1118
- globalThis.${globalName} = var_${globalName};
1119
- `;
1120
- }
1121
- const [moduleSpecifier, exportName] = globalInject;
1122
- return `import var_${globalName} from "${moduleSpecifier}";
1123
- globalThis.${globalName} = var_${globalName}.${exportName};
1124
- `;
1125
- }).join("\n");
1126
- const modified = new MagicString(code);
1127
- modified.prepend(injectedCode);
1128
- return {
1129
- code: modified.toString(),
1130
- map: modified.generateMap({ hires: "boundary", source: id })
1131
- };
1132
- }
1133
- function getNodeCompatAliases() {
1134
- const aliases = {};
1135
- Object.keys(preset.alias).forEach((key) => {
1136
- if (!preset.external.includes(key)) {
1137
- aliases[key] = CLOUDFLARE_VIRTUAL_PREFIX + key;
1138
- }
1139
- });
1140
- return aliases;
1141
- }
1142
- function resolveNodeCompatId(environment, workerConfig, id) {
1143
- const aliased = resolveNodeAliases(id, workerConfig) ?? id;
1144
- if (aliased.startsWith("unenv/")) {
1145
- const resolvedDep = require2.resolve(aliased).replace(/\.cjs$/, ".mjs");
1146
- if (environment.mode === "dev" && environment.depsOptimizer) {
1147
- const dep = environment.depsOptimizer.registerMissingImport(
1148
- aliased,
1149
- resolvedDep
1150
- );
1151
- return dep.id;
1152
- } else {
1153
- return resolvedDep;
1154
- }
1155
- }
1156
- }
1157
- function getNodeCompatExternals() {
1158
- return preset.external;
1159
- }
1160
- function resolveNodeAliases(source, workerConfig) {
1161
- if (!source.startsWith(CLOUDFLARE_VIRTUAL_PREFIX) || !isNodeCompat(workerConfig)) {
1162
- return;
1163
- }
1164
- const from = source.slice(CLOUDFLARE_VIRTUAL_PREFIX.length);
1165
- const alias = preset.alias[from];
1166
- if (alias && preset.external.includes(alias)) {
1167
- throw new Error(`Alias to external: ${source} -> ${alias}`);
1168
- }
1169
- return alias;
1170
- }
1171
-
1172
1080
  // src/constants.ts
1173
1081
  var ROUTER_WORKER_NAME = "__router-worker__";
1174
1082
  var ASSET_WORKER_NAME = "__asset-worker__";
@@ -1314,6 +1222,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
1314
1222
  // We need to enable `emitAssets` in order to support additional modules defined by `rules`
1315
1223
  emitAssets: true,
1316
1224
  outDir: getOutputDirectory(userConfig, environmentName),
1225
+ copyPublicDir: false,
1317
1226
  ssr: true,
1318
1227
  rollupOptions: {
1319
1228
  // Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input
@@ -1321,7 +1230,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
1321
1230
  // dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set
1322
1231
  // optimizeDeps.entries in the dev config)
1323
1232
  input: workerConfig.main,
1324
- external: [...cloudflareBuiltInModules, ...getNodeCompatExternals()]
1233
+ external: [...cloudflareBuiltInModules]
1325
1234
  }
1326
1235
  },
1327
1236
  optimizeDeps: {
@@ -1867,13 +1776,94 @@ function miniflareLogLevelFromViteLogLevel(level = "info") {
1867
1776
  }
1868
1777
  }
1869
1778
 
1779
+ // src/node-js-compat.ts
1780
+ import assert5 from "node:assert";
1781
+ import { cloudflare } from "@cloudflare/unenv-preset";
1782
+ import { getNodeCompat } from "miniflare";
1783
+ import { defineEnv } from "unenv";
1784
+ var { env } = defineEnv({
1785
+ nodeCompat: true,
1786
+ presets: [cloudflare]
1787
+ });
1788
+ var CLOUDFLARE_VIRTUAL_PREFIX = "\0__CLOUDFLARE_NODEJS_COMPAT__";
1789
+ function isNodeCompat(workerConfig) {
1790
+ if (workerConfig === void 0) {
1791
+ return false;
1792
+ }
1793
+ const nodeCompatMode = getNodeCompat(
1794
+ workerConfig.compatibility_date,
1795
+ workerConfig.compatibility_flags ?? []
1796
+ ).mode;
1797
+ if (nodeCompatMode === "v2") {
1798
+ return true;
1799
+ }
1800
+ if (nodeCompatMode === "legacy") {
1801
+ throw new Error(
1802
+ "Unsupported Node.js compat mode (legacy). Remove the `node_compat` setting and add the `nodejs_compat` flag instead."
1803
+ );
1804
+ }
1805
+ if (nodeCompatMode === "v1") {
1806
+ throw new Error(
1807
+ `Unsupported Node.js compat mode (v1). Only the v2 mode is supported, either change your compat date to "2024-09-23" or later, or set the "nodejs_compat_v2" compatibility flag`
1808
+ );
1809
+ }
1810
+ return false;
1811
+ }
1812
+ function injectGlobalCode(id, code) {
1813
+ const injectedCode = Object.entries(env.inject).map(([globalName, globalInject]) => {
1814
+ if (typeof globalInject === "string") {
1815
+ const moduleSpecifier2 = globalInject;
1816
+ return `import var_${globalName} from "${moduleSpecifier2}";
1817
+ globalThis.${globalName} = var_${globalName};
1818
+ `;
1819
+ }
1820
+ const [moduleSpecifier, exportName] = globalInject;
1821
+ return `import var_${globalName} from "${moduleSpecifier}";
1822
+ globalThis.${globalName} = var_${globalName}.${exportName};
1823
+ `;
1824
+ }).join("\n");
1825
+ const modified = new MagicString(code);
1826
+ modified.prepend(injectedCode);
1827
+ return {
1828
+ code: modified.toString(),
1829
+ map: modified.generateMap({ hires: "boundary", source: id })
1830
+ };
1831
+ }
1832
+ function getNodeCompatAliases() {
1833
+ const aliases = {};
1834
+ Object.keys(env.alias).forEach((key) => {
1835
+ if (!env.external.includes(key)) {
1836
+ aliases[key] = CLOUDFLARE_VIRTUAL_PREFIX + key;
1837
+ }
1838
+ });
1839
+ return aliases;
1840
+ }
1841
+ function getNodeCompatExternals() {
1842
+ return env.external;
1843
+ }
1844
+ function maybeStripNodeJsVirtualPrefix(source) {
1845
+ return source.startsWith(CLOUDFLARE_VIRTUAL_PREFIX) ? source.slice(CLOUDFLARE_VIRTUAL_PREFIX.length) : void 0;
1846
+ }
1847
+ function dealiasVirtualNodeJSImport(source) {
1848
+ const alias = env.alias[source];
1849
+ assert5(
1850
+ alias,
1851
+ `Expected "${source}" to have a Node.js compat alias, but none was found`
1852
+ );
1853
+ assert5(
1854
+ !env.external.includes(alias),
1855
+ `Unexpected unenv alias to external module: ${source} -> ${alias}`
1856
+ );
1857
+ return alias;
1858
+ }
1859
+
1870
1860
  // src/plugin-config.ts
1871
- import assert6 from "node:assert";
1861
+ import assert7 from "node:assert";
1872
1862
  import * as path5 from "node:path";
1873
1863
  import * as vite5 from "vite";
1874
1864
 
1875
1865
  // src/workers-configs.ts
1876
- import assert5 from "node:assert";
1866
+ import assert6 from "node:assert";
1877
1867
  import * as fs3 from "node:fs";
1878
1868
  import * as path4 from "node:path";
1879
1869
  import { unstable_readConfig as unstable_readConfig2 } from "wrangler";
@@ -2049,17 +2039,17 @@ function getWorkerConfig(configPath, env2, opts) {
2049
2039
  }
2050
2040
  const { raw, config, nonApplicable } = readWorkerConfig(configPath, env2);
2051
2041
  opts?.visitedConfigPaths?.add(configPath);
2052
- assert5(
2042
+ assert6(
2053
2043
  config.topLevelName,
2054
2044
  missingFieldErrorMessage(`top-level 'name'`, configPath, env2)
2055
2045
  );
2056
- assert5(config.name, missingFieldErrorMessage(`'name'`, configPath, env2));
2057
- assert5(
2046
+ assert6(config.name, missingFieldErrorMessage(`'name'`, configPath, env2));
2047
+ assert6(
2058
2048
  config.compatibility_date,
2059
2049
  missingFieldErrorMessage(`'compatibility_date'`, configPath, env2)
2060
2050
  );
2061
2051
  if (opts?.isEntryWorker && !config.main) {
2062
- assert5(
2052
+ assert6(
2063
2053
  config.assets,
2064
2054
  missingFieldErrorMessage(`'main' or 'assets'`, configPath, env2)
2065
2055
  );
@@ -2076,7 +2066,7 @@ function getWorkerConfig(configPath, env2, opts) {
2076
2066
  nonApplicable
2077
2067
  };
2078
2068
  }
2079
- assert5(config.main, missingFieldErrorMessage(`'main'`, configPath, env2));
2069
+ assert6(config.main, missingFieldErrorMessage(`'main'`, configPath, env2));
2080
2070
  return {
2081
2071
  type: "worker",
2082
2072
  raw,
@@ -2114,7 +2104,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
2114
2104
  ""
2115
2105
  );
2116
2106
  const configPath = pluginConfig.configPath ? path5.resolve(root, pluginConfig.configPath) : findWranglerConfig(root);
2117
- assert6(
2107
+ assert7(
2118
2108
  configPath,
2119
2109
  `Config not found. Have you created a wrangler.json(c) or wrangler.toml file?`
2120
2110
  );
@@ -2149,7 +2139,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
2149
2139
  }
2150
2140
  );
2151
2141
  auxiliaryWorkersResolvedConfigs.push(workerResolvedConfig);
2152
- assert6(
2142
+ assert7(
2153
2143
  workerResolvedConfig.type === "worker",
2154
2144
  "Unexpected error: received AssetsOnlyResult with auxiliary workers."
2155
2145
  );
@@ -2266,9 +2256,6 @@ function cloudflare2(pluginConfig = {}) {
2266
2256
  }
2267
2257
  return {
2268
2258
  appType: "custom",
2269
- resolve: {
2270
- alias: getNodeCompatAliases()
2271
- },
2272
2259
  environments: resolvedPluginConfig.type === "workers" ? {
2273
2260
  ...Object.fromEntries(
2274
2261
  Object.entries(resolvedPluginConfig.workers).map(
@@ -2305,7 +2292,7 @@ function cloudflare2(pluginConfig = {}) {
2305
2292
  resolvedPluginConfig.workers
2306
2293
  ).map((environmentName) => {
2307
2294
  const environment = builder.environments[environmentName];
2308
- assert7(
2295
+ assert8(
2309
2296
  environment,
2310
2297
  `${environmentName} environment not found`
2311
2298
  );
@@ -2325,29 +2312,6 @@ function cloudflare2(pluginConfig = {}) {
2325
2312
  configResolved(config) {
2326
2313
  resolvedViteConfig = config;
2327
2314
  },
2328
- async resolveId(source) {
2329
- if (resolvedPluginConfig.type === "assets-only") {
2330
- return;
2331
- }
2332
- const workerConfig = resolvedPluginConfig.workers[this.environment.name];
2333
- if (!workerConfig) {
2334
- return;
2335
- }
2336
- return resolveNodeCompatId(this.environment, workerConfig, source);
2337
- },
2338
- async transform(code, id) {
2339
- if (resolvedPluginConfig.type === "assets-only") {
2340
- return;
2341
- }
2342
- const workerConfig = resolvedPluginConfig.workers[this.environment.name];
2343
- if (!workerConfig) {
2344
- return;
2345
- }
2346
- const resolvedId = await this.resolve(workerConfig.main);
2347
- if (id === resolvedId?.id) {
2348
- return injectGlobalCode(id, code, workerConfig);
2349
- }
2350
- },
2351
2315
  generateBundle(_, bundle) {
2352
2316
  let config;
2353
2317
  if (resolvedPluginConfig.type === "workers") {
@@ -2363,7 +2327,7 @@ function cloudflare2(pluginConfig = {}) {
2363
2327
  if (isEntryWorker && workerConfig.assets) {
2364
2328
  const workerOutputDirectory = this.environment.config.build.outDir;
2365
2329
  const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
2366
- assert7(
2330
+ assert8(
2367
2331
  clientOutputDirectory,
2368
2332
  "Unexpected error: client output directory is undefined"
2369
2333
  );
@@ -2424,7 +2388,7 @@ function cloudflare2(pluginConfig = {}) {
2424
2388
  }
2425
2389
  },
2426
2390
  async configureServer(viteDevServer) {
2427
- assert7(
2391
+ assert8(
2428
2392
  viteDevServer.httpServer,
2429
2393
  "Unexpected error: No Vite HTTP server"
2430
2394
  );
@@ -2483,19 +2447,14 @@ function cloudflare2(pluginConfig = {}) {
2483
2447
  // Otherwise the `vite:wasm-fallback` plugin prevents the `.wasm` extension being used for module imports.
2484
2448
  enforce: "pre",
2485
2449
  applyToEnvironment(environment) {
2486
- if (resolvedPluginConfig.type === "assets-only") {
2487
- return false;
2488
- }
2489
- return Object.keys(resolvedPluginConfig.workers).includes(
2490
- environment.name
2491
- );
2450
+ return getWorkerConfig2(environment.name) !== void 0;
2492
2451
  },
2493
2452
  async resolveId(source, importer) {
2494
2453
  if (!source.endsWith(".wasm")) {
2495
2454
  return;
2496
2455
  }
2497
2456
  const resolved = await this.resolve(source, importer);
2498
- assert7(
2457
+ assert8(
2499
2458
  resolved,
2500
2459
  `Unexpected error: could not resolve Wasm module ${source}`
2501
2460
  );
@@ -2511,7 +2470,7 @@ function cloudflare2(pluginConfig = {}) {
2511
2470
  while (match = moduleRE.exec(code)) {
2512
2471
  magicString ??= new MagicString(code);
2513
2472
  const [full, moduleType, modulePath] = match;
2514
- assert7(
2473
+ assert8(
2515
2474
  modulePath,
2516
2475
  `Unexpected error: module path not found in reference ${full}.`
2517
2476
  );
@@ -2547,8 +2506,74 @@ function cloudflare2(pluginConfig = {}) {
2547
2506
  };
2548
2507
  }
2549
2508
  }
2509
+ },
2510
+ // Plugin that can provide Node.js compatibility support for Vite Environments that are hosted in Cloudflare Workers.
2511
+ {
2512
+ name: "vite-plugin-cloudflare:nodejs-compat",
2513
+ apply(_config, env2) {
2514
+ return !env2.isPreview;
2515
+ },
2516
+ config() {
2517
+ return {
2518
+ resolve: {
2519
+ alias: getNodeCompatAliases()
2520
+ }
2521
+ };
2522
+ },
2523
+ configEnvironment(environmentName) {
2524
+ const workerConfig = getWorkerConfig2(environmentName);
2525
+ if (isNodeCompat(workerConfig)) {
2526
+ return {
2527
+ build: {
2528
+ rollupOptions: {
2529
+ external: getNodeCompatExternals()
2530
+ }
2531
+ }
2532
+ };
2533
+ }
2534
+ },
2535
+ async resolveId(source, importer, options) {
2536
+ const from = maybeStripNodeJsVirtualPrefix(source);
2537
+ if (!from) {
2538
+ return;
2539
+ }
2540
+ const workerConfig = getWorkerConfig2(this.environment.name);
2541
+ if (!isNodeCompat(workerConfig)) {
2542
+ return this.resolve(from, importer, options);
2543
+ }
2544
+ const unresolvedAlias = dealiasVirtualNodeJSImport(from);
2545
+ const resolvedAlias = await this.resolve(
2546
+ unresolvedAlias,
2547
+ import.meta.url
2548
+ );
2549
+ assert8(
2550
+ resolvedAlias,
2551
+ "Failed to resolve aliased nodejs import: " + unresolvedAlias
2552
+ );
2553
+ if (this.environment.mode === "dev" && this.environment.depsOptimizer) {
2554
+ this.environment.depsOptimizer.registerMissingImport(
2555
+ unresolvedAlias,
2556
+ resolvedAlias.id
2557
+ );
2558
+ }
2559
+ return resolvedAlias;
2560
+ },
2561
+ async transform(code, id) {
2562
+ const workerConfig = getWorkerConfig2(this.environment.name);
2563
+ if (!isNodeCompat(workerConfig)) {
2564
+ return;
2565
+ }
2566
+ const resolvedId = await this.resolve(workerConfig.main);
2567
+ if (id === resolvedId?.id) {
2568
+ return injectGlobalCode(id, code);
2569
+ }
2570
+ }
2550
2571
  }
2551
2572
  ];
2573
+ function getWorkerConfig2(environmentName) {
2574
+ assert8(resolvedPluginConfig, "Expected resolvedPluginConfig to be defined");
2575
+ return resolvedPluginConfig.type !== "assets-only" ? resolvedPluginConfig.workers[environmentName] : void 0;
2576
+ }
2552
2577
  }
2553
2578
  function getDotDevDotVarsContent(configPath, cloudflareEnv) {
2554
2579
  const configDir = path6.dirname(configPath);
@@ -25,7 +25,7 @@ function stripInternalEnv(internalEnv) {
25
25
  return userEnv;
26
26
  }
27
27
 
28
- // ../../node_modules/.pnpm/vite@6.0.7_@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.74_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-027698c05",
3
+ "version": "0.0.0-060a4db04",
4
4
  "description": "Cloudflare plugin for Vite",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -33,10 +33,11 @@
33
33
  "dist"
34
34
  ],
35
35
  "dependencies": {
36
+ "@cloudflare/unenv-preset": "1.1.1",
36
37
  "@hattip/adapter-node": "^0.0.49",
37
- "unenv": "npm:unenv-nightly@2.0.0-20241218-183400-5d6aec3",
38
+ "unenv": "2.0.0-rc.1",
38
39
  "ws": "^8.18.0",
39
- "miniflare": "0.0.0-027698c05"
40
+ "miniflare": "0.0.0-060a4db04"
40
41
  },
41
42
  "devDependencies": {
42
43
  "@cloudflare/workers-types": "^4.20250204.0",
@@ -45,13 +46,13 @@
45
46
  "magic-string": "^0.30.12",
46
47
  "tsup": "8.3.0",
47
48
  "typescript": "^5.7.2",
48
- "vite": "^6.0.7",
49
- "@cloudflare/workers-shared": "0.0.0-027698c05",
50
- "wrangler": "0.0.0-027698c05",
51
- "@cloudflare/workers-tsconfig": "0.0.0"
49
+ "vite": "^6.1.0",
50
+ "@cloudflare/workers-shared": "0.0.0-060a4db04",
51
+ "@cloudflare/workers-tsconfig": "0.0.0",
52
+ "wrangler": "0.0.0-060a4db04"
52
53
  },
53
54
  "peerDependencies": {
54
- "vite": "^6.0.7",
55
+ "vite": "^6.1.0",
55
56
  "wrangler": "^3.101.0"
56
57
  },
57
58
  "publishConfig": {