-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As we are adding `wrangler.toml` support for Pages, we want to ensure that `wrangler pages dev` works with a configuration file. This commit adds `wrangler.toml` support for local development using `pages dev`. Co-authored-by: Carmen Popoviciu <cpopoviciu@cloudflare.com>
- Loading branch information
1 parent
904f91a
commit f5e2367
Showing
32 changed files
with
1,119 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feat: Add `wrangler.toml` support in `wrangler pages dev` | ||
|
||
As we are adding `wrangler.toml` support for Pages, we want to ensure that `wrangler pages dev` works with a configuration file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# ⚡️ pages-functions-with-config-file-app | ||
|
||
`pages-functions-with-config-file-app` is a test fixture that sets up a ⚡️Pages⚡️ Functions project, with a `wrangler.toml` [configuration file](hhttps://developers.cloudflare.com/workers/wrangler/configuration). The purpose of this fixture is to demonstrate that `wrangler pages dev` can take configuration from a `wrangler.toml` file. | ||
|
||
## Local dev | ||
|
||
To test this fixture run `wrangler pages dev` in the fixture folder: | ||
|
||
```bash | ||
# cd into the test fixture folder | ||
cd fixtures/pages-functions-with-config-file-app | ||
|
||
# Start local dev server | ||
npx wrangler pages dev | ||
``` | ||
|
||
Once the local dev server was started, you should see the configuration specified in the `wrangler.toml` at the root of the fixture folder, affect the generated Worker. | ||
|
||
## Run tests | ||
|
||
```bash | ||
# cd into the test fixture folder | ||
cd fixtures/pages-functions-with-config-file-app | ||
|
||
# Run tests | ||
npm run test | ||
``` | ||
|
||
You can still override what is in the wrangler.toml by adding command line args: wrangler pages dev --binding=KEY:VALUE |
11 changes: 11 additions & 0 deletions
11
fixtures/pages-functions-with-config-file-app/functions/celebrate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export async function onRequest(context) { | ||
return new Response( | ||
`[/celebrate]:\n` + | ||
`🎵 🎵 🎵\n` + | ||
`You can turn this world around\n` + | ||
`And bring back all of those happy days\n` + | ||
`Put your troubles down\n` + | ||
`It's time to ${context.env.VAR2}\n` + | ||
`🎵 🎵 🎵` | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
fixtures/pages-functions-with-config-file-app/functions/holiday.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export async function onRequest(context) { | ||
return new Response( | ||
`[/holiday]:\n` + | ||
`🎵 🎵 🎵\n` + | ||
`If we took a ${context.env.VAR1}\n` + | ||
`Took some time to ${context.env.VAR2}\n` + | ||
`Just one day out of life\n` + | ||
`It would be, it would be so nice\n` + | ||
`🎵 🎵 🎵` | ||
); | ||
} |
20 changes: 20 additions & 0 deletions
20
fixtures/pages-functions-with-config-file-app/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "pages-functions-with-config-file-app", | ||
"private": true, | ||
"sideEffects": false, | ||
"scripts": { | ||
"check:type": "tsc", | ||
"dev": "npx wrangler pages dev", | ||
"test": "vitest run", | ||
"test:watch": "vitest", | ||
"type:tests": "tsc -p ./tests/tsconfig.json" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"undici": "^5.28.3", | ||
"wrangler": "workspace:*" | ||
}, | ||
"engines": { | ||
"node": ">=16.13" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
fixtures/pages-functions-with-config-file-app/public/_routes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": 1, | ||
"include": ["/celebrate"], | ||
"exclude": ["/holiday"] | ||
} |
6 changes: 6 additions & 0 deletions
6
fixtures/pages-functions-with-config-file-app/public/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>Celebrate! Pages now supports 'wrangler.toml' 🎉</h1> | ||
</body> | ||
</html> |
48 changes: 48 additions & 0 deletions
48
fixtures/pages-functions-with-config-file-app/tests/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { resolve } from "node:path"; | ||
import { fetch } from "undici"; | ||
import { afterAll, beforeAll, describe, it } from "vitest"; | ||
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; | ||
|
||
describe("Pages Functions with wrangler.toml", () => { | ||
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; | ||
|
||
beforeAll(async () => { | ||
({ ip, port, stop } = await runWranglerPagesDev( | ||
resolve(__dirname, ".."), | ||
undefined, | ||
["--port=0", "--inspector-port=0"] | ||
)); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stop?.(); | ||
}); | ||
|
||
it("should render static pages", async ({ expect }) => { | ||
const response = await fetch(`http://${ip}:${port}`); | ||
const text = await response.text(); | ||
expect(text).toContain("Celebrate! Pages now supports 'wrangler.toml' 🎉"); | ||
}); | ||
|
||
it("should correctly apply the routing rules provided in the custom _routes.json file", async ({ | ||
expect, | ||
}) => { | ||
// matches `/celebrate` include rule | ||
let response = await fetch(`http://${ip}:${port}/celebrate`); | ||
let text = await response.text(); | ||
expect(text).toEqual( | ||
`[/celebrate]:\n` + | ||
`🎵 🎵 🎵\n` + | ||
`You can turn this world around\n` + | ||
`And bring back all of those happy days\n` + | ||
`Put your troubles down\n` + | ||
`It's time to celebrate\n` + | ||
`🎵 🎵 🎵` | ||
); | ||
|
||
// matches `/holiday` exclude rule | ||
response = await fetch(`http://${ip}:${port}/holiday`); | ||
text = await response.text(); | ||
expect(text).toContain("Celebrate! Pages now supports 'wrangler.toml' 🎉"); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
fixtures/pages-functions-with-config-file-app/tests/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"], | ||
"esModuleInterop": true | ||
}, | ||
"include": ["**/*.ts", "../../../node-types.d.ts"] | ||
} |
11 changes: 11 additions & 0 deletions
11
fixtures/pages-functions-with-config-file-app/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["@cloudflare/workers-types"], | ||
"moduleResolution": "node", | ||
"noEmit": true | ||
}, | ||
"include": ["functions"] | ||
} |
9 changes: 9 additions & 0 deletions
9
fixtures/pages-functions-with-config-file-app/vitest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineProject, mergeConfig } from "vitest/config"; | ||
import configShared from "../../vitest.shared"; | ||
|
||
export default mergeConfig( | ||
configShared, | ||
defineProject({ | ||
test: {}, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pages_build_output_dir = "./public" | ||
name = "pages-with-config-file-app" | ||
compatibility_date = "2024-01-01" | ||
#compatibility_flags = [] | ||
|
||
[vars] | ||
VAR1 = "holiday" | ||
VAR2 = "celebrate" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# ⚡️ pages-workerjs-with-config-file-app | ||
|
||
`pages-workerjs-with-config-file-app` is a test fixture that sets up an [Advanced Mode](https://developers.cloudflare.com/pages/platform/functions/#advanced-mode) ⚡️Pages⚡️ project with a `wrangler.toml` [configuration file](hhttps://developers.cloudflare.com/workers/wrangler/configuration). he purpose of this fixture is to demonstrate that `wrangler pages dev` can take configuration from a `wrangler.toml` file. | ||
|
||
## Local dev | ||
|
||
To test this fixture run `wrangler pages dev` in the fixture folder: | ||
|
||
```bash | ||
# cd into the test fixture folder | ||
cd fixtures/pages-workerjs-with-config-file-app | ||
|
||
# Start local dev server | ||
npx wrangler pages dev | ||
``` | ||
|
||
Once the local dev server was started, you should see the configuration specified in the `wrangler.toml` at the root of the fixture folder, affect the generated Worker. | ||
|
||
## Run tests | ||
|
||
```bash | ||
# cd into the test fixture folder | ||
cd fixtures/pages-workerjs-with-config-file-app | ||
|
||
# Run tests | ||
npm run test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "pages-workerjs-with-config-file-app", | ||
"private": true, | ||
"sideEffects": false, | ||
"scripts": { | ||
"check:type": "tsc", | ||
"dev": "npx wrangler pages dev", | ||
"test": "vitest run", | ||
"test:watch": "vitest", | ||
"type:tests": "tsc -p ./tests/tsconfig.json" | ||
}, | ||
"devDependencies": { | ||
"undici": "^5.28.3", | ||
"wrangler": "workspace:*", | ||
"@cloudflare/workers-tsconfig": "workspace:*" | ||
}, | ||
"engines": { | ||
"node": ">=16.13" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
fixtures/pages-workerjs-with-config-file-app/public/_routes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": 1, | ||
"include": ["/holiday", "/celebrate"], | ||
"exclude": [] | ||
} |
37 changes: 37 additions & 0 deletions
37
fixtures/pages-workerjs-with-config-file-app/public/_worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export default { | ||
async fetch(request, env) { | ||
const url = new URL(request.url); | ||
|
||
if (url.pathname === "/holiday") { | ||
return new Response( | ||
`[/holiday]:\n` + | ||
`🎶 🎶 🎶\n` + | ||
`If we took a ${env.VAR2}\n` + | ||
`Took some time to ${env.VAR1}\n` + | ||
`Just one day out of life\n` + | ||
`It would be, it would be so nice 🎉\n` + | ||
`🎶 🎶 🎶` | ||
); | ||
} | ||
|
||
if (url.pathname === "/celebrate") { | ||
return new Response( | ||
`[/celebrate]:\n` + | ||
`🎶 🎶 🎶\n` + | ||
`Everybody spread the word\n` + | ||
`We're gonna have a ${env.VAR3}\n` + | ||
`All across the world\n` + | ||
`In every nation\n` + | ||
`🎶 🎶 🎶` | ||
); | ||
} | ||
|
||
if (url.pathname === "/oh-yeah") { | ||
return new Response( | ||
`[/oh-yeah]: 🎶 🎶 🎶 ${env.VAR2} (ooh yeah, ooh yeah)🎶 🎶 🎶` | ||
); | ||
} | ||
|
||
return env.ASSETS.fetch(request); | ||
}, | ||
}; |
6 changes: 6 additions & 0 deletions
6
fixtures/pages-workerjs-with-config-file-app/public/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>🪩 Holiday! Celebrate! Pages supports 'wrangler.toml' 🪩</h1> | ||
</body> | ||
</html> |
65 changes: 65 additions & 0 deletions
65
fixtures/pages-workerjs-with-config-file-app/tests/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { resolve } from "node:path"; | ||
import { fetch } from "undici"; | ||
import { afterAll, beforeAll, describe, it } from "vitest"; | ||
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived"; | ||
|
||
describe("Pages Advanced Mode with wrangler.toml", () => { | ||
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; | ||
|
||
beforeAll(async () => { | ||
({ ip, port, stop } = await runWranglerPagesDev( | ||
resolve(__dirname, ".."), | ||
undefined, | ||
["--port=0", "--inspector-port=0"] | ||
)); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stop?.(); | ||
}); | ||
|
||
it("should render static pages", async ({ expect }) => { | ||
const response = await fetch(`http://${ip}:${port}/`); | ||
const text = await response.text(); | ||
expect(text).toContain( | ||
"🪩 Holiday! Celebrate! Pages supports 'wrangler.toml' 🪩" | ||
); | ||
}); | ||
|
||
it("should run our _worker.js, and correctly apply the routing rules provided in the custom _routes.json file", async ({ | ||
expect, | ||
}) => { | ||
// matches `/holiday` include rule | ||
let response = await fetch(`http://${ip}:${port}/holiday`); | ||
let text = await response.text(); | ||
expect(text).toEqual( | ||
`[/holiday]:\n` + | ||
`🎶 🎶 🎶\n` + | ||
`If we took a holiday\n` + | ||
`Took some time to celebrate\n` + | ||
`Just one day out of life\n` + | ||
`It would be, it would be so nice 🎉\n` + | ||
`🎶 🎶 🎶` | ||
); | ||
|
||
// matches `/celebrate` include rule | ||
response = await fetch(`http://${ip}:${port}/celebrate`); | ||
text = await response.text(); | ||
expect(text).toEqual( | ||
`[/celebrate]:\n` + | ||
`🎶 🎶 🎶\n` + | ||
`Everybody spread the word\n` + | ||
`We're gonna have a celebration\n` + | ||
`All across the world\n` + | ||
`In every nation\n` + | ||
`🎶 🎶 🎶` | ||
); | ||
|
||
// matches `/oh-yeah` exclude rule | ||
response = await fetch(`http://${ip}:${port}/oh-yeah`); | ||
text = await response.text(); | ||
expect(text).toContain( | ||
"🪩 Holiday! Celebrate! Pages supports 'wrangler.toml' 🪩" | ||
); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
fixtures/pages-workerjs-with-config-file-app/tests/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"include": ["**/*.ts", "../../../node-types.d.ts"] | ||
} |
13 changes: 13 additions & 0 deletions
13
fixtures/pages-workerjs-with-config-file-app/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"esModuleInterop": true, | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["node"], | ||
"moduleResolution": "node", | ||
"noEmit": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["tests", "../../node-types.d.ts"] | ||
} |
9 changes: 9 additions & 0 deletions
9
fixtures/pages-workerjs-with-config-file-app/vitest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineProject, mergeConfig } from "vitest/config"; | ||
import configShared from "../../vitest.shared"; | ||
|
||
export default mergeConfig( | ||
configShared, | ||
defineProject({ | ||
test: {}, | ||
}) | ||
); |
10 changes: 10 additions & 0 deletions
10
fixtures/pages-workerjs-with-config-file-app/wrangler.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pages_build_output_dir = "./public" | ||
|
||
name = "pages-with-config-file-app" | ||
compatibility_date = "2024-01-01" | ||
#compatibility_flags = [] | ||
|
||
[vars] | ||
VAR1 = "celebrate" | ||
VAR2 = "holiday" | ||
VAR3 = "celebration" |
Oops, something went wrong.