@cloudflare/vite-plugin 0.0.0-6b111231d → 0.0.0-6fe4a67d0
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/README.md +7 -0
- package/dist/asset-workers/asset-worker.js +829 -684
- package/dist/asset-workers/router-worker.js +697 -611
- package/dist/index.d.ts +1 -1
- package/dist/index.js +551 -349
- package/dist/runner-worker/index.js +15 -3
- package/package.json +12 -11
package/dist/index.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import
|
|
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";
|
|
6
|
-
import { Miniflare } from "miniflare";
|
|
7
|
-
import "vite";
|
|
8
|
-
|
|
9
|
-
// src/cloudflare-environment.ts
|
|
10
|
-
import assert from "node:assert";
|
|
11
|
-
import { builtinModules } from "node:module";
|
|
12
|
-
import * as vite2 from "vite";
|
|
13
|
-
|
|
14
|
-
// src/node-js-compat.ts
|
|
15
|
-
import { createRequire } from "node:module";
|
|
16
6
|
|
|
17
7
|
// ../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
|
|
18
8
|
var comma = ",".charCodeAt(0);
|
|
@@ -1078,100 +1068,25 @@ var MagicString = class _MagicString {
|
|
|
1078
1068
|
}
|
|
1079
1069
|
};
|
|
1080
1070
|
|
|
1081
|
-
// src/
|
|
1082
|
-
import {
|
|
1083
|
-
import * as
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
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
|
-
}
|
|
1071
|
+
// src/index.ts
|
|
1072
|
+
import { Miniflare } from "miniflare";
|
|
1073
|
+
import * as vite6 from "vite";
|
|
1074
|
+
|
|
1075
|
+
// src/cloudflare-environment.ts
|
|
1076
|
+
import assert from "node:assert";
|
|
1077
|
+
import { builtinModules } from "node:module";
|
|
1078
|
+
import * as vite2 from "vite";
|
|
1079
|
+
|
|
1080
|
+
// src/constants.ts
|
|
1081
|
+
var ROUTER_WORKER_NAME = "__router-worker__";
|
|
1082
|
+
var ASSET_WORKER_NAME = "__asset-worker__";
|
|
1083
|
+
var ASSET_WORKERS_COMPATIBILITY_DATE = "2024-10-04";
|
|
1084
|
+
var MODULE_TYPES = ["CompiledWasm"];
|
|
1171
1085
|
|
|
1172
1086
|
// src/shared.ts
|
|
1173
1087
|
var UNKNOWN_HOST = "http://localhost";
|
|
1174
1088
|
var INIT_PATH = "/__vite_plugin_cloudflare_init__";
|
|
1089
|
+
var MODULE_PATTERN = `__CLOUDFLARE_MODULE__(${MODULE_TYPES.join("|")})__(.*?)__`;
|
|
1175
1090
|
|
|
1176
1091
|
// src/utils.ts
|
|
1177
1092
|
import * as path from "node:path";
|
|
@@ -1268,7 +1183,10 @@ var CloudflareDevEnvironment = class extends vite2.DevEnvironment {
|
|
|
1268
1183
|
}
|
|
1269
1184
|
}
|
|
1270
1185
|
);
|
|
1271
|
-
assert(
|
|
1186
|
+
assert(
|
|
1187
|
+
response.ok,
|
|
1188
|
+
`Failed to initialize module runner, error: ${await response.text()}`
|
|
1189
|
+
);
|
|
1272
1190
|
const webSocket = response.webSocket;
|
|
1273
1191
|
assert(webSocket, "Failed to establish WebSocket");
|
|
1274
1192
|
webSocket.accept();
|
|
@@ -1281,6 +1199,7 @@ var cloudflareBuiltInModules = [
|
|
|
1281
1199
|
"cloudflare:workers",
|
|
1282
1200
|
"cloudflare:workflows"
|
|
1283
1201
|
];
|
|
1202
|
+
var defaultConditions = ["workerd", "module", "browser"];
|
|
1284
1203
|
function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmentName) {
|
|
1285
1204
|
return {
|
|
1286
1205
|
resolve: {
|
|
@@ -1288,7 +1207,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
1288
1207
|
// dependencies as not external
|
|
1289
1208
|
noExternal: true,
|
|
1290
1209
|
// We want to use `workerd` package exports if available (e.g. for postgres).
|
|
1291
|
-
conditions: [
|
|
1210
|
+
conditions: [...defaultConditions, "development|production"]
|
|
1292
1211
|
},
|
|
1293
1212
|
dev: {
|
|
1294
1213
|
createEnvironment(name2, config) {
|
|
@@ -1299,7 +1218,11 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
1299
1218
|
createEnvironment(name2, config) {
|
|
1300
1219
|
return new vite2.BuildEnvironment(name2, config);
|
|
1301
1220
|
},
|
|
1221
|
+
target: "es2022",
|
|
1222
|
+
// We need to enable `emitAssets` in order to support additional modules defined by `rules`
|
|
1223
|
+
emitAssets: true,
|
|
1302
1224
|
outDir: getOutputDirectory(userConfig, environmentName),
|
|
1225
|
+
copyPublicDir: false,
|
|
1303
1226
|
ssr: true,
|
|
1304
1227
|
rollupOptions: {
|
|
1305
1228
|
// Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input
|
|
@@ -1307,12 +1230,13 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
1307
1230
|
// dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set
|
|
1308
1231
|
// optimizeDeps.entries in the dev config)
|
|
1309
1232
|
input: workerConfig.main,
|
|
1310
|
-
external: [...cloudflareBuiltInModules
|
|
1233
|
+
external: [...cloudflareBuiltInModules]
|
|
1311
1234
|
}
|
|
1312
1235
|
},
|
|
1313
1236
|
optimizeDeps: {
|
|
1314
1237
|
// Note: ssr pre-bundling is opt-in and we need to enable it by setting `noDiscovery` to false
|
|
1315
1238
|
noDiscovery: false,
|
|
1239
|
+
entries: workerConfig.main,
|
|
1316
1240
|
exclude: [
|
|
1317
1241
|
...cloudflareBuiltInModules,
|
|
1318
1242
|
// we have to exclude all node modules to work in dev-mode not just the unenv externals...
|
|
@@ -1320,6 +1244,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
1320
1244
|
],
|
|
1321
1245
|
esbuildOptions: {
|
|
1322
1246
|
platform: "neutral",
|
|
1247
|
+
conditions: [...defaultConditions, "development"],
|
|
1323
1248
|
resolveExtensions: [
|
|
1324
1249
|
".mjs",
|
|
1325
1250
|
".js",
|
|
@@ -1397,46 +1322,39 @@ function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
|
1397
1322
|
};
|
|
1398
1323
|
fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig));
|
|
1399
1324
|
} else {
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
}
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1325
|
+
let entryWorkerConfigPath;
|
|
1326
|
+
const auxiliaryWorkers = [];
|
|
1327
|
+
for (const environmentName of Object.keys(resolvedPluginConfig.workers)) {
|
|
1328
|
+
const outputDirectory = resolvedViteConfig.environments[environmentName]?.build.outDir;
|
|
1329
|
+
assert2(
|
|
1330
|
+
outputDirectory,
|
|
1331
|
+
`Unexpected error: ${environmentName} environment output directory is undefined`
|
|
1332
|
+
);
|
|
1333
|
+
const configPath = getRelativePathToWorkerConfig(
|
|
1334
|
+
deployConfigDirectory,
|
|
1335
|
+
resolvedViteConfig.root,
|
|
1336
|
+
outputDirectory
|
|
1337
|
+
);
|
|
1338
|
+
if (environmentName === resolvedPluginConfig.entryWorkerEnvironmentName) {
|
|
1339
|
+
entryWorkerConfigPath = configPath;
|
|
1340
|
+
} else {
|
|
1341
|
+
auxiliaryWorkers.push({ configPath });
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1419
1344
|
assert2(
|
|
1420
|
-
|
|
1421
|
-
`Unexpected error:
|
|
1345
|
+
entryWorkerConfigPath,
|
|
1346
|
+
`Unexpected error: entryWorkerConfigPath is undefined`
|
|
1422
1347
|
);
|
|
1423
|
-
const
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1348
|
+
const deployConfig = {
|
|
1349
|
+
configPath: entryWorkerConfigPath,
|
|
1350
|
+
auxiliaryWorkers
|
|
1351
|
+
};
|
|
1427
1352
|
fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig));
|
|
1428
1353
|
}
|
|
1429
1354
|
}
|
|
1430
1355
|
|
|
1431
1356
|
// src/dev.ts
|
|
1432
1357
|
import assert3 from "node:assert";
|
|
1433
|
-
|
|
1434
|
-
// src/constants.ts
|
|
1435
|
-
var ROUTER_WORKER_NAME = "__router-worker__";
|
|
1436
|
-
var ASSET_WORKER_NAME = "__asset-worker__";
|
|
1437
|
-
var ASSET_WORKERS_COMPATIBILITY_DATE = "2024-10-04";
|
|
1438
|
-
|
|
1439
|
-
// src/dev.ts
|
|
1440
1358
|
function getDevEntryWorker(resolvedPluginConfig, miniflare) {
|
|
1441
1359
|
const entryWorkerConfig = resolvedPluginConfig.type === "assets-only" ? resolvedPluginConfig.config : resolvedPluginConfig.workers[resolvedPluginConfig.entryWorkerEnvironmentName];
|
|
1442
1360
|
assert3(entryWorkerConfig, "Unexpected error: No entry worker configuration");
|
|
@@ -1634,10 +1552,13 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
1634
1552
|
];
|
|
1635
1553
|
const userWorkers = resolvedPluginConfig.type === "workers" ? Object.entries(resolvedPluginConfig.workers).map(
|
|
1636
1554
|
([environmentName, workerConfig]) => {
|
|
1637
|
-
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1555
|
+
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(
|
|
1556
|
+
{
|
|
1557
|
+
...workerConfig,
|
|
1558
|
+
assets: void 0
|
|
1559
|
+
},
|
|
1560
|
+
resolvedPluginConfig.cloudflareEnv
|
|
1561
|
+
);
|
|
1641
1562
|
const { ratelimits, ...workerOptions } = miniflareWorkerOptions.workerOptions;
|
|
1642
1563
|
return {
|
|
1643
1564
|
...workerOptions,
|
|
@@ -1663,16 +1584,24 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
1663
1584
|
`Invalid invoke event: ${invokePayloadData.name}`
|
|
1664
1585
|
);
|
|
1665
1586
|
const [moduleId] = invokePayloadData.data;
|
|
1587
|
+
const moduleRE = new RegExp(MODULE_PATTERN);
|
|
1588
|
+
if (moduleRE.test(moduleId)) {
|
|
1589
|
+
const result2 = {
|
|
1590
|
+
externalize: moduleId,
|
|
1591
|
+
type: "module"
|
|
1592
|
+
};
|
|
1593
|
+
return MiniflareResponse.json({ result: result2 });
|
|
1594
|
+
}
|
|
1666
1595
|
if (moduleId.startsWith("cloudflare:")) {
|
|
1667
1596
|
const result2 = {
|
|
1668
1597
|
externalize: moduleId,
|
|
1669
1598
|
type: "builtin"
|
|
1670
1599
|
};
|
|
1671
|
-
return
|
|
1600
|
+
return MiniflareResponse.json({ result: result2 });
|
|
1672
1601
|
}
|
|
1673
1602
|
const devEnvironment = viteDevServer.environments[environmentName];
|
|
1674
1603
|
const result = await devEnvironment.hot.handleInvoke(payload);
|
|
1675
|
-
return
|
|
1604
|
+
return MiniflareResponse.json(result);
|
|
1676
1605
|
}
|
|
1677
1606
|
}
|
|
1678
1607
|
};
|
|
@@ -1751,10 +1680,37 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
1751
1680
|
fileURLToPath(new URL(RUNNER_PATH, import.meta.url))
|
|
1752
1681
|
)
|
|
1753
1682
|
}
|
|
1754
|
-
]
|
|
1683
|
+
],
|
|
1684
|
+
unsafeUseModuleFallbackService: true
|
|
1755
1685
|
};
|
|
1756
1686
|
})
|
|
1757
|
-
]
|
|
1687
|
+
],
|
|
1688
|
+
unsafeModuleFallbackService(request) {
|
|
1689
|
+
const url = new URL(request.url);
|
|
1690
|
+
const rawSpecifier = url.searchParams.get("rawSpecifier");
|
|
1691
|
+
assert4(
|
|
1692
|
+
rawSpecifier,
|
|
1693
|
+
`Unexpected error: no specifier in request to module fallback service.`
|
|
1694
|
+
);
|
|
1695
|
+
const moduleRE = new RegExp(MODULE_PATTERN);
|
|
1696
|
+
const match = moduleRE.exec(rawSpecifier);
|
|
1697
|
+
assert4(match, `Unexpected error: no match for module ${rawSpecifier}.`);
|
|
1698
|
+
const [full, moduleType, modulePath] = match;
|
|
1699
|
+
assert4(
|
|
1700
|
+
modulePath,
|
|
1701
|
+
`Unexpected error: module path not found in reference ${full}.`
|
|
1702
|
+
);
|
|
1703
|
+
let source;
|
|
1704
|
+
try {
|
|
1705
|
+
source = fs2.readFileSync(modulePath);
|
|
1706
|
+
} catch (error) {
|
|
1707
|
+
throw new Error(`Import ${modulePath} not found. Does the file exist?`);
|
|
1708
|
+
}
|
|
1709
|
+
return MiniflareResponse.json({
|
|
1710
|
+
// Cap'n Proto expects byte arrays for `:Data` typed fields from JSON
|
|
1711
|
+
wasm: Array.from(source)
|
|
1712
|
+
});
|
|
1713
|
+
}
|
|
1758
1714
|
};
|
|
1759
1715
|
}
|
|
1760
1716
|
function getPreviewMiniflareOptions(vitePreviewServer, persistState) {
|
|
@@ -1821,13 +1777,94 @@ function miniflareLogLevelFromViteLogLevel(level = "info") {
|
|
|
1821
1777
|
}
|
|
1822
1778
|
}
|
|
1823
1779
|
|
|
1780
|
+
// src/node-js-compat.ts
|
|
1781
|
+
import assert5 from "node:assert";
|
|
1782
|
+
import { cloudflare } from "@cloudflare/unenv-preset";
|
|
1783
|
+
import { getNodeCompat } from "miniflare";
|
|
1784
|
+
import { defineEnv } from "unenv";
|
|
1785
|
+
var { env } = defineEnv({
|
|
1786
|
+
nodeCompat: true,
|
|
1787
|
+
presets: [cloudflare]
|
|
1788
|
+
});
|
|
1789
|
+
var CLOUDFLARE_VIRTUAL_PREFIX = "\0__CLOUDFLARE_NODEJS_COMPAT__";
|
|
1790
|
+
function isNodeCompat(workerConfig) {
|
|
1791
|
+
if (workerConfig === void 0) {
|
|
1792
|
+
return false;
|
|
1793
|
+
}
|
|
1794
|
+
const nodeCompatMode = getNodeCompat(
|
|
1795
|
+
workerConfig.compatibility_date,
|
|
1796
|
+
workerConfig.compatibility_flags ?? []
|
|
1797
|
+
).mode;
|
|
1798
|
+
if (nodeCompatMode === "v2") {
|
|
1799
|
+
return true;
|
|
1800
|
+
}
|
|
1801
|
+
if (nodeCompatMode === "legacy") {
|
|
1802
|
+
throw new Error(
|
|
1803
|
+
"Unsupported Node.js compat mode (legacy). Remove the `node_compat` setting and add the `nodejs_compat` flag instead."
|
|
1804
|
+
);
|
|
1805
|
+
}
|
|
1806
|
+
if (nodeCompatMode === "v1") {
|
|
1807
|
+
throw new Error(
|
|
1808
|
+
`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`
|
|
1809
|
+
);
|
|
1810
|
+
}
|
|
1811
|
+
return false;
|
|
1812
|
+
}
|
|
1813
|
+
function injectGlobalCode(id, code) {
|
|
1814
|
+
const injectedCode = Object.entries(env.inject).map(([globalName, globalInject]) => {
|
|
1815
|
+
if (typeof globalInject === "string") {
|
|
1816
|
+
const moduleSpecifier2 = globalInject;
|
|
1817
|
+
return `import var_${globalName} from "${moduleSpecifier2}";
|
|
1818
|
+
globalThis.${globalName} = var_${globalName};
|
|
1819
|
+
`;
|
|
1820
|
+
}
|
|
1821
|
+
const [moduleSpecifier, exportName] = globalInject;
|
|
1822
|
+
return `import var_${globalName} from "${moduleSpecifier}";
|
|
1823
|
+
globalThis.${globalName} = var_${globalName}.${exportName};
|
|
1824
|
+
`;
|
|
1825
|
+
}).join("\n");
|
|
1826
|
+
const modified = new MagicString(code);
|
|
1827
|
+
modified.prepend(injectedCode);
|
|
1828
|
+
return {
|
|
1829
|
+
code: modified.toString(),
|
|
1830
|
+
map: modified.generateMap({ hires: "boundary", source: id })
|
|
1831
|
+
};
|
|
1832
|
+
}
|
|
1833
|
+
function getNodeCompatAliases() {
|
|
1834
|
+
const aliases = {};
|
|
1835
|
+
Object.keys(env.alias).forEach((key) => {
|
|
1836
|
+
if (!env.external.includes(key)) {
|
|
1837
|
+
aliases[key] = CLOUDFLARE_VIRTUAL_PREFIX + key;
|
|
1838
|
+
}
|
|
1839
|
+
});
|
|
1840
|
+
return aliases;
|
|
1841
|
+
}
|
|
1842
|
+
function getNodeCompatExternals() {
|
|
1843
|
+
return env.external;
|
|
1844
|
+
}
|
|
1845
|
+
function maybeStripNodeJsVirtualPrefix(source) {
|
|
1846
|
+
return source.startsWith(CLOUDFLARE_VIRTUAL_PREFIX) ? source.slice(CLOUDFLARE_VIRTUAL_PREFIX.length) : void 0;
|
|
1847
|
+
}
|
|
1848
|
+
function dealiasVirtualNodeJSImport(source) {
|
|
1849
|
+
const alias = env.alias[source];
|
|
1850
|
+
assert5(
|
|
1851
|
+
alias,
|
|
1852
|
+
`Expected "${source}" to have a Node.js compat alias, but none was found`
|
|
1853
|
+
);
|
|
1854
|
+
assert5(
|
|
1855
|
+
!env.external.includes(alias),
|
|
1856
|
+
`Unexpected unenv alias to external module: ${source} -> ${alias}`
|
|
1857
|
+
);
|
|
1858
|
+
return alias;
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1824
1861
|
// src/plugin-config.ts
|
|
1825
|
-
import
|
|
1862
|
+
import assert7 from "node:assert";
|
|
1826
1863
|
import * as path5 from "node:path";
|
|
1827
1864
|
import * as vite5 from "vite";
|
|
1828
1865
|
|
|
1829
1866
|
// src/workers-configs.ts
|
|
1830
|
-
import
|
|
1867
|
+
import assert6 from "node:assert";
|
|
1831
1868
|
import * as fs3 from "node:fs";
|
|
1832
1869
|
import * as path4 from "node:path";
|
|
1833
1870
|
import { unstable_readConfig as unstable_readConfig2 } from "wrangler";
|
|
@@ -2003,17 +2040,17 @@ function getWorkerConfig(configPath, env2, opts) {
|
|
|
2003
2040
|
}
|
|
2004
2041
|
const { raw, config, nonApplicable } = readWorkerConfig(configPath, env2);
|
|
2005
2042
|
opts?.visitedConfigPaths?.add(configPath);
|
|
2006
|
-
|
|
2043
|
+
assert6(
|
|
2007
2044
|
config.topLevelName,
|
|
2008
2045
|
missingFieldErrorMessage(`top-level 'name'`, configPath, env2)
|
|
2009
2046
|
);
|
|
2010
|
-
|
|
2011
|
-
|
|
2047
|
+
assert6(config.name, missingFieldErrorMessage(`'name'`, configPath, env2));
|
|
2048
|
+
assert6(
|
|
2012
2049
|
config.compatibility_date,
|
|
2013
2050
|
missingFieldErrorMessage(`'compatibility_date'`, configPath, env2)
|
|
2014
2051
|
);
|
|
2015
2052
|
if (opts?.isEntryWorker && !config.main) {
|
|
2016
|
-
|
|
2053
|
+
assert6(
|
|
2017
2054
|
config.assets,
|
|
2018
2055
|
missingFieldErrorMessage(`'main' or 'assets'`, configPath, env2)
|
|
2019
2056
|
);
|
|
@@ -2030,7 +2067,7 @@ function getWorkerConfig(configPath, env2, opts) {
|
|
|
2030
2067
|
nonApplicable
|
|
2031
2068
|
};
|
|
2032
2069
|
}
|
|
2033
|
-
|
|
2070
|
+
assert6(config.main, missingFieldErrorMessage(`'main'`, configPath, env2));
|
|
2034
2071
|
return {
|
|
2035
2072
|
type: "worker",
|
|
2036
2073
|
raw,
|
|
@@ -2061,20 +2098,21 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
2061
2098
|
const configPaths = /* @__PURE__ */ new Set();
|
|
2062
2099
|
const persistState = pluginConfig.persistState ?? true;
|
|
2063
2100
|
const root = userConfig.root ? path5.resolve(userConfig.root) : process.cwd();
|
|
2064
|
-
const { CLOUDFLARE_ENV } = vite5.loadEnv(
|
|
2101
|
+
const { CLOUDFLARE_ENV: cloudflareEnv } = vite5.loadEnv(
|
|
2102
|
+
viteEnv.mode,
|
|
2103
|
+
root,
|
|
2104
|
+
/* prefixes */
|
|
2105
|
+
""
|
|
2106
|
+
);
|
|
2065
2107
|
const configPath = pluginConfig.configPath ? path5.resolve(root, pluginConfig.configPath) : findWranglerConfig(root);
|
|
2066
|
-
|
|
2108
|
+
assert7(
|
|
2067
2109
|
configPath,
|
|
2068
2110
|
`Config not found. Have you created a wrangler.json(c) or wrangler.toml file?`
|
|
2069
2111
|
);
|
|
2070
|
-
const entryWorkerResolvedConfig = getWorkerConfig(
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
visitedConfigPaths: configPaths,
|
|
2075
|
-
isEntryWorker: true
|
|
2076
|
-
}
|
|
2077
|
-
);
|
|
2112
|
+
const entryWorkerResolvedConfig = getWorkerConfig(configPath, cloudflareEnv, {
|
|
2113
|
+
visitedConfigPaths: configPaths,
|
|
2114
|
+
isEntryWorker: true
|
|
2115
|
+
});
|
|
2078
2116
|
if (entryWorkerResolvedConfig.type === "assets-only") {
|
|
2079
2117
|
return {
|
|
2080
2118
|
type: "assets-only",
|
|
@@ -2083,7 +2121,8 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
2083
2121
|
persistState,
|
|
2084
2122
|
rawConfigs: {
|
|
2085
2123
|
entryWorker: entryWorkerResolvedConfig
|
|
2086
|
-
}
|
|
2124
|
+
},
|
|
2125
|
+
cloudflareEnv
|
|
2087
2126
|
};
|
|
2088
2127
|
}
|
|
2089
2128
|
const entryWorkerConfig = entryWorkerResolvedConfig.config;
|
|
@@ -2095,13 +2134,13 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
2095
2134
|
for (const auxiliaryWorker of pluginConfig.auxiliaryWorkers ?? []) {
|
|
2096
2135
|
const workerResolvedConfig = getWorkerConfig(
|
|
2097
2136
|
path5.resolve(root, auxiliaryWorker.configPath),
|
|
2098
|
-
|
|
2137
|
+
cloudflareEnv,
|
|
2099
2138
|
{
|
|
2100
2139
|
visitedConfigPaths: configPaths
|
|
2101
2140
|
}
|
|
2102
2141
|
);
|
|
2103
2142
|
auxiliaryWorkersResolvedConfigs.push(workerResolvedConfig);
|
|
2104
|
-
|
|
2143
|
+
assert7(
|
|
2105
2144
|
workerResolvedConfig.type === "worker",
|
|
2106
2145
|
"Unexpected error: received AssetsOnlyResult with auxiliary workers."
|
|
2107
2146
|
);
|
|
@@ -2123,7 +2162,8 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
2123
2162
|
rawConfigs: {
|
|
2124
2163
|
entryWorker: entryWorkerResolvedConfig,
|
|
2125
2164
|
auxiliaryWorkers: auxiliaryWorkersResolvedConfigs
|
|
2126
|
-
}
|
|
2165
|
+
},
|
|
2166
|
+
cloudflareEnv
|
|
2127
2167
|
};
|
|
2128
2168
|
}
|
|
2129
2169
|
|
|
@@ -2167,8 +2207,10 @@ ${event.error?.stack || event.error?.message}`,
|
|
|
2167
2207
|
workerWebSocket.addEventListener("close", () => {
|
|
2168
2208
|
clientWebSocket.close();
|
|
2169
2209
|
});
|
|
2170
|
-
clientWebSocket.on("message", (
|
|
2171
|
-
workerWebSocket.send(
|
|
2210
|
+
clientWebSocket.on("message", (data, isBinary) => {
|
|
2211
|
+
workerWebSocket.send(
|
|
2212
|
+
isBinary ? Array.isArray(data) ? Buffer.concat(data) : data : data.toString()
|
|
2213
|
+
);
|
|
2172
2214
|
});
|
|
2173
2215
|
clientWebSocket.on("error", (error) => {
|
|
2174
2216
|
logger.error(`WebSocket error:
|
|
@@ -2192,211 +2234,371 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2192
2234
|
let resolvedViteConfig;
|
|
2193
2235
|
let miniflare;
|
|
2194
2236
|
let workersConfigsWarningShown = false;
|
|
2195
|
-
return
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2237
|
+
return [
|
|
2238
|
+
{
|
|
2239
|
+
name: "vite-plugin-cloudflare",
|
|
2240
|
+
config(userConfig, env2) {
|
|
2241
|
+
if (env2.isPreview) {
|
|
2242
|
+
return { appType: "custom" };
|
|
2243
|
+
}
|
|
2244
|
+
resolvedPluginConfig = resolvePluginConfig(
|
|
2245
|
+
pluginConfig,
|
|
2246
|
+
userConfig,
|
|
2247
|
+
env2
|
|
2206
2248
|
);
|
|
2207
|
-
if (
|
|
2208
|
-
|
|
2249
|
+
if (!workersConfigsWarningShown) {
|
|
2250
|
+
workersConfigsWarningShown = true;
|
|
2251
|
+
const workersConfigsWarning = getWarningForWorkersConfigs(
|
|
2252
|
+
resolvedPluginConfig.rawConfigs
|
|
2253
|
+
);
|
|
2254
|
+
if (workersConfigsWarning) {
|
|
2255
|
+
console.warn(workersConfigsWarning);
|
|
2256
|
+
}
|
|
2209
2257
|
}
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2258
|
+
return {
|
|
2259
|
+
appType: "custom",
|
|
2260
|
+
environments: resolvedPluginConfig.type === "workers" ? {
|
|
2261
|
+
...Object.fromEntries(
|
|
2262
|
+
Object.entries(resolvedPluginConfig.workers).map(
|
|
2263
|
+
([environmentName, workerConfig]) => {
|
|
2264
|
+
return [
|
|
2265
|
+
environmentName,
|
|
2266
|
+
createCloudflareEnvironmentOptions(
|
|
2267
|
+
workerConfig,
|
|
2268
|
+
userConfig,
|
|
2269
|
+
environmentName
|
|
2270
|
+
)
|
|
2271
|
+
];
|
|
2272
|
+
}
|
|
2273
|
+
)
|
|
2274
|
+
),
|
|
2275
|
+
client: {
|
|
2276
|
+
build: {
|
|
2277
|
+
outDir: getOutputDirectory(userConfig, "client")
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
} : void 0,
|
|
2281
|
+
builder: {
|
|
2282
|
+
async buildApp(builder) {
|
|
2283
|
+
const clientEnvironment = builder.environments.client;
|
|
2284
|
+
const defaultHtmlPath = path6.resolve(
|
|
2285
|
+
builder.config.root,
|
|
2286
|
+
"index.html"
|
|
2287
|
+
);
|
|
2288
|
+
if (clientEnvironment && (clientEnvironment.config.build.rollupOptions.input || fs4.existsSync(defaultHtmlPath))) {
|
|
2289
|
+
await builder.build(clientEnvironment);
|
|
2290
|
+
}
|
|
2291
|
+
if (resolvedPluginConfig.type === "workers") {
|
|
2292
|
+
const workerEnvironments = Object.keys(
|
|
2293
|
+
resolvedPluginConfig.workers
|
|
2294
|
+
).map((environmentName) => {
|
|
2295
|
+
const environment = builder.environments[environmentName];
|
|
2296
|
+
assert8(
|
|
2297
|
+
environment,
|
|
2298
|
+
`${environmentName} environment not found`
|
|
2299
|
+
);
|
|
2300
|
+
return environment;
|
|
2301
|
+
});
|
|
2302
|
+
await Promise.all(
|
|
2303
|
+
workerEnvironments.map(
|
|
2304
|
+
(environment) => builder.build(environment)
|
|
2226
2305
|
)
|
|
2227
|
-
|
|
2306
|
+
);
|
|
2228
2307
|
}
|
|
2229
|
-
)
|
|
2230
|
-
),
|
|
2231
|
-
client: {
|
|
2232
|
-
build: {
|
|
2233
|
-
outDir: getOutputDirectory(userConfig, "client")
|
|
2234
2308
|
}
|
|
2235
2309
|
}
|
|
2236
|
-
}
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2310
|
+
};
|
|
2311
|
+
},
|
|
2312
|
+
configResolved(config) {
|
|
2313
|
+
resolvedViteConfig = config;
|
|
2314
|
+
},
|
|
2315
|
+
generateBundle(_, bundle) {
|
|
2316
|
+
let config;
|
|
2317
|
+
if (resolvedPluginConfig.type === "workers") {
|
|
2318
|
+
const workerConfig = resolvedPluginConfig.workers[this.environment.name];
|
|
2319
|
+
const entryChunk = Object.entries(bundle).find(
|
|
2320
|
+
([_2, chunk]) => chunk.type === "chunk" && chunk.isEntry
|
|
2321
|
+
);
|
|
2322
|
+
if (!workerConfig || !entryChunk) {
|
|
2323
|
+
return;
|
|
2324
|
+
}
|
|
2325
|
+
workerConfig.main = entryChunk[0];
|
|
2326
|
+
const isEntryWorker = this.environment.name === resolvedPluginConfig.entryWorkerEnvironmentName;
|
|
2327
|
+
if (isEntryWorker && workerConfig.assets) {
|
|
2328
|
+
const workerOutputDirectory = this.environment.config.build.outDir;
|
|
2329
|
+
const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
|
|
2330
|
+
assert8(
|
|
2331
|
+
clientOutputDirectory,
|
|
2332
|
+
"Unexpected error: client output directory is undefined"
|
|
2243
2333
|
);
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2334
|
+
workerConfig.assets.directory = path6.relative(
|
|
2335
|
+
path6.resolve(resolvedViteConfig.root, workerOutputDirectory),
|
|
2336
|
+
path6.resolve(resolvedViteConfig.root, clientOutputDirectory)
|
|
2337
|
+
);
|
|
2338
|
+
}
|
|
2339
|
+
config = workerConfig;
|
|
2340
|
+
if (workerConfig.configPath) {
|
|
2341
|
+
const dotDevDotVarsContent = getDotDevDotVarsContent(
|
|
2342
|
+
workerConfig.configPath,
|
|
2343
|
+
resolvedPluginConfig.cloudflareEnv
|
|
2344
|
+
);
|
|
2345
|
+
if (dotDevDotVarsContent) {
|
|
2346
|
+
this.emitFile({
|
|
2347
|
+
type: "asset",
|
|
2348
|
+
fileName: ".dev.vars",
|
|
2349
|
+
source: dotDevDotVarsContent
|
|
2254
2350
|
});
|
|
2255
|
-
await Promise.all(
|
|
2256
|
-
workerEnvironments.map(
|
|
2257
|
-
(environment) => builder.build(environment)
|
|
2258
|
-
)
|
|
2259
|
-
);
|
|
2260
2351
|
}
|
|
2261
|
-
writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
|
|
2262
2352
|
}
|
|
2353
|
+
} else if (this.environment.name === "client") {
|
|
2354
|
+
const assetsOnlyConfig = resolvedPluginConfig.config;
|
|
2355
|
+
assetsOnlyConfig.assets.directory = ".";
|
|
2356
|
+
const filesToAssetsIgnore = ["wrangler.json", ".dev.vars"];
|
|
2357
|
+
this.emitFile({
|
|
2358
|
+
type: "asset",
|
|
2359
|
+
fileName: ".assetsignore",
|
|
2360
|
+
source: `${filesToAssetsIgnore.join("\n")}
|
|
2361
|
+
`
|
|
2362
|
+
});
|
|
2363
|
+
config = assetsOnlyConfig;
|
|
2263
2364
|
}
|
|
2264
|
-
|
|
2265
|
-
},
|
|
2266
|
-
configResolved(config) {
|
|
2267
|
-
resolvedViteConfig = config;
|
|
2268
|
-
},
|
|
2269
|
-
async resolveId(source) {
|
|
2270
|
-
if (resolvedPluginConfig.type === "assets-only") {
|
|
2271
|
-
return;
|
|
2272
|
-
}
|
|
2273
|
-
const workerConfig = resolvedPluginConfig.workers[this.environment.name];
|
|
2274
|
-
if (!workerConfig) {
|
|
2275
|
-
return;
|
|
2276
|
-
}
|
|
2277
|
-
return resolveNodeCompatId(this.environment, workerConfig, source);
|
|
2278
|
-
},
|
|
2279
|
-
async transform(code, id) {
|
|
2280
|
-
if (resolvedPluginConfig.type === "assets-only") {
|
|
2281
|
-
return;
|
|
2282
|
-
}
|
|
2283
|
-
const workerConfig = resolvedPluginConfig.workers[this.environment.name];
|
|
2284
|
-
if (!workerConfig) {
|
|
2285
|
-
return;
|
|
2286
|
-
}
|
|
2287
|
-
const resolvedId = await this.resolve(workerConfig.main);
|
|
2288
|
-
if (id === resolvedId?.id) {
|
|
2289
|
-
return injectGlobalCode(id, code, workerConfig);
|
|
2290
|
-
}
|
|
2291
|
-
},
|
|
2292
|
-
generateBundle(_, bundle) {
|
|
2293
|
-
let config;
|
|
2294
|
-
if (resolvedPluginConfig.type === "workers") {
|
|
2295
|
-
const workerConfig = resolvedPluginConfig.workers[this.environment.name];
|
|
2296
|
-
const entryChunk = Object.entries(bundle).find(
|
|
2297
|
-
([_2, chunk]) => chunk.type === "chunk" && chunk.isEntry
|
|
2298
|
-
);
|
|
2299
|
-
if (!workerConfig || !entryChunk) {
|
|
2365
|
+
if (!config) {
|
|
2300
2366
|
return;
|
|
2301
2367
|
}
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
if (
|
|
2305
|
-
|
|
2306
|
-
const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
|
|
2307
|
-
assert7(
|
|
2308
|
-
clientOutputDirectory,
|
|
2309
|
-
"Unexpected error: client output directory is undefined"
|
|
2310
|
-
);
|
|
2311
|
-
workerConfig.assets.directory = path6.relative(
|
|
2312
|
-
path6.resolve(resolvedViteConfig.root, workerOutputDirectory),
|
|
2313
|
-
path6.resolve(resolvedViteConfig.root, clientOutputDirectory)
|
|
2314
|
-
);
|
|
2368
|
+
config.no_bundle = true;
|
|
2369
|
+
config.rules = [{ type: "ESModule", globs: ["**/*.js"] }];
|
|
2370
|
+
if (config.unsafe && Object.keys(config.unsafe).length === 0) {
|
|
2371
|
+
config.unsafe = void 0;
|
|
2315
2372
|
}
|
|
2316
|
-
config = workerConfig;
|
|
2317
|
-
} else if (this.environment.name === "client") {
|
|
2318
|
-
const assetsOnlyConfig = resolvedPluginConfig.config;
|
|
2319
|
-
assetsOnlyConfig.assets.directory = ".";
|
|
2320
2373
|
this.emitFile({
|
|
2321
2374
|
type: "asset",
|
|
2322
|
-
fileName: ".
|
|
2323
|
-
source:
|
|
2375
|
+
fileName: "wrangler.json",
|
|
2376
|
+
source: JSON.stringify(config)
|
|
2324
2377
|
});
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2378
|
+
},
|
|
2379
|
+
writeBundle() {
|
|
2380
|
+
if (this.environment.name === (resolvedPluginConfig.type === "assets-only" ? "client" : resolvedPluginConfig.entryWorkerEnvironmentName)) {
|
|
2381
|
+
writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
|
|
2382
|
+
}
|
|
2383
|
+
},
|
|
2384
|
+
handleHotUpdate(options) {
|
|
2385
|
+
if (resolvedPluginConfig.configPaths.has(options.file)) {
|
|
2386
|
+
options.server.restart();
|
|
2387
|
+
}
|
|
2388
|
+
},
|
|
2389
|
+
async buildEnd() {
|
|
2390
|
+
if (miniflare) {
|
|
2391
|
+
await miniflare.dispose();
|
|
2392
|
+
miniflare = void 0;
|
|
2393
|
+
}
|
|
2394
|
+
},
|
|
2395
|
+
async configureServer(viteDevServer) {
|
|
2396
|
+
assert8(
|
|
2397
|
+
viteDevServer.httpServer,
|
|
2398
|
+
"Unexpected error: No Vite HTTP server"
|
|
2399
|
+
);
|
|
2400
|
+
miniflare = new Miniflare(
|
|
2401
|
+
getDevMiniflareOptions(resolvedPluginConfig, viteDevServer)
|
|
2402
|
+
);
|
|
2403
|
+
await initRunners(resolvedPluginConfig, viteDevServer, miniflare);
|
|
2404
|
+
const entryWorker = await getDevEntryWorker(
|
|
2405
|
+
resolvedPluginConfig,
|
|
2406
|
+
miniflare
|
|
2407
|
+
);
|
|
2408
|
+
const middleware = createMiddleware(
|
|
2409
|
+
({ request }) => {
|
|
2410
|
+
return entryWorker.fetch(toMiniflareRequest(request), {
|
|
2411
|
+
redirect: "manual"
|
|
2412
|
+
});
|
|
2413
|
+
},
|
|
2414
|
+
{ alwaysCallNext: false }
|
|
2415
|
+
);
|
|
2416
|
+
handleWebSocket(
|
|
2417
|
+
viteDevServer.httpServer,
|
|
2418
|
+
entryWorker.fetch,
|
|
2419
|
+
viteDevServer.config.logger
|
|
2420
|
+
);
|
|
2421
|
+
return () => {
|
|
2422
|
+
viteDevServer.middlewares.use((req, res, next) => {
|
|
2423
|
+
middleware(req, res, next);
|
|
2424
|
+
});
|
|
2425
|
+
};
|
|
2426
|
+
},
|
|
2427
|
+
configurePreviewServer(vitePreviewServer) {
|
|
2428
|
+
const miniflare2 = new Miniflare(
|
|
2429
|
+
getPreviewMiniflareOptions(
|
|
2430
|
+
vitePreviewServer,
|
|
2431
|
+
pluginConfig.persistState ?? true
|
|
2432
|
+
)
|
|
2433
|
+
);
|
|
2434
|
+
const middleware = createMiddleware(
|
|
2435
|
+
({ request }) => {
|
|
2436
|
+
return miniflare2.dispatchFetch(toMiniflareRequest(request), {
|
|
2437
|
+
redirect: "manual"
|
|
2438
|
+
});
|
|
2439
|
+
},
|
|
2440
|
+
{ alwaysCallNext: false }
|
|
2441
|
+
);
|
|
2442
|
+
handleWebSocket(
|
|
2443
|
+
vitePreviewServer.httpServer,
|
|
2444
|
+
miniflare2.dispatchFetch,
|
|
2445
|
+
vitePreviewServer.config.logger
|
|
2446
|
+
);
|
|
2447
|
+
return () => {
|
|
2448
|
+
vitePreviewServer.middlewares.use((req, res, next) => {
|
|
2449
|
+
middleware(req, res, next);
|
|
2450
|
+
});
|
|
2451
|
+
};
|
|
2329
2452
|
}
|
|
2330
|
-
config.no_bundle = true;
|
|
2331
|
-
config.rules = [{ type: "ESModule", globs: ["**/*.js"] }];
|
|
2332
|
-
config.unsafe = void 0;
|
|
2333
|
-
this.emitFile({
|
|
2334
|
-
type: "asset",
|
|
2335
|
-
fileName: "wrangler.json",
|
|
2336
|
-
source: JSON.stringify(config)
|
|
2337
|
-
});
|
|
2338
2453
|
},
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2454
|
+
// Plugin to support `CompiledWasm` modules
|
|
2455
|
+
{
|
|
2456
|
+
name: "vite-plugin-cloudflare:modules",
|
|
2457
|
+
// We set `enforce: "pre"` so that this plugin runs before the Vite core plugins.
|
|
2458
|
+
// Otherwise the `vite:wasm-fallback` plugin prevents the `.wasm` extension being used for module imports.
|
|
2459
|
+
enforce: "pre",
|
|
2460
|
+
applyToEnvironment(environment) {
|
|
2461
|
+
return getWorkerConfig2(environment.name) !== void 0;
|
|
2462
|
+
},
|
|
2463
|
+
async resolveId(source, importer) {
|
|
2464
|
+
if (!source.endsWith(".wasm")) {
|
|
2465
|
+
return;
|
|
2466
|
+
}
|
|
2467
|
+
const resolved = await this.resolve(source, importer);
|
|
2468
|
+
assert8(
|
|
2469
|
+
resolved,
|
|
2470
|
+
`Unexpected error: could not resolve Wasm module ${source}`
|
|
2471
|
+
);
|
|
2472
|
+
return {
|
|
2473
|
+
external: true,
|
|
2474
|
+
id: createModuleReference("CompiledWasm", resolved.id)
|
|
2475
|
+
};
|
|
2476
|
+
},
|
|
2477
|
+
renderChunk(code, chunk) {
|
|
2478
|
+
const moduleRE = new RegExp(MODULE_PATTERN, "g");
|
|
2479
|
+
let match;
|
|
2480
|
+
let magicString;
|
|
2481
|
+
while (match = moduleRE.exec(code)) {
|
|
2482
|
+
magicString ??= new MagicString(code);
|
|
2483
|
+
const [full, moduleType, modulePath] = match;
|
|
2484
|
+
assert8(
|
|
2485
|
+
modulePath,
|
|
2486
|
+
`Unexpected error: module path not found in reference ${full}.`
|
|
2487
|
+
);
|
|
2488
|
+
let source;
|
|
2489
|
+
try {
|
|
2490
|
+
source = fs4.readFileSync(modulePath);
|
|
2491
|
+
} catch (error) {
|
|
2492
|
+
throw new Error(
|
|
2493
|
+
`Import ${modulePath} not found. Does the file exist?`
|
|
2494
|
+
);
|
|
2495
|
+
}
|
|
2496
|
+
const referenceId = this.emitFile({
|
|
2497
|
+
type: "asset",
|
|
2498
|
+
name: path6.basename(modulePath),
|
|
2499
|
+
originalFileName: modulePath,
|
|
2500
|
+
source
|
|
2501
|
+
});
|
|
2502
|
+
const emittedFileName = this.getFileName(referenceId);
|
|
2503
|
+
const relativePath = vite6.normalizePath(
|
|
2504
|
+
path6.relative(path6.dirname(chunk.fileName), emittedFileName)
|
|
2505
|
+
);
|
|
2506
|
+
const importPath = relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
2507
|
+
magicString.update(
|
|
2508
|
+
match.index,
|
|
2509
|
+
match.index + full.length,
|
|
2510
|
+
importPath
|
|
2511
|
+
);
|
|
2512
|
+
}
|
|
2513
|
+
if (magicString) {
|
|
2514
|
+
return {
|
|
2515
|
+
code: magicString.toString(),
|
|
2516
|
+
map: this.environment.config.build.sourcemap ? magicString.generateMap({ hires: "boundary" }) : null
|
|
2517
|
+
};
|
|
2518
|
+
}
|
|
2342
2519
|
}
|
|
2343
2520
|
},
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2521
|
+
// Plugin that can provide Node.js compatibility support for Vite Environments that are hosted in Cloudflare Workers.
|
|
2522
|
+
{
|
|
2523
|
+
name: "vite-plugin-cloudflare:nodejs-compat",
|
|
2524
|
+
apply(_config, env2) {
|
|
2525
|
+
return !env2.isPreview;
|
|
2526
|
+
},
|
|
2527
|
+
config() {
|
|
2528
|
+
return {
|
|
2529
|
+
resolve: {
|
|
2530
|
+
alias: getNodeCompatAliases()
|
|
2531
|
+
}
|
|
2532
|
+
};
|
|
2533
|
+
},
|
|
2534
|
+
configEnvironment(environmentName) {
|
|
2535
|
+
const workerConfig = getWorkerConfig2(environmentName);
|
|
2536
|
+
if (isNodeCompat(workerConfig)) {
|
|
2537
|
+
return {
|
|
2538
|
+
build: {
|
|
2539
|
+
rollupOptions: {
|
|
2540
|
+
external: getNodeCompatExternals()
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
};
|
|
2544
|
+
}
|
|
2545
|
+
},
|
|
2546
|
+
async resolveId(source, importer, options) {
|
|
2547
|
+
const from = maybeStripNodeJsVirtualPrefix(source);
|
|
2548
|
+
if (!from) {
|
|
2549
|
+
return;
|
|
2550
|
+
}
|
|
2551
|
+
const workerConfig = getWorkerConfig2(this.environment.name);
|
|
2552
|
+
if (!isNodeCompat(workerConfig)) {
|
|
2553
|
+
return this.resolve(from, importer, options);
|
|
2554
|
+
}
|
|
2555
|
+
const unresolvedAlias = dealiasVirtualNodeJSImport(from);
|
|
2556
|
+
const resolvedAlias = await this.resolve(
|
|
2557
|
+
unresolvedAlias,
|
|
2558
|
+
import.meta.url
|
|
2559
|
+
);
|
|
2560
|
+
assert8(
|
|
2561
|
+
resolvedAlias,
|
|
2562
|
+
"Failed to resolve aliased nodejs import: " + unresolvedAlias
|
|
2563
|
+
);
|
|
2564
|
+
if (this.environment.mode === "dev" && this.environment.depsOptimizer) {
|
|
2565
|
+
this.environment.depsOptimizer.registerMissingImport(
|
|
2566
|
+
unresolvedAlias,
|
|
2567
|
+
resolvedAlias.id
|
|
2568
|
+
);
|
|
2569
|
+
}
|
|
2570
|
+
return resolvedAlias;
|
|
2571
|
+
},
|
|
2572
|
+
async transform(code, id) {
|
|
2573
|
+
const workerConfig = getWorkerConfig2(this.environment.name);
|
|
2574
|
+
if (!isNodeCompat(workerConfig)) {
|
|
2575
|
+
return;
|
|
2576
|
+
}
|
|
2577
|
+
const resolvedId = await this.resolve(workerConfig.main);
|
|
2578
|
+
if (id === resolvedId?.id) {
|
|
2579
|
+
return injectGlobalCode(id, code);
|
|
2580
|
+
}
|
|
2348
2581
|
}
|
|
2349
|
-
},
|
|
2350
|
-
async configureServer(viteDevServer) {
|
|
2351
|
-
assert7(viteDevServer.httpServer, "Unexpected error: No Vite HTTP server");
|
|
2352
|
-
miniflare = new Miniflare(
|
|
2353
|
-
getDevMiniflareOptions(resolvedPluginConfig, viteDevServer)
|
|
2354
|
-
);
|
|
2355
|
-
await initRunners(resolvedPluginConfig, viteDevServer, miniflare);
|
|
2356
|
-
const entryWorker = await getDevEntryWorker(
|
|
2357
|
-
resolvedPluginConfig,
|
|
2358
|
-
miniflare
|
|
2359
|
-
);
|
|
2360
|
-
const middleware = createMiddleware(({ request }) => {
|
|
2361
|
-
return entryWorker.fetch(toMiniflareRequest(request), {
|
|
2362
|
-
redirect: "manual"
|
|
2363
|
-
});
|
|
2364
|
-
});
|
|
2365
|
-
handleWebSocket(
|
|
2366
|
-
viteDevServer.httpServer,
|
|
2367
|
-
entryWorker.fetch,
|
|
2368
|
-
viteDevServer.config.logger
|
|
2369
|
-
);
|
|
2370
|
-
return () => {
|
|
2371
|
-
viteDevServer.middlewares.use((req, res, next) => {
|
|
2372
|
-
middleware(req, res, next);
|
|
2373
|
-
});
|
|
2374
|
-
};
|
|
2375
|
-
},
|
|
2376
|
-
configurePreviewServer(vitePreviewServer) {
|
|
2377
|
-
const miniflare2 = new Miniflare(
|
|
2378
|
-
getPreviewMiniflareOptions(
|
|
2379
|
-
vitePreviewServer,
|
|
2380
|
-
pluginConfig.persistState ?? true
|
|
2381
|
-
)
|
|
2382
|
-
);
|
|
2383
|
-
const middleware = createMiddleware(({ request }) => {
|
|
2384
|
-
return miniflare2.dispatchFetch(toMiniflareRequest(request), {
|
|
2385
|
-
redirect: "manual"
|
|
2386
|
-
});
|
|
2387
|
-
});
|
|
2388
|
-
handleWebSocket(
|
|
2389
|
-
vitePreviewServer.httpServer,
|
|
2390
|
-
miniflare2.dispatchFetch,
|
|
2391
|
-
vitePreviewServer.config.logger
|
|
2392
|
-
);
|
|
2393
|
-
return () => {
|
|
2394
|
-
vitePreviewServer.middlewares.use((req, res, next) => {
|
|
2395
|
-
middleware(req, res, next);
|
|
2396
|
-
});
|
|
2397
|
-
};
|
|
2398
2582
|
}
|
|
2399
|
-
|
|
2583
|
+
];
|
|
2584
|
+
function getWorkerConfig2(environmentName) {
|
|
2585
|
+
assert8(resolvedPluginConfig, "Expected resolvedPluginConfig to be defined");
|
|
2586
|
+
return resolvedPluginConfig.type !== "assets-only" ? resolvedPluginConfig.workers[environmentName] : void 0;
|
|
2587
|
+
}
|
|
2588
|
+
}
|
|
2589
|
+
function getDotDevDotVarsContent(configPath, cloudflareEnv) {
|
|
2590
|
+
const configDir = path6.dirname(configPath);
|
|
2591
|
+
const defaultDotDevDotVarsPath = `${configDir}/.dev.vars`;
|
|
2592
|
+
const inputDotDevDotVarsPath = `${defaultDotDevDotVarsPath}${cloudflareEnv ? `.${cloudflareEnv}` : ""}`;
|
|
2593
|
+
const targetPath = fs4.existsSync(inputDotDevDotVarsPath) ? inputDotDevDotVarsPath : fs4.existsSync(defaultDotDevDotVarsPath) ? defaultDotDevDotVarsPath : null;
|
|
2594
|
+
if (targetPath) {
|
|
2595
|
+
const dotDevDotVarsContent = fs4.readFileSync(targetPath);
|
|
2596
|
+
return dotDevDotVarsContent;
|
|
2597
|
+
}
|
|
2598
|
+
return null;
|
|
2599
|
+
}
|
|
2600
|
+
function createModuleReference(type, id) {
|
|
2601
|
+
return `__CLOUDFLARE_MODULE__${type}__${id}__`;
|
|
2400
2602
|
}
|
|
2401
2603
|
export {
|
|
2402
2604
|
cloudflare2 as cloudflare
|