Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions packages/open-next/src/build/copyTracedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export async function copyTracedFiles(
const standaloneNextDir = path.join(standaloneDir, packagePath, ".next");
const outputNextDir = path.join(outputDir, packagePath, ".next");

const extractFiles = (files: string[], from = standaloneNextDir) => {
return files.map((f) => path.resolve(from, f));
};
const extractFiles = (files: string[], from = standaloneNextDir) =>
files.map((f) => path.resolve(from, f));

// On next 14+, we might not have to include those files
// For next 13, we need to include them otherwise we get runtime error
Expand Down Expand Up @@ -203,25 +202,36 @@ File ${fullFilePath} does not exist
}
});

readdirSync(standaloneNextDir).forEach((f) => {
if (statSync(path.join(standaloneNextDir, f)).isDirectory()) return;
copyFileSync(path.join(standaloneNextDir, f), path.join(outputNextDir, f));
});
readdirSync(standaloneNextDir)
.filter(
(fileOrDir) =>
!statSync(path.join(standaloneNextDir, fileOrDir)).isDirectory(),
)
.forEach((file) =>
copyFileSync(
path.join(standaloneNextDir, file),
path.join(outputNextDir, file),
),
);

// We then need to copy all the files at the root of server

mkdirSync(path.join(outputNextDir, "server"), { recursive: true });

readdirSync(path.join(standaloneNextDir, "server")).forEach((f) => {
if (statSync(path.join(standaloneNextDir, "server", f)).isDirectory())
return;
if (f !== "server.js") {
readdirSync(path.join(standaloneNextDir, "server"))
.filter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do it in 1 filter instead of 2, it avoids having to iterate over everything twice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we could.
however I like to have "simple" filters.
I don't think the time lost iterating a list is significant vs i/o (copy the files).

Happy to change if you feel strongly about this one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok fine for me then, i don't have that strong of an opinion on this

(fileOrDir) =>
!statSync(
path.join(standaloneNextDir, "server", fileOrDir),
).isDirectory(),
)
.filter((file) => file !== "server.js")
.forEach((file) =>
copyFileSync(
path.join(standaloneNextDir, "server", f),
path.join(path.join(outputNextDir, "server"), f),
);
}
});
path.join(standaloneNextDir, "server", file),
path.join(path.join(outputNextDir, "server"), file),
),
);

// Copy patch file
copyPatchFile(path.join(outputDir, packagePath));
Expand Down Expand Up @@ -285,11 +295,9 @@ File ${fullFilePath} does not exist
}
});

staticFiles.forEach((f: string) => {
if (f.endsWith(".html")) {
copyStaticFile(`server/${f}`);
}
});
staticFiles
.filter((file) => file.endsWith(".html"))
.forEach((file) => copyStaticFile(`server/${file}`));
}

logger.debug("copyTracedFiles:", Date.now() - tsStart, "ms");
Expand Down
6 changes: 2 additions & 4 deletions packages/open-next/src/plugins/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,11 @@ ${contents}
const MiddlewareManifest = loadMiddlewareManifest(nextDir);

const contents = `
import path from "path";
import path from "node:path";

import { debug } from "../logger";

if(!globalThis.__dirname) {
globalThis.__dirname = ""
}
globalThis.__dirname ??= "";

export const NEXT_DIR = path.join(__dirname, ".next");
export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");
Expand Down