Skip to content

Commit 0837a8d

Browse files
authored
Support packages and virtual modules in the main field of the Worker config (#10212)
* Add experimental.preserveOriginalMain options to readConfig * Add main-resolution playground * Add tests * Support virtual modules and package exports in main field * Use virtual module for injecting nodejs_compat polyfills * Add changesets * PR feedback * Remove experimental from preserveOriginalMain naming
1 parent 7aa31dc commit 0837a8d

38 files changed

+417
-115
lines changed

.changeset/eighty-nights-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Add `preserveOriginalMain` option to `unstable_readConfig`. This will pass the original `main` value through, without converting it to an absolute path.

.changeset/gold-dodos-stick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": minor
3+
---
4+
5+
Support packages and virtual modules in the `main` field of the Worker config. The primary use case is to enable users to directly provide a file exported by a framework as the Worker entry module.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, test } from "vitest";
2+
import { getJsonResponse } from "../../__test-utils__";
3+
4+
test("resolves `main` relative to a root level Worker config", async () => {
5+
expect(await getJsonResponse()).toEqual({
6+
entry: "Root config",
7+
imported: "Root config import",
8+
});
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, test } from "vitest";
2+
import { getJsonResponse } from "../../../__test-utils__";
3+
4+
test("resolves `main` relative to a nested Worker config", async () => {
5+
expect(await getJsonResponse()).toEqual({
6+
entry: "Nested config",
7+
imported: "Nested config import",
8+
});
9+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from "vitest";
2+
import { getTextResponse } from "../../../__test-utils__";
3+
4+
test("supports package exports in the `main` field", async () => {
5+
expect(await getTextResponse()).toBe("Package export as Worker entry file");
6+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from "vitest";
2+
import { getTextResponse } from "../../../__test-utils__";
3+
4+
test("supports virtual modules in the `main` field", async () => {
5+
expect(await getTextResponse()).toBe("Virtual module as Worker entry file");
6+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const text = "Nested config import";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { text } from "./another/another";
2+
3+
export default {
4+
async fetch() {
5+
return Response.json({ entry: "Nested config", imported: text });
6+
},
7+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "worker",
3+
"main": "./index.ts",
4+
"compatibility_date": "2024-12-30",
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@playground/main-resolution",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "vite build",
7+
"build:nested-config": "vite build -c vite.config.nested-config.ts",
8+
"build:package-export-main": "vite build -c vite.config.package-export-main.ts",
9+
"build:virtual-module-main": "vite build -c vite.config.virtual-module-main.ts",
10+
"check:types": "tsc --build",
11+
"dev": "vite dev",
12+
"dev:nested-config": "vite dev -c vite.config.nested-config.ts",
13+
"dev:package-export-main": "vite dev -c vite.config.package-export-main.ts",
14+
"dev:virtual-module-main": "vite dev -c vite.config.virtual-module-main.ts",
15+
"preview": "vite preview"
16+
},
17+
"devDependencies": {
18+
"@cloudflare/vite-plugin": "workspace:*",
19+
"@cloudflare/workers-tsconfig": "workspace:*",
20+
"@cloudflare/workers-types": "catalog:default",
21+
"@playground/main-resolution-package": "file:./package",
22+
"typescript": "catalog:default",
23+
"vite": "catalog:vite-plugin",
24+
"wrangler": "workspace:*"
25+
}
26+
}

0 commit comments

Comments
 (0)