Skip to content

Commit

Permalink
Add support for Next.js "src" directory (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang authored Jan 6, 2023
1 parent 9c56400 commit 229c032
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ function initOutputDir() {
fs.mkdirSync(tempDir, { recursive: true });
}

function isMiddlewareEnabled() {
function getMiddlewareName() {
// note: middleware can be at the root or inside "src"
const filePath = path.join(appPath, ".next", "server", "middleware-manifest.json");
const json = fs.readFileSync(filePath, "utf-8");
return JSON.parse(json).sortedMiddleware.length > 0;
return JSON.parse(json).middleware?.["/"]?.name;
}

function createServerBundle(monorepoRoot: string) {
Expand Down Expand Up @@ -219,7 +220,8 @@ function createImageOptimizationBundle() {
}

function createMiddlewareBundle(buildOutput: any) {
if (isMiddlewareEnabled()) {
const middlewareName = getMiddlewareName();
if (middlewareName) {
console.info(`Bundling middleware edge function...`);
}
else {
Expand All @@ -232,7 +234,7 @@ function createMiddlewareBundle(buildOutput: any) {
fs.mkdirSync(outputPath, { recursive: true });

// Save middleware code to file
const src: string = buildOutput.output.middleware.files["index.js"].data;
const src: string = buildOutput.output[middlewareName].files["index.js"].data;
fs.writeFileSync(path.join(tempDir, "middleware.js"), src);
fs.copyFileSync(
path.join(__dirname, "adapters", "middleware-adapter.js"),
Expand Down

0 comments on commit 229c032

Please sign in to comment.