|
1 | 1 | import { AdapterMetadata, MiddlewareManifest } from "./interfaces.js";
|
2 |
| -import { loadRouteManifest, writeRouteManifest, loadMiddlewareManifest } from "./utils.js"; |
| 2 | +import { |
| 3 | + loadRouteManifest, |
| 4 | + writeRouteManifest, |
| 5 | + loadMiddlewareManifest, |
| 6 | + exists, |
| 7 | + writeFile, |
| 8 | +} from "./utils.js"; |
| 9 | +import { join } from "path"; |
| 10 | +import { readFileSync } from "fs"; |
3 | 11 |
|
4 |
| -export async function overrideNextConfig(nextConfigFileName: string) {} |
| 12 | +export async function overrideNextConfig(nextConfigFileName: string) { |
| 13 | + // Check if the file exists in the current working directory |
| 14 | + const cwd = process.cwd(); |
| 15 | + const configPath = join(cwd, nextConfigFileName); |
| 16 | + |
| 17 | + if (!(await exists(configPath))) { |
| 18 | + console.log(`No Next.js config file found at ${configPath}`); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // Determine the file extension |
| 23 | + const fileExtension = nextConfigFileName.split(".").pop() || "js"; |
| 24 | + const originalConfigName = `next.config.original.${fileExtension}`; |
| 25 | + const newConfigName = `next.config.${fileExtension}`; |
| 26 | + |
| 27 | + // Rename the original config file |
| 28 | + try { |
| 29 | + const originalContent = readFileSync(configPath, "utf-8"); |
| 30 | + await writeFile(join(cwd, originalConfigName), originalContent); |
| 31 | + |
| 32 | + // Create a new config file with the appropriate import |
| 33 | + let importStatement; |
| 34 | + if (fileExtension === "js" || fileExtension === "cjs") { |
| 35 | + importStatement = `const originalConfig = require('./${originalConfigName}');`; |
| 36 | + } else if (fileExtension === "mjs") { |
| 37 | + importStatement = `import originalConfig from './${originalConfigName}';`; |
| 38 | + } else if (fileExtension === "ts") { |
| 39 | + importStatement = `import originalConfig from './${originalConfigName.replace(".ts", "")}';`; |
| 40 | + } else { |
| 41 | + throw new Error(`Unsupported file extension: ${fileExtension}`); |
| 42 | + } |
| 43 | + |
| 44 | + // Create the new config content with our overrides |
| 45 | + const newConfigContent = `${importStatement} |
| 46 | +
|
| 47 | +// This file was automatically generated by Firebase App Hosting adapter |
| 48 | +const config = typeof originalConfig === 'function' |
| 49 | + ? (...args) => { |
| 50 | + const resolvedConfig = originalConfig(...args); |
| 51 | + return { |
| 52 | + ...resolvedConfig, |
| 53 | + images: { |
| 54 | + ...(resolvedConfig.images || {}), |
| 55 | + unoptimized: true, |
| 56 | + }, |
| 57 | + }; |
| 58 | + } |
| 59 | + : { |
| 60 | + ...originalConfig, |
| 61 | + images: { |
| 62 | + ...(originalConfig.images || {}), |
| 63 | + unoptimized: true, |
| 64 | + }, |
| 65 | + }; |
| 66 | +
|
| 67 | +${fileExtension === "mjs" ? "export default config;" : "module.exports = config;"} |
| 68 | +`; |
| 69 | + |
| 70 | + // Write the new config file |
| 71 | + await writeFile(join(cwd, newConfigName), newConfigContent); |
| 72 | + console.log(`Successfully created ${newConfigName} with Firebase App Hosting overrides`); |
| 73 | + } catch (error) { |
| 74 | + console.error(`Error overriding Next.js config: ${error}`); |
| 75 | + throw error; |
| 76 | + } |
| 77 | +} |
5 | 78 |
|
6 | 79 | /**
|
7 | 80 | * Modifies the app's route manifest (routes-manifest.json) to add Firebase App Hosting
|
|
0 commit comments