@cloudflare/vite-plugin 0.0.0-d3c687ba6 → 0.0.0-d42966125
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 +1 -61
- package/dist/index.d.ts +0 -1
- package/dist/index.js +44 -172
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# `@cloudflare/vite-plugin`
|
|
2
2
|
|
|
3
|
-
[Intro](#intro) | [Quick start](#quick-start) | [Tutorial](#tutorial) | [API](#api) | [Cloudflare environments](#cloudflare-environments) | [
|
|
3
|
+
[Intro](#intro) | [Quick start](#quick-start) | [Tutorial](#tutorial) | [API](#api) | [Cloudflare environments](#cloudflare-environments) | [Migrating from `wrangler dev`](#migrating-from-wrangler-dev)
|
|
4
4
|
|
|
5
5
|
## Intro
|
|
6
6
|
|
|
@@ -374,10 +374,6 @@ It accepts an optional `PluginConfig` parameter.
|
|
|
374
374
|
All requests are routed through your entry Worker.
|
|
375
375
|
During the build, each Worker is output to a separate subdirectory of `dist`.
|
|
376
376
|
|
|
377
|
-
- `inspectorPort?: number | false`
|
|
378
|
-
|
|
379
|
-
Optional inspector port to use for debugging your workers, for more details on debugging see the [devtools section](#devtools). Can be set to `false` to disable the debugging inspector altogether. Defaults to `9229`.
|
|
380
|
-
|
|
381
377
|
> [!NOTE]
|
|
382
378
|
> When running `wrangler deploy`, only your main (entry) Worker will be deployed.
|
|
383
379
|
> If using multiple Workers, each must be deployed individually.
|
|
@@ -486,62 +482,6 @@ Secrets can be provided to your Worker in local development using a [`.dev.vars`
|
|
|
486
482
|
> [!NOTE]
|
|
487
483
|
> The `vite build` command copies the relevant `.dev.vars[.env-name]` file to the output directory. This is only used when running `vite preview` and is not deployed with your Worker.
|
|
488
484
|
|
|
489
|
-
## Debugging
|
|
490
|
-
|
|
491
|
-
The Cloudflare Vite plugin allows you to conveniently debug your Worker code during local development.
|
|
492
|
-
|
|
493
|
-
By default the inspector port used by the plugin is `9229`, which can be customized by providing a different port to the plugin's `inspectorPort` option.
|
|
494
|
-
|
|
495
|
-
There are two recommended ways of doing so, which we'll explore in this section.
|
|
496
|
-
|
|
497
|
-
### Devtools
|
|
498
|
-
|
|
499
|
-
When running `vite dev` or `vite preview` a `/__debug` route in your local server will be made available which gives you access to [Cloudflare's implementation](/packages/chrome-devtools-patches) of [Chrome's DevTools](https://developer.chrome.com/docs/devtools/overview).
|
|
500
|
-
|
|
501
|
-
Navigating to this route will open a devtools tab for each of the workers in your application (Note: in case of multiple workers you might need to allow your browser to open pop-ups).
|
|
502
|
-
|
|
503
|
-
Once the tab or tabs are open, you can make a request to your application and start debugging your workers' code.
|
|
504
|
-
|
|
505
|
-
Note: If you're not interested in debugging all your workers you can close the tabs of the workers you don't want to debug.
|
|
506
|
-
|
|
507
|
-
### VS Code
|
|
508
|
-
|
|
509
|
-
To setup VS Code for breakpoint debugging for your application, you will need to create a `.vscode/launch.json` file that contains a configuration following this structure:
|
|
510
|
-
|
|
511
|
-
```json
|
|
512
|
-
{
|
|
513
|
-
"configurations": [
|
|
514
|
-
{
|
|
515
|
-
"name": "<NAME_OF_WORKER>",
|
|
516
|
-
"type": "node",
|
|
517
|
-
"request": "attach",
|
|
518
|
-
"websocketAddress": "ws://localhost:9229/<NAME_OF_WORKER>",
|
|
519
|
-
"resolveSourceMapLocations": null,
|
|
520
|
-
"attachExistingChildren": false,
|
|
521
|
-
"autoAttachChildProcesses": false,
|
|
522
|
-
"sourceMaps": true
|
|
523
|
-
}
|
|
524
|
-
],
|
|
525
|
-
"compounds": [
|
|
526
|
-
{
|
|
527
|
-
"name": "Debug All Workers",
|
|
528
|
-
"configurations": ["<NAME_OF_WORKER>"],
|
|
529
|
-
"stopAll": true
|
|
530
|
-
}
|
|
531
|
-
]
|
|
532
|
-
}
|
|
533
|
-
```
|
|
534
|
-
|
|
535
|
-
Where, `<NAME_OF_WORKER>` indicates the name of your worker as specified in your Wrangler configuration.
|
|
536
|
-
|
|
537
|
-
Note: if you customized your `inspectorPort` you need to use that port in the `websocketAddress` field.
|
|
538
|
-
|
|
539
|
-
If you have more than one worker you need add a configuration in the `configurations` field for each one and then include the configuration name in the `configurations` array in the compound configuration.
|
|
540
|
-
|
|
541
|
-
Once your `launch.json` file is ready, after running `vite dev` or `vite preview` you can select **Debug All Workers** at the top of the **Run & Debug** panel to attach debuggers to all the various Workers. Then you can add breakpoints to your code and start debugging.
|
|
542
|
-
|
|
543
|
-
Note: You can also manually select the configurations to run (e.g. **Debug Worker1**) to filter which workers you want to debug.
|
|
544
|
-
|
|
545
485
|
## Migrating from `wrangler dev`
|
|
546
486
|
|
|
547
487
|
Migrating from `wrangler dev` is a simple process and you can follow the instructions in the [Quick start](#quick-start) to get started.
|
package/dist/index.d.ts
CHANGED
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 fs5 from "node:fs";
|
|
4
4
|
import * as fsp2 from "node:fs/promises";
|
|
5
5
|
import { builtinModules as builtinModules2 } from "node:module";
|
|
@@ -1083,7 +1083,6 @@ var ADDITIONAL_MODULE_TYPES = [
|
|
|
1083
1083
|
"Data",
|
|
1084
1084
|
"Text"
|
|
1085
1085
|
];
|
|
1086
|
-
var DEFAULT_INSPECTOR_PORT = 9229;
|
|
1087
1086
|
|
|
1088
1087
|
// src/additional-modules.ts
|
|
1089
1088
|
var moduleRules = [
|
|
@@ -8342,65 +8341,15 @@ function initRunners(resolvedPluginConfig, viteDevServer, miniflare) {
|
|
|
8342
8341
|
);
|
|
8343
8342
|
}
|
|
8344
8343
|
|
|
8345
|
-
// src/debugging.ts
|
|
8346
|
-
import assert4 from "node:assert";
|
|
8347
|
-
import colors from "picocolors";
|
|
8348
|
-
var debuggingPath = "/__debug";
|
|
8349
|
-
function addDebugToVitePrintUrls(server) {
|
|
8350
|
-
const originalPrintUrls = server.printUrls;
|
|
8351
|
-
server.printUrls = () => {
|
|
8352
|
-
originalPrintUrls();
|
|
8353
|
-
const localUrl = server.resolvedUrls?.local[0];
|
|
8354
|
-
if (localUrl) {
|
|
8355
|
-
const { protocol, hostname, port } = new URL(localUrl);
|
|
8356
|
-
const colorDebugUrl = (url) => colors.dim(
|
|
8357
|
-
colors.yellow(
|
|
8358
|
-
url.replace(/:(\d+)\//, (_, port2) => `:${colors.bold(port2)}/`)
|
|
8359
|
-
)
|
|
8360
|
-
);
|
|
8361
|
-
server.config.logger.info(
|
|
8362
|
-
` ${colors.green("\u279C")} ${colors.bold("Debug")}: ${colorDebugUrl(`${protocol}//${hostname}:${port}${debuggingPath}`)}`
|
|
8363
|
-
);
|
|
8364
|
-
}
|
|
8365
|
-
};
|
|
8366
|
-
}
|
|
8367
|
-
function getDebugPathHtml(workerNames, inspectorPort) {
|
|
8368
|
-
assert4(workerNames.length >= 1, "no workers present to debug");
|
|
8369
|
-
const workerDevtoolsUrls = workerNames.map((workerName) => {
|
|
8370
|
-
const localHost = `localhost:${inspectorPort}/${workerName}`;
|
|
8371
|
-
const searchParams = new URLSearchParams({
|
|
8372
|
-
theme: "systemPreferred",
|
|
8373
|
-
debugger: "true",
|
|
8374
|
-
ws: localHost,
|
|
8375
|
-
domain: workerName
|
|
8376
|
-
});
|
|
8377
|
-
const devtoolsFrontendUrl = `https://devtools.devprod.cloudflare.dev/js_app?${searchParams}`;
|
|
8378
|
-
return devtoolsFrontendUrl;
|
|
8379
|
-
});
|
|
8380
|
-
return `
|
|
8381
|
-
<script>
|
|
8382
|
-
const workerUrls = ${JSON.stringify(workerDevtoolsUrls)};
|
|
8383
|
-
const [firstUrl, ...rest] = workerUrls;
|
|
8384
|
-
for (const workerUrl of rest) {
|
|
8385
|
-
// open new tabs for the devtools of the various workers
|
|
8386
|
-
window.open(workerUrl);
|
|
8387
|
-
}
|
|
8388
|
-
// redirect the current tab to the devtools of the first worker
|
|
8389
|
-
window.location.replace(firstUrl);
|
|
8390
|
-
</script>
|
|
8391
|
-
`;
|
|
8392
|
-
}
|
|
8393
|
-
|
|
8394
8344
|
// src/deploy-config.ts
|
|
8395
|
-
import
|
|
8345
|
+
import assert4 from "node:assert";
|
|
8396
8346
|
import * as fs2 from "node:fs";
|
|
8397
8347
|
import * as path3 from "node:path";
|
|
8398
8348
|
import "vite";
|
|
8399
|
-
import { unstable_readConfig } from "wrangler";
|
|
8400
8349
|
function getDeployConfigPath(root) {
|
|
8401
8350
|
return path3.resolve(root, ".wrangler", "deploy", "config.json");
|
|
8402
8351
|
}
|
|
8403
|
-
function
|
|
8352
|
+
function getWorkerConfigPaths(root) {
|
|
8404
8353
|
const deployConfigPath = getDeployConfigPath(root);
|
|
8405
8354
|
const deployConfig = JSON.parse(
|
|
8406
8355
|
fs2.readFileSync(deployConfigPath, "utf-8")
|
|
@@ -8408,13 +8357,9 @@ function getWorkerConfigs(root) {
|
|
|
8408
8357
|
return [
|
|
8409
8358
|
{ configPath: deployConfig.configPath },
|
|
8410
8359
|
...deployConfig.auxiliaryWorkers
|
|
8411
|
-
].map(
|
|
8412
|
-
|
|
8413
|
-
|
|
8414
|
-
configPath
|
|
8415
|
-
);
|
|
8416
|
-
return unstable_readConfig({ config: resolvedConfigPath });
|
|
8417
|
-
});
|
|
8360
|
+
].map(
|
|
8361
|
+
({ configPath }) => path3.resolve(path3.dirname(deployConfigPath), configPath)
|
|
8362
|
+
);
|
|
8418
8363
|
}
|
|
8419
8364
|
function getRelativePathToWorkerConfig(deployConfigDirectory, root, outputDirectory) {
|
|
8420
8365
|
return path3.relative(
|
|
@@ -8428,7 +8373,7 @@ function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
|
8428
8373
|
fs2.mkdirSync(deployConfigDirectory, { recursive: true });
|
|
8429
8374
|
if (resolvedPluginConfig.type === "assets-only") {
|
|
8430
8375
|
const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
|
|
8431
|
-
|
|
8376
|
+
assert4(
|
|
8432
8377
|
clientOutputDirectory,
|
|
8433
8378
|
"Unexpected error: client environment output directory is undefined"
|
|
8434
8379
|
);
|
|
@@ -8446,7 +8391,7 @@ function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
|
8446
8391
|
const auxiliaryWorkers = [];
|
|
8447
8392
|
for (const environmentName of Object.keys(resolvedPluginConfig.workers)) {
|
|
8448
8393
|
const outputDirectory = resolvedViteConfig.environments[environmentName]?.build.outDir;
|
|
8449
|
-
|
|
8394
|
+
assert4(
|
|
8450
8395
|
outputDirectory,
|
|
8451
8396
|
`Unexpected error: ${environmentName} environment output directory is undefined`
|
|
8452
8397
|
);
|
|
@@ -8461,7 +8406,7 @@ function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
|
8461
8406
|
auxiliaryWorkers.push({ configPath });
|
|
8462
8407
|
}
|
|
8463
8408
|
}
|
|
8464
|
-
|
|
8409
|
+
assert4(
|
|
8465
8410
|
entryWorkerConfigPath,
|
|
8466
8411
|
`Unexpected error: entryWorkerConfigPath is undefined`
|
|
8467
8412
|
);
|
|
@@ -8474,7 +8419,7 @@ function writeDeployConfig(resolvedPluginConfig, resolvedViteConfig) {
|
|
|
8474
8419
|
}
|
|
8475
8420
|
|
|
8476
8421
|
// src/miniflare-options.ts
|
|
8477
|
-
import
|
|
8422
|
+
import assert5 from "node:assert";
|
|
8478
8423
|
import * as fs3 from "node:fs";
|
|
8479
8424
|
import * as fsp from "node:fs/promises";
|
|
8480
8425
|
import * as path4 from "node:path";
|
|
@@ -8488,7 +8433,8 @@ import {
|
|
|
8488
8433
|
import { globSync } from "tinyglobby";
|
|
8489
8434
|
import "vite";
|
|
8490
8435
|
import {
|
|
8491
|
-
unstable_getMiniflareWorkerOptions
|
|
8436
|
+
unstable_getMiniflareWorkerOptions,
|
|
8437
|
+
unstable_readConfig
|
|
8492
8438
|
} from "wrangler";
|
|
8493
8439
|
function getPersistence(root, persistState) {
|
|
8494
8440
|
if (persistState === false) {
|
|
@@ -8521,7 +8467,7 @@ function getWorkerToWorkerEntrypointNamesMap(workers) {
|
|
|
8521
8467
|
if (typeof value === "object" && "name" in value && value.entrypoint !== void 0 && value.entrypoint !== "default") {
|
|
8522
8468
|
const targetWorkerName = value.name === kCurrentWorker ? worker.name : value.name;
|
|
8523
8469
|
const entrypointNames = workerToWorkerEntrypointNamesMap.get(targetWorkerName);
|
|
8524
|
-
|
|
8470
|
+
assert5(entrypointNames, missingWorkerErrorMessage(targetWorkerName));
|
|
8525
8471
|
entrypointNames.add(value.entrypoint);
|
|
8526
8472
|
}
|
|
8527
8473
|
}
|
|
@@ -8536,20 +8482,20 @@ function getWorkerToDurableObjectClassNamesMap(workers) {
|
|
|
8536
8482
|
for (const value of Object.values(worker.durableObjects ?? {})) {
|
|
8537
8483
|
if (typeof value === "string") {
|
|
8538
8484
|
const classNames = workerToDurableObjectClassNamesMap.get(worker.name);
|
|
8539
|
-
|
|
8485
|
+
assert5(classNames, missingWorkerErrorMessage(worker.name));
|
|
8540
8486
|
classNames.add(value);
|
|
8541
8487
|
} else if (typeof value === "object") {
|
|
8542
8488
|
if (value.scriptName) {
|
|
8543
8489
|
const classNames = workerToDurableObjectClassNamesMap.get(
|
|
8544
8490
|
value.scriptName
|
|
8545
8491
|
);
|
|
8546
|
-
|
|
8492
|
+
assert5(classNames, missingWorkerErrorMessage(value.scriptName));
|
|
8547
8493
|
classNames.add(value.className);
|
|
8548
8494
|
} else {
|
|
8549
8495
|
const classNames = workerToDurableObjectClassNamesMap.get(
|
|
8550
8496
|
worker.name
|
|
8551
8497
|
);
|
|
8552
|
-
|
|
8498
|
+
assert5(classNames, missingWorkerErrorMessage(worker.name));
|
|
8553
8499
|
classNames.add(value.className);
|
|
8554
8500
|
}
|
|
8555
8501
|
}
|
|
@@ -8567,13 +8513,13 @@ function getWorkerToWorkflowEntrypointClassNamesMap(workers) {
|
|
|
8567
8513
|
const classNames = workerToWorkflowEntrypointClassNamesMap.get(
|
|
8568
8514
|
value.scriptName
|
|
8569
8515
|
);
|
|
8570
|
-
|
|
8516
|
+
assert5(classNames, missingWorkerErrorMessage(value.scriptName));
|
|
8571
8517
|
classNames.add(value.className);
|
|
8572
8518
|
} else {
|
|
8573
8519
|
const classNames = workerToWorkflowEntrypointClassNamesMap.get(
|
|
8574
8520
|
worker.name
|
|
8575
8521
|
);
|
|
8576
|
-
|
|
8522
|
+
assert5(classNames, missingWorkerErrorMessage(worker.name));
|
|
8577
8523
|
classNames.add(value.className);
|
|
8578
8524
|
}
|
|
8579
8525
|
}
|
|
@@ -8682,7 +8628,6 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8682
8628
|
worker: {
|
|
8683
8629
|
...workerOptions,
|
|
8684
8630
|
name: workerOptions.name ?? workerConfig.name,
|
|
8685
|
-
unsafeInspectorProxy: resolvedPluginConfig.inspectorPort !== false,
|
|
8686
8631
|
modulesRoot: miniflareModulesRoot,
|
|
8687
8632
|
unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
|
|
8688
8633
|
serviceBindings: {
|
|
@@ -8693,7 +8638,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8693
8638
|
__VITE_INVOKE_MODULE__: async (request) => {
|
|
8694
8639
|
const payload = await request.json();
|
|
8695
8640
|
const invokePayloadData = payload.data;
|
|
8696
|
-
|
|
8641
|
+
assert5(
|
|
8697
8642
|
invokePayloadData.name === "fetchModule",
|
|
8698
8643
|
`Invalid invoke event: ${invokePayloadData.name}`
|
|
8699
8644
|
);
|
|
@@ -8724,8 +8669,6 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8724
8669
|
const logger = new ViteMiniflareLogger(resolvedViteConfig);
|
|
8725
8670
|
return {
|
|
8726
8671
|
log: logger,
|
|
8727
|
-
inspectorPort: resolvedPluginConfig.inspectorPort || void 0,
|
|
8728
|
-
unsafeInspectorProxy: resolvedPluginConfig.inspectorPort !== false,
|
|
8729
8672
|
handleRuntimeStdio(stdout, stderr) {
|
|
8730
8673
|
const decoder = new TextDecoder();
|
|
8731
8674
|
stdout.forEach((data2) => logger.info(decoder.decode(data2)));
|
|
@@ -8748,7 +8691,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8748
8691
|
const workerEntrypointNames = workerToWorkerEntrypointNamesMap.get(
|
|
8749
8692
|
workerOptions.name
|
|
8750
8693
|
);
|
|
8751
|
-
|
|
8694
|
+
assert5(
|
|
8752
8695
|
workerEntrypointNames,
|
|
8753
8696
|
`WorkerEntrypoint names not found for worker ${workerOptions.name}`
|
|
8754
8697
|
);
|
|
@@ -8760,7 +8703,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8760
8703
|
const durableObjectClassNames = workerToDurableObjectClassNamesMap.get(
|
|
8761
8704
|
workerOptions.name
|
|
8762
8705
|
);
|
|
8763
|
-
|
|
8706
|
+
assert5(
|
|
8764
8707
|
durableObjectClassNames,
|
|
8765
8708
|
`DurableObject class names not found for worker ${workerOptions.name}`
|
|
8766
8709
|
);
|
|
@@ -8770,7 +8713,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8770
8713
|
);
|
|
8771
8714
|
}
|
|
8772
8715
|
const workflowEntrypointClassNames = workerToWorkflowEntrypointClassNamesMap.get(workerOptions.name);
|
|
8773
|
-
|
|
8716
|
+
assert5(
|
|
8774
8717
|
workflowEntrypointClassNames,
|
|
8775
8718
|
`WorkflowEntrypoint class names not found for worker: ${workerOptions.name}`
|
|
8776
8719
|
);
|
|
@@ -8802,18 +8745,18 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8802
8745
|
async unsafeModuleFallbackService(request) {
|
|
8803
8746
|
const url = new URL(request.url);
|
|
8804
8747
|
const rawSpecifier = url.searchParams.get("rawSpecifier");
|
|
8805
|
-
|
|
8748
|
+
assert5(
|
|
8806
8749
|
rawSpecifier,
|
|
8807
8750
|
`Unexpected error: no specifier in request to module fallback service.`
|
|
8808
8751
|
);
|
|
8809
8752
|
const match = additionalModuleRE.exec(rawSpecifier);
|
|
8810
|
-
|
|
8753
|
+
assert5(match, `Unexpected error: no match for module: ${rawSpecifier}.`);
|
|
8811
8754
|
const [full, moduleType, modulePath] = match;
|
|
8812
|
-
|
|
8755
|
+
assert5(
|
|
8813
8756
|
moduleType,
|
|
8814
8757
|
`Unexpected error: module type not found in reference: ${full}.`
|
|
8815
8758
|
);
|
|
8816
|
-
|
|
8759
|
+
assert5(
|
|
8817
8760
|
modulePath,
|
|
8818
8761
|
`Unexpected error: module path not found in reference: ${full}.`
|
|
8819
8762
|
);
|
|
@@ -8843,7 +8786,7 @@ function getDevMiniflareOptions(resolvedPluginConfig, viteDevServer) {
|
|
|
8843
8786
|
};
|
|
8844
8787
|
}
|
|
8845
8788
|
function getPreviewModules(main, modulesRules) {
|
|
8846
|
-
|
|
8789
|
+
assert5(modulesRules, `Unexpected error: 'modulesRules' is undefined`);
|
|
8847
8790
|
const rootPath = path4.dirname(main);
|
|
8848
8791
|
const entryPath = path4.basename(main);
|
|
8849
8792
|
return {
|
|
@@ -8862,8 +8805,12 @@ function getPreviewModules(main, modulesRules) {
|
|
|
8862
8805
|
]
|
|
8863
8806
|
};
|
|
8864
8807
|
}
|
|
8865
|
-
function getPreviewMiniflareOptions(vitePreviewServer,
|
|
8808
|
+
function getPreviewMiniflareOptions(vitePreviewServer, persistState) {
|
|
8866
8809
|
const resolvedViteConfig = vitePreviewServer.config;
|
|
8810
|
+
const configPaths = getWorkerConfigPaths(resolvedViteConfig.root);
|
|
8811
|
+
const workerConfigs = configPaths.map(
|
|
8812
|
+
(configPath) => unstable_readConfig({ config: configPath })
|
|
8813
|
+
);
|
|
8867
8814
|
const workers = workerConfigs.flatMap((config) => {
|
|
8868
8815
|
const miniflareWorkerOptions = unstable_getMiniflareWorkerOptions(config);
|
|
8869
8816
|
const { externalWorkers } = miniflareWorkerOptions;
|
|
@@ -8872,7 +8819,6 @@ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistSta
|
|
|
8872
8819
|
{
|
|
8873
8820
|
...workerOptions,
|
|
8874
8821
|
name: workerOptions.name ?? config.name,
|
|
8875
|
-
unsafeInspectorProxy: inspectorPort !== false,
|
|
8876
8822
|
...miniflareWorkerOptions.main ? getPreviewModules(miniflareWorkerOptions.main, modulesRules) : { modules: true, script: "" }
|
|
8877
8823
|
},
|
|
8878
8824
|
...externalWorkers
|
|
@@ -8881,8 +8827,6 @@ function getPreviewMiniflareOptions(vitePreviewServer, workerConfigs, persistSta
|
|
|
8881
8827
|
const logger = new ViteMiniflareLogger(resolvedViteConfig);
|
|
8882
8828
|
return {
|
|
8883
8829
|
log: logger,
|
|
8884
|
-
inspectorPort: inspectorPort || void 0,
|
|
8885
|
-
unsafeInspectorProxy: inspectorPort !== false,
|
|
8886
8830
|
handleRuntimeStdio(stdout, stderr) {
|
|
8887
8831
|
const decoder = new TextDecoder();
|
|
8888
8832
|
stdout.forEach((data2) => logger.info(decoder.decode(data2)));
|
|
@@ -8928,14 +8872,14 @@ function miniflareLogLevelFromViteLogLevel(level = "info") {
|
|
|
8928
8872
|
}
|
|
8929
8873
|
|
|
8930
8874
|
// src/plugin-config.ts
|
|
8931
|
-
import
|
|
8875
|
+
import assert6 from "node:assert";
|
|
8932
8876
|
import * as path6 from "node:path";
|
|
8933
8877
|
import * as vite5 from "vite";
|
|
8934
8878
|
|
|
8935
8879
|
// src/workers-configs.ts
|
|
8936
8880
|
import * as fs4 from "node:fs";
|
|
8937
8881
|
import * as path5 from "node:path";
|
|
8938
|
-
import { unstable_readConfig as
|
|
8882
|
+
import { unstable_readConfig as unstable_readConfig2 } from "wrangler";
|
|
8939
8883
|
var nonApplicableWorkerConfigs = {
|
|
8940
8884
|
/**
|
|
8941
8885
|
* Object containing configs that have a vite replacement, the object's field contain details about the config's replacement
|
|
@@ -8986,7 +8930,7 @@ function readWorkerConfig(configPath, env2) {
|
|
|
8986
8930
|
replacedByVite: /* @__PURE__ */ new Set(),
|
|
8987
8931
|
notRelevant: /* @__PURE__ */ new Set()
|
|
8988
8932
|
};
|
|
8989
|
-
const config =
|
|
8933
|
+
const config = unstable_readConfig2({ config: configPath, env: env2 }, {});
|
|
8990
8934
|
const raw = structuredClone(config);
|
|
8991
8935
|
nullableNonApplicable.forEach((prop) => {
|
|
8992
8936
|
if (config[prop] !== void 0) {
|
|
@@ -9157,7 +9101,6 @@ function workerNameToEnvironmentName(workerName) {
|
|
|
9157
9101
|
function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
9158
9102
|
const configPaths = /* @__PURE__ */ new Set();
|
|
9159
9103
|
const persistState = pluginConfig.persistState ?? true;
|
|
9160
|
-
const inspectorPort = pluginConfig.inspectorPort ?? DEFAULT_INSPECTOR_PORT;
|
|
9161
9104
|
const root = userConfig.root ? path6.resolve(userConfig.root) : process.cwd();
|
|
9162
9105
|
const { CLOUDFLARE_ENV: cloudflareEnv } = vite5.loadEnv(
|
|
9163
9106
|
viteEnv.mode,
|
|
@@ -9180,7 +9123,6 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
9180
9123
|
type: "assets-only",
|
|
9181
9124
|
config: entryWorkerResolvedConfig.config,
|
|
9182
9125
|
configPaths,
|
|
9183
|
-
inspectorPort,
|
|
9184
9126
|
persistState,
|
|
9185
9127
|
rawConfigs: {
|
|
9186
9128
|
entryWorker: entryWorkerResolvedConfig
|
|
@@ -9203,7 +9145,7 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
9203
9145
|
}
|
|
9204
9146
|
);
|
|
9205
9147
|
auxiliaryWorkersResolvedConfigs.push(workerResolvedConfig);
|
|
9206
|
-
|
|
9148
|
+
assert6(
|
|
9207
9149
|
workerResolvedConfig.type === "worker",
|
|
9208
9150
|
"Unexpected error: received AssetsOnlyResult with auxiliary workers."
|
|
9209
9151
|
);
|
|
@@ -9220,7 +9162,6 @@ function resolvePluginConfig(pluginConfig, userConfig, viteEnv) {
|
|
|
9220
9162
|
type: "workers",
|
|
9221
9163
|
configPaths,
|
|
9222
9164
|
persistState,
|
|
9223
|
-
inspectorPort,
|
|
9224
9165
|
workers,
|
|
9225
9166
|
entryWorkerEnvironmentName,
|
|
9226
9167
|
rawConfigs: {
|
|
@@ -9335,7 +9276,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9335
9276
|
resolvedPluginConfig.workers
|
|
9336
9277
|
).map((environmentName) => {
|
|
9337
9278
|
const environment = builder.environments[environmentName];
|
|
9338
|
-
|
|
9279
|
+
assert7(
|
|
9339
9280
|
environment,
|
|
9340
9281
|
`${environmentName} environment not found`
|
|
9341
9282
|
);
|
|
@@ -9376,7 +9317,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9376
9317
|
if (isEntryWorker && hasClientBuild) {
|
|
9377
9318
|
const workerOutputDirectory = this.environment.config.build.outDir;
|
|
9378
9319
|
const clientOutputDirectory = resolvedViteConfig.environments.client?.build.outDir;
|
|
9379
|
-
|
|
9320
|
+
assert7(
|
|
9380
9321
|
clientOutputDirectory,
|
|
9381
9322
|
"Unexpected error: client output directory is undefined"
|
|
9382
9323
|
);
|
|
@@ -9451,7 +9392,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9451
9392
|
}
|
|
9452
9393
|
},
|
|
9453
9394
|
async configureServer(viteDevServer) {
|
|
9454
|
-
|
|
9395
|
+
assert7(
|
|
9455
9396
|
viteDevServer.httpServer,
|
|
9456
9397
|
"Unexpected error: No Vite HTTP server"
|
|
9457
9398
|
);
|
|
@@ -9476,13 +9417,10 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9476
9417
|
};
|
|
9477
9418
|
},
|
|
9478
9419
|
configurePreviewServer(vitePreviewServer) {
|
|
9479
|
-
const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
|
|
9480
9420
|
const miniflare2 = new Miniflare(
|
|
9481
9421
|
getPreviewMiniflareOptions(
|
|
9482
9422
|
vitePreviewServer,
|
|
9483
|
-
|
|
9484
|
-
pluginConfig.persistState ?? true,
|
|
9485
|
-
pluginConfig.inspectorPort
|
|
9423
|
+
pluginConfig.persistState ?? true
|
|
9486
9424
|
)
|
|
9487
9425
|
);
|
|
9488
9426
|
const middleware = createMiddleware(
|
|
@@ -9499,25 +9437,6 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9499
9437
|
});
|
|
9500
9438
|
}
|
|
9501
9439
|
},
|
|
9502
|
-
// Plugin to support `.wasm?init` extension
|
|
9503
|
-
{
|
|
9504
|
-
name: "vite-plugin-cloudflare:wasm-helper",
|
|
9505
|
-
enforce: "pre",
|
|
9506
|
-
applyToEnvironment(environment) {
|
|
9507
|
-
return getWorkerConfig2(environment.name) !== void 0;
|
|
9508
|
-
},
|
|
9509
|
-
load(id) {
|
|
9510
|
-
if (!id.endsWith(".wasm?init")) {
|
|
9511
|
-
return;
|
|
9512
|
-
}
|
|
9513
|
-
return `
|
|
9514
|
-
import wasm from "${cleanUrl(id)}";
|
|
9515
|
-
export default function(opts = {}) {
|
|
9516
|
-
return WebAssembly.instantiate(wasm, opts);
|
|
9517
|
-
}
|
|
9518
|
-
`;
|
|
9519
|
-
}
|
|
9520
|
-
},
|
|
9521
9440
|
// Plugin to support additional modules
|
|
9522
9441
|
{
|
|
9523
9442
|
name: "vite-plugin-cloudflare:additional-modules",
|
|
@@ -9557,7 +9476,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9557
9476
|
for (const match of matches) {
|
|
9558
9477
|
magicString ??= new MagicString(code);
|
|
9559
9478
|
const [full, _, modulePath] = match;
|
|
9560
|
-
|
|
9479
|
+
assert7(
|
|
9561
9480
|
modulePath,
|
|
9562
9481
|
`Unexpected error: module path not found in reference ${full}.`
|
|
9563
9482
|
);
|
|
@@ -9640,7 +9559,7 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9640
9559
|
return this.resolve(source, importer, options);
|
|
9641
9560
|
}
|
|
9642
9561
|
if (this.environment.mode === "dev") {
|
|
9643
|
-
|
|
9562
|
+
assert7(
|
|
9644
9563
|
this.environment.depsOptimizer,
|
|
9645
9564
|
"depsOptimizer is required in dev mode"
|
|
9646
9565
|
);
|
|
@@ -9654,63 +9573,16 @@ function cloudflare2(pluginConfig = {}) {
|
|
|
9654
9573
|
},
|
|
9655
9574
|
async transform(code, id) {
|
|
9656
9575
|
const workerConfig = getWorkerConfig2(this.environment.name);
|
|
9657
|
-
|
|
9576
|
+
assert7(workerConfig, "Expected a worker config");
|
|
9658
9577
|
const resolvedId = await this.resolve(workerConfig.main);
|
|
9659
9578
|
if (id === resolvedId?.id) {
|
|
9660
9579
|
return injectGlobalCode(id, code);
|
|
9661
9580
|
}
|
|
9662
9581
|
}
|
|
9663
|
-
},
|
|
9664
|
-
// Plugin that provides an __debug path for debugging the Cloudflare Workers.
|
|
9665
|
-
{
|
|
9666
|
-
name: "vite-plugin-cloudflare:debug",
|
|
9667
|
-
// Note: this plugin needs to run before the main vite-plugin-cloudflare so that
|
|
9668
|
-
// the preview middleware here can take precedence
|
|
9669
|
-
enforce: "pre",
|
|
9670
|
-
configureServer(viteDevServer) {
|
|
9671
|
-
if (resolvedPluginConfig.type === "workers" && resolvedPluginConfig.inspectorPort !== false) {
|
|
9672
|
-
addDebugToVitePrintUrls(viteDevServer);
|
|
9673
|
-
}
|
|
9674
|
-
const workerNames = resolvedPluginConfig.type === "assets-only" ? [] : Object.values(resolvedPluginConfig.workers).map(
|
|
9675
|
-
(worker) => worker.name
|
|
9676
|
-
);
|
|
9677
|
-
viteDevServer.middlewares.use((req, res, next) => {
|
|
9678
|
-
if (req.url === debuggingPath && resolvedPluginConfig.inspectorPort !== false) {
|
|
9679
|
-
const html = getDebugPathHtml(
|
|
9680
|
-
workerNames,
|
|
9681
|
-
resolvedPluginConfig.inspectorPort
|
|
9682
|
-
);
|
|
9683
|
-
res.setHeader("Content-Type", "text/html");
|
|
9684
|
-
return res.end(html);
|
|
9685
|
-
}
|
|
9686
|
-
next();
|
|
9687
|
-
});
|
|
9688
|
-
},
|
|
9689
|
-
configurePreviewServer(vitePreviewServer) {
|
|
9690
|
-
const workerConfigs = getWorkerConfigs(vitePreviewServer.config.root);
|
|
9691
|
-
if (workerConfigs.length >= 1 && pluginConfig.inspectorPort !== false) {
|
|
9692
|
-
addDebugToVitePrintUrls(vitePreviewServer);
|
|
9693
|
-
}
|
|
9694
|
-
const workerNames = workerConfigs.map((worker) => {
|
|
9695
|
-
assert8(worker.name);
|
|
9696
|
-
return worker.name;
|
|
9697
|
-
});
|
|
9698
|
-
vitePreviewServer.middlewares.use((req, res, next) => {
|
|
9699
|
-
if (req.url === debuggingPath && pluginConfig.inspectorPort !== false) {
|
|
9700
|
-
const html = getDebugPathHtml(
|
|
9701
|
-
workerNames,
|
|
9702
|
-
pluginConfig.inspectorPort ?? DEFAULT_INSPECTOR_PORT
|
|
9703
|
-
);
|
|
9704
|
-
res.setHeader("Content-Type", "text/html");
|
|
9705
|
-
return res.end(html);
|
|
9706
|
-
}
|
|
9707
|
-
next();
|
|
9708
|
-
});
|
|
9709
|
-
}
|
|
9710
9582
|
}
|
|
9711
9583
|
];
|
|
9712
9584
|
function getWorkerConfig2(environmentName) {
|
|
9713
|
-
|
|
9585
|
+
assert7(resolvedPluginConfig, "Expected resolvedPluginConfig to be defined");
|
|
9714
9586
|
return resolvedPluginConfig.type !== "assets-only" ? resolvedPluginConfig.workers[environmentName] : void 0;
|
|
9715
9587
|
}
|
|
9716
9588
|
}
|
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-d42966125",
|
|
4
4
|
"description": "Cloudflare plugin for Vite",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -34,13 +34,12 @@
|
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@hattip/adapter-node": "^0.0.49",
|
|
37
|
-
"picocolors": "^1.1.1",
|
|
38
37
|
"tinyglobby": "^0.2.12",
|
|
39
38
|
"unenv": "2.0.0-rc.15",
|
|
40
39
|
"ws": "8.18.0",
|
|
41
|
-
"@cloudflare/unenv-preset": "0.0.0-
|
|
42
|
-
"wrangler": "0.0.0-
|
|
43
|
-
"miniflare": "0.0.0-
|
|
40
|
+
"@cloudflare/unenv-preset": "0.0.0-d42966125",
|
|
41
|
+
"wrangler": "0.0.0-d42966125",
|
|
42
|
+
"miniflare": "0.0.0-d42966125"
|
|
44
43
|
},
|
|
45
44
|
"devDependencies": {
|
|
46
45
|
"@cloudflare/workers-types": "^4.20250319.0",
|
|
@@ -54,7 +53,7 @@
|
|
|
54
53
|
"vite": "^6.1.0",
|
|
55
54
|
"vitest": "~3.0.8",
|
|
56
55
|
"@cloudflare/mock-npm-registry": "0.0.0",
|
|
57
|
-
"@cloudflare/workers-shared": "0.0.0-
|
|
56
|
+
"@cloudflare/workers-shared": "0.0.0-d42966125",
|
|
58
57
|
"@cloudflare/workers-tsconfig": "0.0.0"
|
|
59
58
|
},
|
|
60
59
|
"peerDependencies": {
|