Skip to content

Commit c5adfaa

Browse files
committed
remove pathTruncationLength + add error prefix
1 parent 8b53be3 commit c5adfaa

File tree

6 files changed

+20
-31
lines changed

6 files changed

+20
-31
lines changed

packages/next/errors.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@
627627
"626": "Intercepting routes are not supported with static export.\\nRead more: https://nextjs.org/docs/app/building-your-application/deploying/static-exports#unsupported-features",
628628
"627": "cacheLife() is only available with the experimental.useCache config.",
629629
"628": "cacheTag() is only available with the experimental.useCache config.",
630-
"629": "Invariant: Module is recorded after the report is generated. This is a Next.js internal bug.",
631-
"630": "Invariant: Module is missing a required debugId. This is a Next.js internal bug.",
632-
"631": "Invariant: Unable to find the start time for a module build. This is a Next.js internal bug.",
633-
"632": "Invariant: Circular dependency detected in module graph. This is a Next.js internal bug."
630+
"629": "Invariant (SlowModuleDetectionPlugin): Unable to find the start time for a module build. This is a Next.js internal bug.",
631+
"630": "Invariant (SlowModuleDetectionPlugin): Module is recorded after the report is generated. This is a Next.js internal bug.",
632+
"631": "Invariant (SlowModuleDetectionPlugin): Circular dependency detected in module graph. This is a Next.js internal bug.",
633+
"632": "Invariant (SlowModuleDetectionPlugin): Module is missing a required debugId. This is a Next.js internal bug."
634634
}

packages/next/src/build/webpack-config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,9 @@ export default async function getBaseWebpackConfig(
19501950
require('./webpack/plugins/slow-module-detection-plugin') as typeof import('./webpack/plugins/slow-module-detection-plugin')
19511951
).default({
19521952
compilerType,
1953-
...config.experimental.slowModuleDetection,
1953+
...(typeof config.experimental.slowModuleDetection === 'object'
1954+
? config.experimental.slowModuleDetection
1955+
: {}),
19541956
}),
19551957
].filter(Boolean as any as ExcludesFalse),
19561958
}

packages/next/src/build/webpack/plugins/slow-module-detection-plugin.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@ const TreeSymbols = {
1212

1313
const DEFAULT_SLOW_MODULE_THRESHOLD_MS = 500
1414

15-
const DEFAULT_PATH_TRUNCATION_LENGTH = 100
15+
const PATH_TRUNCATION_LENGTH = 100
1616

1717
// Matches node_modules paths, including pnpm-style paths
1818
const NODE_MODULES_PATH_PATTERN = /node_modules(?:\/\.pnpm)?\/(.*)/
1919

2020
interface ModuleBuildTimeAnalyzerOptions {
2121
compilerType: CompilerNameValues
2222
slowModuleThresholdMs?: number
23-
pathTruncationLength?: number
2423
}
2524

2625
const getModuleIdentifier = (module: Module): string => {
2726
const debugId = module.debugId
2827
if (!debugId) {
2928
throw new Error(
30-
`Invariant: Module is missing a required debugId. This is a Next.js internal bug.`
29+
`Invariant (SlowModuleDetectionPlugin): Module is missing a required debugId. This is a Next.js internal bug.`
3130
)
3231
}
3332
return String(debugId)
@@ -67,19 +66,16 @@ class ModuleBuildTimeAnalyzer {
6766
private moduleChildren = new Map<Module, Map<string, Module>>()
6867
private isFinalized = false
6968
private slowModuleThresholdMs: number
70-
private pathTruncationLength: number
7169

7270
constructor(private options: ModuleBuildTimeAnalyzerOptions) {
7371
this.slowModuleThresholdMs =
7472
options.slowModuleThresholdMs ?? DEFAULT_SLOW_MODULE_THRESHOLD_MS
75-
this.pathTruncationLength =
76-
options.pathTruncationLength ?? DEFAULT_PATH_TRUNCATION_LENGTH
7773
}
7874

7975
recordModuleBuildTime(module: Module, duration: number) {
8076
if (this.isFinalized) {
8177
throw new Error(
82-
`Invariant: Module is recorded after the report is generated. This is a Next.js internal bug.`
78+
`Invariant (SlowModuleDetectionPlugin): Module is recorded after the report is generated. This is a Next.js internal bug.`
8379
)
8480
}
8581

@@ -116,7 +112,7 @@ class ModuleBuildTimeAnalyzer {
116112
if (!issuerModule) break
117113
if (visitedModules.has(issuerModule)) {
118114
throw new Error(
119-
`Invariant: Circular dependency detected in module graph. This is a Next.js internal bug.`
115+
`Invariant (SlowModuleDetectionPlugin): Circular dependency detected in module graph. This is a Next.js internal bug.`
120116
)
121117
}
122118
chain.push(issuerModule)
@@ -164,13 +160,10 @@ class ModuleBuildTimeAnalyzer {
164160
)
165161

166162
// Truncates long paths by keeping start and end portions
167-
const truncatePath = (
168-
path: string,
169-
maxLength = this.pathTruncationLength
170-
): string => {
171-
if (path.length <= maxLength) return path
172-
const startSegment = path.slice(0, maxLength / 3)
173-
const endSegment = path.slice((-maxLength * 2) / 3)
163+
const truncatePath = (path: string): string => {
164+
if (path.length <= PATH_TRUNCATION_LENGTH) return path
165+
const startSegment = path.slice(0, PATH_TRUNCATION_LENGTH / 3)
166+
const endSegment = path.slice((-PATH_TRUNCATION_LENGTH * 2) / 3)
174167
return `${startSegment}...${endSegment}`
175168
}
176169

@@ -241,7 +234,7 @@ export default class SlowModuleDetectionPlugin {
241234
const startTime = moduleBuildStartTimes.get(module)
242235
if (!startTime) {
243236
throw new Error(
244-
`Invariant: Unable to find the start time for a module build. This is a Next.js internal bug.`
237+
`Invariant (SlowModuleDetectionPlugin): Unable to find the start time for a module build. This is a Next.js internal bug.`
245238
)
246239
}
247240
analyzer.recordModuleBuildTime(module, performance.now() - startTime)

packages/next/src/server/config-schema.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
446446
useCache: z.boolean().optional(),
447447
slowModuleDetection: z
448448
.object({
449-
slowModuleThresholdMs: z.number().optional(),
450-
pathTruncationLength: z.number().optional(),
449+
slowModuleThresholdMs: z.number().positive().optional(),
451450
})
452451
.optional(),
453452
})

packages/next/src/server/config-shared.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -620,14 +620,10 @@ export interface ExperimentalConfig {
620620
/**
621621
* Time threshold in milliseconds for considering a module "slow".
622622
* Modules taking longer than this to build will be reported.
623+
*
624+
* @default 500
623625
*/
624626
slowModuleThresholdMs?: number
625-
626-
/**
627-
* Maximum length for displayed module paths in the build report.
628-
* Longer paths will be truncated.
629-
*/
630-
pathTruncationLength?: number
631627
}
632628
}
633629

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module.exports = {
22
experimental: {
33
slowModuleDetection: {
4-
slowModuleThresholdMs: 50, // Lower threshold for testing
5-
pathTruncationLength: 50,
4+
slowModuleThresholdMs: 50,
65
},
76
},
87
}

0 commit comments

Comments
 (0)