@cloudflare/vite-plugin 0.0.0-6fe4a67d0 → 0.0.0-7006630cf
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/asset-workers/asset-worker.js +664 -754
- package/dist/asset-workers/router-worker.js +644 -720
- package/dist/index.js +178 -216
- package/dist/runner-worker/index.js +1 -1
- package/package.json +7 -8
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import
|
|
2
|
+
import assert7 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,6 +1077,98 @@ 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
|
+
|
|
1080
1172
|
// src/constants.ts
|
|
1081
1173
|
var ROUTER_WORKER_NAME = "__router-worker__";
|
|
1082
1174
|
var ASSET_WORKER_NAME = "__asset-worker__";
|
|
@@ -1222,7 +1314,6 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
1222
1314
|
// We need to enable `emitAssets` in order to support additional modules defined by `rules`
|
|
1223
1315
|
emitAssets: true,
|
|
1224
1316
|
outDir: getOutputDirectory(userConfig, environmentName),
|
|
1225
|
-
copyPublicDir: false,
|
|
1226
1317
|
ssr: true,
|
|
1227
1318
|
rollupOptions: {
|
|
1228
1319
|
// Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input
|
|
@@ -1230,13 +1321,12 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
1230
1321
|
// dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set
|
|
1231
1322
|
// optimizeDeps.entries in the dev config)
|
|
1232
1323
|
input: workerConfig.main,
|
|
1233
|
-
external: [...cloudflareBuiltInModules]
|
|
1324
|
+
external: [...cloudflareBuiltInModules, ...getNodeCompatExternals()]
|
|
1234
1325
|
}
|
|
1235
1326
|
},
|
|
1236
1327
|
optimizeDeps: {
|
|
1237
1328
|
// Note: ssr pre-bundling is opt-in and we need to enable it by setting `noDiscovery` to false
|
|
1238
1329
|
noDiscovery: false,
|
|
1239
|
-
entries: workerConfig.main,
|
|
1240
1330
|
exclude: [
|
|
1241
1331
|
...cloudflareBuiltInModules,
|
|
1242
1332
|
// we have to exclude all node modules to work in dev-mode not just the unenv externals...
|
|
@@ -1322,33 +1412,33 @@ function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
|
1322
1412
|
};
|
|
1323
1413
|
fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig));
|
|
1324
1414
|
} else {
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1415
|
+
const workerConfigPaths = Object.fromEntries(
|
|
1416
|
+
Object.keys(resolvedPluginConfig.workers).map((environmentName) => {
|
|
1417
|
+
const outputDirectory = resolvedViteConfig.environments[environmentName]?.build.outDir;
|
|
1418
|
+
assert2(
|
|
1419
|
+
outputDirectory,
|
|
1420
|
+
`Unexpected error: ${environmentName} environment output directory is undefined`
|
|
1421
|
+
);
|
|
1422
|
+
return [
|
|
1423
|
+
environmentName,
|
|
1424
|
+
getRelativePathToWorkerConfig(
|
|
1425
|
+
deployConfigDirectory,
|
|
1426
|
+
resolvedViteConfig.root,
|
|
1427
|
+
outputDirectory
|
|
1428
|
+
)
|
|
1429
|
+
];
|
|
1430
|
+
})
|
|
1431
|
+
);
|
|
1432
|
+
const { entryWorkerEnvironmentName } = resolvedPluginConfig;
|
|
1433
|
+
const configPath = workerConfigPaths[entryWorkerEnvironmentName];
|
|
1344
1434
|
assert2(
|
|
1345
|
-
|
|
1346
|
-
`Unexpected error:
|
|
1435
|
+
configPath,
|
|
1436
|
+
`Unexpected error: ${entryWorkerEnvironmentName} environment output directory is undefined`
|
|
1347
1437
|
);
|
|
1348
|
-
const
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
};
|
|
1438
|
+
const auxiliaryWorkers = Object.entries(workerConfigPaths).filter(
|
|
1439
|
+
([environmentName]) => environmentName !== entryWorkerEnvironmentName
|
|
1440
|
+
).map(([_, configPath2]) => ({ configPath: configPath2 }));
|
|
1441
|
+
const deployConfig = { configPath, auxiliaryWorkers };
|
|
1352
1442
|
fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig));
|
|
1353
1443
|
}
|
|
1354
1444
|
}
|
|
@@ -1777,94 +1867,13 @@ function miniflareLogLevelFromViteLogLevel(level = "info") {
|
|
|
1777
1867
|
}
|
|
1778
1868
|
}
|
|
1779
1869
|
|
|
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
|
-
|
|
1861
1870
|
// src/plugin-config.ts
|
|
1862
|
-
import
|
|
1871
|
+
import assert6 from "node:assert";
|
|
1863
1872
|
import * as path5 from "node:path";
|
|
1864
1873
|
import * as vite5 from "vite";
|
|
1865
1874
|
|
|
1866
1875
|
// src/workers-configs.ts
|
|
1867
|
-
import
|
|
1876
|
+
import assert5 from "node:assert";
|
|
1868
1877
|
import * as fs3 from "node:fs";
|
|
1869
1878
|
import * as path4 from "node:path";
|
|
1870
1879
|
import { unstable_readConfig as unstable_readConfig2 } from "wrangler";
|
|
@@ -2040,17 +2049,17 @@ function getWorkerConfig(configPath, env2, opts) {
|
|
|
2040
2049
|
}
|
|
2041
2050
|
const { raw, config, nonApplicable } = readWorkerConfig(configPath, env2);
|
|
2042
2051
|
opts?.visitedConfigPaths?.add(configPath);
|
|
2043
|
-
|
|
2052
|
+
assert5(
|
|
2044
2053
|
config.topLevelName,
|
|
2045
2054
|
missingFieldErrorMessage(`top-level 'name'`, configPath, env2)
|
|
2046
2055
|
);
|
|
2047
|
-
|
|
2048
|
-
|
|
2056
|
+
assert5(config.name, missingFieldErrorMessage(`'name'`, configPath, env2));
|
|
2057
|
+
assert5(
|
|
2049
2058
|
config.compatibility_date,
|
|
2050
2059
|
missingFieldErrorMessage(`'compatibility_date'`, configPath, env2)
|
|
2051
2060
|
);
|
|
2052
2061
|
if (opts?.isEntryWorker && !config.main) {
|
|
2053
|
-
|
|
2062
|
+
assert5(
|
|
2054
2063
|
config.assets,
|
|
2055
2064
|
missingFieldErrorMessage(`'main' or 'assets'`, configPath, env2)
|
|
2056
2065
|
);
|
|
@@ -2067,7 +2076,7 @@ function getWorkerConfig(configPath, env2, opts) {
|
|
|
2067
2076
|
nonApplicable
|
|
2068
2077
|
};
|
|
2069
2078
|
}
|
|
2070
|
-
|
|
2079
|
+
assert5(config.main, missingFieldErrorMessage(`'main'`, configPath, env2));
|
|
2071
2080
|
return {
|
|
2072
2081
|
type: "worker",
|
|
2073
2082
|
raw,
|
|
@@ -2105,7 +2114,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
2105
2114
|
""
|
|
2106
2115
|
);
|
|
2107
2116
|
const configPath = pluginConfig.configPath ? path5.resolve(root, pluginConfig.configPath) : findWranglerConfig(root);
|
|
2108
|
-
|
|
2117
|
+
assert6(
|
|
2109
2118
|
configPath,
|
|
2110
2119
|
`Config not found. Have you created a wrangler.json(c) or wrangler.toml file?`
|
|
2111
2120
|
);
|
|
@@ -2140,7 +2149,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
2140
2149
|
}
|
|
2141
2150
|
);
|
|
2142
2151
|
auxiliaryWorkersResolvedConfigs.push(workerResolvedConfig);
|
|
2143
|
-
|
|
2152
|
+
assert6(
|
|
2144
2153
|
workerResolvedConfig.type === "worker",
|
|
2145
2154
|
"Unexpected error: received AssetsOnlyResult with auxiliary workers."
|
|
2146
2155
|
);
|
|
@@ -2207,10 +2216,8 @@ ${event.error?.stack || event.error?.message}`,
|
|
|
2207
2216
|
workerWebSocket.addEventListener("close", () => {
|
|
2208
2217
|
clientWebSocket.close();
|
|
2209
2218
|
});
|
|
2210
|
-
clientWebSocket.on("message", (
|
|
2211
|
-
workerWebSocket.send(
|
|
2212
|
-
isBinary ? Array.isArray(data) ? Buffer.concat(data) : data : data.toString()
|
|
2213
|
-
);
|
|
2219
|
+
clientWebSocket.on("message", (event) => {
|
|
2220
|
+
workerWebSocket.send(event);
|
|
2214
2221
|
});
|
|
2215
2222
|
clientWebSocket.on("error", (error) => {
|
|
2216
2223
|
logger.error(`WebSocket error:
|
|
@@ -2257,6 +2264,9 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2257
2264
|
}
|
|
2258
2265
|
return {
|
|
2259
2266
|
appType: "custom",
|
|
2267
|
+
resolve: {
|
|
2268
|
+
alias: getNodeCompatAliases()
|
|
2269
|
+
},
|
|
2260
2270
|
environments: resolvedPluginConfig.type === "workers" ? {
|
|
2261
2271
|
...Object.fromEntries(
|
|
2262
2272
|
Object.entries(resolvedPluginConfig.workers).map(
|
|
@@ -2293,7 +2303,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2293
2303
|
resolvedPluginConfig.workers
|
|
2294
2304
|
).map((environmentName) => {
|
|
2295
2305
|
const environment = builder.environments[environmentName];
|
|
2296
|
-
|
|
2306
|
+
assert7(
|
|
2297
2307
|
environment,
|
|
2298
2308
|
`${environmentName} environment not found`
|
|
2299
2309
|
);
|
|
@@ -2305,6 +2315,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2305
2315
|
)
|
|
2306
2316
|
);
|
|
2307
2317
|
}
|
|
2318
|
+
writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
|
|
2308
2319
|
}
|
|
2309
2320
|
}
|
|
2310
2321
|
};
|
|
@@ -2312,6 +2323,29 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2312
2323
|
configResolved(config) {
|
|
2313
2324
|
resolvedViteConfig = config;
|
|
2314
2325
|
},
|
|
2326
|
+
async resolveId(source) {
|
|
2327
|
+
if (resolvedPluginConfig.type === "assets-only") {
|
|
2328
|
+
return;
|
|
2329
|
+
}
|
|
2330
|
+
const workerConfig = resolvedPluginConfig.workers[this.environment.name];
|
|
2331
|
+
if (!workerConfig) {
|
|
2332
|
+
return;
|
|
2333
|
+
}
|
|
2334
|
+
return resolveNodeCompatId(this.environment, workerConfig, source);
|
|
2335
|
+
},
|
|
2336
|
+
async transform(code, id) {
|
|
2337
|
+
if (resolvedPluginConfig.type === "assets-only") {
|
|
2338
|
+
return;
|
|
2339
|
+
}
|
|
2340
|
+
const workerConfig = resolvedPluginConfig.workers[this.environment.name];
|
|
2341
|
+
if (!workerConfig) {
|
|
2342
|
+
return;
|
|
2343
|
+
}
|
|
2344
|
+
const resolvedId = await this.resolve(workerConfig.main);
|
|
2345
|
+
if (id === resolvedId?.id) {
|
|
2346
|
+
return injectGlobalCode(id, code, workerConfig);
|
|
2347
|
+
}
|
|
2348
|
+
},
|
|
2315
2349
|
generateBundle(_, bundle) {
|
|
2316
2350
|
let config;
|
|
2317
2351
|
if (resolvedPluginConfig.type === "workers") {
|
|
@@ -2327,7 +2361,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2327
2361
|
if (isEntryWorker && workerConfig.assets) {
|
|
2328
2362
|
const workerOutputDirectory = this.environment.config.build.outDir;
|
|
2329
2363
|
const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
|
|
2330
|
-
|
|
2364
|
+
assert7(
|
|
2331
2365
|
clientOutputDirectory,
|
|
2332
2366
|
"Unexpected error: client output directory is undefined"
|
|
2333
2367
|
);
|
|
@@ -2376,11 +2410,6 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2376
2410
|
source: JSON.stringify(config)
|
|
2377
2411
|
});
|
|
2378
2412
|
},
|
|
2379
|
-
writeBundle() {
|
|
2380
|
-
if (this.environment.name === (resolvedPluginConfig.type === "assets-only" ? "client" : resolvedPluginConfig.entryWorkerEnvironmentName)) {
|
|
2381
|
-
writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
|
|
2382
|
-
}
|
|
2383
|
-
},
|
|
2384
2413
|
handleHotUpdate(options) {
|
|
2385
2414
|
if (resolvedPluginConfig.configPaths.has(options.file)) {
|
|
2386
2415
|
options.server.restart();
|
|
@@ -2393,7 +2422,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2393
2422
|
}
|
|
2394
2423
|
},
|
|
2395
2424
|
async configureServer(viteDevServer) {
|
|
2396
|
-
|
|
2425
|
+
assert7(
|
|
2397
2426
|
viteDevServer.httpServer,
|
|
2398
2427
|
"Unexpected error: No Vite HTTP server"
|
|
2399
2428
|
);
|
|
@@ -2405,14 +2434,11 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2405
2434
|
resolvedPluginConfig,
|
|
2406
2435
|
miniflare
|
|
2407
2436
|
);
|
|
2408
|
-
const middleware = createMiddleware(
|
|
2409
|
-
(
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
},
|
|
2414
|
-
{ alwaysCallNext: false }
|
|
2415
|
-
);
|
|
2437
|
+
const middleware = createMiddleware(({ request }) => {
|
|
2438
|
+
return entryWorker.fetch(toMiniflareRequest(request), {
|
|
2439
|
+
redirect: "manual"
|
|
2440
|
+
});
|
|
2441
|
+
});
|
|
2416
2442
|
handleWebSocket(
|
|
2417
2443
|
viteDevServer.httpServer,
|
|
2418
2444
|
entryWorker.fetch,
|
|
@@ -2431,14 +2457,11 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2431
2457
|
pluginConfig.persistState ?? true
|
|
2432
2458
|
)
|
|
2433
2459
|
);
|
|
2434
|
-
const middleware = createMiddleware(
|
|
2435
|
-
(
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
},
|
|
2440
|
-
{ alwaysCallNext: false }
|
|
2441
|
-
);
|
|
2460
|
+
const middleware = createMiddleware(({ request }) => {
|
|
2461
|
+
return miniflare2.dispatchFetch(toMiniflareRequest(request), {
|
|
2462
|
+
redirect: "manual"
|
|
2463
|
+
});
|
|
2464
|
+
});
|
|
2442
2465
|
handleWebSocket(
|
|
2443
2466
|
vitePreviewServer.httpServer,
|
|
2444
2467
|
miniflare2.dispatchFetch,
|
|
@@ -2458,14 +2481,19 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2458
2481
|
// Otherwise the `vite:wasm-fallback` plugin prevents the `.wasm` extension being used for module imports.
|
|
2459
2482
|
enforce: "pre",
|
|
2460
2483
|
applyToEnvironment(environment) {
|
|
2461
|
-
|
|
2484
|
+
if (resolvedPluginConfig.type === "assets-only") {
|
|
2485
|
+
return false;
|
|
2486
|
+
}
|
|
2487
|
+
return Object.keys(resolvedPluginConfig.workers).includes(
|
|
2488
|
+
environment.name
|
|
2489
|
+
);
|
|
2462
2490
|
},
|
|
2463
2491
|
async resolveId(source, importer) {
|
|
2464
2492
|
if (!source.endsWith(".wasm")) {
|
|
2465
2493
|
return;
|
|
2466
2494
|
}
|
|
2467
2495
|
const resolved = await this.resolve(source, importer);
|
|
2468
|
-
|
|
2496
|
+
assert7(
|
|
2469
2497
|
resolved,
|
|
2470
2498
|
`Unexpected error: could not resolve Wasm module ${source}`
|
|
2471
2499
|
);
|
|
@@ -2481,7 +2509,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2481
2509
|
while (match = moduleRE.exec(code)) {
|
|
2482
2510
|
magicString ??= new MagicString(code);
|
|
2483
2511
|
const [full, moduleType, modulePath] = match;
|
|
2484
|
-
|
|
2512
|
+
assert7(
|
|
2485
2513
|
modulePath,
|
|
2486
2514
|
`Unexpected error: module path not found in reference ${full}.`
|
|
2487
2515
|
);
|
|
@@ -2517,74 +2545,8 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
2517
2545
|
};
|
|
2518
2546
|
}
|
|
2519
2547
|
}
|
|
2520
|
-
},
|
|
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
|
-
}
|
|
2581
|
-
}
|
|
2582
2548
|
}
|
|
2583
2549
|
];
|
|
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
2550
|
}
|
|
2589
2551
|
function getDotDevDotVarsContent(configPath, cloudflareEnv) {
|
|
2590
2552
|
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.
|
|
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
|
|
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-
|
|
3
|
+
"version": "0.0.0-7006630cf",
|
|
4
4
|
"description": "Cloudflare plugin for Vite",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -33,11 +33,10 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@cloudflare/unenv-preset": "1.1.1",
|
|
37
36
|
"@hattip/adapter-node": "^0.0.49",
|
|
38
|
-
"unenv": "2.0.0-
|
|
37
|
+
"unenv": "npm:unenv-nightly@2.0.0-20241218-183400-5d6aec3",
|
|
39
38
|
"ws": "^8.18.0",
|
|
40
|
-
"miniflare": "0.0.0-
|
|
39
|
+
"miniflare": "0.0.0-7006630cf"
|
|
41
40
|
},
|
|
42
41
|
"devDependencies": {
|
|
43
42
|
"@cloudflare/workers-types": "^4.20250204.0",
|
|
@@ -46,13 +45,13 @@
|
|
|
46
45
|
"magic-string": "^0.30.12",
|
|
47
46
|
"tsup": "8.3.0",
|
|
48
47
|
"typescript": "^5.7.2",
|
|
49
|
-
"vite": "^6.
|
|
50
|
-
"@cloudflare/workers-shared": "0.0.0-6fe4a67d0",
|
|
48
|
+
"vite": "^6.0.7",
|
|
51
49
|
"@cloudflare/workers-tsconfig": "0.0.0",
|
|
52
|
-
"
|
|
50
|
+
"@cloudflare/workers-shared": "0.0.0-7006630cf",
|
|
51
|
+
"wrangler": "0.0.0-7006630cf"
|
|
53
52
|
},
|
|
54
53
|
"peerDependencies": {
|
|
55
|
-
"vite": "^6.
|
|
54
|
+
"vite": "^6.0.7",
|
|
56
55
|
"wrangler": "^3.101.0"
|
|
57
56
|
},
|
|
58
57
|
"publishConfig": {
|