Skip to content

Commit 8076b05

Browse files
committed
refactor: restructure config types
1 parent aca0e8e commit 8076b05

File tree

12 files changed

+87
-63
lines changed

12 files changed

+87
-63
lines changed

packages/cloudflare/src/cli/build/build-worker.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,26 @@ export async function buildWorker(config: Config): Promise<void> {
3030
console.log(`\x1b[35m⚙️ Copying files...\n\x1b[0m`);
3131

3232
// Copy over client-side generated files
33-
await cp(join(config.paths.dotNext, "static"), join(config.paths.outputDir, "assets", "_next", "static"), {
33+
await cp(join(config.paths.source.dotNext, "static"), join(config.paths.output.assets, "_next", "static"), {
3434
recursive: true,
3535
});
3636

3737
// Copy over any static files (e.g. images) from the source project
38-
const publicDir = join(config.paths.sourceDir, "public");
38+
const publicDir = join(config.paths.source.root, "public");
3939
if (existsSync(publicDir)) {
40-
await cp(publicDir, join(config.paths.outputDir, "assets"), {
41-
recursive: true,
42-
});
40+
await cp(publicDir, config.paths.output.assets, { recursive: true });
4341
}
4442

4543
// Copy over prerendered assets (e.g. SSG routes)
4644
copyPrerenderedRoutes(config);
4745

4846
copyPackageCliFiles(packageDistDir, config);
4947

50-
const workerEntrypoint = join(config.paths.internalTemplates, "worker.ts");
51-
const workerOutputFile = join(config.paths.outputDir, "index.mjs");
48+
const workerEntrypoint = join(config.paths.internal.templates, "worker.ts");
49+
const workerOutputFile = join(config.paths.output.root, "index.mjs");
5250

5351
const nextConfigStr =
54-
readFileSync(join(config.paths.standaloneApp, "/server.js"), "utf8")?.match(
52+
readFileSync(join(config.paths.output.standaloneApp, "/server.js"), "utf8")?.match(
5553
/const nextConfig = ({.+?})\n/
5654
)?.[1] ?? {};
5755

@@ -72,15 +70,15 @@ export async function buildWorker(config: Config): Promise<void> {
7270
// Note: we apply an empty shim to next/dist/compiled/ws because it generates two `eval`s:
7371
// eval("require")("bufferutil");
7472
// eval("require")("utf-8-validate");
75-
"next/dist/compiled/ws": join(config.paths.internalTemplates, "shims", "empty.ts"),
73+
"next/dist/compiled/ws": join(config.paths.internal.templates, "shims", "empty.ts"),
7674
// Note: we apply an empty shim to next/dist/compiled/edge-runtime since (amongst others) it generated the following `eval`:
7775
// eval(getModuleCode)(module, module.exports, throwingRequire, params.context, ...Object.values(params.scopedContext));
7876
// which comes from https://github.com/vercel/edge-runtime/blob/6e96b55f/packages/primitives/src/primitives/load.js#L57-L63
7977
// QUESTION: Why did I encountered this but mhart didn't?
80-
"next/dist/compiled/edge-runtime": join(config.paths.internalTemplates, "shims", "empty.ts"),
78+
"next/dist/compiled/edge-runtime": join(config.paths.internal.templates, "shims", "empty.ts"),
8179
// `@next/env` is a library Next.js uses for loading dotenv files, for obvious reasons we need to stub it here
8280
// source: https://github.com/vercel/next.js/tree/0ac10d79720/packages/next-env
83-
"@next/env": join(config.paths.internalTemplates, "shims", "env.ts"),
81+
"@next/env": join(config.paths.internal.templates, "shims", "env.ts"),
8482
},
8583
define: {
8684
// config file used by Next.js, see: https://github.com/vercel/next.js/blob/68a7128/packages/next/src/build/utils.ts#L2137-L2139
@@ -174,10 +172,10 @@ function createFixRequiresESBuildPlugin(config: Config): Plugin {
174172
setup(build) {
175173
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
176174
build.onResolve({ filter: /^\.\/require-hook$/ }, () => ({
177-
path: join(config.paths.internalTemplates, "shims", "empty.ts"),
175+
path: join(config.paths.internal.templates, "shims", "empty.ts"),
178176
}));
179177
build.onResolve({ filter: /\.\/lib\/node-fs-methods$/ }, () => ({
180-
path: join(config.paths.internalTemplates, "shims", "empty.ts"),
178+
path: join(config.paths.internal.templates, "shims", "empty.ts"),
181179
}));
182180
},
183181
};

packages/cloudflare/src/cli/build/patches/investigated/copy-package-cli-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { join } from "node:path";
88
export function copyPackageCliFiles(packageDistDir: string, config: Config) {
99
console.log("# copyPackageTemplateFiles");
1010
const sourceDir = join(packageDistDir, "cli");
11-
const destinationDir = join(config.paths.internalPackage, "cli");
11+
const destinationDir = join(config.paths.internal.package, "cli");
1212

1313
cpSync(sourceDir, destinationDir, { recursive: true });
1414
}

packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export async function patchCache(code: string, config: Config): Promise<string>
1717
console.log("# patchCache");
1818

1919
const cacheHandlerFileName = "cache-handler.mjs";
20-
const cacheHandlerEntrypoint = join(config.paths.internalTemplates, "cache-handler", "index.ts");
21-
const cacheHandlerOutputFile = join(config.paths.outputDir, cacheHandlerFileName);
20+
const cacheHandlerEntrypoint = join(config.paths.internal.templates, "cache-handler", "index.ts");
21+
const cacheHandlerOutputFile = join(config.paths.output.root, cacheHandlerFileName);
2222

2323
await build({
2424
entryPoints: [cacheHandlerEntrypoint],

packages/cloudflare/src/cli/build/patches/investigated/update-webpack-chunks-file/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { join } from "node:path";
1111
*/
1212
export async function updateWebpackChunksFile(config: Config) {
1313
console.log("# updateWebpackChunksFile");
14-
const webpackRuntimeFile = join(config.paths.standaloneAppServer, "webpack-runtime.js");
14+
const webpackRuntimeFile = join(config.paths.output.standaloneAppServer, "webpack-runtime.js");
1515

1616
const fileContent = readFileSync(webpackRuntimeFile, "utf-8");
1717

18-
const chunks = readdirSync(join(config.paths.standaloneAppServer, "chunks"))
18+
const chunks = readdirSync(join(config.paths.output.standaloneAppServer, "chunks"))
1919
.filter((chunk) => /^\d+\.js$/.test(chunk))
2020
.map((chunk) => {
2121
console.log(` - chunk ${chunk}`);

packages/cloudflare/src/cli/build/patches/to-investigate/inline-eval-manifest.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ import { normalizePath } from "../../utils";
1313
export function inlineEvalManifest(code: string, config: Config): string {
1414
console.log("# inlineEvalManifest");
1515
const manifestJss = globSync(
16-
normalizePath(join(config.paths.standaloneAppDotNext, "**", "*_client-reference-manifest.js"))
17-
).map((file) => normalizePath(file).replace(normalizePath(config.paths.standaloneApp) + posix.sep, ""));
16+
normalizePath(join(config.paths.output.standaloneAppDotNext, "**", "*_client-reference-manifest.js"))
17+
).map((file) =>
18+
normalizePath(file).replace(normalizePath(config.paths.output.standaloneApp) + posix.sep, "")
19+
);
1820
return code.replace(
1921
/function evalManifest\((.+?), .+?\) {/,
2022
`$&
2123
${manifestJss
2224
.map(
2325
(manifestJs) => `
2426
if ($1.endsWith("${manifestJs}")) {
25-
require(${JSON.stringify(join(config.paths.standaloneApp, manifestJs))});
27+
require(${JSON.stringify(join(config.paths.output.standaloneApp, manifestJs))});
2628
return {
2729
__RSC_MANIFEST: {
2830
"${manifestJs

packages/cloudflare/src/cli/build/patches/to-investigate/inline-middleware-manifest-require.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { join } from "node:path";
99
export function inlineMiddlewareManifestRequire(code: string, config: Config) {
1010
console.log("# inlineMiddlewareManifestRequire");
1111

12-
const middlewareManifestPath = join(config.paths.standaloneAppServer, "middleware-manifest.json");
12+
const middlewareManifestPath = join(config.paths.output.standaloneAppServer, "middleware-manifest.json");
1313

1414
const middlewareManifest = existsSync(middlewareManifestPath)
1515
? JSON.parse(readFileSync(middlewareManifestPath, "utf-8"))

packages/cloudflare/src/cli/build/patches/to-investigate/inline-next-require.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { join } from "node:path";
88
*/
99
export function inlineNextRequire(code: string, config: Config) {
1010
console.log("# inlineNextRequire");
11-
const pagesManifestFile = join(config.paths.standaloneAppServer, "pages-manifest.json");
12-
const appPathsManifestFile = join(config.paths.standaloneAppServer, "app-paths-manifest.json");
11+
const pagesManifestFile = join(config.paths.output.standaloneAppServer, "pages-manifest.json");
12+
const appPathsManifestFile = join(config.paths.output.standaloneAppServer, "app-paths-manifest.json");
1313

1414
const pagesManifestFiles = existsSync(pagesManifestFile)
1515
? Object.values(JSON.parse(readFileSync(pagesManifestFile, "utf-8"))).map(
@@ -33,7 +33,7 @@ export function inlineNextRequire(code: string, config: Config) {
3333
.map(
3434
(htmlPage) => `
3535
if (pagePath.endsWith("${htmlPage}")) {
36-
return ${JSON.stringify(readFileSync(join(config.paths.standaloneApp, htmlPage), "utf-8"))};
36+
return ${JSON.stringify(readFileSync(join(config.paths.output.standaloneApp, htmlPage), "utf-8"))};
3737
}
3838
`
3939
)
@@ -42,7 +42,7 @@ export function inlineNextRequire(code: string, config: Config) {
4242
.map(
4343
(module) => `
4444
if (pagePath.endsWith("${module}")) {
45-
return require(${JSON.stringify(join(config.paths.standaloneApp, module))});
45+
return require(${JSON.stringify(join(config.paths.output.standaloneApp, module))});
4646
}
4747
`
4848
)

packages/cloudflare/src/cli/build/patches/to-investigate/patch-find-dir.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export function patchFindDir(code: string, config: Config): string {
1515
`function findDir(dir, name) {
1616
if (dir.endsWith(".next/server")) {
1717
if (name === "app") {
18-
return ${existsSync(`${join(config.paths.standaloneAppServer, "app")}`)};
18+
return ${existsSync(`${join(config.paths.output.standaloneAppServer, "app")}`)};
1919
}
2020
if (name === "pages") {
21-
return ${existsSync(`${join(config.paths.standaloneAppServer, "pages")}`)};
21+
return ${existsSync(`${join(config.paths.output.standaloneAppServer, "pages")}`)};
2222
}
2323
}
2424
throw new Error("Unknown findDir call: " + dir + " " + name);

packages/cloudflare/src/cli/build/patches/to-investigate/patch-read-file.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@ export function patchReadFile(code: string, config: Config): string {
1313
code = code.replace(
1414
"getBuildId() {",
1515
`getBuildId() {
16-
return ${JSON.stringify(readFileSync(join(config.paths.standaloneAppDotNext, "BUILD_ID"), "utf-8"))};
16+
return ${JSON.stringify(readFileSync(join(config.paths.output.standaloneAppDotNext, "BUILD_ID"), "utf-8"))};
1717
`
1818
);
1919

2020
// Same as above, the next-server code loads the manifests with `readFileSync` and we want to avoid that
2121
// (source: https://github.com/vercel/next.js/blob/15aeb92e/packages/next/src/server/load-manifest.ts#L34-L56)
2222
// Note: we could/should probably just patch readFileSync here or something!
2323
const manifestJsons = globSync(
24-
normalizePath(join(config.paths.standaloneAppDotNext, "**", "*-manifest.json"))
25-
).map((file) => normalizePath(file).replace(normalizePath(config.paths.standaloneApp) + posix.sep, ""));
24+
normalizePath(join(config.paths.output.standaloneAppDotNext, "**", "*-manifest.json"))
25+
).map((file) =>
26+
normalizePath(file).replace(normalizePath(config.paths.output.standaloneApp) + posix.sep, "")
27+
);
2628
code = code.replace(
2729
/function loadManifest\((.+?), .+?\) {/,
2830
`$&
2931
${manifestJsons
3032
.map(
3133
(manifestJson) => `
3234
if ($1.endsWith("${manifestJson}")) {
33-
return ${readFileSync(join(config.paths.standaloneApp, manifestJson), "utf-8")};
35+
return ${readFileSync(join(config.paths.output.standaloneApp, manifestJson), "utf-8")};
3436
}
3537
`
3638
)

packages/cloudflare/src/cli/build/patches/to-investigate/wrangler-deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function patchWranglerDeps(config: Config) {
5555
* @returns the node_modules/next/dist directory path
5656
*/
5757
function getDistPath(config: Config): string {
58-
for (const root of [config.paths.standaloneApp, config.paths.standaloneRoot]) {
58+
for (const root of [config.paths.output.standaloneApp, config.paths.output.standaloneRoot]) {
5959
try {
6060
const distPath = join(root, "node_modules", "next", "dist");
6161
if (statSync(distPath).isDirectory()) return distPath;

0 commit comments

Comments
 (0)