Skip to content
Open
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
67 changes: 55 additions & 12 deletions packages/@apphosting/adapter-nextjs/src/overrides.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from "assert";
import fs from "fs";
import path from "path";
import os from "os";
import { pathToFileURL } from "url";
import type { MiddlewareMatcher } from "next/dist/build/analysis/get-page-static-info.js";
import { RoutesManifest, MiddlewareManifest } from "./interfaces.js";
const importOverrides = import("@apphosting/adapter-nextjs/dist/overrides.js");
Expand Down Expand Up @@ -289,12 +290,12 @@ describe("next config overrides", () => {
},
});

const fahOptimize = (value) =>
typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value);
Comment on lines +293 to +294

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Update the expected template string for fahOptimize to match the defensive check for falsy values.

Suggested change
const fahOptimize = (value) =>
typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value);
const fahOptimize = (value) =>
!value ? value : (typeof value.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value));


const config = typeof originalConfig === 'function'
? async (...args) => {
const resolvedConfig = await originalConfig(...args);
return fahOptimizedConfig(resolvedConfig);
}
: fahOptimizedConfig(originalConfig);
? (...args) => fahOptimize(originalConfig(...args))
: fahOptimize(originalConfig);
`;

beforeEach(() => {
Expand Down Expand Up @@ -421,7 +422,7 @@ describe("next config overrides", () => {

${nextConfigOverrideBody}

export default config;
export default config as typeof originalConfig;
`),
);
});
Expand Down Expand Up @@ -451,11 +452,53 @@ describe("next config overrides", () => {

${nextConfigOverrideBody}

export default config;
export default config as typeof originalConfig;
`),
);
});

const shapes = [
{
shape: "object",
original: `export default { distDir: "out" };`,
isFunction: false,
isPromise: false,
},
{
shape: "promise",
original: `export default Promise.resolve({ distDir: "out" });`,
isFunction: false,
isPromise: true,
},
{
shape: "sync function",
original: `export default (phase) => ({ distDir: phase });`,
isFunction: true,
isPromise: false,
},
{
shape: "async function",
original: `export default async (phase) => ({ distDir: phase });`,
isFunction: true,
isPromise: true,
},
];
for (const { shape, original, isFunction, isPromise } of shapes) {
it(`should keep the shape of the config - ${shape}`, async () => {
const { overrideNextConfig } = await importOverrides;
fs.writeFileSync(path.join(tmpDir, "next.config.mjs"), original);
await overrideNextConfig(tmpDir, "next.config.mjs");

const { default: config } = await import(
pathToFileURL(path.join(tmpDir, "next.config.mjs")).href
);
assert.equal(typeof config === "function", isFunction);
const result = isFunction ? config("out") : config;
assert.equal(typeof result?.then === "function", isPromise);
assert.deepEqual(await result, { distDir: "out", images: { unoptimized: true } });
});
}

it("should restore the original config when the override fails", async () => {
const { overrideNextConfig } = await importOverrides;
const originalConfig = `module.exports = { /* config options here */ }`;
Expand Down Expand Up @@ -559,12 +602,12 @@ describe("next config restore", () => {
},
});

const fahOptimize = (value) =>
typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value);
Comment thread
onlined marked this conversation as resolved.
Resolved

const config = typeof originalConfig === 'function'
? async (...args) => {
const resolvedConfig = await originalConfig(...args);
return fahOptimizedConfig(resolvedConfig);
}
: fahOptimizedConfig(originalConfig);
? (...args) => fahOptimize(originalConfig(...args))
: fahOptimize(originalConfig);
`;

beforeEach(() => {
Expand Down
27 changes: 20 additions & 7 deletions packages/@apphosting/adapter-nextjs/src/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ export async function overrideNextConfig(projectRoot: string, nextConfigFileName
* @return The custom Next.js config.
*/
function getCustomNextConfig(importStatement: string, fileExtension: string) {
const isEsm = [".mjs", ".mts", ".ts"].includes(fileExtension);
let exportStatement;
switch (fileExtension) {
case ".js":
exportStatement = "module.exports = config;";
break;
case ".mjs":
exportStatement = "export default config;";
break;
default:
// .ts and .mts. The wrapper keeps the original's shape (object, promise,
// function, and a function's sync or async result), so importers of
// next.config see the same type they see without the adapter.
exportStatement = "export default config as typeof originalConfig;";
}
return `
// @ts-nocheck
${importStatement}
Expand All @@ -96,14 +109,14 @@ function getCustomNextConfig(importStatement: string, fileExtension: string) {
},
});

const fahOptimize = (value) =>
typeof value?.then === 'function' ? value.then(fahOptimizedConfig) : fahOptimizedConfig(value);
Comment thread
onlined marked this conversation as resolved.
Resolved

const config = typeof originalConfig === 'function'
? async (...args) => {
const resolvedConfig = await originalConfig(...args);
return fahOptimizedConfig(resolvedConfig);
}
: fahOptimizedConfig(originalConfig);
? (...args) => fahOptimize(originalConfig(...args))
: fahOptimize(originalConfig);

${isEsm ? "export default config;" : "module.exports = config;"}
${exportStatement}
`;
}

Expand Down