@cloudflare/vite-plugin 0.0.0-d611caf71 → 0.0.0-d65346879
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 +15 -540
- package/dist/asset-workers/asset-worker.js +489 -489
- package/dist/asset-workers/router-worker.js +1 -1
- package/dist/index.js +537 -192
- package/dist/runner-worker/index.js +1 -15
- package/package.json +9 -7
package/dist/index.js
CHANGED
|
@@ -248,17 +248,17 @@ var require_ignore = __commonJS({
|
|
|
248
248
|
var throwError = (message, Ctor) => {
|
|
249
249
|
throw new Ctor(message);
|
|
250
250
|
};
|
|
251
|
-
var checkPath = (
|
|
252
|
-
if (!isString(
|
|
251
|
+
var checkPath = (path10, originalPath, doThrow) => {
|
|
252
|
+
if (!isString(path10)) {
|
|
253
253
|
return doThrow(
|
|
254
254
|
`path must be a string, but got \`${originalPath}\``,
|
|
255
255
|
TypeError
|
|
256
256
|
);
|
|
257
257
|
}
|
|
258
|
-
if (!
|
|
258
|
+
if (!path10) {
|
|
259
259
|
return doThrow(`path must not be empty`, TypeError);
|
|
260
260
|
}
|
|
261
|
-
if (checkPath.isNotRelative(
|
|
261
|
+
if (checkPath.isNotRelative(path10)) {
|
|
262
262
|
const r2 = "`path.relative()`d";
|
|
263
263
|
return doThrow(
|
|
264
264
|
`path should be a ${r2} string, but got "${originalPath}"`,
|
|
@@ -267,7 +267,7 @@ var require_ignore = __commonJS({
|
|
|
267
267
|
}
|
|
268
268
|
return true;
|
|
269
269
|
};
|
|
270
|
-
var isNotRelative = (
|
|
270
|
+
var isNotRelative = (path10) => REGEX_TEST_INVALID_PATH.test(path10);
|
|
271
271
|
checkPath.isNotRelative = isNotRelative;
|
|
272
272
|
checkPath.convert = (p) => p;
|
|
273
273
|
var Ignore = class {
|
|
@@ -326,7 +326,7 @@ var require_ignore = __commonJS({
|
|
|
326
326
|
// setting `checkUnignored` to `false` could reduce additional
|
|
327
327
|
// path matching.
|
|
328
328
|
// @returns {TestResult} true if a file is ignored
|
|
329
|
-
_testOne(
|
|
329
|
+
_testOne(path10, checkUnignored) {
|
|
330
330
|
let ignored = false;
|
|
331
331
|
let unignored = false;
|
|
332
332
|
this._rules.forEach((rule) => {
|
|
@@ -334,7 +334,7 @@ var require_ignore = __commonJS({
|
|
|
334
334
|
if (unignored === negative && ignored !== unignored || negative && !ignored && !unignored && !checkUnignored) {
|
|
335
335
|
return;
|
|
336
336
|
}
|
|
337
|
-
const matched = rule.regex.test(
|
|
337
|
+
const matched = rule.regex.test(path10);
|
|
338
338
|
if (matched) {
|
|
339
339
|
ignored = !negative;
|
|
340
340
|
unignored = negative;
|
|
@@ -347,24 +347,24 @@ var require_ignore = __commonJS({
|
|
|
347
347
|
}
|
|
348
348
|
// @returns {TestResult}
|
|
349
349
|
_test(originalPath, cache2, checkUnignored, slices) {
|
|
350
|
-
const
|
|
350
|
+
const path10 = originalPath && checkPath.convert(originalPath);
|
|
351
351
|
checkPath(
|
|
352
|
-
|
|
352
|
+
path10,
|
|
353
353
|
originalPath,
|
|
354
354
|
this._allowRelativePaths ? RETURN_FALSE : throwError
|
|
355
355
|
);
|
|
356
|
-
return this._t(
|
|
356
|
+
return this._t(path10, cache2, checkUnignored, slices);
|
|
357
357
|
}
|
|
358
|
-
_t(
|
|
359
|
-
if (
|
|
360
|
-
return cache2[
|
|
358
|
+
_t(path10, cache2, checkUnignored, slices) {
|
|
359
|
+
if (path10 in cache2) {
|
|
360
|
+
return cache2[path10];
|
|
361
361
|
}
|
|
362
362
|
if (!slices) {
|
|
363
|
-
slices =
|
|
363
|
+
slices = path10.split(SLASH);
|
|
364
364
|
}
|
|
365
365
|
slices.pop();
|
|
366
366
|
if (!slices.length) {
|
|
367
|
-
return cache2[
|
|
367
|
+
return cache2[path10] = this._testOne(path10, checkUnignored);
|
|
368
368
|
}
|
|
369
369
|
const parent = this._t(
|
|
370
370
|
slices.join(SLASH) + SLASH,
|
|
@@ -372,24 +372,24 @@ var require_ignore = __commonJS({
|
|
|
372
372
|
checkUnignored,
|
|
373
373
|
slices
|
|
374
374
|
);
|
|
375
|
-
return cache2[
|
|
375
|
+
return cache2[path10] = parent.ignored ? parent : this._testOne(path10, checkUnignored);
|
|
376
376
|
}
|
|
377
|
-
ignores(
|
|
378
|
-
return this._test(
|
|
377
|
+
ignores(path10) {
|
|
378
|
+
return this._test(path10, this._ignoreCache, false).ignored;
|
|
379
379
|
}
|
|
380
380
|
createFilter() {
|
|
381
|
-
return (
|
|
381
|
+
return (path10) => !this.ignores(path10);
|
|
382
382
|
}
|
|
383
383
|
filter(paths) {
|
|
384
384
|
return makeArray(paths).filter(this.createFilter());
|
|
385
385
|
}
|
|
386
386
|
// @returns {TestResult}
|
|
387
|
-
test(
|
|
388
|
-
return this._test(
|
|
387
|
+
test(path10) {
|
|
388
|
+
return this._test(path10, this._testCache, true);
|
|
389
389
|
}
|
|
390
390
|
};
|
|
391
391
|
var factory = (options) => new Ignore(options);
|
|
392
|
-
var isPathValid = (
|
|
392
|
+
var isPathValid = (path10) => checkPath(path10 && checkPath.convert(path10), path10, RETURN_FALSE);
|
|
393
393
|
factory.isPathValid = isPathValid;
|
|
394
394
|
factory.default = factory;
|
|
395
395
|
module.exports = factory;
|
|
@@ -400,7 +400,7 @@ var require_ignore = __commonJS({
|
|
|
400
400
|
const makePosix = (str) => /^\\\\\?\\/.test(str) || /["<>|\u0000-\u001F]+/u.test(str) ? str : str.replace(/\\/g, "/");
|
|
401
401
|
checkPath.convert = makePosix;
|
|
402
402
|
const REGIX_IS_WINDOWS_PATH_ABSOLUTE = /^[a-z]:\//i;
|
|
403
|
-
checkPath.isNotRelative = (
|
|
403
|
+
checkPath.isNotRelative = (path10) => REGIX_IS_WINDOWS_PATH_ABSOLUTE.test(path10) || isNotRelative(path10);
|
|
404
404
|
}
|
|
405
405
|
}
|
|
406
406
|
});
|
|
@@ -443,11 +443,11 @@ var require_Mime = __commonJS({
|
|
|
443
443
|
}
|
|
444
444
|
}
|
|
445
445
|
};
|
|
446
|
-
Mime.prototype.getType = function(
|
|
447
|
-
|
|
448
|
-
let last =
|
|
446
|
+
Mime.prototype.getType = function(path10) {
|
|
447
|
+
path10 = String(path10);
|
|
448
|
+
let last = path10.replace(/^.*[/\\]/, "").toLowerCase();
|
|
449
449
|
let ext = last.replace(/^.*\./, "").toLowerCase();
|
|
450
|
-
let hasPath = last.length <
|
|
450
|
+
let hasPath = last.length < path10.length;
|
|
451
451
|
let hasDot = ext.length < last.length - 1;
|
|
452
452
|
return (hasDot || !hasPath) && this._types[ext] || null;
|
|
453
453
|
};
|
|
@@ -485,12 +485,12 @@ var require_mime = __commonJS({
|
|
|
485
485
|
});
|
|
486
486
|
|
|
487
487
|
// src/index.ts
|
|
488
|
-
import
|
|
488
|
+
import assert10 from "node:assert";
|
|
489
489
|
import * as fs5 from "node:fs";
|
|
490
490
|
import * as fsp2 from "node:fs/promises";
|
|
491
|
-
import
|
|
492
|
-
import * as path8 from "node:path";
|
|
491
|
+
import * as path9 from "node:path";
|
|
493
492
|
import { createMiddleware } from "@hattip/adapter-node";
|
|
493
|
+
import replace from "@rollup/plugin-replace";
|
|
494
494
|
|
|
495
495
|
// ../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
|
|
496
496
|
var comma = ",".charCodeAt(0);
|
|
@@ -1558,7 +1558,8 @@ var MagicString = class _MagicString {
|
|
|
1558
1558
|
|
|
1559
1559
|
// src/index.ts
|
|
1560
1560
|
import { Miniflare } from "miniflare";
|
|
1561
|
-
import
|
|
1561
|
+
import colors2 from "picocolors";
|
|
1562
|
+
import * as vite7 from "vite";
|
|
1562
1563
|
|
|
1563
1564
|
// src/constants.ts
|
|
1564
1565
|
var ROUTER_WORKER_NAME = "__router-worker__";
|
|
@@ -1726,11 +1727,11 @@ ${invalidHeaderRulesList}`
|
|
|
1726
1727
|
}
|
|
1727
1728
|
|
|
1728
1729
|
// ../workers-shared/utils/configuration/validateURL.ts
|
|
1729
|
-
var extractPathname = (
|
|
1730
|
-
if (!
|
|
1731
|
-
|
|
1730
|
+
var extractPathname = (path10 = "/", includeSearch, includeHash) => {
|
|
1731
|
+
if (!path10.startsWith("/")) {
|
|
1732
|
+
path10 = `/${path10}`;
|
|
1732
1733
|
}
|
|
1733
|
-
const url = new URL(`//${
|
|
1734
|
+
const url = new URL(`//${path10}`, "relative://");
|
|
1734
1735
|
return `${url.pathname}${includeSearch ? url.search : ""}${includeHash ? url.hash : ""}`;
|
|
1735
1736
|
};
|
|
1736
1737
|
var URL_REGEX = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/;
|
|
@@ -1763,8 +1764,8 @@ var validateUrl = (token, onlyRelative = false, disallowPorts = false, includeSe
|
|
|
1763
1764
|
if (!token.startsWith("/") && onlyRelative) {
|
|
1764
1765
|
token = `/${token}`;
|
|
1765
1766
|
}
|
|
1766
|
-
const
|
|
1767
|
-
if (
|
|
1767
|
+
const path10 = PATH_REGEX.exec(token);
|
|
1768
|
+
if (path10) {
|
|
1768
1769
|
try {
|
|
1769
1770
|
return [extractPathname(token, includeSearch, includeHash), void 0];
|
|
1770
1771
|
} catch {
|
|
@@ -1825,7 +1826,7 @@ function parseHeaders(input, {
|
|
|
1825
1826
|
});
|
|
1826
1827
|
}
|
|
1827
1828
|
}
|
|
1828
|
-
const [
|
|
1829
|
+
const [path10, pathError] = validateUrl(line, false, true);
|
|
1829
1830
|
if (pathError) {
|
|
1830
1831
|
invalid.push({
|
|
1831
1832
|
line,
|
|
@@ -1836,7 +1837,7 @@ function parseHeaders(input, {
|
|
|
1836
1837
|
continue;
|
|
1837
1838
|
}
|
|
1838
1839
|
rule = {
|
|
1839
|
-
path:
|
|
1840
|
+
path: path10,
|
|
1840
1841
|
line,
|
|
1841
1842
|
headers: {},
|
|
1842
1843
|
unsetHeaders: []
|
|
@@ -2411,8 +2412,8 @@ function getErrorMap() {
|
|
|
2411
2412
|
return overrideErrorMap;
|
|
2412
2413
|
}
|
|
2413
2414
|
var makeIssue = (params) => {
|
|
2414
|
-
const { data: data2, path:
|
|
2415
|
-
const fullPath = [...
|
|
2415
|
+
const { data: data2, path: path10, errorMaps, issueData } = params;
|
|
2416
|
+
const fullPath = [...path10, ...issueData.path || []];
|
|
2416
2417
|
const fullIssue = {
|
|
2417
2418
|
...issueData,
|
|
2418
2419
|
path: fullPath
|
|
@@ -2511,11 +2512,11 @@ var errorUtil;
|
|
|
2511
2512
|
errorUtil2.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
2512
2513
|
})(errorUtil || (errorUtil = {}));
|
|
2513
2514
|
var ParseInputLazyPath = class {
|
|
2514
|
-
constructor(parent, value,
|
|
2515
|
+
constructor(parent, value, path10, key) {
|
|
2515
2516
|
this._cachedPath = [];
|
|
2516
2517
|
this.parent = parent;
|
|
2517
2518
|
this.data = value;
|
|
2518
|
-
this._path =
|
|
2519
|
+
this._path = path10;
|
|
2519
2520
|
this._key = key;
|
|
2520
2521
|
}
|
|
2521
2522
|
get path() {
|
|
@@ -5783,7 +5784,8 @@ var z = /* @__PURE__ */ Object.freeze({
|
|
|
5783
5784
|
// ../workers-shared/utils/types.ts
|
|
5784
5785
|
var InternalConfigSchema = z.object({
|
|
5785
5786
|
account_id: z.number().optional(),
|
|
5786
|
-
script_id: z.number().optional()
|
|
5787
|
+
script_id: z.number().optional(),
|
|
5788
|
+
debug: z.boolean().optional()
|
|
5787
5789
|
});
|
|
5788
5790
|
var RouterConfigSchema = z.object({
|
|
5789
5791
|
invoke_user_worker_ahead_of_assets: z.boolean().optional(),
|
|
@@ -5831,14 +5833,14 @@ var AssetConfigSchema = z.object({
|
|
|
5831
5833
|
});
|
|
5832
5834
|
|
|
5833
5835
|
// src/asset-config.ts
|
|
5834
|
-
function hasAssetsConfigChanged(resolvedPluginConfig, resolvedViteConfig,
|
|
5836
|
+
function hasAssetsConfigChanged(resolvedPluginConfig, resolvedViteConfig, changedFilePath) {
|
|
5835
5837
|
if (!resolvedPluginConfig.experimental?.headersAndRedirectsDevModeSupport) {
|
|
5836
5838
|
return false;
|
|
5837
5839
|
}
|
|
5838
5840
|
return [
|
|
5839
5841
|
getRedirectsConfigPath(resolvedViteConfig),
|
|
5840
5842
|
getHeadersConfigPath(resolvedViteConfig)
|
|
5841
|
-
].includes(
|
|
5843
|
+
].includes(changedFilePath);
|
|
5842
5844
|
}
|
|
5843
5845
|
function getAssetsConfig(resolvedPluginConfig, entryWorkerConfig, resolvedConfig) {
|
|
5844
5846
|
const assetsConfig = resolvedPluginConfig.type === "assets-only" ? resolvedPluginConfig.config.assets : entryWorkerConfig?.assets;
|
|
@@ -5905,10 +5907,12 @@ function getHeadersConfigPath(config) {
|
|
|
5905
5907
|
|
|
5906
5908
|
// src/cloudflare-environment.ts
|
|
5907
5909
|
import assert3 from "node:assert";
|
|
5908
|
-
import * as
|
|
5910
|
+
import * as vite3 from "vite";
|
|
5909
5911
|
|
|
5910
5912
|
// src/node-js-compat.ts
|
|
5911
5913
|
import assert2 from "node:assert";
|
|
5914
|
+
import { builtinModules as builtinModules2 } from "node:module";
|
|
5915
|
+
import path3 from "node:path";
|
|
5912
5916
|
import { cloudflare } from "@cloudflare/unenv-preset";
|
|
5913
5917
|
import { getNodeCompat } from "miniflare";
|
|
5914
5918
|
|
|
@@ -11460,17 +11464,17 @@ function withTrailingSlash(input = "", respectQueryAndFragment) {
|
|
|
11460
11464
|
if (hasTrailingSlash(input, true)) {
|
|
11461
11465
|
return input || "/";
|
|
11462
11466
|
}
|
|
11463
|
-
let
|
|
11467
|
+
let path10 = input;
|
|
11464
11468
|
let fragment = "";
|
|
11465
11469
|
const fragmentIndex = input.indexOf("#");
|
|
11466
11470
|
if (fragmentIndex >= 0) {
|
|
11467
|
-
|
|
11471
|
+
path10 = input.slice(0, fragmentIndex);
|
|
11468
11472
|
fragment = input.slice(fragmentIndex);
|
|
11469
|
-
if (!
|
|
11473
|
+
if (!path10) {
|
|
11470
11474
|
return fragment;
|
|
11471
11475
|
}
|
|
11472
11476
|
}
|
|
11473
|
-
const [s0, ...s] =
|
|
11477
|
+
const [s0, ...s] = path10.split("?");
|
|
11474
11478
|
return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
|
|
11475
11479
|
}
|
|
11476
11480
|
function isNonEmptyURL(url) {
|
|
@@ -11504,8 +11508,8 @@ import path2, { dirname as dirname2 } from "node:path";
|
|
|
11504
11508
|
import v8 from "node:v8";
|
|
11505
11509
|
import { format as format2, inspect } from "node:util";
|
|
11506
11510
|
var BUILTIN_MODULES = new Set(builtinModules);
|
|
11507
|
-
function normalizeSlash(
|
|
11508
|
-
return
|
|
11511
|
+
function normalizeSlash(path10) {
|
|
11512
|
+
return path10.replace(/\\/g, "/");
|
|
11509
11513
|
}
|
|
11510
11514
|
var own$1 = {}.hasOwnProperty;
|
|
11511
11515
|
var classRegExp = /^([A-Z][a-z\d]*)+$/;
|
|
@@ -11618,8 +11622,8 @@ codes.ERR_INVALID_PACKAGE_CONFIG = createError(
|
|
|
11618
11622
|
* @param {string} [base]
|
|
11619
11623
|
* @param {string} [message]
|
|
11620
11624
|
*/
|
|
11621
|
-
(
|
|
11622
|
-
return `Invalid package config ${
|
|
11625
|
+
(path10, base, message) => {
|
|
11626
|
+
return `Invalid package config ${path10}${base ? ` while importing ${base}` : ""}${message ? `. ${message}` : ""}`;
|
|
11623
11627
|
},
|
|
11624
11628
|
Error
|
|
11625
11629
|
);
|
|
@@ -11651,8 +11655,8 @@ codes.ERR_MODULE_NOT_FOUND = createError(
|
|
|
11651
11655
|
* @param {string} base
|
|
11652
11656
|
* @param {boolean} [exactUrl]
|
|
11653
11657
|
*/
|
|
11654
|
-
(
|
|
11655
|
-
return `Cannot find ${exactUrl ? "module" : "package"} '${
|
|
11658
|
+
(path10, base, exactUrl = false) => {
|
|
11659
|
+
return `Cannot find ${exactUrl ? "module" : "package"} '${path10}' imported from ${base}`;
|
|
11656
11660
|
},
|
|
11657
11661
|
Error
|
|
11658
11662
|
);
|
|
@@ -11703,8 +11707,8 @@ codes.ERR_UNKNOWN_FILE_EXTENSION = createError(
|
|
|
11703
11707
|
* @param {string} extension
|
|
11704
11708
|
* @param {string} path
|
|
11705
11709
|
*/
|
|
11706
|
-
(extension,
|
|
11707
|
-
return `Unknown file extension "${extension}" for ${
|
|
11710
|
+
(extension, path10) => {
|
|
11711
|
+
return `Unknown file extension "${extension}" for ${path10}`;
|
|
11708
11712
|
},
|
|
11709
11713
|
TypeError
|
|
11710
11714
|
);
|
|
@@ -12076,9 +12080,9 @@ Default "index" lookups for the main are deprecated for ES modules.`,
|
|
|
12076
12080
|
);
|
|
12077
12081
|
}
|
|
12078
12082
|
}
|
|
12079
|
-
function tryStatSync(
|
|
12083
|
+
function tryStatSync(path10) {
|
|
12080
12084
|
try {
|
|
12081
|
-
return statSync(
|
|
12085
|
+
return statSync(path10);
|
|
12082
12086
|
} catch {
|
|
12083
12087
|
}
|
|
12084
12088
|
}
|
|
@@ -12845,12 +12849,20 @@ function resolvePathSync(id, options) {
|
|
|
12845
12849
|
|
|
12846
12850
|
// src/node-js-compat.ts
|
|
12847
12851
|
import { defineEnv } from "unenv";
|
|
12852
|
+
import "vite";
|
|
12848
12853
|
var { env } = defineEnv({
|
|
12849
12854
|
nodeCompat: true,
|
|
12850
12855
|
presets: [cloudflare]
|
|
12851
12856
|
});
|
|
12852
12857
|
var nodeCompatExternals = new Set(env.external);
|
|
12853
12858
|
var nodeCompatEntries = getNodeCompatEntries();
|
|
12859
|
+
var nodejsBuiltins = /* @__PURE__ */ new Set([
|
|
12860
|
+
...builtinModules2,
|
|
12861
|
+
...builtinModules2.map((m) => `node:${m}`)
|
|
12862
|
+
]);
|
|
12863
|
+
var NODEJS_MODULES_RE = new RegExp(
|
|
12864
|
+
`^(node:)?(${builtinModules2.join("|")})$`
|
|
12865
|
+
);
|
|
12854
12866
|
function isNodeCompat(workerConfig) {
|
|
12855
12867
|
if (workerConfig === void 0) {
|
|
12856
12868
|
return false;
|
|
@@ -12869,6 +12881,15 @@ function isNodeCompat(workerConfig) {
|
|
|
12869
12881
|
}
|
|
12870
12882
|
return false;
|
|
12871
12883
|
}
|
|
12884
|
+
function isNodeAls(workerConfig) {
|
|
12885
|
+
return workerConfig !== void 0 && getNodeCompat(
|
|
12886
|
+
workerConfig.compatibility_date,
|
|
12887
|
+
workerConfig.compatibility_flags ?? []
|
|
12888
|
+
).mode === "als";
|
|
12889
|
+
}
|
|
12890
|
+
function isNodeAlsModule(path10) {
|
|
12891
|
+
return /^(node:)?async_hooks$/.test(path10);
|
|
12892
|
+
}
|
|
12872
12893
|
function injectGlobalCode(id, code) {
|
|
12873
12894
|
const injectedCode = Object.entries(env.inject).map(([globalName, globalInject]) => {
|
|
12874
12895
|
if (typeof globalInject === "string") {
|
|
@@ -12899,7 +12920,7 @@ globalThis.${globalName} = var_${globalName}.${exportName};
|
|
|
12899
12920
|
}
|
|
12900
12921
|
function resolveNodeJSImport(source) {
|
|
12901
12922
|
const alias = env.alias[source];
|
|
12902
|
-
if (alias) {
|
|
12923
|
+
if (alias && !nodeCompatExternals.has(alias)) {
|
|
12903
12924
|
return {
|
|
12904
12925
|
unresolved: alias,
|
|
12905
12926
|
resolved: resolvePathSync(alias, { url: import.meta.url })
|
|
@@ -12929,6 +12950,45 @@ function getNodeCompatEntries() {
|
|
|
12929
12950
|
nodeCompatExternals.forEach((external) => entries.delete(external));
|
|
12930
12951
|
return entries;
|
|
12931
12952
|
}
|
|
12953
|
+
var NodeJsCompatWarnings = class {
|
|
12954
|
+
constructor(environmentName, resolvedViteConfig) {
|
|
12955
|
+
this.environmentName = environmentName;
|
|
12956
|
+
this.resolvedViteConfig = resolvedViteConfig;
|
|
12957
|
+
}
|
|
12958
|
+
sources = /* @__PURE__ */ new Map();
|
|
12959
|
+
timer;
|
|
12960
|
+
registerImport(source, importer = "<unknown>") {
|
|
12961
|
+
const importers = this.sources.get(source) ?? /* @__PURE__ */ new Set();
|
|
12962
|
+
this.sources.set(source, importers);
|
|
12963
|
+
importers.add(importer);
|
|
12964
|
+
this.renderWarningsOnIdle();
|
|
12965
|
+
}
|
|
12966
|
+
renderWarningsOnIdle() {
|
|
12967
|
+
if (this.timer) {
|
|
12968
|
+
clearTimeout(this.timer);
|
|
12969
|
+
}
|
|
12970
|
+
this.timer = setTimeout(() => {
|
|
12971
|
+
this.renderWarnings();
|
|
12972
|
+
this.timer = void 0;
|
|
12973
|
+
}, 500);
|
|
12974
|
+
}
|
|
12975
|
+
renderWarnings() {
|
|
12976
|
+
if (this.sources.size > 0) {
|
|
12977
|
+
let message = `Unexpected Node.js imports for environment "${this.environmentName}". Do you need to enable the "nodejs_compat" compatibility flag? Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details.
|
|
12978
|
+
`;
|
|
12979
|
+
this.sources.forEach((importers, source) => {
|
|
12980
|
+
importers.forEach((importer) => {
|
|
12981
|
+
message += ` - "${source}" imported from "${path3.relative(this.resolvedViteConfig.root, importer)}"
|
|
12982
|
+
`;
|
|
12983
|
+
});
|
|
12984
|
+
});
|
|
12985
|
+
this.resolvedViteConfig.logger.warn(message, {
|
|
12986
|
+
timestamp: true
|
|
12987
|
+
});
|
|
12988
|
+
this.sources.clear();
|
|
12989
|
+
}
|
|
12990
|
+
}
|
|
12991
|
+
};
|
|
12932
12992
|
|
|
12933
12993
|
// src/shared.ts
|
|
12934
12994
|
var UNKNOWN_HOST = "http://localhost";
|
|
@@ -12942,17 +13002,22 @@ var additionalModuleGlobalRE = new RegExp(
|
|
|
12942
13002
|
var VITE_DEV_METADATA_HEADER = "__VITE_DEV_METADATA__";
|
|
12943
13003
|
|
|
12944
13004
|
// src/utils.ts
|
|
12945
|
-
import * as
|
|
13005
|
+
import * as path4 from "node:path";
|
|
13006
|
+
import getPort, { portNumbers } from "get-port";
|
|
12946
13007
|
import { Request as MiniflareRequest } from "miniflare";
|
|
12947
13008
|
import "vite";
|
|
12948
13009
|
function getOutputDirectory(userConfig, environmentName) {
|
|
12949
13010
|
const rootOutputDirectory = userConfig.build?.outDir ?? "dist";
|
|
12950
|
-
return userConfig.environments?.[environmentName]?.build?.outDir ??
|
|
13011
|
+
return userConfig.environments?.[environmentName]?.build?.outDir ?? path4.join(rootOutputDirectory, environmentName);
|
|
12951
13012
|
}
|
|
12952
|
-
function getRouterWorker(
|
|
12953
|
-
return
|
|
13013
|
+
function getRouterWorker(miniflare2) {
|
|
13014
|
+
return miniflare2.getWorker(ROUTER_WORKER_NAME);
|
|
12954
13015
|
}
|
|
12955
13016
|
function toMiniflareRequest(request) {
|
|
13017
|
+
const host = request.headers.get("Host");
|
|
13018
|
+
if (host) {
|
|
13019
|
+
request.headers.set("X-Forwarded-Host", host);
|
|
13020
|
+
}
|
|
12956
13021
|
const secFetchMode = request.headers.get("Sec-Fetch-Mode");
|
|
12957
13022
|
if (secFetchMode) {
|
|
12958
13023
|
request.headers.set("X-Mf-Sec-Fetch-Mode", secFetchMode);
|
|
@@ -12981,6 +13046,9 @@ var postfixRE = /[?#].*$/;
|
|
|
12981
13046
|
function cleanUrl(url) {
|
|
12982
13047
|
return url.replace(postfixRE, "");
|
|
12983
13048
|
}
|
|
13049
|
+
function getFirstAvailablePort(start) {
|
|
13050
|
+
return getPort({ port: portNumbers(start, 65535) });
|
|
13051
|
+
}
|
|
12984
13052
|
|
|
12985
13053
|
// src/cloudflare-environment.ts
|
|
12986
13054
|
var webSocketUndefinedError = "The WebSocket is undefined";
|
|
@@ -13026,7 +13094,7 @@ function createHotChannel(webSocketContainer) {
|
|
|
13026
13094
|
}
|
|
13027
13095
|
};
|
|
13028
13096
|
}
|
|
13029
|
-
var CloudflareDevEnvironment = class extends
|
|
13097
|
+
var CloudflareDevEnvironment = class extends vite3.DevEnvironment {
|
|
13030
13098
|
#webSocketContainer;
|
|
13031
13099
|
#worker;
|
|
13032
13100
|
constructor(name, config) {
|
|
@@ -13087,7 +13155,7 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
13087
13155
|
},
|
|
13088
13156
|
build: {
|
|
13089
13157
|
createEnvironment(name, config) {
|
|
13090
|
-
return new
|
|
13158
|
+
return new vite3.BuildEnvironment(name, config);
|
|
13091
13159
|
},
|
|
13092
13160
|
target,
|
|
13093
13161
|
// We need to enable `emitAssets` in order to support additional modules defined by `rules`
|
|
@@ -13130,14 +13198,14 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
13130
13198
|
keepProcessEnv: isNodeCompat(workerConfig)
|
|
13131
13199
|
};
|
|
13132
13200
|
}
|
|
13133
|
-
function initRunners(resolvedPluginConfig, viteDevServer,
|
|
13201
|
+
function initRunners(resolvedPluginConfig, viteDevServer, miniflare2) {
|
|
13134
13202
|
if (resolvedPluginConfig.type === "assets-only") {
|
|
13135
13203
|
return;
|
|
13136
13204
|
}
|
|
13137
13205
|
return Promise.all(
|
|
13138
13206
|
Object.entries(resolvedPluginConfig.workers).map(
|
|
13139
13207
|
async ([environmentName, workerConfig]) => {
|
|
13140
|
-
const worker = await
|
|
13208
|
+
const worker = await miniflare2.getWorker(workerConfig.name);
|
|
13141
13209
|
return viteDevServer.environments[environmentName].initRunner(worker, viteDevServer.config.root, workerConfig);
|
|
13142
13210
|
}
|
|
13143
13211
|
)
|
|
@@ -13196,11 +13264,11 @@ function getDebugPathHtml(workerNames, inspectorPort) {
|
|
|
13196
13264
|
// src/deploy-config.ts
|
|
13197
13265
|
import assert5 from "node:assert";
|
|
13198
13266
|
import * as fs2 from "node:fs";
|
|
13199
|
-
import * as
|
|
13267
|
+
import * as path5 from "node:path";
|
|
13200
13268
|
import "vite";
|
|
13201
13269
|
import { unstable_readConfig } from "wrangler";
|
|
13202
13270
|
function getDeployConfigPath(root) {
|
|
13203
|
-
return
|
|
13271
|
+
return path5.resolve(root, ".wrangler", "deploy", "config.json");
|
|
13204
13272
|
}
|
|
13205
13273
|
function getWorkerConfigs(root) {
|
|
13206
13274
|
const deployConfigPath = getDeployConfigPath(root);
|
|
@@ -13211,22 +13279,22 @@ function getWorkerConfigs(root) {
|
|
|
13211
13279
|
{ configPath: deployConfig.configPath },
|
|
13212
13280
|
...deployConfig.auxiliaryWorkers
|
|
13213
13281
|
].map(({ configPath }) => {
|
|
13214
|
-
const resolvedConfigPath =
|
|
13215
|
-
|
|
13282
|
+
const resolvedConfigPath = path5.resolve(
|
|
13283
|
+
path5.dirname(deployConfigPath),
|
|
13216
13284
|
configPath
|
|
13217
13285
|
);
|
|
13218
13286
|
return unstable_readConfig({ config: resolvedConfigPath });
|
|
13219
13287
|
});
|
|
13220
13288
|
}
|
|
13221
13289
|
function getRelativePathToWorkerConfig(deployConfigDirectory, root, outputDirectory) {
|
|
13222
|
-
return
|
|
13290
|
+
return path5.relative(
|
|
13223
13291
|
deployConfigDirectory,
|
|
13224
|
-
|
|
13292
|
+
path5.resolve(root, outputDirectory, "wrangler.json")
|
|
13225
13293
|
);
|
|
13226
13294
|
}
|
|
13227
13295
|
function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
13228
13296
|
const deployConfigPath = getDeployConfigPath(resolvedViteConfig.root);
|
|
13229
|
-
const deployConfigDirectory =
|
|
13297
|
+
const deployConfigDirectory = path5.dirname(deployConfigPath);
|
|
13230
13298
|
fs2.mkdirSync(deployConfigDirectory, { recursive: true });
|
|
13231
13299
|
if (resolvedPluginConfig.type === "assets-only") {
|
|
13232
13300
|
const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
|
|
@@ -13279,7 +13347,7 @@ function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
|
13279
13347
|
import assert6 from "node:assert";
|
|
13280
13348
|
import * as fs3 from "node:fs";
|
|
13281
13349
|
import * as fsp from "node:fs/promises";
|
|
13282
|
-
import * as
|
|
13350
|
+
import * as path6 from "node:path";
|
|
13283
13351
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
13284
13352
|
import {
|
|
13285
13353
|
kCurrentWorker,
|
|
@@ -13295,18 +13363,18 @@ function getPersistence(root, persistState) {
|
|
|
13295
13363
|
return {};
|
|
13296
13364
|
}
|
|
13297
13365
|
const defaultPersistPath = ".wrangler/state";
|
|
13298
|
-
const persistPath =
|
|
13366
|
+
const persistPath = path6.resolve(
|
|
13299
13367
|
root,
|
|
13300
13368
|
typeof persistState === "object" ? persistState.path : defaultPersistPath,
|
|
13301
13369
|
"v3"
|
|
13302
13370
|
);
|
|
13303
13371
|
return {
|
|
13304
|
-
cachePersist:
|
|
13305
|
-
d1Persist:
|
|
13306
|
-
durableObjectsPersist:
|
|
13307
|
-
kvPersist:
|
|
13308
|
-
r2Persist:
|
|
13309
|
-
workflowsPersist:
|
|
13372
|
+
cachePersist: path6.join(persistPath, "cache"),
|
|
13373
|
+
d1Persist: path6.join(persistPath, "d1"),
|
|
13374
|
+
durableObjectsPersist: path6.join(persistPath, "do"),
|
|
13375
|
+
kvPersist: path6.join(persistPath, "kv"),
|
|
13376
|
+
r2Persist: path6.join(persistPath, "r2"),
|
|
13377
|
+
workflowsPersist: path6.join(persistPath, "workflows")
|
|
13310
13378
|
};
|
|
13311
13379
|
}
|
|
13312
13380
|
function missingWorkerErrorMessage(workerName) {
|
|
@@ -13391,7 +13459,7 @@ function getEntryWorkerConfig(resolvedPluginConfig) {
|
|
|
13391
13459
|
}
|
|
13392
13460
|
return resolvedPluginConfig.workers[resolvedPluginConfig.entryWorkerEnvironmentName];
|
|
13393
13461
|
}
|
|
13394
|
-
function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
13462
|
+
function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer, inspectorPort) {
|
|
13395
13463
|
const resolvedViteConfig = viteDevServer.config;
|
|
13396
13464
|
const entryWorkerConfig = getEntryWorkerConfig(resolvedPluginConfig);
|
|
13397
13465
|
const assetsConfig = getAssetsConfig(
|
|
@@ -13407,7 +13475,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13407
13475
|
modules: [
|
|
13408
13476
|
{
|
|
13409
13477
|
type: "ESModule",
|
|
13410
|
-
path:
|
|
13478
|
+
path: path6.join(miniflareModulesRoot, ROUTER_WORKER_PATH),
|
|
13411
13479
|
contents: fs3.readFileSync(
|
|
13412
13480
|
fileURLToPath2(new URL(ROUTER_WORKER_PATH, import.meta.url))
|
|
13413
13481
|
)
|
|
@@ -13430,7 +13498,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13430
13498
|
modules: [
|
|
13431
13499
|
{
|
|
13432
13500
|
type: "ESModule",
|
|
13433
|
-
path:
|
|
13501
|
+
path: path6.join(miniflareModulesRoot, ASSET_WORKER_PATH),
|
|
13434
13502
|
contents: fs3.readFileSync(
|
|
13435
13503
|
fileURLToPath2(new URL(ASSET_WORKER_PATH, import.meta.url))
|
|
13436
13504
|
)
|
|
@@ -13442,7 +13510,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13442
13510
|
serviceBindings: {
|
|
13443
13511
|
__VITE_ASSET_EXISTS__: async (request) => {
|
|
13444
13512
|
const { pathname } = new URL(request.url);
|
|
13445
|
-
const filePath =
|
|
13513
|
+
const filePath = path6.join(resolvedViteConfig.root, pathname);
|
|
13446
13514
|
let exists;
|
|
13447
13515
|
try {
|
|
13448
13516
|
exists = fs3.statSync(filePath).isFile();
|
|
@@ -13453,7 +13521,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13453
13521
|
},
|
|
13454
13522
|
__VITE_FETCH_ASSET__: async (request) => {
|
|
13455
13523
|
const { pathname } = new URL(request.url);
|
|
13456
|
-
const filePath =
|
|
13524
|
+
const filePath = path6.join(resolvedViteConfig.root, pathname);
|
|
13457
13525
|
try {
|
|
13458
13526
|
let html = await fsp.readFile(filePath, "utf-8");
|
|
13459
13527
|
html = await viteDevServer.transformIndexHtml(pathname, html);
|
|
@@ -13483,7 +13551,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13483
13551
|
worker: {
|
|
13484
13552
|
...workerOptions,
|
|
13485
13553
|
name: workerOptions.name ?? workerConfig.name,
|
|
13486
|
-
unsafeInspectorProxy:
|
|
13554
|
+
unsafeInspectorProxy: inspectorPort !== false,
|
|
13487
13555
|
modulesRoot: miniflareModulesRoot,
|
|
13488
13556
|
unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
|
|
13489
13557
|
serviceBindings: {
|
|
@@ -13525,8 +13593,8 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13525
13593
|
const logger = new ViteMiniflareLogger(resolvedViteConfig);
|
|
13526
13594
|
return {
|
|
13527
13595
|
log: logger,
|
|
13528
|
-
inspectorPort:
|
|
13529
|
-
unsafeInspectorProxy:
|
|
13596
|
+
inspectorPort: inspectorPort === false ? void 0 : inspectorPort,
|
|
13597
|
+
unsafeInspectorProxy: inspectorPort !== false,
|
|
13530
13598
|
handleRuntimeStdio(stdout, stderr) {
|
|
13531
13599
|
const decoder = new TextDecoder();
|
|
13532
13600
|
stdout.forEach((data2) => logger.info(decoder.decode(data2)));
|
|
@@ -13585,12 +13653,12 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13585
13653
|
modules: [
|
|
13586
13654
|
{
|
|
13587
13655
|
type: "ESModule",
|
|
13588
|
-
path:
|
|
13656
|
+
path: path6.join(miniflareModulesRoot, WRAPPER_PATH),
|
|
13589
13657
|
contents: wrappers.join("\n")
|
|
13590
13658
|
},
|
|
13591
13659
|
{
|
|
13592
13660
|
type: "ESModule",
|
|
13593
|
-
path:
|
|
13661
|
+
path: path6.join(miniflareModulesRoot, RUNNER_PATH),
|
|
13594
13662
|
contents: fs3.readFileSync(
|
|
13595
13663
|
fileURLToPath2(new URL(RUNNER_PATH, import.meta.url))
|
|
13596
13664
|
)
|
|
@@ -13645,8 +13713,8 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
13645
13713
|
}
|
|
13646
13714
|
function getPreviewModules(main, modulesRules) {
|
|
13647
13715
|
assert6(modulesRules, `Unexpected error: 'modulesRules' is undefined`);
|
|
13648
|
-
const rootPath =
|
|
13649
|
-
const entryPath =
|
|
13716
|
+
const rootPath = path6.dirname(main);
|
|
13717
|
+
const entryPath = path6.basename(main);
|
|
13650
13718
|
return {
|
|
13651
13719
|
rootPath,
|
|
13652
13720
|
modules: [
|
|
@@ -13655,15 +13723,15 @@ function getPreviewModules(main, modulesRules) {
|
|
|
13655
13723
|
path: entryPath
|
|
13656
13724
|
},
|
|
13657
13725
|
...modulesRules.flatMap(
|
|
13658
|
-
({ type, include }) => globSync(include, { cwd: rootPath, ignore: entryPath }).map((
|
|
13726
|
+
({ type, include }) => globSync(include, { cwd: rootPath, ignore: entryPath }).map((path10) => ({
|
|
13659
13727
|
type,
|
|
13660
|
-
path:
|
|
13728
|
+
path: path10
|
|
13661
13729
|
}))
|
|
13662
13730
|
)
|
|
13663
13731
|
]
|
|
13664
13732
|
};
|
|
13665
13733
|
}
|
|
13666
|
-
function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistState, inspectorPort
|
|
13734
|
+
function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistState, inspectorPort) {
|
|
13667
13735
|
const resolvedViteConfig = vitePreviewServer.config;
|
|
13668
13736
|
const workers = workerConfigs.flatMap((config) => {
|
|
13669
13737
|
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(config);
|
|
@@ -13682,7 +13750,7 @@ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistSta
|
|
|
13682
13750
|
const logger = new ViteMiniflareLogger(resolvedViteConfig);
|
|
13683
13751
|
return {
|
|
13684
13752
|
log: logger,
|
|
13685
|
-
inspectorPort: inspectorPort
|
|
13753
|
+
inspectorPort: inspectorPort === false ? void 0 : inspectorPort,
|
|
13686
13754
|
unsafeInspectorProxy: inspectorPort !== false,
|
|
13687
13755
|
handleRuntimeStdio(stdout, stderr) {
|
|
13688
13756
|
const decoder = new TextDecoder();
|
|
@@ -13695,6 +13763,7 @@ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistSta
|
|
|
13695
13763
|
workers
|
|
13696
13764
|
};
|
|
13697
13765
|
}
|
|
13766
|
+
var removedMessages = [/^Ready on http/, /^Updated and ready on http/];
|
|
13698
13767
|
var ViteMiniflareLogger = class extends Log {
|
|
13699
13768
|
logger;
|
|
13700
13769
|
constructor(config) {
|
|
@@ -13702,8 +13771,10 @@ var ViteMiniflareLogger = class extends Log {
|
|
|
13702
13771
|
this.logger = config.logger;
|
|
13703
13772
|
}
|
|
13704
13773
|
logWithLevel(level, message) {
|
|
13705
|
-
|
|
13706
|
-
|
|
13774
|
+
for (const removedMessage of removedMessages) {
|
|
13775
|
+
if (removedMessage.test(message)) {
|
|
13776
|
+
return;
|
|
13777
|
+
}
|
|
13707
13778
|
}
|
|
13708
13779
|
switch (level) {
|
|
13709
13780
|
case LogLevel.ERROR:
|
|
@@ -13729,13 +13800,14 @@ function miniflareLogLevelFromViteLogLevel(level = "info") {
|
|
|
13729
13800
|
}
|
|
13730
13801
|
|
|
13731
13802
|
// src/plugin-config.ts
|
|
13732
|
-
import
|
|
13733
|
-
import * as
|
|
13734
|
-
import * as
|
|
13803
|
+
import assert8 from "node:assert";
|
|
13804
|
+
import * as path8 from "node:path";
|
|
13805
|
+
import * as vite6 from "vite";
|
|
13735
13806
|
|
|
13736
13807
|
// src/workers-configs.ts
|
|
13808
|
+
import assert7 from "node:assert";
|
|
13737
13809
|
import * as fs4 from "node:fs";
|
|
13738
|
-
import * as
|
|
13810
|
+
import * as path7 from "node:path";
|
|
13739
13811
|
import { unstable_readConfig as unstable_readConfig2 } from "wrangler";
|
|
13740
13812
|
var nonApplicableWorkerConfigs = {
|
|
13741
13813
|
/**
|
|
@@ -13767,8 +13839,7 @@ var nonApplicableWorkerConfigs = {
|
|
|
13767
13839
|
"preserve_file_names",
|
|
13768
13840
|
"rules",
|
|
13769
13841
|
"site",
|
|
13770
|
-
"tsconfig"
|
|
13771
|
-
"upload_source_maps"
|
|
13842
|
+
"tsconfig"
|
|
13772
13843
|
]
|
|
13773
13844
|
};
|
|
13774
13845
|
var nullableNonApplicable = [
|
|
@@ -13779,8 +13850,7 @@ var nullableNonApplicable = [
|
|
|
13779
13850
|
"no_bundle",
|
|
13780
13851
|
"preserve_file_names",
|
|
13781
13852
|
"site",
|
|
13782
|
-
"tsconfig"
|
|
13783
|
-
"upload_source_maps"
|
|
13853
|
+
"tsconfig"
|
|
13784
13854
|
];
|
|
13785
13855
|
function readWorkerConfig(configPath, env2) {
|
|
13786
13856
|
const nonApplicable = {
|
|
@@ -13829,7 +13899,7 @@ function getWarningForWorkersConfigs(configs) {
|
|
|
13829
13899
|
const lines2 = [
|
|
13830
13900
|
`
|
|
13831
13901
|
|
|
13832
|
-
\x1B[43mWARNING\x1B[0m: your worker config${configs.entryWorker.config.configPath ? ` (at \`${
|
|
13902
|
+
\x1B[43mWARNING\x1B[0m: your worker config${configs.entryWorker.config.configPath ? ` (at \`${path7.relative("", configs.entryWorker.config.configPath)}\`)` : ""} contains the following configuration options which are ignored since they are not applicable when using Vite:`
|
|
13833
13903
|
];
|
|
13834
13904
|
nonApplicableLines.forEach((line) => lines2.push(line));
|
|
13835
13905
|
lines2.push("");
|
|
@@ -13843,7 +13913,7 @@ function getWarningForWorkersConfigs(configs) {
|
|
|
13843
13913
|
);
|
|
13844
13914
|
if (nonApplicableLines.length > 0) {
|
|
13845
13915
|
lines.push(
|
|
13846
|
-
` - (${isEntryWorker ? "entry" : "auxiliary"}) worker${workerConfig.config.name ? ` "${workerConfig.config.name}"` : ""}${workerConfig.config.configPath ? ` (config at \`${
|
|
13916
|
+
` - (${isEntryWorker ? "entry" : "auxiliary"}) worker${workerConfig.config.name ? ` "${workerConfig.config.name}"` : ""}${workerConfig.config.configPath ? ` (config at \`${path7.relative("", workerConfig.config.configPath)}\`)` : ""}`
|
|
13847
13917
|
);
|
|
13848
13918
|
nonApplicableLines.forEach((line) => lines.push(line));
|
|
13849
13919
|
}
|
|
@@ -13942,14 +14012,52 @@ function getWorkerConfig(configPath, env2, opts) {
|
|
|
13942
14012
|
nonApplicable
|
|
13943
14013
|
};
|
|
13944
14014
|
}
|
|
14015
|
+
function getValidatedWranglerConfigPath(root, requestedConfigPath, isForAuxiliaryWorker = false) {
|
|
14016
|
+
if (requestedConfigPath) {
|
|
14017
|
+
const configPath2 = path7.resolve(root, requestedConfigPath);
|
|
14018
|
+
const forAuxiliaryWorkerErrorMessage = isForAuxiliaryWorker ? " requested for one of your auxiliary workers" : "";
|
|
14019
|
+
const errorMessagePrefix = `The provided configPath (${configPath2})${forAuxiliaryWorkerErrorMessage}`;
|
|
14020
|
+
const fileExtension = path7.extname(configPath2).slice(1);
|
|
14021
|
+
if (!allowedWranglerConfigExtensions.includes(fileExtension)) {
|
|
14022
|
+
const foundExtensionMessage = !fileExtension ? "no extension found" : `"${fileExtension}" found`;
|
|
14023
|
+
throw new Error(
|
|
14024
|
+
`${errorMessagePrefix} doesn't point to a file with the correct file extension. It should point to a jsonc, json or toml file (${foundExtensionMessage} instead)`
|
|
14025
|
+
);
|
|
14026
|
+
}
|
|
14027
|
+
const mainStat = fs4.statSync(configPath2, { throwIfNoEntry: false });
|
|
14028
|
+
if (!mainStat) {
|
|
14029
|
+
throw new Error(
|
|
14030
|
+
`${errorMessagePrefix} doesn't point to an existing file`
|
|
14031
|
+
);
|
|
14032
|
+
}
|
|
14033
|
+
if (mainStat.isDirectory()) {
|
|
14034
|
+
throw new Error(
|
|
14035
|
+
`${errorMessagePrefix} points to a directory. It should point to a file.`
|
|
14036
|
+
);
|
|
14037
|
+
}
|
|
14038
|
+
return configPath2;
|
|
14039
|
+
}
|
|
14040
|
+
assert7(
|
|
14041
|
+
isForAuxiliaryWorker === false,
|
|
14042
|
+
"Unexpected Error: trying to find the wrangler config for an auxiliary worker"
|
|
14043
|
+
);
|
|
14044
|
+
const configPath = findWranglerConfig(root);
|
|
14045
|
+
if (!configPath) {
|
|
14046
|
+
throw new Error(
|
|
14047
|
+
`No config file found in the ${root} directory. Please add a wrangler.(jsonc|json|toml) file.`
|
|
14048
|
+
);
|
|
14049
|
+
}
|
|
14050
|
+
return configPath;
|
|
14051
|
+
}
|
|
13945
14052
|
function findWranglerConfig(root) {
|
|
13946
|
-
for (const extension of
|
|
13947
|
-
const configPath =
|
|
14053
|
+
for (const extension of allowedWranglerConfigExtensions) {
|
|
14054
|
+
const configPath = path7.join(root, `wrangler.${extension}`);
|
|
13948
14055
|
if (fs4.existsSync(configPath)) {
|
|
13949
14056
|
return configPath;
|
|
13950
14057
|
}
|
|
13951
14058
|
}
|
|
13952
14059
|
}
|
|
14060
|
+
var allowedWranglerConfigExtensions = ["jsonc", "json", "toml"];
|
|
13953
14061
|
|
|
13954
14062
|
// src/plugin-config.ts
|
|
13955
14063
|
function workerNameToEnvironmentName(workerName) {
|
|
@@ -13958,31 +14066,31 @@ function workerNameToEnvironmentName(workerName) {
|
|
|
13958
14066
|
function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
13959
14067
|
const configPaths = /* @__PURE__ */ new Set();
|
|
13960
14068
|
const persistState = pluginConfig.persistState ?? true;
|
|
13961
|
-
const inspectorPort = pluginConfig.inspectorPort ?? DEFAULT_INSPECTOR_PORT;
|
|
13962
14069
|
const experimental = pluginConfig.experimental ?? {};
|
|
13963
|
-
const root = userConfig.root ?
|
|
13964
|
-
const { CLOUDFLARE_ENV: cloudflareEnv } =
|
|
14070
|
+
const root = userConfig.root ? path8.resolve(userConfig.root) : process.cwd();
|
|
14071
|
+
const { CLOUDFLARE_ENV: cloudflareEnv } = vite6.loadEnv(
|
|
13965
14072
|
viteEnv.mode,
|
|
13966
14073
|
root,
|
|
13967
14074
|
/* prefixes */
|
|
13968
14075
|
""
|
|
13969
14076
|
);
|
|
13970
|
-
const
|
|
13971
|
-
|
|
13972
|
-
|
|
13973
|
-
|
|
13974
|
-
|
|
13975
|
-
|
|
13976
|
-
|
|
13977
|
-
|
|
13978
|
-
|
|
13979
|
-
|
|
14077
|
+
const entryWorkerConfigPath = getValidatedWranglerConfigPath(
|
|
14078
|
+
root,
|
|
14079
|
+
pluginConfig.configPath
|
|
14080
|
+
);
|
|
14081
|
+
const entryWorkerResolvedConfig = getWorkerConfig(
|
|
14082
|
+
entryWorkerConfigPath,
|
|
14083
|
+
cloudflareEnv,
|
|
14084
|
+
{
|
|
14085
|
+
visitedConfigPaths: configPaths,
|
|
14086
|
+
isEntryWorker: true
|
|
14087
|
+
}
|
|
14088
|
+
);
|
|
13980
14089
|
if (entryWorkerResolvedConfig.type === "assets-only") {
|
|
13981
14090
|
return {
|
|
13982
14091
|
type: "assets-only",
|
|
13983
14092
|
config: entryWorkerResolvedConfig.config,
|
|
13984
14093
|
configPaths,
|
|
13985
|
-
inspectorPort,
|
|
13986
14094
|
persistState,
|
|
13987
14095
|
rawConfigs: {
|
|
13988
14096
|
entryWorker: entryWorkerResolvedConfig
|
|
@@ -13998,15 +14106,20 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
13998
14106
|
};
|
|
13999
14107
|
const auxiliaryWorkersResolvedConfigs = [];
|
|
14000
14108
|
for (const auxiliaryWorker of pluginConfig.auxiliaryWorkers ?? []) {
|
|
14109
|
+
const workerConfigPath = getValidatedWranglerConfigPath(
|
|
14110
|
+
root,
|
|
14111
|
+
auxiliaryWorker.configPath,
|
|
14112
|
+
true
|
|
14113
|
+
);
|
|
14001
14114
|
const workerResolvedConfig = getWorkerConfig(
|
|
14002
|
-
|
|
14115
|
+
workerConfigPath,
|
|
14003
14116
|
cloudflareEnv,
|
|
14004
14117
|
{
|
|
14005
14118
|
visitedConfigPaths: configPaths
|
|
14006
14119
|
}
|
|
14007
14120
|
);
|
|
14008
14121
|
auxiliaryWorkersResolvedConfigs.push(workerResolvedConfig);
|
|
14009
|
-
|
|
14122
|
+
assert8(
|
|
14010
14123
|
workerResolvedConfig.type === "worker",
|
|
14011
14124
|
"Unexpected error: received AssetsOnlyResult with auxiliary workers."
|
|
14012
14125
|
);
|
|
@@ -14023,7 +14136,6 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
14023
14136
|
type: "workers",
|
|
14024
14137
|
configPaths,
|
|
14025
14138
|
persistState,
|
|
14026
|
-
inspectorPort,
|
|
14027
14139
|
workers,
|
|
14028
14140
|
entryWorkerEnvironmentName,
|
|
14029
14141
|
rawConfigs: {
|
|
@@ -14038,7 +14150,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
14038
14150
|
// src/websockets.ts
|
|
14039
14151
|
import { coupleWebSocket } from "miniflare";
|
|
14040
14152
|
import { WebSocketServer } from "ws";
|
|
14041
|
-
function handleWebSocket(httpServer,
|
|
14153
|
+
function handleWebSocket(httpServer, getFetcher) {
|
|
14042
14154
|
const nodeWebSocket = new WebSocketServer({ noServer: true });
|
|
14043
14155
|
httpServer.on(
|
|
14044
14156
|
"upgrade",
|
|
@@ -14048,6 +14160,7 @@ function handleWebSocket(httpServer, fetcher) {
|
|
|
14048
14160
|
return;
|
|
14049
14161
|
}
|
|
14050
14162
|
const headers = nodeHeadersToWebHeaders(request.headers);
|
|
14163
|
+
const fetcher = await getFetcher();
|
|
14051
14164
|
const response = await fetcher(url, {
|
|
14052
14165
|
headers,
|
|
14053
14166
|
method: request.method
|
|
@@ -14070,13 +14183,65 @@ function handleWebSocket(httpServer, fetcher) {
|
|
|
14070
14183
|
);
|
|
14071
14184
|
}
|
|
14072
14185
|
|
|
14186
|
+
// src/worker-environments-validation.ts
|
|
14187
|
+
import assert9 from "node:assert";
|
|
14188
|
+
function validateWorkerEnvironmentsResolvedConfigs(resolvedPluginConfig, resolvedViteConfig) {
|
|
14189
|
+
const workersEnvironmentNames = Object.keys(resolvedPluginConfig.workers);
|
|
14190
|
+
const disallowedEnvsConfigs = /* @__PURE__ */ new Map();
|
|
14191
|
+
for (const envName of workersEnvironmentNames) {
|
|
14192
|
+
const workerEnvConfig = resolvedViteConfig.environments[envName];
|
|
14193
|
+
assert9(workerEnvConfig, `Missing environment config for "${envName}"`);
|
|
14194
|
+
const { optimizeDeps, resolve: resolve7 } = workerEnvConfig;
|
|
14195
|
+
const disallowedConfig = {};
|
|
14196
|
+
const disallowedOptimizeDepsExcludeEntries = (optimizeDeps.exclude ?? []).filter((entry) => {
|
|
14197
|
+
if (cloudflareBuiltInModules.includes(entry)) {
|
|
14198
|
+
return false;
|
|
14199
|
+
}
|
|
14200
|
+
if (isNodeAlsModule(entry) && isNodeAls(resolvedPluginConfig.workers[envName])) {
|
|
14201
|
+
return false;
|
|
14202
|
+
}
|
|
14203
|
+
if (NODEJS_MODULES_RE.test(entry) && isNodeCompat(resolvedPluginConfig.workers[envName])) {
|
|
14204
|
+
return false;
|
|
14205
|
+
}
|
|
14206
|
+
return true;
|
|
14207
|
+
});
|
|
14208
|
+
if (disallowedOptimizeDepsExcludeEntries.length > 0) {
|
|
14209
|
+
disallowedConfig.optimizeDepsExclude = disallowedOptimizeDepsExcludeEntries;
|
|
14210
|
+
}
|
|
14211
|
+
if (resolve7.external === true || resolve7.external.length > 0) {
|
|
14212
|
+
disallowedConfig.resolveExternal = resolve7.external;
|
|
14213
|
+
}
|
|
14214
|
+
if (Object.keys(disallowedConfig).length > 0) {
|
|
14215
|
+
disallowedEnvsConfigs.set(envName, disallowedConfig);
|
|
14216
|
+
}
|
|
14217
|
+
}
|
|
14218
|
+
if (disallowedEnvsConfigs.size > 0) {
|
|
14219
|
+
const errorMessage = `The following environment configurations are incompatible with the Cloudflare Vite plugin:
|
|
14220
|
+
${[
|
|
14221
|
+
...disallowedEnvsConfigs
|
|
14222
|
+
].map(
|
|
14223
|
+
([envName, disallowedConfig]) => [
|
|
14224
|
+
disallowedConfig.optimizeDepsExclude ? ` - "${envName}" environment: \`optimizeDeps.exclude\`: ${JSON.stringify(disallowedConfig.optimizeDepsExclude)}
|
|
14225
|
+
` : null,
|
|
14226
|
+
disallowedConfig.resolveExternal ? ` - "${envName}" environment: \`resolve.external\`: ${JSON.stringify(disallowedConfig.resolveExternal)}
|
|
14227
|
+
` : null
|
|
14228
|
+
].join("")
|
|
14229
|
+
).join(
|
|
14230
|
+
""
|
|
14231
|
+
)}To resolve this issue, avoid setting \`optimizeDeps.exclude\` and \`resolve.external\` in your Cloudflare Worker environments.
|
|
14232
|
+
`;
|
|
14233
|
+
throw new Error(errorMessage);
|
|
14234
|
+
}
|
|
14235
|
+
}
|
|
14236
|
+
|
|
14073
14237
|
// src/index.ts
|
|
14074
14238
|
var workersConfigsWarningShown = false;
|
|
14239
|
+
var miniflare;
|
|
14075
14240
|
function cloudflare2(pluginConfig = {}) {
|
|
14076
14241
|
let resolvedPluginConfig;
|
|
14077
14242
|
let resolvedViteConfig;
|
|
14078
|
-
let miniflare;
|
|
14079
14243
|
const additionalModulePaths = /* @__PURE__ */ new Set();
|
|
14244
|
+
const nodeJsCompatWarningsMap = /* @__PURE__ */ new Map();
|
|
14080
14245
|
let hasClientBuild = false;
|
|
14081
14246
|
return [
|
|
14082
14247
|
{
|
|
@@ -14127,7 +14292,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14127
14292
|
builder: {
|
|
14128
14293
|
buildApp: userConfig.builder?.buildApp ?? (async (builder) => {
|
|
14129
14294
|
const clientEnvironment = builder.environments.client;
|
|
14130
|
-
const defaultHtmlPath =
|
|
14295
|
+
const defaultHtmlPath = path9.resolve(
|
|
14131
14296
|
builder.config.root,
|
|
14132
14297
|
"index.html"
|
|
14133
14298
|
);
|
|
@@ -14139,7 +14304,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14139
14304
|
resolvedPluginConfig.workers
|
|
14140
14305
|
).map((environmentName) => {
|
|
14141
14306
|
const environment = builder.environments[environmentName];
|
|
14142
|
-
|
|
14307
|
+
assert10(
|
|
14143
14308
|
environment,
|
|
14144
14309
|
`${environmentName} environment not found`
|
|
14145
14310
|
);
|
|
@@ -14160,6 +14325,12 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14160
14325
|
},
|
|
14161
14326
|
configResolved(config) {
|
|
14162
14327
|
resolvedViteConfig = config;
|
|
14328
|
+
if (resolvedPluginConfig?.type === "workers") {
|
|
14329
|
+
validateWorkerEnvironmentsResolvedConfigs(
|
|
14330
|
+
resolvedPluginConfig,
|
|
14331
|
+
resolvedViteConfig
|
|
14332
|
+
);
|
|
14333
|
+
}
|
|
14163
14334
|
},
|
|
14164
14335
|
generateBundle(_, bundle) {
|
|
14165
14336
|
let config;
|
|
@@ -14180,15 +14351,15 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14180
14351
|
if (isEntryWorker && hasClientBuild) {
|
|
14181
14352
|
const workerOutputDirectory = this.environment.config.build.outDir;
|
|
14182
14353
|
const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
|
|
14183
|
-
|
|
14354
|
+
assert10(
|
|
14184
14355
|
clientOutputDirectory,
|
|
14185
14356
|
"Unexpected error: client output directory is undefined"
|
|
14186
14357
|
);
|
|
14187
14358
|
workerConfig.assets = {
|
|
14188
14359
|
...workerConfig.assets,
|
|
14189
|
-
directory:
|
|
14190
|
-
|
|
14191
|
-
|
|
14360
|
+
directory: path9.relative(
|
|
14361
|
+
path9.resolve(resolvedViteConfig.root, workerOutputDirectory),
|
|
14362
|
+
path9.resolve(resolvedViteConfig.root, clientOutputDirectory)
|
|
14192
14363
|
)
|
|
14193
14364
|
};
|
|
14194
14365
|
} else {
|
|
@@ -14243,54 +14414,77 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14243
14414
|
writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
|
|
14244
14415
|
}
|
|
14245
14416
|
},
|
|
14246
|
-
|
|
14247
|
-
|
|
14417
|
+
hotUpdate(options) {
|
|
14418
|
+
const changedFilePath = path9.resolve(options.file);
|
|
14419
|
+
if (resolvedPluginConfig.configPaths.has(changedFilePath) || hasDotDevDotVarsFileChanged(resolvedPluginConfig, changedFilePath) || hasAssetsConfigChanged(
|
|
14248
14420
|
resolvedPluginConfig,
|
|
14249
14421
|
resolvedViteConfig,
|
|
14250
|
-
|
|
14422
|
+
changedFilePath
|
|
14251
14423
|
)) {
|
|
14252
14424
|
options.server.restart();
|
|
14253
|
-
|
|
14254
|
-
},
|
|
14255
|
-
async buildEnd() {
|
|
14256
|
-
if (miniflare) {
|
|
14257
|
-
await miniflare.dispose();
|
|
14258
|
-
miniflare = void 0;
|
|
14425
|
+
return [];
|
|
14259
14426
|
}
|
|
14260
14427
|
},
|
|
14261
14428
|
async configureServer(viteDevServer) {
|
|
14262
|
-
|
|
14429
|
+
assert10(
|
|
14263
14430
|
viteDevServer.httpServer,
|
|
14264
14431
|
"Unexpected error: No Vite HTTP server"
|
|
14265
14432
|
);
|
|
14266
|
-
|
|
14267
|
-
|
|
14433
|
+
const inputInspectorPort = await getInputInspectorPortOption(
|
|
14434
|
+
pluginConfig,
|
|
14435
|
+
viteDevServer
|
|
14268
14436
|
);
|
|
14437
|
+
if (!miniflare) {
|
|
14438
|
+
miniflare = new Miniflare(
|
|
14439
|
+
getDevMiniflareOptions(
|
|
14440
|
+
resolvedPluginConfig,
|
|
14441
|
+
viteDevServer,
|
|
14442
|
+
inputInspectorPort
|
|
14443
|
+
)
|
|
14444
|
+
);
|
|
14445
|
+
} else {
|
|
14446
|
+
await miniflare.setOptions(
|
|
14447
|
+
getDevMiniflareOptions(
|
|
14448
|
+
resolvedPluginConfig,
|
|
14449
|
+
viteDevServer,
|
|
14450
|
+
inputInspectorPort
|
|
14451
|
+
)
|
|
14452
|
+
);
|
|
14453
|
+
}
|
|
14269
14454
|
await initRunners(resolvedPluginConfig, viteDevServer, miniflare);
|
|
14270
|
-
const routerWorker = await getRouterWorker(miniflare);
|
|
14271
14455
|
const middleware = createMiddleware(
|
|
14272
|
-
({ request }) => {
|
|
14456
|
+
async ({ request }) => {
|
|
14457
|
+
assert10(miniflare, `Miniflare not defined`);
|
|
14458
|
+
const routerWorker = await getRouterWorker(miniflare);
|
|
14273
14459
|
return routerWorker.fetch(toMiniflareRequest(request), {
|
|
14274
14460
|
redirect: "manual"
|
|
14275
14461
|
});
|
|
14276
14462
|
},
|
|
14277
14463
|
{ alwaysCallNext: false }
|
|
14278
14464
|
);
|
|
14279
|
-
handleWebSocket(viteDevServer.httpServer,
|
|
14465
|
+
handleWebSocket(viteDevServer.httpServer, async () => {
|
|
14466
|
+
assert10(miniflare, `Miniflare not defined`);
|
|
14467
|
+
const routerWorker = await getRouterWorker(miniflare);
|
|
14468
|
+
return routerWorker.fetch;
|
|
14469
|
+
});
|
|
14280
14470
|
return () => {
|
|
14281
14471
|
viteDevServer.middlewares.use((req, res, next) => {
|
|
14282
14472
|
middleware(req, res, next);
|
|
14283
14473
|
});
|
|
14284
14474
|
};
|
|
14285
14475
|
},
|
|
14286
|
-
configurePreviewServer(vitePreviewServer) {
|
|
14476
|
+
async configurePreviewServer(vitePreviewServer) {
|
|
14287
14477
|
const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
|
|
14478
|
+
const inputInspectorPort = await getInputInspectorPortOption(
|
|
14479
|
+
pluginConfig,
|
|
14480
|
+
vitePreviewServer
|
|
14481
|
+
);
|
|
14288
14482
|
const miniflare2 = new Miniflare(
|
|
14289
14483
|
getPreviewMiniflareOptions(
|
|
14290
14484
|
vitePreviewServer,
|
|
14291
14485
|
workerConfigs,
|
|
14292
14486
|
pluginConfig.persistState ?? true,
|
|
14293
|
-
|
|
14487
|
+
inputInspectorPort
|
|
14294
14488
|
)
|
|
14295
14489
|
);
|
|
14296
14490
|
const middleware = createMiddleware(
|
|
@@ -14301,7 +14495,10 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14301
14495
|
},
|
|
14302
14496
|
{ alwaysCallNext: false }
|
|
14303
14497
|
);
|
|
14304
|
-
handleWebSocket(
|
|
14498
|
+
handleWebSocket(
|
|
14499
|
+
vitePreviewServer.httpServer,
|
|
14500
|
+
() => miniflare2.dispatchFetch
|
|
14501
|
+
);
|
|
14305
14502
|
vitePreviewServer.middlewares.use((req, res, next) => {
|
|
14306
14503
|
middleware(req, res, next);
|
|
14307
14504
|
});
|
|
@@ -14357,6 +14554,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14357
14554
|
hotUpdate(options) {
|
|
14358
14555
|
if (additionalModulePaths.has(options.file)) {
|
|
14359
14556
|
options.server.restart();
|
|
14557
|
+
return [];
|
|
14360
14558
|
}
|
|
14361
14559
|
},
|
|
14362
14560
|
async renderChunk(code, chunk) {
|
|
@@ -14365,7 +14563,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14365
14563
|
for (const match of matches) {
|
|
14366
14564
|
magicString ??= new MagicString(code);
|
|
14367
14565
|
const [full, _, modulePath] = match;
|
|
14368
|
-
|
|
14566
|
+
assert10(
|
|
14369
14567
|
modulePath,
|
|
14370
14568
|
`Unexpected error: module path not found in reference ${full}.`
|
|
14371
14569
|
);
|
|
@@ -14379,13 +14577,13 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14379
14577
|
}
|
|
14380
14578
|
const referenceId = this.emitFile({
|
|
14381
14579
|
type: "asset",
|
|
14382
|
-
name:
|
|
14580
|
+
name: path9.basename(modulePath),
|
|
14383
14581
|
originalFileName: modulePath,
|
|
14384
14582
|
source
|
|
14385
14583
|
});
|
|
14386
14584
|
const emittedFileName = this.getFileName(referenceId);
|
|
14387
|
-
const relativePath =
|
|
14388
|
-
|
|
14585
|
+
const relativePath = vite7.normalizePath(
|
|
14586
|
+
path9.relative(path9.dirname(chunk.fileName), emittedFileName)
|
|
14389
14587
|
);
|
|
14390
14588
|
const importPath = relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
14391
14589
|
magicString.update(
|
|
@@ -14411,26 +14609,28 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14411
14609
|
configEnvironment(name) {
|
|
14412
14610
|
if (isNodeCompat(getWorkerConfig2(name))) {
|
|
14413
14611
|
return {
|
|
14612
|
+
build: {
|
|
14613
|
+
rollupOptions: {
|
|
14614
|
+
plugins: [
|
|
14615
|
+
replace({
|
|
14616
|
+
"process.env.NODE_ENV": JSON.stringify(
|
|
14617
|
+
process.env.NODE_ENV ?? "production"
|
|
14618
|
+
),
|
|
14619
|
+
preventAssignment: true
|
|
14620
|
+
})
|
|
14621
|
+
]
|
|
14622
|
+
}
|
|
14623
|
+
},
|
|
14414
14624
|
resolve: {
|
|
14415
14625
|
builtins: [...nodeCompatExternals]
|
|
14416
14626
|
},
|
|
14417
14627
|
optimizeDeps: {
|
|
14418
|
-
// This is a list of dependency entry-points that should be pre-bundled.
|
|
14419
|
-
// In this case we provide a list of all the possible polyfills so that they are pre-bundled,
|
|
14420
|
-
// ready ahead the first request to the dev server.
|
|
14421
|
-
// Without this the dependency optimizer will try to bundle them on-the-fly in the middle of the first request,
|
|
14422
|
-
// which can potentially cause problems if it leads to previous pre-bundling to become stale and needing to be reloaded.
|
|
14423
|
-
// TODO: work out how to re-enable pre-bundling of these
|
|
14424
|
-
// include: [...getNodeCompatEntries()],
|
|
14425
14628
|
// This is a list of module specifiers that the dependency optimizer should not follow when doing import analysis.
|
|
14426
14629
|
// 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.
|
|
14427
14630
|
// Obviously we don't want/need the optimizer to try to process modules that are built-in;
|
|
14428
14631
|
// But also we want to avoid following the ones that are polyfilled since the dependency-optimizer import analyzer does not
|
|
14429
14632
|
// resolve these imports using our `resolveId()` hook causing the optimization step to fail.
|
|
14430
|
-
exclude: [
|
|
14431
|
-
...builtinModules2,
|
|
14432
|
-
...builtinModules2.map((m) => `node:${m}`)
|
|
14433
|
-
]
|
|
14633
|
+
exclude: [...nodejsBuiltins]
|
|
14434
14634
|
}
|
|
14435
14635
|
};
|
|
14436
14636
|
}
|
|
@@ -14448,7 +14648,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14448
14648
|
return this.resolve(source, importer, options);
|
|
14449
14649
|
}
|
|
14450
14650
|
if (this.environment.mode === "dev") {
|
|
14451
|
-
|
|
14651
|
+
assert10(
|
|
14452
14652
|
this.environment.depsOptimizer,
|
|
14453
14653
|
"depsOptimizer is required in dev mode"
|
|
14454
14654
|
);
|
|
@@ -14462,11 +14662,54 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14462
14662
|
},
|
|
14463
14663
|
async transform(code, id) {
|
|
14464
14664
|
const workerConfig = getWorkerConfig2(this.environment.name);
|
|
14465
|
-
|
|
14665
|
+
if (!workerConfig) {
|
|
14666
|
+
return;
|
|
14667
|
+
}
|
|
14466
14668
|
const resolvedId = await this.resolve(workerConfig.main);
|
|
14467
14669
|
if (id === resolvedId?.id) {
|
|
14468
14670
|
return injectGlobalCode(id, code);
|
|
14469
14671
|
}
|
|
14672
|
+
},
|
|
14673
|
+
async configureServer(viteDevServer) {
|
|
14674
|
+
await Promise.all(
|
|
14675
|
+
Object.values(viteDevServer.environments).flatMap(
|
|
14676
|
+
async (environment) => {
|
|
14677
|
+
const workerConfig = getWorkerConfig2(environment.name);
|
|
14678
|
+
if (isNodeCompat(workerConfig)) {
|
|
14679
|
+
await environment.depsOptimizer?.init();
|
|
14680
|
+
return Array.from(nodeCompatEntries).map((entry) => {
|
|
14681
|
+
const result = resolveNodeJSImport(entry);
|
|
14682
|
+
if (result) {
|
|
14683
|
+
const registration = environment.depsOptimizer?.registerMissingImport(
|
|
14684
|
+
result.unresolved,
|
|
14685
|
+
result.resolved
|
|
14686
|
+
);
|
|
14687
|
+
return registration?.processing;
|
|
14688
|
+
}
|
|
14689
|
+
});
|
|
14690
|
+
}
|
|
14691
|
+
}
|
|
14692
|
+
)
|
|
14693
|
+
);
|
|
14694
|
+
}
|
|
14695
|
+
},
|
|
14696
|
+
// Plugin that handles Node.js Async Local Storage (ALS) compatibility support for Vite Environments that are hosted in Cloudflare Workers.
|
|
14697
|
+
{
|
|
14698
|
+
name: "vite-plugin-cloudflare:nodejs-als",
|
|
14699
|
+
apply(_config, env2) {
|
|
14700
|
+
return !env2.isPreview;
|
|
14701
|
+
},
|
|
14702
|
+
configEnvironment(name, config) {
|
|
14703
|
+
if (isNodeAls(getWorkerConfig2(name))) {
|
|
14704
|
+
return {
|
|
14705
|
+
resolve: {
|
|
14706
|
+
builtins: ["async_hooks", "node:async_hooks"]
|
|
14707
|
+
},
|
|
14708
|
+
optimizeDeps: {
|
|
14709
|
+
exclude: ["async_hooks", "node:async_hooks"]
|
|
14710
|
+
}
|
|
14711
|
+
};
|
|
14712
|
+
}
|
|
14470
14713
|
}
|
|
14471
14714
|
},
|
|
14472
14715
|
// Plugin that provides an __debug path for debugging the Cloudflare Workers.
|
|
@@ -14476,54 +14719,143 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
14476
14719
|
// the preview middleware here can take precedence
|
|
14477
14720
|
enforce: "pre",
|
|
14478
14721
|
configureServer(viteDevServer) {
|
|
14479
|
-
if (resolvedPluginConfig.type === "workers" &&
|
|
14722
|
+
if (resolvedPluginConfig.type === "workers" && pluginConfig.inspectorPort !== false) {
|
|
14480
14723
|
addDebugToVitePrintUrls(viteDevServer);
|
|
14481
14724
|
}
|
|
14482
14725
|
const workerNames = resolvedPluginConfig.type === "assets-only" ? [] : Object.values(resolvedPluginConfig.workers).map(
|
|
14483
14726
|
(worker) => worker.name
|
|
14484
14727
|
);
|
|
14485
|
-
viteDevServer.middlewares.use((req, res, next) => {
|
|
14486
|
-
|
|
14487
|
-
|
|
14488
|
-
|
|
14489
|
-
resolvedPluginConfig.inspectorPort
|
|
14490
|
-
);
|
|
14728
|
+
viteDevServer.middlewares.use(async (req, res, next) => {
|
|
14729
|
+
const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
|
|
14730
|
+
if (req.url === debuggingPath && resolvedInspectorPort) {
|
|
14731
|
+
const html = getDebugPathHtml(workerNames, resolvedInspectorPort);
|
|
14491
14732
|
res.setHeader("Content-Type", "text/html");
|
|
14492
14733
|
return res.end(html);
|
|
14493
14734
|
}
|
|
14494
14735
|
next();
|
|
14495
14736
|
});
|
|
14496
14737
|
},
|
|
14497
|
-
configurePreviewServer(vitePreviewServer) {
|
|
14738
|
+
async configurePreviewServer(vitePreviewServer) {
|
|
14498
14739
|
const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
|
|
14499
14740
|
if (workerConfigs.length >= 1 && pluginConfig.inspectorPort !== false) {
|
|
14500
14741
|
addDebugToVitePrintUrls(vitePreviewServer);
|
|
14501
14742
|
}
|
|
14502
14743
|
const workerNames = workerConfigs.map((worker) => {
|
|
14503
|
-
|
|
14744
|
+
assert10(worker.name, "Expected the Worker to have a name");
|
|
14504
14745
|
return worker.name;
|
|
14505
14746
|
});
|
|
14506
|
-
vitePreviewServer.middlewares.use((req, res, next) => {
|
|
14507
|
-
|
|
14508
|
-
|
|
14509
|
-
|
|
14510
|
-
pluginConfig.inspectorPort ?? DEFAULT_INSPECTOR_PORT
|
|
14511
|
-
);
|
|
14747
|
+
vitePreviewServer.middlewares.use(async (req, res, next) => {
|
|
14748
|
+
const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
|
|
14749
|
+
if (req.url === debuggingPath && resolvedInspectorPort) {
|
|
14750
|
+
const html = getDebugPathHtml(workerNames, resolvedInspectorPort);
|
|
14512
14751
|
res.setHeader("Content-Type", "text/html");
|
|
14513
14752
|
return res.end(html);
|
|
14514
14753
|
}
|
|
14515
14754
|
next();
|
|
14516
14755
|
});
|
|
14517
14756
|
}
|
|
14757
|
+
},
|
|
14758
|
+
// Plugin to warn if Node.js APIs are being used without nodejs_compat turned on
|
|
14759
|
+
{
|
|
14760
|
+
name: "vite-plugin-cloudflare:nodejs-compat-warnings",
|
|
14761
|
+
apply(_config, env2) {
|
|
14762
|
+
return !env2.isPreview;
|
|
14763
|
+
},
|
|
14764
|
+
// We must ensure that the `resolveId` hook runs before the built-in ones.
|
|
14765
|
+
// Otherwise we never see the Node.js built-in imports since they get handled by default Vite behavior.
|
|
14766
|
+
enforce: "pre",
|
|
14767
|
+
configEnvironment(environmentName) {
|
|
14768
|
+
const workerConfig = getWorkerConfig2(environmentName);
|
|
14769
|
+
if (workerConfig && !isNodeCompat(workerConfig)) {
|
|
14770
|
+
return {
|
|
14771
|
+
optimizeDeps: {
|
|
14772
|
+
esbuildOptions: {
|
|
14773
|
+
plugins: [
|
|
14774
|
+
{
|
|
14775
|
+
name: "vite-plugin-cloudflare:nodejs-compat-warnings-resolver",
|
|
14776
|
+
setup(build) {
|
|
14777
|
+
build.onResolve(
|
|
14778
|
+
{ filter: NODEJS_MODULES_RE },
|
|
14779
|
+
({ path: path10, importer }) => {
|
|
14780
|
+
if (isNodeAls(workerConfig) && isNodeAlsModule(path10)) {
|
|
14781
|
+
return;
|
|
14782
|
+
}
|
|
14783
|
+
const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
|
|
14784
|
+
nodeJsCompatWarnings?.registerImport(path10, importer);
|
|
14785
|
+
return { path: path10, external: true };
|
|
14786
|
+
}
|
|
14787
|
+
);
|
|
14788
|
+
}
|
|
14789
|
+
}
|
|
14790
|
+
]
|
|
14791
|
+
}
|
|
14792
|
+
}
|
|
14793
|
+
};
|
|
14794
|
+
}
|
|
14795
|
+
},
|
|
14796
|
+
configResolved(resolvedViteConfig2) {
|
|
14797
|
+
for (const environmentName of Object.keys(
|
|
14798
|
+
resolvedViteConfig2.environments
|
|
14799
|
+
)) {
|
|
14800
|
+
const workerConfig = getWorkerConfig2(environmentName);
|
|
14801
|
+
if (workerConfig && !isNodeCompat(workerConfig)) {
|
|
14802
|
+
nodeJsCompatWarningsMap.set(
|
|
14803
|
+
workerConfig,
|
|
14804
|
+
new NodeJsCompatWarnings(environmentName, resolvedViteConfig2)
|
|
14805
|
+
);
|
|
14806
|
+
}
|
|
14807
|
+
}
|
|
14808
|
+
},
|
|
14809
|
+
async resolveId(source, importer) {
|
|
14810
|
+
const workerConfig = getWorkerConfig2(this.environment.name);
|
|
14811
|
+
if (workerConfig && !isNodeCompat(workerConfig)) {
|
|
14812
|
+
if (isNodeAls(workerConfig) && isNodeAlsModule(source)) {
|
|
14813
|
+
return;
|
|
14814
|
+
}
|
|
14815
|
+
const nodeJsCompatWarnings = nodeJsCompatWarningsMap.get(workerConfig);
|
|
14816
|
+
if (nodejsBuiltins.has(source)) {
|
|
14817
|
+
nodeJsCompatWarnings?.registerImport(source, importer);
|
|
14818
|
+
return {
|
|
14819
|
+
id: source,
|
|
14820
|
+
external: true
|
|
14821
|
+
};
|
|
14822
|
+
}
|
|
14823
|
+
}
|
|
14824
|
+
}
|
|
14518
14825
|
}
|
|
14519
14826
|
];
|
|
14520
14827
|
function getWorkerConfig2(environmentName) {
|
|
14521
|
-
|
|
14828
|
+
assert10(resolvedPluginConfig, "Expected resolvedPluginConfig to be defined");
|
|
14522
14829
|
return resolvedPluginConfig.type !== "assets-only" ? resolvedPluginConfig.workers[environmentName] : void 0;
|
|
14523
14830
|
}
|
|
14524
14831
|
}
|
|
14832
|
+
async function getInputInspectorPortOption(pluginConfig, viteServer) {
|
|
14833
|
+
if (pluginConfig.inspectorPort === void 0 || pluginConfig.inspectorPort === 0) {
|
|
14834
|
+
const resolvedInspectorPort = await getResolvedInspectorPort(pluginConfig);
|
|
14835
|
+
if (resolvedInspectorPort !== null) {
|
|
14836
|
+
return resolvedInspectorPort;
|
|
14837
|
+
}
|
|
14838
|
+
}
|
|
14839
|
+
const inputInspectorPort = pluginConfig.inspectorPort ?? await getFirstAvailablePort(DEFAULT_INSPECTOR_PORT);
|
|
14840
|
+
if (pluginConfig.inspectorPort === void 0 && inputInspectorPort !== DEFAULT_INSPECTOR_PORT) {
|
|
14841
|
+
viteServer.config.logger.warn(
|
|
14842
|
+
colors2.dim(
|
|
14843
|
+
`Default inspector port ${DEFAULT_INSPECTOR_PORT} not available, using ${inputInspectorPort} instead
|
|
14844
|
+
`
|
|
14845
|
+
)
|
|
14846
|
+
);
|
|
14847
|
+
}
|
|
14848
|
+
return inputInspectorPort;
|
|
14849
|
+
}
|
|
14850
|
+
async function getResolvedInspectorPort(pluginConfig) {
|
|
14851
|
+
if (miniflare && pluginConfig.inspectorPort !== false) {
|
|
14852
|
+
const miniflareInspectorUrl = await miniflare.getInspectorURL();
|
|
14853
|
+
return Number.parseInt(miniflareInspectorUrl.port);
|
|
14854
|
+
}
|
|
14855
|
+
return null;
|
|
14856
|
+
}
|
|
14525
14857
|
function getDotDevDotVarsContent(configPath, cloudflareEnv) {
|
|
14526
|
-
const configDir =
|
|
14858
|
+
const configDir = path9.dirname(configPath);
|
|
14527
14859
|
const defaultDotDevDotVarsPath = `${configDir}/.dev.vars`;
|
|
14528
14860
|
const inputDotDevDotVarsPath = `${defaultDotDevDotVarsPath}${cloudflareEnv ? `.${cloudflareEnv}` : ""}`;
|
|
14529
14861
|
const targetPath = fs5.existsSync(inputDotDevDotVarsPath) ? inputDotDevDotVarsPath : fs5.existsSync(defaultDotDevDotVarsPath) ? defaultDotDevDotVarsPath : null;
|
|
@@ -14533,6 +14865,19 @@ function getDotDevDotVarsContent(configPath, cloudflareEnv) {
|
|
|
14533
14865
|
}
|
|
14534
14866
|
return null;
|
|
14535
14867
|
}
|
|
14868
|
+
function hasDotDevDotVarsFileChanged(resolvedPluginConfig, changedFilePath) {
|
|
14869
|
+
return [...resolvedPluginConfig.configPaths].some((configPath) => {
|
|
14870
|
+
const dotDevDotVars = path9.join(path9.dirname(configPath), ".dev.vars");
|
|
14871
|
+
if (dotDevDotVars === changedFilePath) {
|
|
14872
|
+
return true;
|
|
14873
|
+
}
|
|
14874
|
+
if (resolvedPluginConfig.cloudflareEnv) {
|
|
14875
|
+
const dotDevDotVarsForEnv = `${dotDevDotVars}.${resolvedPluginConfig.cloudflareEnv}`;
|
|
14876
|
+
return dotDevDotVarsForEnv === changedFilePath;
|
|
14877
|
+
}
|
|
14878
|
+
return false;
|
|
14879
|
+
});
|
|
14880
|
+
}
|
|
14536
14881
|
export {
|
|
14537
14882
|
cloudflare2 as cloudflare
|
|
14538
14883
|
};
|