Skip to content

Commit 3a5d74d

Browse files
committed
polish
1 parent a5f9222 commit 3a5d74d

File tree

4 files changed

+26
-39
lines changed

4 files changed

+26
-39
lines changed

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

+19-33
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const NODE_MODULES_PATH_PATTERN = /node_modules(?:\/\.pnpm)?\/(.*)/
1616

1717
interface ModuleBuildTimeAnalyzerOptions {
1818
compilerType: CompilerNameValues
19-
slowModuleThresholdMs: number
19+
buildTimeThresholdMs: number
2020
}
2121

2222
const getModuleIdentifier = (module: Module): string => {
@@ -45,28 +45,15 @@ const getModuleDisplayName = (module: Module): string | undefined => {
4545
}
4646

4747
/**
48-
* Truncates a given path to a maximum length defined by PATH_TRUNCATION_LENGTH.
49-
* If the path exceeds this length, it will be truncated in the middle and replaced with '...'.
50-
*
51-
* @param {string} path - The file path to be truncated.
52-
* @returns {string} - The truncated path if it exceeds the maximum length, otherwise the original path.
53-
*
54-
* @example
55-
* // Given a path shorter than the truncation length
56-
* truncatePath('src/components/Button.js');
57-
* // Returns: 'src/components/Button.js'
58-
*
59-
* @example
60-
* // Given a path longer than the truncation length
61-
* truncatePath('/Users/username/projects/my-app/src/components/Button.js');
62-
* // Returns: '/Users/username/proj...components/Button.js'
48+
* Truncates a path to a maximum length. If the path exceeds this length,
49+
* it will be truncated in the middle and replaced with '...'.
6350
*/
64-
function truncatePath(path: string): string {
51+
function truncatePath(path: string, maxLength: number): string {
6552
// If the path length is within the limit, return it as is
66-
if (path.length <= PATH_TRUNCATION_LENGTH) return path
53+
if (path.length <= maxLength) return path
6754

6855
// Calculate the available length for the start and end segments after accounting for '...'
69-
const availableLength = PATH_TRUNCATION_LENGTH - 3
56+
const availableLength = maxLength - 3
7057
const startSegmentLength = Math.ceil(availableLength / 2)
7158
const endSegmentLength = Math.floor(availableLength / 2)
7259

@@ -84,11 +71,11 @@ class ModuleBuildTimeAnalyzer {
8471
private moduleParents = new Map<Module, Module>()
8572
private moduleChildren = new Map<Module, Map<string, Module>>()
8673
private isFinalized = false
87-
private slowModuleThresholdMs: number
74+
private buildTimeThresholdMs: number
8875
private moduleBuildTimes = new WeakMap<Module, number>()
8976

9077
constructor(private options: ModuleBuildTimeAnalyzerOptions) {
91-
this.slowModuleThresholdMs = options.slowModuleThresholdMs
78+
this.buildTimeThresholdMs = options.buildTimeThresholdMs
9279
}
9380

9481
recordModuleBuildTime(module: Module, duration: number) {
@@ -100,7 +87,7 @@ class ModuleBuildTimeAnalyzer {
10087
)
10188
}
10289

103-
if (duration < this.slowModuleThresholdMs) {
90+
if (duration < this.buildTimeThresholdMs) {
10491
return // Skip fast modules
10592
}
10693

@@ -179,25 +166,24 @@ class ModuleBuildTimeAnalyzer {
179166
const formatModuleNode = (node: Module, depth: number): string => {
180167
const moduleName = getModuleDisplayName(node) || ''
181168

182-
// Skip internal/unnamed modules but continue processing their children
183-
// This keeps the report focused on meaningful source files and node_modules
184169
if (!moduleName) {
185170
return formatChildModules(node, depth)
186171
}
187172

188-
const buildTimeMs = this.moduleBuildTimes.get(node)
173+
const prefix =
174+
' ' + TreeSymbols.VERTICAL_LINE.repeat(depth) + TreeSymbols.BRANCH
189175

190-
const duration =
191-
buildTimeMs && buildTimeMs > 0
192-
? yellow(` (${Math.ceil(buildTimeMs)}ms)`)
193-
: ''
176+
const moduleText = blue(
177+
truncatePath(moduleName, PATH_TRUNCATION_LENGTH - prefix.length)
178+
)
194179

195-
const indentation = ' ' + TreeSymbols.VERTICAL_LINE.repeat(depth)
196-
const moduleText = blue(truncatePath(moduleName))
180+
const buildTimeMs = this.moduleBuildTimes.get(node)
181+
const duration = buildTimeMs
182+
? yellow(` (${Math.ceil(buildTimeMs)}ms)`)
183+
: ''
197184

198185
return (
199-
indentation +
200-
TreeSymbols.BRANCH +
186+
prefix +
201187
moduleText +
202188
duration +
203189
'\n' +

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
446446
useCache: z.boolean().optional(),
447447
slowModuleDetection: z
448448
.object({
449-
slowModuleThresholdMs: z.number().int(),
449+
buildTimeThresholdMs: z.number().int(),
450450
})
451451
.optional(),
452452
})

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,15 @@ export interface ExperimentalConfig {
614614
useCache?: boolean
615615

616616
/**
617-
* When enabled, will detect and report slow modules in the dev build in the terminal.
617+
* Enables detection and reporting of slow modules during development builds.
618+
* Enabling this may impact build performance to ensure accurate measurements.
618619
*/
619620
slowModuleDetection?: {
620621
/**
621-
* Time threshold in milliseconds for considering a module "slow".
622-
* Modules taking longer than this to build will be reported.
622+
* The time threshold in milliseconds for identifying slow modules.
623+
* Modules taking longer than this build time threshold will be reported.
623624
*/
624-
slowModuleThresholdMs: number
625+
buildTimeThresholdMs: number
625626
}
626627
}
627628

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
experimental: {
33
slowModuleDetection: {
4-
slowModuleThresholdMs: 50,
4+
buildTimeThresholdMs: 50,
55
},
66
},
77
}

0 commit comments

Comments
 (0)