Skip to content

Commit 4f15699

Browse files
Throw a helpful error if Vite can't resolve the main entrypoint (#11466)
* Throw a helpful error if Vite can't resolve the main entrypoint * Apply suggestions from code review Co-authored-by: James Opstad <[email protected]> * Format --------- Co-authored-by: James Opstad <[email protected]>
1 parent 79d30d4 commit 4f15699

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

‎.changeset/breezy-dots-visit.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Throw a more helpful error message when a Worker's entry module can't be resolved
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@cloudflare/vite-plugin-e2e-unresolved-main",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite dev"
7+
},
8+
"devDependencies": {
9+
"@cloudflare/vite-plugin": "*",
10+
"vite": "^6.1.0"
11+
}
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { cloudflare } from "@cloudflare/vite-plugin";
2+
import { defineConfig } from "vite";
3+
4+
export default defineConfig({
5+
plugins: [cloudflare({ inspectorPort: false, persistState: false })],
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "worker",
3+
"main": "nonexistent-bare-module",
4+
"compatibility_date": "2025-11-28",
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, test } from "vitest";
2+
import { runLongLived, seed } from "./helpers";
3+
4+
describe("unresolved main entry file", () => {
5+
const projectPath = seed("unresolved-main", "pnpm");
6+
7+
test("throws an error when the main entry file cannot be resolved", async ({
8+
expect,
9+
}) => {
10+
const proc = await runLongLived("pnpm", "dev", projectPath);
11+
expect(await proc.exitCode).not.toBe(0);
12+
expect(proc.stderr).toContain(
13+
'Failed to resolve main entry file "nonexistent-bare-module"'
14+
);
15+
});
16+
});

‎packages/vite-plugin-cloudflare/src/plugins/virtual-modules.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ export const virtualModulesPlugin = createPlugin("virtual-modules", (ctx) => {
1717
applyToEnvironment(environment) {
1818
return ctx.getWorkerConfig(environment.name) !== undefined;
1919
},
20-
resolveId(source) {
20+
async resolveId(source) {
2121
if (source === VIRTUAL_WORKER_ENTRY || source === VIRTUAL_EXPORT_TYPES) {
2222
return `\0${source}`;
2323
}
2424

2525
if (source === VIRTUAL_USER_ENTRY) {
2626
const workerConfig = ctx.getWorkerConfig(this.environment.name);
2727
assert(workerConfig, "Expected `workerConfig` to be defined");
28-
29-
return this.resolve(workerConfig.main);
28+
const main = await this.resolve(workerConfig.main);
29+
if (!main) {
30+
throw new Error(
31+
`Failed to resolve main entry file "${workerConfig.main}" for environment "${this.environment.name}"`
32+
);
33+
}
34+
return main;
3035
}
3136
},
3237
load(id) {

0 commit comments

Comments
 (0)