diff --git a/index.ts b/index.ts index 4ef1e6a..e73659c 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,12 @@ import { default as CircularDependencyPlugin } from "circular-dependency-plugin"; import type { NextConfig } from "next/types"; +const detectedCircularDependencies = { + server: [] as string[], + 'edge-server': [] as string[], + client: [] as string[], +}; + const defaultOptions = { /** * Exclude modules from the bundle that are not used by the project. @@ -10,7 +16,7 @@ const defaultOptions = { exclude: /node_modules/, include: /.*/, /** - * Wether to fail the build if there are circular dependencies. + * Whether to fail the build if there are circular dependencies. * @default false */ failOnError: false, @@ -18,28 +24,48 @@ const defaultOptions = { cwd: process.cwd(), /** * Log the start of the check - * @param x */ - onStart: (x) => { - console.debug(`🔎 Checking ${x.compilation.name} for circular dependencies`) + onStart: ({ compilation }) => { + console.debug(`🔎 Checking ${compilation.name} for circular dependencies`) }, /** - * Log the end of the check - * @param x + * Log the detected circular dependency */ - onEnd: (x) => { - if (x.compilation.errors.length === 0) { - console.debug(`✅ No circular dependencies found in ${x.compilation.name}`) - } else { - console.error(`❌ ${x.compilation.errors.length} circular dependencies found in ${x.compilation.name}`) - } + onDetected: ({ compilation, paths }) => { + const message = `♻️ Circular dependency detected: ${paths.join(" -> ")}`; + console.error(message); + detectedCircularDependencies[ + compilation.name as keyof typeof detectedCircularDependencies + ].push(message); }, /** - * Log the detected circular dependency - * @param x + * Log the end of the check */ - onDetected: ({ paths }) => { - console.error(`♻️ Circular dependency detected: ${paths.join(" -> ")}`); + onEnd: ({ compilation }) => { + const detectedCircularDependenciesCount = + detectedCircularDependencies[ + compilation.name as keyof typeof detectedCircularDependencies + ].length; + if (detectedCircularDependenciesCount === 0) { + console.debug(`✅ No circular dependencies detected in ${compilation.name}`) + } else { + console.error(`❌ ${detectedCircularDependenciesCount} circular dependencies detected in ${compilation.name}`) + } + + // Exit with code 1 if circular dependencies detected during the last compilation (currently `client`, as of Next.js 15.3.2) + if ( + compilation.name === 'client' && + [ + ...detectedCircularDependencies.client, + ...detectedCircularDependencies['edge-server'], + ...detectedCircularDependencies.server, + ].length > 0 + ) { + console.error( + `❌ Exiting, circular dependencies detected` + ); + process.exit(1); + } }, } satisfies CircularDependencyPlugin.Options;