Skip to content

Commit 297e147

Browse files
authored
refactor: improve separation of source / output paths in config (#100)
* refactor: use source dir to get app path in config * refactor: restructure config types * remove source dir change
1 parent b1954e9 commit 297e147

File tree

12 files changed

+88
-63
lines changed

12 files changed

+88
-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
@@ -32,28 +32,26 @@ export async function buildWorker(config: Config): Promise<void> {
3232
console.log(`\x1b[35m⚙️ Copying files...\n\x1b[0m`);
3333

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

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

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

5048
copyPackageCliFiles(packageDistDir, config);
5149

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

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

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

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
@@ -9,7 +9,7 @@ import { Config } from "../../../config";
99
export function copyPackageCliFiles(packageDistDir: string, config: Config) {
1010
console.log("# copyPackageTemplateFiles");
1111
const sourceDir = join(packageDistDir, "cli");
12-
const destinationDir = join(config.paths.internalPackage, "cli");
12+
const destinationDir = join(config.paths.internal.package, "cli");
1313

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

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

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

2121
const cacheHandlerFileName = "cache-handler.mjs";
22-
const cacheHandlerEntrypoint = join(config.paths.internalTemplates, "cache-handler", "index.ts");
23-
const cacheHandlerOutputFile = join(config.paths.outputDir, cacheHandlerFileName);
22+
const cacheHandlerEntrypoint = join(config.paths.internal.templates, "cache-handler", "index.ts");
23+
const cacheHandlerOutputFile = join(config.paths.output.root, cacheHandlerFileName);
2424

2525
await build({
2626
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
@@ -12,11 +12,11 @@ import { getUpdatedWebpackChunksFileContent } from "./get-updated-webpack-chunks
1212
*/
1313
export async function updateWebpackChunksFile(config: Config) {
1414
console.log("# updateWebpackChunksFile");
15-
const webpackRuntimeFile = join(config.paths.standaloneAppServer, "webpack-runtime.js");
15+
const webpackRuntimeFile = join(config.paths.output.standaloneAppServer, "webpack-runtime.js");
1616

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

19-
const chunks = readdirSync(join(config.paths.standaloneAppServer, "chunks"))
19+
const chunks = readdirSync(join(config.paths.output.standaloneAppServer, "chunks"))
2020
.filter((chunk) => /^\d+\.js$/.test(chunk))
2121
.map((chunk) => {
2222
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
@@ -15,16 +15,18 @@ import { normalizePath } from "../../utils";
1515
export function inlineEvalManifest(code: string, config: Config): string {
1616
console.log("# inlineEvalManifest");
1717
const manifestJss = globSync(
18-
normalizePath(join(config.paths.standaloneAppDotNext, "**", "*_client-reference-manifest.js"))
19-
).map((file) => normalizePath(file).replace(normalizePath(config.paths.standaloneApp) + posix.sep, ""));
18+
normalizePath(join(config.paths.output.standaloneAppDotNext, "**", "*_client-reference-manifest.js"))
19+
).map((file) =>
20+
normalizePath(file).replace(normalizePath(config.paths.output.standaloneApp) + posix.sep, "")
21+
);
2022
return code.replace(
2123
/function evalManifest\((.+?), .+?\) {/,
2224
`$&
2325
${manifestJss
2426
.map(
2527
(manifestJs) => `
2628
if ($1.endsWith("${manifestJs}")) {
27-
require(${JSON.stringify(join(config.paths.standaloneApp, manifestJs))});
29+
require(${JSON.stringify(join(config.paths.output.standaloneApp, manifestJs))});
2830
return {
2931
__RSC_MANIFEST: {
3032
"${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
@@ -10,7 +10,7 @@ import { Config } from "../../../config";
1010
export function inlineMiddlewareManifestRequire(code: string, config: Config) {
1111
console.log("# inlineMiddlewareManifestRequire");
1212

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

1515
const middlewareManifest = existsSync(middlewareManifestPath)
1616
? 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
@@ -9,8 +9,8 @@ import { Config } from "../../../config";
99
*/
1010
export function inlineNextRequire(code: string, config: Config) {
1111
console.log("# inlineNextRequire");
12-
const pagesManifestFile = join(config.paths.standaloneAppServer, "pages-manifest.json");
13-
const appPathsManifestFile = join(config.paths.standaloneAppServer, "app-paths-manifest.json");
12+
const pagesManifestFile = join(config.paths.output.standaloneAppServer, "pages-manifest.json");
13+
const appPathsManifestFile = join(config.paths.output.standaloneAppServer, "app-paths-manifest.json");
1414

1515
const pagesManifestFiles = existsSync(pagesManifestFile)
1616
? Object.values(JSON.parse(readFileSync(pagesManifestFile, "utf-8"))).map(
@@ -34,7 +34,7 @@ export function inlineNextRequire(code: string, config: Config) {
3434
.map(
3535
(htmlPage) => `
3636
if (pagePath.endsWith("${htmlPage}")) {
37-
return ${JSON.stringify(readFileSync(join(config.paths.standaloneApp, htmlPage), "utf-8"))};
37+
return ${JSON.stringify(readFileSync(join(config.paths.output.standaloneApp, htmlPage), "utf-8"))};
3838
}
3939
`
4040
)
@@ -43,7 +43,7 @@ export function inlineNextRequire(code: string, config: Config) {
4343
.map(
4444
(module) => `
4545
if (pagePath.endsWith("${module}")) {
46-
return require(${JSON.stringify(join(config.paths.standaloneApp, module))});
46+
return require(${JSON.stringify(join(config.paths.output.standaloneApp, module))});
4747
}
4848
`
4949
)

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
@@ -16,10 +16,10 @@ export function patchFindDir(code: string, config: Config): string {
1616
`function findDir(dir, name) {
1717
if (dir.endsWith(".next/server")) {
1818
if (name === "app") {
19-
return ${existsSync(`${join(config.paths.standaloneAppServer, "app")}`)};
19+
return ${existsSync(`${join(config.paths.output.standaloneAppServer, "app")}`)};
2020
}
2121
if (name === "pages") {
22-
return ${existsSync(`${join(config.paths.standaloneAppServer, "pages")}`)};
22+
return ${existsSync(`${join(config.paths.output.standaloneAppServer, "pages")}`)};
2323
}
2424
}
2525
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
@@ -15,24 +15,26 @@ export function patchReadFile(code: string, config: Config): string {
1515
code = code.replace(
1616
"getBuildId() {",
1717
`getBuildId() {
18-
return ${JSON.stringify(readFileSync(join(config.paths.standaloneAppDotNext, "BUILD_ID"), "utf-8"))};
18+
return ${JSON.stringify(readFileSync(join(config.paths.output.standaloneAppDotNext, "BUILD_ID"), "utf-8"))};
1919
`
2020
);
2121

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

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
@@ -56,7 +56,7 @@ export function patchWranglerDeps(config: Config) {
5656
* @returns the node_modules/next/dist directory path
5757
*/
5858
function getDistPath(config: Config): string {
59-
for (const root of [config.paths.standaloneApp, config.paths.standaloneRoot]) {
59+
for (const root of [config.paths.output.standaloneApp, config.paths.output.standaloneRoot]) {
6060
try {
6161
const distPath = join(root, "node_modules", "next", "dist");
6262
if (statSync(distPath).isDirectory()) return distPath;

0 commit comments

Comments
 (0)