Skip to content

Commit 301168a

Browse files
committed
consolidate rpp logic in ProjectStructure
1 parent ecc5b54 commit 301168a

5 files changed

Lines changed: 49 additions & 54 deletions

File tree

packages/pages/src/build/build.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { Command } from "commander";
22
import { build } from "vite";
33
import { ProjectStructure } from "../common/src/project/structure.js";
4-
import {
5-
applyReverseProxyOverride,
6-
buildReverseProxyOverride,
7-
} from "../util/reverseProxyOverride.js";
84

95
/**
106
* The arguments passed to the build CLI command.
@@ -22,18 +18,7 @@ export interface BuildArgs {
2218
*/
2319
const handler = async (buildArgs: BuildArgs) => {
2420
const { scope, pluginFilesizeLimit, pluginTotalFilesizeLimit, reverseProxyPrefix } = buildArgs;
25-
const trimmedReverseProxyPrefix = reverseProxyPrefix?.trim();
26-
const projectStructure = await ProjectStructure.init({
27-
scope,
28-
reverseProxyPrefix: trimmedReverseProxyPrefix || undefined,
29-
});
30-
31-
if (projectStructure.config.reverseProxyPrefix) {
32-
const reverseProxyOverride = buildReverseProxyOverride(
33-
projectStructure.config.reverseProxyPrefix
34-
);
35-
applyReverseProxyOverride(projectStructure, reverseProxyOverride);
36-
}
21+
const projectStructure = await ProjectStructure.init({ scope }, reverseProxyPrefix);
3722

3823
// Pass CLI arguments as env variables to use in vite-plugin
3924
if (scope) {

packages/pages/src/common/src/project/structure.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,30 @@ describe("ProjectStructure.init", () => {
7575
try {
7676
fs.writeFileSync(
7777
path.join(tempDir, "vite.config.js"),
78-
'throw new Error("vite config loaded before reverse proxy override");\n'
78+
'throw new Error("vite config loaded before reverse proxy override");\nexport default { build: {} };\n'
7979
);
80+
fs.writeFileSync(path.join(tempDir, "config.yaml"), "");
8081
process.chdir(tempDir);
8182

82-
const projectStructure = await ProjectStructure.init({
83+
const projectStructure = await ProjectStructure.init(
84+
undefined,
85+
" www.brand.com/locations "
86+
);
87+
88+
expect(projectStructure.config.reverseProxyOverride).toEqual({
8389
reverseProxyPrefix: "www.brand.com/locations",
90+
assetsDir: "locations/assets",
91+
dynamicRoute: {
92+
from: "/assets/*",
93+
to: "/locations/assets/:splat",
94+
status: 200,
95+
},
8496
});
85-
8697
expect(projectStructure.config.subfolders.assets).toBe("locations/assets");
87-
expect(new ProjectStructure().config.reverseProxyPrefix).toBeUndefined();
98+
expect(fs.readFileSync(path.join(tempDir, "vite.config.js"), "utf-8")).toContain(
99+
'assetsDir: "locations/assets"'
100+
);
101+
expect(new ProjectStructure().config.reverseProxyOverride).toBeUndefined();
88102
} finally {
89103
fs.rmSync(tempDir, { recursive: true, force: true });
90104
}

packages/pages/src/common/src/project/structure.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import pathLib from "node:path";
22
import merge from "lodash/merge.js";
33
import fs from "node:fs";
44
import { Path } from "./path.js";
5-
import { parseReverseProxyPrefix } from "../../../util/reverseProxyOverride.js";
5+
import {
6+
applyReverseProxyOverride,
7+
buildReverseProxyOverride,
8+
type ReverseProxyOverride,
9+
} from "../../../util/reverseProxyOverride.js";
610
import { determineAssetsFilepath } from "../assets/getAssetsFilepath.js";
711
import { determinePublicFilepath } from "../assets/getPublicFilepath.js";
812

@@ -143,9 +147,9 @@ export interface ProjectStructureConfig {
143147
scope?: string;
144148

145149
/**
146-
* The reverse proxy prefix for the current build.
150+
* The build configuration derived from the reverse proxy prefix.
147151
*/
148-
reverseProxyPrefix?: string;
152+
reverseProxyOverride?: ReverseProxyOverride;
149153
}
150154

151155
const DEFAULT_ASSETS_DIR = "assets";
@@ -221,16 +225,20 @@ export class ProjectStructure {
221225
this.config = mergedConfig;
222226
}
223227

224-
static init = async (projectStructureConfig?: Optional<ProjectStructureConfig>) => {
228+
static init = async (
229+
projectStructureConfig?: Optional<ProjectStructureConfig>,
230+
reverseProxyPrefix?: string
231+
) => {
225232
const projectStructure = new ProjectStructure(projectStructureConfig);
226233
const config = projectStructure.config;
227234

228235
const viteConfigPath = projectStructure.getViteConfigPath()?.getAbsolutePath() ?? "";
229236

230-
if (config.reverseProxyPrefix) {
231-
const parsedReverseProxyPrefix = parseReverseProxyPrefix(config.reverseProxyPrefix);
232-
config.reverseProxyPrefix = parsedReverseProxyPrefix.reverseProxyPrefix;
233-
config.subfolders.assets = `${parsedReverseProxyPrefix.subpath}/assets`;
237+
if (reverseProxyPrefix) {
238+
const reverseProxyOverride = buildReverseProxyOverride(reverseProxyPrefix);
239+
config.reverseProxyOverride = reverseProxyOverride;
240+
config.subfolders.assets = reverseProxyOverride.assetsDir;
241+
applyReverseProxyOverride(projectStructure);
234242
} else {
235243
// TODO: handle other extensions
236244
const assetsDir = await determineAssetsFilepath(DEFAULT_ASSETS_DIR, viteConfigPath);

packages/pages/src/util/reverseProxyOverride.test.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import path from "node:path";
44
import { afterEach, describe, expect, it } from "vitest";
55
import { ProjectStructure } from "../common/src/project/structure.js";
66
import {
7-
applyReverseProxyOverride,
87
buildReverseProxyOverride,
98
updateConfigYaml,
109
updateViteConfig,
@@ -278,7 +277,7 @@ export default defineConfig({
278277
});
279278
});
280279

281-
describe("applyReverseProxyOverride", () => {
280+
describe("ProjectStructure.init with a reverse proxy override", () => {
282281
const previousCwd = process.cwd();
283282

284283
afterEach(() => {
@@ -302,12 +301,7 @@ describe("applyReverseProxyOverride", () => {
302301
);
303302
process.chdir(tempDir);
304303

305-
const reverseProxyOverride = buildReverseProxyOverride("www.brand.com/locations");
306-
const projectStructure = await ProjectStructure.init({
307-
scope: "brand",
308-
reverseProxyPrefix: reverseProxyOverride.reverseProxyPrefix,
309-
});
310-
applyReverseProxyOverride(projectStructure, reverseProxyOverride);
304+
await ProjectStructure.init({ scope: "brand" }, "www.brand.com/locations");
311305

312306
expect(fs.readFileSync(path.join(tempDir, "brand", "config.yaml"), "utf-8")).toContain(
313307
"reverseProxyPrefix: www.brand.com/locations"
@@ -338,12 +332,9 @@ describe("applyReverseProxyOverride", () => {
338332
);
339333
process.chdir(tempDir);
340334

341-
const reverseProxyOverride = buildReverseProxyOverride("www.brand.com/locations");
342-
const projectStructure = await ProjectStructure.init({
343-
scope: "brand",
344-
reverseProxyPrefix: reverseProxyOverride.reverseProxyPrefix,
345-
});
346-
expect(() => applyReverseProxyOverride(projectStructure, reverseProxyOverride)).not.toThrow();
335+
await expect(
336+
ProjectStructure.init({ scope: "brand" }, "www.brand.com/locations")
337+
).resolves.toBeDefined();
347338
expect(fs.readFileSync(path.join(tempDir, "vite.config.js"), "utf-8")).toContain(
348339
'assetsDir: "locations/assets"'
349340
);
@@ -360,14 +351,9 @@ describe("applyReverseProxyOverride", () => {
360351
fs.writeFileSync(path.join(tempDir, "vite.config.js"), "export default { build: {} };\n");
361352
process.chdir(tempDir);
362353

363-
const reverseProxyOverride = buildReverseProxyOverride("www.brand.com/locations");
364-
const projectStructure = await ProjectStructure.init({
365-
scope: "brand",
366-
reverseProxyPrefix: reverseProxyOverride.reverseProxyPrefix,
367-
});
368-
expect(() => applyReverseProxyOverride(projectStructure, reverseProxyOverride)).toThrow(
369-
/config\.yaml does not exist/
370-
);
354+
await expect(
355+
ProjectStructure.init({ scope: "brand" }, "www.brand.com/locations")
356+
).rejects.toThrow(/config\.yaml does not exist/);
371357
} finally {
372358
fs.rmSync(tempDir, { recursive: true, force: true });
373359
}

packages/pages/src/util/reverseProxyOverride.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ export const buildReverseProxyOverride = (reverseProxyPrefix: string): ReversePr
9595
* Updates the scoped config.yaml and vite.config.js files that the build will
9696
* read so the normal build pipeline picks up the reverse proxy override.
9797
*/
98-
export const applyReverseProxyOverride = (
99-
projectStructure: ProjectStructure,
100-
reverseProxyOverride: ReverseProxyOverride
101-
): void => {
98+
export const applyReverseProxyOverride = (projectStructure: ProjectStructure): void => {
99+
const reverseProxyOverride = projectStructure.config.reverseProxyOverride;
100+
if (!reverseProxyOverride) {
101+
throw new Error("Cannot apply reverse proxy override because none is configured.");
102+
}
103+
102104
const finisher = logger.timedLog({
103105
startLog: "Applying reverse proxy override",
104106
});

0 commit comments

Comments
 (0)