Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -10,36 +16,56 @@ 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,
allowAsyncCycles: false,
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;

Expand Down