-
Notifications
You must be signed in to change notification settings - Fork 119
feat(dev): report why the dev server reloaded or restarted #1402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
538324f
refactor(paths): extract `relativeTo` helper for formatting paths aga…
danielroe 6111b22
feat(dev): always report why the dev server reloaded or restarted
danielroe 975d980
test: fix assertion
danielroe f9a53e5
test: assert native path separators in restart reason spec
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { relativeTo } from '../utils/paths' | ||
|
|
||
| /** Structured cause of a dev server reload or restart. */ | ||
| export type DevRestartReason | ||
| = | { type: 'config', files: string[], keys?: string[] } | ||
| | { type: 'dist-removed' } | ||
| | { type: 'hook' } | ||
| | { type: 'shortcut' } | ||
| | { type: 'error', message: string } | ||
|
|
||
| export interface FormatRestartReasonOptions { | ||
| rootDir: string | ||
| /** A new process is taking over, rather than reloading in place. */ | ||
| hard?: boolean | ||
| /** Render file names as terminal hyperlinks. Disable for non-terminal output. */ | ||
| link?: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Combine a queued reason with a newer one, so a debounced reload can report | ||
| * every file that changed within the window. | ||
| */ | ||
| export function mergeRestartReasons(previous: DevRestartReason | undefined, next: DevRestartReason): DevRestartReason { | ||
| if (previous?.type !== 'config' || next.type !== 'config') { | ||
| return next | ||
| } | ||
| return { | ||
| type: 'config', | ||
| files: [...new Set([...previous.files, ...next.files])], | ||
| keys: previous.keys || next.keys | ||
| ? [...new Set([...previous.keys || [], ...next.keys || []])] | ||
| : undefined, | ||
| } | ||
| } | ||
|
|
||
| /** Describe what caused a reload or restart, without saying what happens next. */ | ||
| export function formatRestartCause(reason: DevRestartReason, options: FormatRestartReasonOptions): string { | ||
| switch (reason.type) { | ||
| case 'config': { | ||
| const keys = reason.keys?.length ? ` (${reason.keys.join(', ')})` : '' | ||
| return `${formatFileList(reason.files, options)} changed${keys}` | ||
| } | ||
| case 'dist-removed': | ||
| return 'Build output was removed' | ||
| case 'hook': | ||
| return 'Nuxt requested a restart' | ||
| case 'shortcut': | ||
| return 'Restart requested' | ||
| case 'error': | ||
| return `Unhandled error: ${reason.message}` | ||
| } | ||
| } | ||
|
|
||
| /** One line saying what caused a reload or restart and what is happening as a result. */ | ||
| export function formatRestartReason(reason: DevRestartReason | undefined, options: FormatRestartReasonOptions): string { | ||
| const action = options.hard ? 'Restarting Nuxt in a new process' : 'Reloading Nuxt' | ||
| if (!reason) { | ||
| return `${action}...` | ||
| } | ||
| return `${formatRestartCause(reason, options)}. ${action}...` | ||
| } | ||
|
|
||
| const MAX_LISTED_FILES = 2 | ||
|
|
||
| function formatFileList(files: string[], options: FormatRestartReasonOptions): string { | ||
| const names = files.map(file => options.link === false ? relativeTo(options.rootDir, file, { link: false }) : relativeTo(options.rootDir, file)) | ||
| if (names.length === 0) { | ||
| return 'A file' | ||
| } | ||
| if (names.length <= MAX_LISTED_FILES) { | ||
| return names.join(' and ') | ||
| } | ||
| const remaining = names.length - MAX_LISTED_FILES | ||
| return `${names.slice(0, MAX_LISTED_FILES).join(', ')} and ${remaining} other file${remaining === 1 ? '' : 's'}` | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Restore the in-place action text.
This returns
Reloading Nuxt..., while the new tests expectReloading Nuxt in place...; both assertions will fail.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents