Skip to content

Commit 40f5dbf

Browse files
dario-piotrowiczkodster28petebacondarwin
authored
add note about environments in generated wrangler configuration section (#21119)
--------- Co-authored-by: Kody Jackson <[email protected]> Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent 2e0b263 commit 40f5dbf

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/content/docs/workers/wrangler/configuration.mdx

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,33 +1478,39 @@ This file must contain only a single JSON object of the form:
14781478
When this `config.json` file exists, Wrangler will follow the `configPath` (relative to the `.wrangler/deploy/config.json` file) to find the generated Wrangler configuration file to load and use in the current command.
14791479
Wrangler will display messaging to the user to indicate that the configuration has been redirected to a different file than the user's configuration file.
14801480

1481+
The generated configuration file should not include any [environments](#environments).
1482+
This is because such a file, when required, should be created as part of a build step, which should already target a specific environment. These build tools should generate distinct deployment configuration files for different environments.
1483+
14811484
### Custom build tool example
14821485

14831486
A common example of using a redirected configuration is where a custom build tool, or framework, wants to modify the user's configuration to be used when deploying, by generating a new configuration in a `dist` directory.
14841487

1485-
- First, the user writes code that uses Cloudflare Workers resources, configured via a user's Wrangler configuration file.
1488+
- First, the user writes code that uses Cloudflare Workers resources, configured via a user's Wrangler configuration file like the following:
14861489

1487-
<WranglerConfig>
1490+
<WranglerConfig>
14881491

1489-
```toml title="wrangler.toml"
1490-
name = "my-worker"
1491-
main = "src/index.ts"
1492-
[[kv_namespaces]]
1493-
binding = "<BINDING_NAME1>"
1494-
id = "<NAMESPACE_ID1>"
1495-
```
1492+
```toml title="wrangler.toml"
1493+
name = "my-worker"
1494+
main = "src/index.ts"
14961495

1497-
</WranglerConfig>
1496+
[vars]
1497+
MY_VARIABLE = "production variable"
1498+
1499+
[env.staging.vars]
1500+
MY_VARIABLE = "staging variable"
1501+
```
1502+
1503+
</WranglerConfig>
14981504

1499-
Note that this configuration points `main` at the user's code entry-point.
1505+
This configuration points `main` at the user's code entry-point and defines the `MY_VARIABLE` variable in two different environments.
15001506

1501-
- Then, the user runs a custom build, which might read the user's Wrangler configuration file to find the source code entry-point:
1507+
- Then, the user runs a custom build for a given environment (for example `staging`). This will read the user's Wrangler configuration file to find the source code entry-point and environment specific settings:
15021508

15031509
```bash
1504-
> my-tool build
1510+
> my-tool build --env=staging
15051511
```
15061512

1507-
- This `my-tool` generates a `dist` directory that contains both compiled code and a new generated deployment configuration file.
1513+
- `my-tool` generates a `dist` directory that contains both compiled code and a new generated deployment configuration file, containing only the settings for the given environment.
15081514
It also creates a `.wrangler/deploy/config.json` file that redirects Wrangler to the new, generated deployment configuration file:
15091515

15101516
<FileTree>
@@ -1524,11 +1530,14 @@ Note that this configuration points `main` at the user's code entry-point.
15241530
{
15251531
"name": "my-worker",
15261532
"main": "./index.js",
1527-
"kv_namespaces": [{ "binding": "<BINDING_NAME1>", "id": "<NAMESPACE_ID1>" }]
1533+
"vars": {
1534+
"MY_VARIABLE": "staging variable"
1535+
}
15281536
}
15291537
```
15301538

1531-
Note that, now, the `main` property points to the generated code entry-point.
1539+
Now, the `main` property points to the generated code entry-point, no environment is defined,
1540+
and the `MY_VARIABLE` variable is resolved to the staging environment value.
15321541

15331542
And the `.wrangler/deploy/config.json` contains the path to the generated configuration file:
15341543

0 commit comments

Comments
 (0)