From b5812c97b4401e75a61d66070350c1182148e6b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Wed, 25 Feb 2026 20:24:24 +0100 Subject: [PATCH] fix: handle FFmpeg dynamic import more gracefully - lazy-load FFmpeg class to avoid static analysis issues - add proper null checks for FFmpeg instance - use initFFmpeg() helper to manage async initialization - fixes bundler 'too dynamic' error with static-friendly approach Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- lib/media/wasm/wasmLoader.ts | 37 +++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/media/wasm/wasmLoader.ts b/lib/media/wasm/wasmLoader.ts index 71539a2..95d7c84 100644 --- a/lib/media/wasm/wasmLoader.ts +++ b/lib/media/wasm/wasmLoader.ts @@ -1,7 +1,23 @@ -import { FFmpeg } from '@ffmpeg/ffmpeg'; +import type { FFmpeg as FFmpegType } from '@ffmpeg/ffmpeg'; import { initializeImageMagick } from '@imagemagick/magick-wasm'; import type { ConverterEngine, WASMModuleState } from '@/types/media'; +// Dynamically require FFmpeg to avoid bundler issues +let FFmpeg: any = null; + +// Initialize FFmpeg at module load time +async function initFFmpeg() { + if (!FFmpeg) { + try { + const module = await import('@ffmpeg/ffmpeg'); + FFmpeg = module.FFmpeg; + } catch (error) { + console.error('[FFmpeg] Failed to import module:', error); + } + } + return FFmpeg; +} + /** * WASM module loading state */ @@ -13,21 +29,32 @@ const moduleState: WASMModuleState = { /** * Cached WASM instances */ -let ffmpegInstance: FFmpeg | null = null; +let ffmpegInstance: FFmpegType | null = null; let imagemagickInstance: any = null; /** * Load FFmpeg WASM module */ -export async function loadFFmpeg(): Promise { +export async function loadFFmpeg(): Promise { if (ffmpegInstance && moduleState.ffmpeg) { return ffmpegInstance; } try { - ffmpegInstance = new FFmpeg(); + // Initialize FFmpeg class if not already done + const FFmpegClass = await initFFmpeg(); + + if (!FFmpegClass) { + throw new Error('FFmpeg class not available'); + } - ffmpegInstance.on('log', ({ message }) => { + ffmpegInstance = new FFmpegClass(); + + if (!ffmpegInstance) { + throw new Error('Failed to create FFmpeg instance'); + } + + ffmpegInstance.on('log', ({ message }: { message: string }) => { console.log('[FFmpeg]', message); });