Skip to content

Commit 27e0aab

Browse files
petebacondarwinpenalosa
authored andcommitted
feat: add support for redirecting Wrangler to a generated config when running deploy commands (#7442)
* Do not console log wrangler output during normal fixture tests This creates a lot of unwanted logging in the console. The logging is still dumped to the console if the test times out, or if `WRANGLER_LOG=debug` is set as env var. * no need to get relative config path for calls to verifyWorkerMatchesCITag() * feat: add support for redirecting Wrangler to a generated config when running deploy 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 commands. It is not expected that developers of Workers will need to use this feature directly. The commands that use this feature are: - `wrangler deploy` - `wrangler dev` - `wrangler versions upload` - `wrangler versions deploy` 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 use the `configPath` (relative to the `deploy.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. 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" } ``` * add config redirect fixture with tests * Add config redirect support for Pages commands * refactor: compute both the actual config path and the original user's configuration * add `userConfigPath` to the `Config` type so it can be used where needed * add fixture and test for redirected config in a pages project * Do not check formatting of generated fixture files * clean node_modules for npm-import fixture before testing * fix unstable_dev tests * run more Wrangler e2e tests on safe ports * Requires that you create a `.env` file in the `packages/wrangler` directory containing: ``` CLOUDFLARE_ACCOUNT_ID=<dev-prod-account-id> CLOUDFLARE_API_TOKEN=<dev-prod-account-api-key> WRANGLER="node <path to workers-sdk/packages/wrangler/bin/wrangler.js>" WRANGLER_IMPORT="<path to /workers-sdk/packages/wrangler/wrangler-dist/cli.js>" ```` * correctly configure python workers when no command line script is provided * test: ensure that `pages functions build-env` command outputs correct relative pages_build_output_dir path * Improve error message when deploy/config.json is not valid * Complete jsdoc description * Update jsdocs to note that the result of `resolveWranglerConfigPath()` contains absolute paths. * PR feedback * rename `useRedirect` to `useRedirectIfAvailable` * Rename command definition behaviour from "useConfigRedirect" to "useConfigRedirectIfAvailable" * Add clarifying comment * Do not store package-lock.json for import-npm fixture This fixture does not need a package lockfile to work. * Remove unnecessary option in test
1 parent 9138692 commit 27e0aab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1788
-793
lines changed

.changeset/thin-pots-camp.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: add support for redirecting Wrangler to a generated config when running deploy-related commands
6+
7+
This new feature is designed for build tools and frameworks to provide a deploy-specific configuration,
8+
which Wrangler can use instead of user configuration when running deploy-related commands.
9+
It is not expected that developers of Workers will need to use this feature directly.
10+
11+
### Affected commands
12+
13+
The commands that use this feature are:
14+
15+
- `wrangler deploy`
16+
- `wrangler dev`
17+
- `wrangler versions upload`
18+
- `wrangler versions deploy`
19+
- `wrangler pages deploy`
20+
- `wrangler pages build`
21+
- `wrangler pages build-env`
22+
23+
### Config redirect file
24+
25+
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:
26+
27+
```json
28+
{ "configPath": "../../path/to/wrangler.json" }
29+
```
30+
31+
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.
32+
33+
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.
34+
35+
### Custom build tool example
36+
37+
A common approach that a build tool might choose to implement.
38+
39+
- The user writes code that uses Cloudflare Workers resources, configured via a user `wrangler.toml` file.
40+
41+
```toml
42+
name = "my-worker"
43+
main = "src/index.ts"
44+
[[kv_namespaces]]
45+
binding = "<BINDING_NAME1>"
46+
id = "<NAMESPACE_ID1>"
47+
```
48+
49+
Note that this configuration points `main` at user code entry-point.
50+
51+
- The user runs a custom build, which might read the `wrangler.toml` to find the entry-point:
52+
53+
```bash
54+
> my-tool build
55+
```
56+
57+
- 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:
58+
59+
```plain
60+
- dist
61+
- index.js
62+
- wrangler.json
63+
- .wrangler
64+
- deploy
65+
- config.json
66+
```
67+
68+
The `dist/wrangler.json` will contain:
69+
70+
```json
71+
{
72+
"name": "my-worker",
73+
"main": "./index.js",
74+
"kv_namespaces": [{ "binding": "<BINDING_NAME1>", "id": "<NAMESPACE_ID1>" }]
75+
}
76+
```
77+
78+
And the `.wrangler/deploy/config.json` will contain:
79+
80+
```json
81+
{
82+
"configPath": "../../dist/wrangler.json"
83+
}
84+
```

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ templates/*/dist
4040

4141
# This file intentionally has a syntax error
4242
fixtures/interactive-dev-tests/src/startup-error.ts
43+
44+
# These are generated by the build step
45+
fixtures/pages-redirected-config/build/*
46+
fixtures/redirected-config-worker/build/*

fixtures/import-npm/package-lock.json

Lines changed: 0 additions & 185 deletions
This file was deleted.

fixtures/import-npm/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"packages/*"
88
],
99
"scripts": {
10-
"check:type": "rm -rf node_modules && npm install && npm run check:type --workspaces",
11-
"test:ci": "npm install && npm run test:ci --workspaces",
12-
"test:watch": "npm install && npm run test:watch --workspaces",
13-
"type:tests": "rm -rf node_modules && npm install && npm run type:tests --workspaces"
10+
"_clean_install": "rm -rf node_modules && npm install --no-package-lock",
11+
"check:type": "npm run check:type --workspaces",
12+
"test:ci": "npm run test:ci --workspaces",
13+
"test:watch": "npm run test:watch --workspaces",
14+
"type:tests": "npm run type:tests --workspaces"
1415
}
1516
}

fixtures/import-npm/turbo.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
{
22
"extends": ["//"],
33
"tasks": {
4-
"test": {
5-
"dependsOn": ["wrangler#build"]
4+
"_clean_install": {},
5+
"check:type": {
6+
"dependsOn": ["_clean_install"]
7+
},
8+
"test:watch": {
9+
"dependsOn": ["_clean_install"]
10+
},
11+
"type:tests": {
12+
"dependsOn": ["_clean_install"]
613
},
714
"test:ci": {
8-
"dependsOn": ["wrangler#build"]
15+
"dependsOn": ["_clean_install", "wrangler#build"]
916
}
1017
}
1118
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
build
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "pages-redirected-config",
3+
"private": true,
4+
"description": "",
5+
"license": "ISC",
6+
"author": "",
7+
"main": "src/index.js",
8+
"scripts": {
9+
"build": "node -r esbuild-register tools/build.ts",
10+
"check:type": "tsc",
11+
"dev": "pnpm run build && wrangler pages dev",
12+
"test:ci": "pnpm run build && vitest run"
13+
},
14+
"devDependencies": {
15+
"@cloudflare/workers-tsconfig": "workspace:^",
16+
"undici": "catalog:default",
17+
"vitest": "catalog:default",
18+
"wrangler": "workspace:*"
19+
},
20+
"volta": {
21+
"extends": "../../package.json"
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
async fetch(request, env) {
3+
return new Response("Generated: " + env.generated ?? false);
4+
},
5+
};

0 commit comments

Comments
 (0)