From a8db8244a3aa0f7ce220ebf97f0050d3569d4233 Mon Sep 17 00:00:00 2001 From: Wesley Luyten Date: Sun, 5 May 2024 09:50:54 -0500 Subject: [PATCH] fix: async function as next config (#252) fix #251 --- examples/default-provider/next.config.mjs | 4 +++- src/config.ts | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/default-provider/next.config.mjs b/examples/default-provider/next.config.mjs index 33337f7..f68dce7 100644 --- a/examples/default-provider/next.config.mjs +++ b/examples/default-provider/next.config.mjs @@ -2,7 +2,9 @@ import { withNextVideo } from 'next-video/process'; /** @type {import('next').NextConfig} */ const nextConfig = {}; -export default withNextVideo(nextConfig); +export default async function config() { + return withNextVideo(nextConfig); +} // Amazon S3 example // export default withNextVideo(nextConfig, { diff --git a/src/config.ts b/src/config.ts index edaf48c..c65eb09 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,7 +1,8 @@ -import { env, cwd } from 'node:process'; +import { cwd } from 'node:process'; import path from 'node:path'; import { pathToFileURL } from 'node:url'; -import nextConfig from 'next/config.js' +import nextConfig from 'next/config.js'; +import type { NextConfig } from 'next'; // @ts-ignore const getConfig = nextConfig.default; @@ -68,7 +69,7 @@ export const videoConfigDefault: VideoConfigComplete = { * The video config is then stored as an environment variable __NEXT_VIDEO_OPTS. */ export async function getVideoConfig(): Promise { - let nextConfig = getConfig(); + let nextConfig: NextConfig | undefined = getConfig(); if (!nextConfig?.serverRuntimeConfig?.nextVideo) { try { @@ -88,5 +89,12 @@ export async function getVideoConfig(): Promise { async function importConfig(file: string) { const absFilePath = path.resolve(cwd(), file); const fileUrl = pathToFileURL(absFilePath).href; - return (await import(/* webpackIgnore: true */ fileUrl))?.default; + + const mod = await import(/* webpackIgnore: true */ fileUrl); + const config: (() => NextConfig) | NextConfig | undefined = mod?.default; + + if (typeof config === 'function') { + return config(); + } + return config; }