Skip to content

Commit

Permalink
fix: self import next.config
Browse files Browse the repository at this point in the history
related #304
  • Loading branch information
luwes committed Oct 10, 2024
1 parent ddbb92a commit 5f79246
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'node:path';
import { cwd } from 'node:process';
import { stat, readFile, writeFile, mkdir } from 'node:fs/promises';
import { stat } from 'node:fs/promises';
import { getVideoConfig } from './config.js';
import { deepMerge, camelCase, isRemote, toSafePath } from './utils/utils.js';
import * as transformers from './providers/transformers.js';
Expand Down
23 changes: 9 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { cwd } from 'node:process';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import nextConfig from 'next/config.js';
import type { NextConfig } from 'next';
import { Asset } from './assets';
import { mkdir, readFile, writeFile } from 'node:fs/promises';

// @ts-ignore
const getConfig = nextConfig.default;

/**
* Video configurations
*/
Expand Down Expand Up @@ -101,21 +97,20 @@ export const videoConfigDefault: VideoConfigComplete = {
/**
* The video config is set in `next.config.js` and passed to the `withNextVideo` function.
* The video config is then stored in `serverRuntimeConfig`.
* Import `next.config.js` ourselves because Next.js serializes the config and
* we need the raw object with functions.
*/
export async function getVideoConfig(): Promise<VideoConfigComplete> {
let nextConfig: NextConfig | undefined = getConfig();
if (!nextConfig?.serverRuntimeConfig?.nextVideo) {
let nextConfig: NextConfig | undefined;
try {
nextConfig = await importConfig('next.config.js');
} catch (err) {
try {
nextConfig = await importConfig('next.config.js');
} catch (err) {
try {
nextConfig = await importConfig('next.config.mjs');
} catch {
console.error('Failed to load next-video config.');
}
nextConfig = await importConfig('next.config.mjs');
} catch {
console.error('Failed to load next-video config.');
}
}

return nextConfig?.serverRuntimeConfig?.nextVideo;
}

Expand Down

0 comments on commit 5f79246

Please sign in to comment.