Skip to content

Commit c1022d6

Browse files
fixup! feat: support extending the user configuration
1 parent 0dddf37 commit c1022d6

File tree

5 files changed

+76
-49
lines changed

5 files changed

+76
-49
lines changed

packages/wrangler/src/__tests__/configuration.test.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe("readConfig()", () => {
7676
Object {
7777
"debug": "",
7878
"err": "",
79-
"info": "Extending with configuration found in .wrangler/config/extra.json.",
79+
"info": "Loading additional configuration from .wrangler/config/extra.json.",
8080
"out": "",
8181
"warn": "",
8282
}
@@ -177,19 +177,23 @@ describe("readConfig()", () => {
177177
}
178178

179179
expect(error.toString().replaceAll("\\", "/")).toMatchInlineSnapshot(`
180-
"Error: Extending with configuration found in .wrangler/config/extra.json.
181-
- Expected \\"compatibility_date\\" to be of type string but got 2021."
180+
"Error: Processing wrangler.toml configuration:
181+
182+
- Processing extra configuration found in .wrangler/config/extra.json.
183+
- Expected \\"compatibility_date\\" to be of type string but got 2021."
182184
`);
183185

184186
expect(std).toMatchInlineSnapshot(`
185187
Object {
186188
"debug": "",
187189
"err": "",
188-
"info": "",
190+
"info": "Loading additional configuration from .wrangler/config/extra.json.",
189191
"out": "",
190-
"warn": "▲ [WARNING] Extending with configuration found in .wrangler/config/extra.json.
192+
"warn": "▲ [WARNING] Processing wrangler.toml configuration:
193+
191194
192-
- Unexpected fields found in extended config field: \\"unexpected_property\\"
195+
- Processing extra configuration found in .wrangler/config/extra.json.
196+
- Unexpected fields found in extended config field: \\"unexpected_property\\"
193197
194198
",
195199
}
@@ -242,6 +246,32 @@ describe("readConfig()", () => {
242246
})
243247
);
244248
});
249+
250+
it("should support extending with a Pages output directory property", () => {
251+
const pages_build_output_dir = "./public";
252+
253+
writeWranglerToml({});
254+
writeExtraJson({
255+
pages_build_output_dir,
256+
});
257+
258+
const config = readConfig("wrangler.toml", {});
259+
expect(config).toEqual(
260+
expect.objectContaining({
261+
pages_build_output_dir: "./public",
262+
})
263+
);
264+
265+
expect(std).toMatchInlineSnapshot(`
266+
Object {
267+
"debug": "",
268+
"err": "",
269+
"info": "Loading additional configuration from .wrangler/config/extra.json.",
270+
"out": "",
271+
"warn": "",
272+
}
273+
`);
274+
});
245275
});
246276
});
247277

packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from "fs";
22
import TOML from "@iarna/toml";
3+
import { PagesConfigFields } from "../../config/config";
34
import { ensureDirectoryExistsSync } from "../../utils/filesystem";
45
import type { RawConfig, RawEnvironment } from "../../config";
56

@@ -36,7 +37,7 @@ export function writeWranglerJson(
3637
}
3738

3839
export function writeExtraJson(
39-
config: RawEnvironment = {},
40+
config: RawEnvironment & Partial<PagesConfigFields> = {},
4041
path = "./.wrangler/config/extra.json"
4142
) {
4243
ensureDirectoryExistsSync(path);

packages/wrangler/src/config/config.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ export type RawConfig = Partial<ConfigFields<RawDevConfig>> &
3030
DeprecatedConfigFields &
3131
EnvironmentMap & { $schema?: string };
3232

33-
// Pages-specific configuration fields
34-
interface PagesConfigFields {
35-
/**
36-
* The directory of static assets to serve.
37-
*
38-
* The presence of this field in `wrangler.toml` indicates a Pages project,
39-
* and will prompt the handling of the configuration file according to the
40-
* Pages-specific validation rules.
41-
*/
42-
pages_build_output_dir?: string;
43-
}
44-
4533
export interface ConfigFields<Dev extends RawDevConfig> {
4634
configPath: string | undefined;
4735

@@ -185,7 +173,7 @@ export interface ConfigFields<Dev extends RawDevConfig> {
185173
}
186174

187175
// Pages-specific configuration fields
188-
interface PagesConfigFields {
176+
export interface PagesConfigFields {
189177
/**
190178
* The directory of static assets to serve.
191179
*
Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { existsSync } from "node:fs";
22
import { dirname, relative, resolve } from "node:path";
33
import { defu } from "defu";
4-
import { UserError } from "../errors";
54
import { logger } from "../logger";
65
import { parseJSONC, readFileSync } from "../parse";
76
import { Diagnostics } from "./diagnostics";
87
import { normalizeAndValidateEnvironment } from "./validation";
98
import { validateAdditionalProperties } from "./validation-helpers";
10-
import type { Config } from "./config";
9+
import type { Config, PagesConfigFields } from "./config";
1110
import type { Environment } from "./environment";
1211

1312
/**
@@ -17,26 +16,25 @@ import type { Environment } from "./environment";
1716
export function extendConfiguration(
1817
configPath: string | undefined,
1918
userConfig: Config,
20-
hideWarnings: boolean
21-
): Config {
19+
hideMessages: boolean
20+
): { config: Config; diagnostics: Diagnostics } {
2221
// Handle extending the user configuration
2322
const extraPath = getExtraConfigPath(configPath && dirname(configPath));
2423
const extra = loadExtraConfig(extraPath);
2524
if (extra === undefined) {
26-
return userConfig;
25+
return { config: userConfig, diagnostics: new Diagnostics("") };
2726
}
2827

29-
const { config, diagnostics } = extra;
30-
if (!hideWarnings && !diagnostics.hasWarnings() && !diagnostics.hasErrors()) {
31-
logger.info(diagnostics.description);
28+
if (!hideMessages) {
29+
logger.info(
30+
`Loading additional configuration from ${relative(process.cwd(), extraPath)}.`
31+
);
3232
}
33-
if (diagnostics.hasWarnings() && !hideWarnings) {
34-
logger.warn(diagnostics.renderWarnings());
35-
}
36-
if (diagnostics.hasErrors()) {
37-
throw new UserError(diagnostics.renderErrors());
38-
}
39-
return defu<Config, [Config]>(config, userConfig);
33+
34+
return {
35+
config: defu<Config, [Config]>(extra.config, userConfig),
36+
diagnostics: extra.diagnostics,
37+
};
4038
}
4139

4240
/**
@@ -53,7 +51,7 @@ function getExtraConfigPath(projectRoot: string | undefined): string {
5351
*/
5452
function loadExtraConfig(configPath: string):
5553
| {
56-
config: Environment;
54+
config: Environment & PagesConfigFields;
5755
diagnostics: Diagnostics;
5856
}
5957
| undefined {
@@ -62,9 +60,12 @@ function loadExtraConfig(configPath: string):
6260
}
6361

6462
const diagnostics = new Diagnostics(
65-
`Extending with configuration found in ${relative(process.cwd(), configPath)}.`
63+
`Processing extra configuration found in ${relative(process.cwd(), configPath)}.`
64+
);
65+
const raw = parseJSONC<Environment & PagesConfigFields>(
66+
readFileSync(configPath),
67+
configPath
6668
);
67-
const raw = parseJSONC<Environment>(readFileSync(configPath), configPath);
6869
const config = normalizeAndValidateEnvironment(
6970
diagnostics,
7071
configPath,
@@ -76,8 +77,11 @@ function loadExtraConfig(configPath: string):
7677
diagnostics,
7778
"extended config",
7879
Object.keys(raw),
79-
[...Object.keys(config)]
80+
[...Object.keys(config), "pages_build_output_dir"]
8081
);
8182

82-
return { config, diagnostics };
83+
return {
84+
config: { ...config, pages_build_output_dir: raw.pages_build_output_dir },
85+
diagnostics,
86+
};
8387
}

packages/wrangler/src/config/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ export function readConfig(
8686
}
8787
}
8888

89+
// Process the top-level configuration. This is common for both
90+
// Workers and Pages
91+
let { config, diagnostics } = normalizeAndValidateConfig(
92+
rawConfig,
93+
configPath,
94+
args
95+
);
96+
97+
const extra = extendConfiguration(configPath, config, hideWarnings);
98+
config = extra.config;
99+
diagnostics.addChild(extra.diagnostics);
100+
89101
/**
90102
* Check if configuration file belongs to a Pages project.
91103
*
@@ -116,14 +128,6 @@ export function readConfig(
116128
);
117129
}
118130

119-
// Process the top-level configuration. This is common for both
120-
// Workers and Pages
121-
const { config, diagnostics } = normalizeAndValidateConfig(
122-
rawConfig,
123-
configPath,
124-
args
125-
);
126-
127131
if (diagnostics.hasWarnings() && !hideWarnings) {
128132
logger.warn(diagnostics.renderWarnings());
129133
}
@@ -152,7 +156,7 @@ export function readConfig(
152156

153157
applyPythonConfig(config, args);
154158

155-
return extendConfiguration(configPath, config, hideWarnings);
159+
return config;
156160
}
157161

158162
/**

0 commit comments

Comments
 (0)