Skip to content

Commit 1ead9db

Browse files
committed
improve
1 parent 7491118 commit 1ead9db

File tree

5 files changed

+72
-42
lines changed

5 files changed

+72
-42
lines changed

.changeset/async-read-config.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
---
22
"wrangler": minor
3-
"@cloudflare/vite-plugin": minor
4-
"@cloudflare/vitest-pool-workers": minor
3+
"@cloudflare/workers-utils": minor
54
---
65

7-
Make the exported APIs from Wrangler `experimental_readRawConfig()`, `unstable_getMiniflareWorkerOptions()`, and `unstable_readConfig()` async.
6+
Add new async config reading APIs to support future code-based config files.
87

9-
If you'd previously been relying on these unstable APIs, update the callsite to `await` the promise:
8+
- `unstable_readConfigAsync` - Async version of `unstable_readConfig` that will support code-based config files (`.ts`, `.js`)
9+
- `experimental_readRawConfigAsync` - Async version of `experimental_readRawConfig`
1010

11-
```diff
12-
- const config = wrangler.unstable_readConfig()
13-
+ const config = await wrangler.unstable_readConfig()
14-
```
11+
The existing sync APIs (`unstable_readConfig`, `experimental_readRawConfig`) continue to work unchanged for data file formats (`.toml`, `.json`, `.jsonc`).
12+
13+
In Wrangler v5, the sync APIs will be removed and the async APIs will become the default.

packages/workers-utils/src/config/index.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,24 @@ const parseRawConfigFile = (configPath: string): RawConfig => {
120120
return {};
121121
};
122122

123+
export type ReadRawConfigResult = {
124+
rawConfig: RawConfig;
125+
configPath: string | undefined;
126+
userConfigPath: string | undefined;
127+
deployConfigPath: string | undefined;
128+
redirected: boolean;
129+
};
130+
131+
/**
132+
* Synchronously read the raw Wrangler configuration from a data file (toml, json, jsonc).
133+
*
134+
* This function only supports data file formats. For code-based config files,
135+
* use `unstable_readRawConfigAsync` instead.
136+
*/
123137
export const experimental_readRawConfig = (
124138
args: ReadConfigCommandArgs,
125139
options: ReadConfigOptions = {}
126-
):
127-
| {
128-
rawConfig: RawConfig;
129-
configPath: string | undefined;
130-
userConfigPath: string | undefined;
131-
deployConfigPath: string | undefined;
132-
redirected: boolean;
133-
}
134-
| Promise<{
135-
rawConfig: RawConfig;
136-
configPath: string | undefined;
137-
userConfigPath: string | undefined;
138-
deployConfigPath: string | undefined;
139-
redirected: boolean;
140-
}> => {
140+
): ReadRawConfigResult => {
141141
// Load the configuration from disk if available
142142
const { configPath, userConfigPath, deployConfigPath, redirected } =
143143
resolveWranglerConfigPath(args, options);
@@ -152,3 +152,18 @@ export const experimental_readRawConfig = (
152152
redirected,
153153
};
154154
};
155+
156+
/**
157+
* Asynchronously read the raw Wrangler configuration.
158+
*
159+
* This function supports both data file formats (toml, json, jsonc) and
160+
* will support code-based config files (ts, js, mjs) in the future.
161+
*
162+
* In Wrangler v5, this will become the default and be renamed to `experimental_readRawConfig`.
163+
*/
164+
export const experimental_readRawConfigAsync = async (
165+
args: ReadConfigCommandArgs,
166+
options: ReadConfigOptions = {}
167+
): Promise<ReadRawConfigResult> => {
168+
return experimental_readRawConfig(args, options);
169+
};

packages/workers-utils/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export {
1616
configFormat,
1717
configFileName,
1818
experimental_readRawConfig,
19+
experimental_readRawConfigAsync,
20+
type ReadRawConfigResult,
1921
} from "./config";
2022
export {
2123
experimental_patchConfig,

packages/wrangler/src/api/integrations/platform/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from "@cloudflare/workers-utils";
77
import { kCurrentWorker, Miniflare } from "miniflare";
88
import { getAssetsOptions, NonExistentAssetsDirError } from "../../../assets";
9-
import { readConfig } from "../../../config";
9+
import { readConfig, readConfigAsync } from "../../../config";
1010
import { partitionDurableObjectBindings } from "../../../deployment-bundle/entry";
1111
import { DEFAULT_MODULE_RULES } from "../../../deployment-bundle/rules";
1212
import { getBindings } from "../../../dev";
@@ -42,6 +42,7 @@ import type {
4242

4343
export { getVarsForDev as unstable_getVarsForDev } from "../../../dev/dev-vars";
4444
export { readConfig as unstable_readConfig };
45+
export { readConfigAsync as unstable_readConfigAsync };
4546
export { getDurableObjectClassNameToUseSQLiteMap as unstable_getDurableObjectClassNameToUseSQLiteMap };
4647

4748
/**
@@ -151,7 +152,7 @@ export async function getPlatformProxy<
151152
): Promise<PlatformProxy<Env, CfProperties>> {
152153
const env = options.environment;
153154

154-
const config = await readConfig({
155+
const config = await readConfigAsync({
155156
config: options.configPath,
156157
env,
157158
});
@@ -388,7 +389,7 @@ export async function unstable_getMiniflareWorkerOptions(
388389
): Promise<Unstable_MiniflareWorkerOptions> {
389390
const config =
390391
typeof configOrConfigPath === "string"
391-
? await readConfig({ config: configOrConfigPath, env })
392+
? await readConfigAsync({ config: configOrConfigPath, env })
392393
: configOrConfigPath;
393394

394395
const modulesRules: ModuleRule[] = config.rules

packages/wrangler/src/config/index.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33
import {
44
configFileName,
55
experimental_readRawConfig,
6+
experimental_readRawConfigAsync,
67
FatalError,
78
isPagesConfig,
89
normalizeAndValidateConfig,
@@ -16,6 +17,7 @@ import type {
1617
Config,
1718
NormalizeAndValidateConfigArgs,
1819
RawConfig,
20+
ReadRawConfigResult,
1921
ResolveConfigPathOptions,
2022
} from "@cloudflare/workers-utils";
2123

@@ -60,7 +62,7 @@ function convertRawConfigToConfig(
6062
userConfigPath,
6163
deployConfigPath,
6264
redirected,
63-
}: Awaited<ReturnType<typeof experimental_readRawConfig>>
65+
}: ReturnType<typeof experimental_readRawConfig>
6466
): Config {
6567
if (redirected) {
6668
assert(configPath, "Redirected config found without a configPath");
@@ -95,36 +97,47 @@ function convertRawConfigToConfig(
9597
}
9698

9799
/**
98-
* Get the Wrangler configuration; read it from the give `configPath` if available.
100+
* Synchronously get the Wrangler configuration from a data file (toml, json, jsonc).
101+
*
102+
* This function only supports data file formats. For code-based config files,
103+
* use `unstable_readConfigAsync` instead.
99104
*/
100105
export function readConfig(
101106
args: ReadConfigCommandArgs,
102107
options: ReadConfigOptions = {}
103-
): Config | Promise<Config> {
104-
const configOrPromise = experimental_readRawConfig(args, options);
105-
if ("then" in configOrPromise) {
106-
return configOrPromise.then((raw) =>
107-
convertRawConfigToConfig(args, options, raw)
108-
);
109-
} else {
110-
return convertRawConfigToConfig(args, options, configOrPromise);
111-
}
108+
): Config {
109+
const raw = experimental_readRawConfig(args, options);
110+
return convertRawConfigToConfig(args, options, raw);
111+
}
112+
113+
/**
114+
* Asynchronously get the Wrangler configuration.
115+
*
116+
* This function supports both data file formats (toml, json, jsonc) and
117+
* will support code-based config files (ts, js, mjs) in the future.
118+
*
119+
* In Wrangler v5, this will become the default `readConfig`.
120+
*/
121+
export async function readConfigAsync(
122+
args: ReadConfigCommandArgs,
123+
options: ReadConfigOptions = {}
124+
): Promise<Config> {
125+
const raw = await experimental_readRawConfigAsync(args, options);
126+
return convertRawConfigToConfig(args, options, raw);
112127
}
113128

114-
export async function readPagesConfig(
129+
export function readPagesConfig(
115130
args: ReadConfigCommandArgs,
116131
options: ReadConfigOptions = {}
117-
): Promise<
118-
Omit<Config, "pages_build_output_dir"> & { pages_build_output_dir: string }
119-
> {
132+
): Omit<Config, "pages_build_output_dir"> & { pages_build_output_dir: string } {
120133
let rawConfig: RawConfig;
121134
let configPath: string | undefined;
122135
let userConfigPath: string | undefined;
123136
let redirected: boolean;
124137
let deployConfigPath: string | undefined;
125138
try {
126139
({ rawConfig, configPath, userConfigPath, deployConfigPath, redirected } =
127-
await experimental_readRawConfig(args, options));
140+
experimental_readRawConfig(args, options));
128141
if (redirected) {
129142
assert(configPath, "Redirected config found without a configPath");
130143
assert(

0 commit comments

Comments
 (0)