-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Graceful error recovery in the dev server #5198
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6d605c
Graceful error recovery in the dev server
matthewp 02ddf91
Fix up Windows path for testing
matthewp 848ab66
some debugging
matthewp 8f5f19c
use posix join
matthewp 362fa6c
finally fixed
matthewp 1c3ea19
Remove leftover debugging
matthewp c6e6946
Reset the timeout
matthewp 278bb7a
Adding a changeset
matthewp 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 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,7 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
HMR - Improved error recovery | ||
|
||
This improves error recovery for HMR. Now when the dev server finds itself in an error state (because a route contained an error), it will recover from that state and refresh the page when the user has corrected the mistake. |
This file contains 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 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,49 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* Copyright (c) 2018 Andy Wermke | ||
* https://github.com/andywer/typed-emitter/blob/9a139b6fa0ec6b0db6141b5b756b784e4f7ef4e4/LICENSE | ||
*/ | ||
|
||
export type EventMap = { | ||
[key: string]: (...args: any[]) => void | ||
} | ||
|
||
/** | ||
* Type-safe event emitter. | ||
* | ||
* Use it like this: | ||
* | ||
* ```typescript | ||
* type MyEvents = { | ||
* error: (error: Error) => void; | ||
* message: (from: string, content: string) => void; | ||
* } | ||
* | ||
* const myEmitter = new EventEmitter() as TypedEmitter<MyEvents>; | ||
* | ||
* myEmitter.emit("error", "x") // <- Will catch this type error; | ||
* ``` | ||
*/ | ||
interface TypedEventEmitter<Events extends EventMap> { | ||
addListener<E extends keyof Events> (event: E, listener: Events[E]): this | ||
on<E extends keyof Events> (event: E, listener: Events[E]): this | ||
once<E extends keyof Events> (event: E, listener: Events[E]): this | ||
prependListener<E extends keyof Events> (event: E, listener: Events[E]): this | ||
prependOnceListener<E extends keyof Events> (event: E, listener: Events[E]): this | ||
|
||
off<E extends keyof Events>(event: E, listener: Events[E]): this | ||
removeAllListeners<E extends keyof Events> (event?: E): this | ||
removeListener<E extends keyof Events> (event: E, listener: Events[E]): this | ||
|
||
emit<E extends keyof Events> (event: E, ...args: Parameters<Events[E]>): boolean | ||
// The sloppy `eventNames()` return type is to mitigate type incompatibilities - see #5 | ||
eventNames (): (keyof Events | string | symbol)[] | ||
rawListeners<E extends keyof Events> (event: E): Events[E][] | ||
listeners<E extends keyof Events> (event: E): Events[E][] | ||
listenerCount<E extends keyof Events> (event: E): number | ||
|
||
getMaxListeners (): number | ||
setMaxListeners (maxListeners: number): this | ||
} | ||
|
||
export default TypedEventEmitter |
This file contains 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 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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
export { | ||
createDefaultDevConfig, | ||
openConfig, | ||
resolveConfigPath, | ||
resolveFlags, | ||
resolveRoot, | ||
validateConfig, | ||
} from './config.js'; | ||
export type { AstroConfigSchema } from './schema'; | ||
export { createSettings } from './settings.js'; | ||
export { createSettings, createDefaultDevSettings } from './settings.js'; | ||
export { loadTSConfig, updateTSConfigForFramework } from './tsconfig.js'; |
This file contains 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 |
---|---|---|
@@ -1,22 +1,43 @@ | ||
import type { AstroConfig, AstroSettings } from '../../@types/astro'; | ||
import type { AstroConfig, AstroSettings, AstroUserConfig } from '../../@types/astro'; | ||
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../constants.js'; | ||
|
||
import { fileURLToPath } from 'url'; | ||
import { createDefaultDevConfig } from './config.js'; | ||
import jsxRenderer from '../../jsx/renderer.js'; | ||
import { loadTSConfig } from './tsconfig.js'; | ||
|
||
export function createSettings(config: AstroConfig, cwd?: string): AstroSettings { | ||
const tsconfig = loadTSConfig(cwd); | ||
|
||
export function createBaseSettings(config: AstroConfig): AstroSettings { | ||
return { | ||
config, | ||
tsConfig: tsconfig?.config, | ||
tsConfigPath: tsconfig?.path, | ||
tsConfig: undefined, | ||
tsConfigPath: undefined, | ||
|
||
adapter: undefined, | ||
injectedRoutes: [], | ||
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS], | ||
renderers: [jsxRenderer], | ||
scripts: [], | ||
watchFiles: tsconfig?.exists ? [tsconfig.path, ...tsconfig.extendedPaths] : [], | ||
watchFiles: [], | ||
}; | ||
} | ||
|
||
export function createSettings(config: AstroConfig, cwd?: string): AstroSettings { | ||
const tsconfig = loadTSConfig(cwd); | ||
const settings = createBaseSettings(config); | ||
settings.tsConfig = tsconfig?.config; | ||
settings.tsConfigPath = tsconfig?.path; | ||
settings.watchFiles = tsconfig?.exists ? [tsconfig.path, ...tsconfig.extendedPaths] : []; | ||
return settings; | ||
} | ||
|
||
export async function createDefaultDevSettings( | ||
userConfig: AstroUserConfig = {}, | ||
root?: string | URL | ||
): Promise<AstroSettings> { | ||
if(root && typeof root !== 'string') { | ||
root = fileURLToPath(root); | ||
} | ||
const config = await createDefaultDevConfig(userConfig, root); | ||
return createBaseSettings(config); | ||
} | ||
|
This file contains 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 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,124 @@ | ||
|
||
import type { AddressInfo } from 'net'; | ||
import type { AstroSettings, AstroUserConfig } from '../../@types/astro'; | ||
import * as http from 'http'; | ||
|
||
import { | ||
runHookConfigDone, | ||
runHookConfigSetup, | ||
runHookServerSetup, | ||
runHookServerStart, | ||
} from '../../integrations/index.js'; | ||
import { createVite } from '../create-vite.js'; | ||
import { LogOptions } from '../logger/core.js'; | ||
import { nodeLogDestination } from '../logger/node.js'; | ||
import nodeFs from 'fs'; | ||
import * as vite from 'vite'; | ||
import { createDefaultDevSettings } from '../config/index.js'; | ||
import { apply as applyPolyfill } from '../polyfill.js'; | ||
|
||
|
||
const defaultLogging: LogOptions = { | ||
dest: nodeLogDestination, | ||
level: 'error', | ||
}; | ||
|
||
export interface Container { | ||
fs: typeof nodeFs; | ||
logging: LogOptions; | ||
settings: AstroSettings; | ||
viteConfig: vite.InlineConfig; | ||
viteServer: vite.ViteDevServer; | ||
handle: (req: http.IncomingMessage, res: http.ServerResponse) => void; | ||
close: () => Promise<void>; | ||
} | ||
|
||
export interface CreateContainerParams { | ||
isRestart?: boolean; | ||
logging?: LogOptions; | ||
userConfig?: AstroUserConfig; | ||
settings?: AstroSettings; | ||
fs?: typeof nodeFs; | ||
root?: string | URL; | ||
} | ||
|
||
export async function createContainer(params: CreateContainerParams = {}): Promise<Container> { | ||
let { | ||
isRestart = false, | ||
logging = defaultLogging, | ||
settings = await createDefaultDevSettings(params.userConfig, params.root), | ||
fs = nodeFs | ||
} = params; | ||
|
||
// Initialize | ||
applyPolyfill(); | ||
settings = await runHookConfigSetup({ | ||
settings, | ||
command: 'dev', | ||
logging, | ||
isRestart, | ||
}); | ||
const { host } = settings.config.server; | ||
|
||
// The client entrypoint for renderers. Since these are imported dynamically | ||
// we need to tell Vite to preoptimize them. | ||
const rendererClientEntries = settings.renderers | ||
.map((r) => r.clientEntrypoint) | ||
.filter(Boolean) as string[]; | ||
|
||
const viteConfig = await createVite( | ||
{ | ||
mode: 'development', | ||
server: { host }, | ||
optimizeDeps: { | ||
include: rendererClientEntries, | ||
}, | ||
define: { | ||
'import.meta.env.BASE_URL': settings.config.base | ||
? `'${settings.config.base}'` | ||
: 'undefined', | ||
}, | ||
}, | ||
{ settings, logging, mode: 'dev', fs } | ||
); | ||
await runHookConfigDone({ settings, logging }); | ||
const viteServer = await vite.createServer(viteConfig); | ||
runHookServerSetup({ config: settings.config, server: viteServer, logging }); | ||
|
||
return { | ||
fs, | ||
logging, | ||
settings, | ||
viteConfig, | ||
viteServer, | ||
|
||
handle(req, res) { | ||
viteServer.middlewares.handle(req, res, Function.prototype); | ||
}, | ||
close() { | ||
return viteServer.close(); | ||
} | ||
}; | ||
} | ||
|
||
export async function startContainer({ settings, viteServer, logging }: Container): Promise<AddressInfo> { | ||
const { port } = settings.config.server; | ||
await viteServer.listen(port); | ||
const devServerAddressInfo = viteServer.httpServer!.address() as AddressInfo; | ||
await runHookServerStart({ | ||
config: settings.config, | ||
address: devServerAddressInfo, | ||
logging, | ||
}); | ||
|
||
return devServerAddressInfo; | ||
} | ||
|
||
export async function runInContainer(params: CreateContainerParams, callback: (container: Container) => Promise<void> | void) { | ||
const container = await createContainer(params); | ||
try { | ||
await callback(container); | ||
} finally { | ||
await container.close(); | ||
} | ||
} |
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.
Note that I brought this package internally because when Vite crawls through package.jsons, this one has a main but the main doesn't export anything so Vite warns about it not being a real ESM module. This is only a (small) types package, so brought it in.