Created
June 12, 2024 08:51
-
-
Save martinratinaud/dd9ab1c752d5301ad3bee9565f11fb6c to your computer and use it in GitHub Desktop.
Modularize next config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import path from 'path'; | |
const loadNextConfigs = async (rootFolder) => { | |
return ( | |
await Promise.all( | |
fs | |
.readdirSync(rootFolder, { withFileTypes: true }) | |
.filter((dirent) => dirent.isDirectory()) | |
.map(async ({ name }) => { | |
const nextConfigPath = path.join(rootFolder, name, 'next.config.mjs'); | |
if (!fs.existsSync(nextConfigPath)) { | |
return undefined; | |
} | |
console.info('->', nextConfigPath); | |
const { default: withConfig } = await import( | |
`../../${nextConfigPath}` | |
); | |
return withConfig; | |
}) | |
) | |
).filter(Boolean); | |
}; | |
const compose = | |
(...fns) => | |
(arg) => | |
fns.reduceRight((res, fn) => fn(res), arg); | |
const withConfig = async ( | |
/** @type {import('next').NextConfig} */ | |
nextConfig, | |
{ folders = ['./lib', './app'] } = {} | |
) => { | |
console.info('Loading next.config.mjs from', folders.join(', ')); | |
const configFunctions = ( | |
await Promise.all(folders.map(loadNextConfigs)) | |
).flat(); | |
return compose(...configFunctions)(nextConfig); | |
}; | |
export default withConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const withUpload = ( | |
/** @type {import('next').NextConfig} */ | |
nextConfig | |
) => { | |
return { | |
...nextConfig, | |
env: { | |
R2_ENDPOINT: process.env.R2_ENDPOINT, | |
R2_BUCKET: process.env.R2_BUCKET, | |
R2_PUBLIC_URL: process.env.R2_PUBLIC_URL, | |
...nextConfig.env, | |
}, | |
images: { | |
...(nextConfig?.images || {}), | |
remotePatterns: [ | |
...(nextConfig?.images?.remotePatterns || []), | |
{ | |
protocol: 'https', | |
hostname: process.env.R2_PUBLIC_URL.replace('https://', ''), | |
}, | |
], | |
}, | |
}; | |
}; | |
export default withUpload; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nextExtendableConfig from './lib/next-extendable-config/index.mjs'; | |
/** @type {import('next').NextConfig} */ | |
const nextConfig = { | |
env: { | |
SITE_NAME: process.env.SITE_NAME, | |
}, | |
}; | |
export default nextExtendableConfig(nextConfig, { | |
folders: ['./lib', './app'], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment