Skip to content

Commit 2808df2

Browse files
extend build tool example with env variable
1 parent b4f21ed commit 2808df2

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,28 +1304,36 @@ This is because such a file, when required, should be created based on a target
13041304

13051305
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.
13061306

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

1309-
<WranglerConfig>
1309+
<WranglerConfig>
13101310

1311-
```toml title="wrangler.toml"
1312-
name = "my-worker"
1313-
main = "src/index.ts"
1314-
[[kv_namespaces]]
1315-
binding = "<BINDING_NAME1>"
1316-
id = "<NAMESPACE_ID1>"
1317-
```
1311+
```toml title="wrangler.toml"
1312+
name = "my-worker"
1313+
main = "src/index.ts"
13181314

1319-
</WranglerConfig>
1315+
[[kv_namespaces]]
1316+
binding = "<BINDING_NAME1>"
1317+
1318+
[vars]
1319+
MY_VARIABLE = "production variable"
1320+
1321+
[env.staging.vars]
1322+
MY_VARIABLE = "staging variable"
1323+
```
1324+
1325+
</WranglerConfig>
13201326

1321-
Note that this configuration points `main` at the user's code entry-point.
1327+
Note that this configuration points `main` at the user's code entry-point and that it also defined the `MY_VARIABLE` in two different environments.
13221328

13231329
- Then, the user runs a custom build, which might read the user's Wrangler configuration file to find the source code entry-point:
13241330

13251331
```bash
13261332
> my-tool build
13271333
```
13281334

1335+
We'll assume that `my-tool` is specifically targeting the `staging` environment (for instance set by the user with some API of `my-tool`).
1336+
13291337
- This `my-tool` generates a `dist` directory that contains both compiled code and a new generated deployment configuration file.
13301338
It also creates a `.wrangler/deploy/config.json` file that redirects Wrangler to the new, generated deployment configuration file:
13311339

@@ -1346,11 +1354,17 @@ Note that this configuration points `main` at the user's code entry-point.
13461354
{
13471355
"name": "my-worker",
13481356
"main": "./index.js",
1349-
"kv_namespaces": [{ "binding": "<BINDING_NAME1>", "id": "<NAMESPACE_ID1>" }]
1357+
"kv_namespaces": [
1358+
{ "binding": "<BINDING_NAME1>", "id": "<NAMESPACE_ID1>" }
1359+
],
1360+
"vars": {
1361+
"MY_VARIABLE": "staging variable"
1362+
}
13501363
}
13511364
```
13521365

1353-
Note that, now, the `main` property points to the generated code entry-point.
1366+
Note that, now, the `main` property points to the generated code entry-point, no environment is defined,
1367+
and the `MY_VARIABLE` variable is resolved to the staging environment value.
13541368

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

0 commit comments

Comments
 (0)