Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for redirecting Wrangler to a generated config when running deploy commands #7442

Merged
merged 24 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fadaab2
Do not console log wrangler output during normal fixture tests
petebacondarwin Dec 12, 2024
0b2d1fe
no need to get relative config path for calls to verifyWorkerMatchesC…
petebacondarwin Dec 13, 2024
583794b
feat: add support for redirecting Wrangler to a generated config when…
petebacondarwin Dec 4, 2024
20b1ae1
add config redirect fixture with tests
petebacondarwin Dec 12, 2024
e7b4a67
Add config redirect support for Pages commands
petebacondarwin Dec 11, 2024
c169d84
refactor: compute both the actual config path and the original user's…
petebacondarwin Dec 13, 2024
e3fa954
add `userConfigPath` to the `Config` type so it can be used where needed
petebacondarwin Dec 13, 2024
c75a983
add fixture and test for redirected config in a pages project
petebacondarwin Dec 13, 2024
dda0e73
Do not check formatting of generated fixture files
petebacondarwin Dec 14, 2024
9bf9d6c
clean node_modules for npm-import fixture before testing
petebacondarwin Dec 15, 2024
722d620
fix unstable_dev tests
petebacondarwin Dec 16, 2024
8141f3b
run more Wrangler e2e tests on safe ports
petebacondarwin Dec 16, 2024
3a2a531
Requires that you create a `.env` file in the `packages/wrangler` dir…
petebacondarwin Dec 16, 2024
04d5e76
correctly configure python workers when no command line script is pro…
petebacondarwin Dec 16, 2024
3b73460
test: ensure that `pages functions build-env` command outputs correct…
petebacondarwin Dec 18, 2024
7b7c6ee
Improve error message when deploy/config.json is not valid
petebacondarwin Dec 20, 2024
1fc2705
Complete jsdoc description
petebacondarwin Dec 20, 2024
dd5b2d7
Update jsdocs to note that the result of `resolveWranglerConfigPath()…
petebacondarwin Dec 20, 2024
db7ff7d
PR feedback
petebacondarwin Dec 23, 2024
fc72a15
rename `useRedirect` to `useRedirectIfAvailable`
petebacondarwin Dec 23, 2024
893f7e9
Rename command definition behaviour from "useConfigRedirect" to "useC…
petebacondarwin Dec 23, 2024
88544ec
Add clarifying comment
petebacondarwin Dec 23, 2024
cb58edb
Do not store package-lock.json for import-npm fixture
petebacondarwin Dec 23, 2024
fb21671
Remove unnecessary option in test
petebacondarwin Dec 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .changeset/thin-pots-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
"wrangler": minor
---

feat: add support for redirecting Wrangler to a generated config when running deploy-related commands

This new feature is designed for build tools and frameworks to provide a deploy-specific configuration,
which Wrangler can use instead of user configuration when running deploy-related commands.
It is not expected that developers of Workers will need to use this feature directly.

### Affected commands

The commands that use this feature are:

- `wrangler deploy`
- `wrangler dev`
- `wrangler versions upload`
- `wrangler versions deploy`
- `wrangler pages deploy`
- `wrangler pages build`
- `wrangler pages build-env`

### Config redirect file

When running these commands, Wrangler will look up the directory tree from the current working directory for a file at the path `.wrangler/deploy/config.json`. This file must contain only a single JSON object of the form:

```json
{ "configPath": "../../path/to/wrangler.json" }
```

When this file exists Wrangler will follow the `configPath` (relative to the `.wrangler/deploy/config.json` file) to find an alternative Wrangler configuration file to load and use as part of this command.

When this happens Wrangler will display a warning to the user to indicate that the configuration has been redirected to a different file than the user's configuration file.

### Custom build tool example

A common approach that a build tool might choose to implement.

- The user writes code that uses Cloudflare Workers resources, configured via a user `wrangler.toml` file.

```toml
name = "my-worker"
main = "src/index.ts"
[[kv_namespaces]]
binding = "<BINDING_NAME1>"
id = "<NAMESPACE_ID1>"
```

Note that this configuration points `main` at user code entry-point.

- The user runs a custom build, which might read the `wrangler.toml` to find the entry-point:

```bash
> my-tool build
```

- This tool generates a `dist` directory that contains both compiled code and a new deployment configuration file, but also a `.wrangler/deploy/config.json` file that redirects Wrangler to this new deployment configuration file:

```plain
- dist
- index.js
- wrangler.json
- .wrangler
- deploy
- config.json
```

The `dist/wrangler.json` will contain:

```json
{
"name": "my-worker",
"main": "./index.js",
"kv_namespaces": [{ "binding": "<BINDING_NAME1>", "id": "<NAMESPACE_ID1>" }]
}
```

And the `.wrangler/deploy/config.json` will contain:

```json
{
"configPath": "../../dist/wrangler.json"
}
```
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ templates/*/dist

# This file intentionally has a syntax error
fixtures/interactive-dev-tests/src/startup-error.ts

# These are generated by the build step
fixtures/pages-redirected-config/build/*
fixtures/redirected-config-worker/build/*
185 changes: 0 additions & 185 deletions fixtures/import-npm/package-lock.json

This file was deleted.

9 changes: 5 additions & 4 deletions fixtures/import-npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"packages/*"
],
"scripts": {
"check:type": "rm -rf node_modules && npm install && npm run check:type --workspaces",
"test:ci": "npm install && npm run test:ci --workspaces",
"test:watch": "npm install && npm run test:watch --workspaces",
"type:tests": "rm -rf node_modules && npm install && npm run type:tests --workspaces"
"_clean_install": "rm -rf node_modules && npm install --no-package-lock",
"check:type": "npm run check:type --workspaces",
"test:ci": "npm run test:ci --workspaces",
"test:watch": "npm run test:watch --workspaces",
"type:tests": "npm run type:tests --workspaces"
}
}
13 changes: 10 additions & 3 deletions fixtures/import-npm/turbo.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"extends": ["//"],
"tasks": {
"test": {
"dependsOn": ["wrangler#build"]
"_clean_install": {},
"check:type": {
"dependsOn": ["_clean_install"]
},
"test:watch": {
"dependsOn": ["_clean_install"]
},
"type:tests": {
"dependsOn": ["_clean_install"]
},
"test:ci": {
"dependsOn": ["wrangler#build"]
"dependsOn": ["_clean_install", "wrangler#build"]
}
}
}
2 changes: 2 additions & 0 deletions fixtures/pages-redirected-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
build
23 changes: 23 additions & 0 deletions fixtures/pages-redirected-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "pages-redirected-config",
"private": true,
"description": "",
"license": "ISC",
"author": "",
"main": "src/index.js",
"scripts": {
"build": "node -r esbuild-register tools/build.ts",
"check:type": "tsc",
"dev": "pnpm run build && wrangler pages dev",
"test:ci": "pnpm run build && vitest run"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:^",
"undici": "catalog:default",
"vitest": "catalog:default",
"wrangler": "workspace:*"
},
"volta": {
"extends": "../../package.json"
}
}
5 changes: 5 additions & 0 deletions fixtures/pages-redirected-config/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch(request, env) {
return new Response("Generated: " + env.generated ?? false);
},
};
Loading
Loading