Skip to content

Commit 2540ed0

Browse files
hi-ogawaclaude
andauthored
feat: forward browser console logs and errors to dev server terminal (#20916)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent b95ca22 commit 2540ed0

24 files changed

Lines changed: 994 additions & 14 deletions

File tree

docs/config/server-options.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,44 @@ The error that appears in the Browser when the fallback happens can be ignored.
224224

225225
:::
226226

227+
## server.forwardConsole
228+
229+
- **Type:** `boolean | { unhandledErrors?: boolean, logLevels?: ('error' | 'warn' | 'info' | 'log' | 'debug')[] }`
230+
- **Default:** auto (`true` when an AI coding agent is detected based on [`@vercel/detect-agent`](https://www.npmjs.com/package/@vercel/detect-agent), otherwise `false`)
231+
232+
Forward browser runtime events to the Vite server console during development.
233+
234+
- `true` enables forwarding unhandled errors and `console.error` / `console.warn` logs.
235+
- `unhandledErrors` controls forwarding uncaught exceptions and unhandled promise rejections.
236+
- `logLevels` controls which `console.*` calls are forwarded.
237+
238+
For example:
239+
240+
```js
241+
export default defineConfig({
242+
server: {
243+
forwardConsole: {
244+
unhandledErrors: true,
245+
logLevels: ['warn', 'error'],
246+
},
247+
},
248+
})
249+
```
250+
251+
When unhandled errors are forwarded, they are logged in the server terminal with enhanced formatting, for example:
252+
253+
```log
254+
1:18:38 AM [vite] (client) [Unhandled error] Error: this is test error
255+
> testError src/main.ts:20:8
256+
18|
257+
19| function testError() {
258+
20| throw new Error('this is test error')
259+
| ^
260+
21| }
261+
22|
262+
> HTMLButtonElement.<anonymous> src/main.ts:6:2
263+
```
264+
227265
## server.warmup
228266

229267
- **Type:** `{ clientFiles?: string[], ssrFiles?: string[] }`

packages/vite/LICENSE.md

Lines changed: 238 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SOFTWARE.
2525

2626
# Licenses of bundled dependencies
2727
The published Vite artifact additionally contains code with the following licenses:
28-
BSD-2-Clause, CC0-1.0, ISC, MIT
28+
Apache-2.0, BSD-2-Clause, CC0-1.0, ISC, MIT
2929

3030
# Bundled dependencies:
3131
## @jridgewell/gen-mapping, @jridgewell/remapping, @jridgewell/sourcemap-codec, @jridgewell/trace-mapping
@@ -132,6 +132,243 @@ Repository: https://github.com/rollup/plugins
132132
133133
---------------------------------------
134134

135+
## @vercel/detect-agent
136+
License: Apache-2.0
137+
By: Vercel
138+
Repository: https://github.com/vercel/vercel
139+
140+
> Apache License
141+
> Version 2.0, January 2004
142+
> http://www.apache.org/licenses/
143+
>
144+
> TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
145+
>
146+
> 1. Definitions.
147+
>
148+
> "License" shall mean the terms and conditions for use, reproduction,
149+
> and distribution as defined by Sections 1 through 9 of this document.
150+
>
151+
> "Licensor" shall mean the copyright owner or entity authorized by
152+
> the copyright owner that is granting the License.
153+
>
154+
> "Legal Entity" shall mean the union of the acting entity and all
155+
> other entities that control, are controlled by, or are under common
156+
> control with that entity. For the purposes of this definition,
157+
> "control" means (i) the power, direct or indirect, to cause the
158+
> direction or management of such entity, whether by contract or
159+
> otherwise, or (ii) ownership of fifty percent (50%) or more of the
160+
> outstanding shares, or (iii) beneficial ownership of such entity.
161+
>
162+
> "You" (or "Your") shall mean an individual or Legal Entity
163+
> exercising permissions granted by this License.
164+
>
165+
> "Source" form shall mean the preferred form for making modifications,
166+
> including but not limited to software source code, documentation
167+
> source, and configuration files.
168+
>
169+
> "Object" form shall mean any form resulting from mechanical
170+
> transformation or translation of a Source form, including but
171+
> not limited to compiled object code, generated documentation,
172+
> and conversions to other media types.
173+
>
174+
> "Work" shall mean the work of authorship, whether in Source or
175+
> Object form, made available under the License, as indicated by a
176+
> copyright notice that is included in or attached to the work
177+
> (an example is provided in the Appendix below).
178+
>
179+
> "Derivative Works" shall mean any work, whether in Source or Object
180+
> form, that is based on (or derived from) the Work and for which the
181+
> editorial revisions, annotations, elaborations, or other modifications
182+
> represent, as a whole, an original work of authorship. For the purposes
183+
> of this License, Derivative Works shall not include works that remain
184+
> separable from, or merely link (or bind by name) to the interfaces of,
185+
> the Work and Derivative Works thereof.
186+
>
187+
> "Contribution" shall mean any work of authorship, including
188+
> the original version of the Work and any modifications or additions
189+
> to that Work or Derivative Works thereof, that is intentionally
190+
> submitted to Licensor for inclusion in the Work by the copyright owner
191+
> or by an individual or Legal Entity authorized to submit on behalf of
192+
> the copyright owner. For the purposes of this definition, "submitted"
193+
> means any form of electronic, verbal, or written communication sent
194+
> to the Licensor or its representatives, including but not limited to
195+
> communication on electronic mailing lists, source code control systems,
196+
> and issue tracking systems that are managed by, or on behalf of, the
197+
> Licensor for the purpose of discussing and improving the Work, but
198+
> excluding communication that is conspicuously marked or otherwise
199+
> designated in writing by the copyright owner as "Not a Contribution."
200+
>
201+
> "Contributor" shall mean Licensor and any individual or Legal Entity
202+
> on behalf of whom a Contribution has been received by Licensor and
203+
> subsequently incorporated within the Work.
204+
>
205+
> 2. Grant of Copyright License. Subject to the terms and conditions of
206+
> this License, each Contributor hereby grants to You a perpetual,
207+
> worldwide, non-exclusive, no-charge, royalty-free, irrevocable
208+
> copyright license to reproduce, prepare Derivative Works of,
209+
> publicly display, publicly perform, sublicense, and distribute the
210+
> Work and such Derivative Works in Source or Object form.
211+
>
212+
> 3. Grant of Patent License. Subject to the terms and conditions of
213+
> this License, each Contributor hereby grants to You a perpetual,
214+
> worldwide, non-exclusive, no-charge, royalty-free, irrevocable
215+
> (except as stated in this section) patent license to make, have made,
216+
> use, offer to sell, sell, import, and otherwise transfer the Work,
217+
> where such license applies only to those patent claims licensable
218+
> by such Contributor that are necessarily infringed by their
219+
> Contribution(s) alone or by combination of their Contribution(s)
220+
> with the Work to which such Contribution(s) was submitted. If You
221+
> institute patent litigation against any entity (including a
222+
> cross-claim or counterclaim in a lawsuit) alleging that the Work
223+
> or a Contribution incorporated within the Work constitutes direct
224+
> or contributory patent infringement, then any patent licenses
225+
> granted to You under this License for that Work shall terminate
226+
> as of the date such litigation is filed.
227+
>
228+
> 4. Redistribution. You may reproduce and distribute copies of the
229+
> Work or Derivative Works thereof in any medium, with or without
230+
> modifications, and in Source or Object form, provided that You
231+
> meet the following conditions:
232+
>
233+
> (a) You must give any other recipients of the Work or
234+
> Derivative Works a copy of this License; and
235+
>
236+
> (b) You must cause any modified files to carry prominent notices
237+
> stating that You changed the files; and
238+
>
239+
> (c) You must retain, in the Source form of any Derivative Works
240+
> that You distribute, all copyright, patent, trademark, and
241+
> attribution notices from the Source form of the Work,
242+
> excluding those notices that do not pertain to any part of
243+
> the Derivative Works; and
244+
>
245+
> (d) If the Work includes a "NOTICE" text file as part of its
246+
> distribution, then any Derivative Works that You distribute must
247+
> include a readable copy of the attribution notices contained
248+
> within such NOTICE file, excluding those notices that do not
249+
> pertain to any part of the Derivative Works, in at least one
250+
> of the following places: within a NOTICE text file distributed
251+
> as part of the Derivative Works; within the Source form or
252+
> documentation, if provided along with the Derivative Works; or,
253+
> within a display generated by the Derivative Works, if and
254+
> wherever such third-party notices normally appear. The contents
255+
> of the NOTICE file are for informational purposes only and
256+
> do not modify the License. You may add Your own attribution
257+
> notices within Derivative Works that You distribute, alongside
258+
> or as an addendum to the NOTICE text from the Work, provided
259+
> that such additional attribution notices cannot be construed
260+
> as modifying the License.
261+
>
262+
> You may add Your own copyright statement to Your modifications and
263+
> may provide additional or different license terms and conditions
264+
> for use, reproduction, or distribution of Your modifications, or
265+
> for any such Derivative Works as a whole, provided Your use,
266+
> reproduction, and distribution of the Work otherwise complies with
267+
> the conditions stated in this License.
268+
>
269+
> 5. Submission of Contributions. Unless You explicitly state otherwise,
270+
> any Contribution intentionally submitted for inclusion in the Work
271+
> by You to the Licensor shall be under the terms and conditions of
272+
> this License, without any additional terms or conditions.
273+
> Notwithstanding the above, nothing herein shall supersede or modify
274+
> the terms of any separate license agreement you may have executed
275+
> with Licensor regarding such Contributions.
276+
>
277+
> 6. Trademarks. This License does not grant permission to use the trade
278+
> names, trademarks, service marks, or product names of the Licensor,
279+
> except as required for reasonable and customary use in describing the
280+
> origin of the Work and reproducing the content of the NOTICE file.
281+
>
282+
> 7. Disclaimer of Warranty. Unless required by applicable law or
283+
> agreed to in writing, Licensor provides the Work (and each
284+
> Contributor provides its Contributions) on an "AS IS" BASIS,
285+
> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
286+
> implied, including, without limitation, any warranties or conditions
287+
> of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
288+
> PARTICULAR PURPOSE. You are solely responsible for determining the
289+
> appropriateness of using or redistributing the Work and assume any
290+
> risks associated with Your exercise of permissions under this License.
291+
>
292+
> 8. Limitation of Liability. In no event and under no legal theory,
293+
> whether in tort (including negligence), contract, or otherwise,
294+
> unless required by applicable law (such as deliberate and grossly
295+
> negligent acts) or agreed to in writing, shall any Contributor be
296+
> liable to You for damages, including any direct, indirect, special,
297+
> incidental, or consequential damages of any character arising as a
298+
> result of this License or out of the use or inability to use the
299+
> Work (including but not limited to damages for loss of goodwill,
300+
> work stoppage, computer failure or malfunction, or any and all
301+
> other commercial damages or losses), even if such Contributor
302+
> has been advised of the possibility of such damages.
303+
>
304+
> 9. Accepting Warranty or Additional Liability. While redistributing
305+
> the Work or Derivative Works thereof, You may choose to offer,
306+
> and charge a fee for, acceptance of support, warranty, indemnity,
307+
> or other liability obligations and/or rights consistent with this
308+
> License. However, in accepting such obligations, You may act only
309+
> on Your own behalf and on Your sole responsibility, not on behalf
310+
> of any other Contributor, and only if You agree to indemnify,
311+
> defend, and hold each Contributor harmless for any liability
312+
> incurred by, or claims asserted against, such Contributor by reason
313+
> of your accepting any such warranty or additional liability.
314+
>
315+
> END OF TERMS AND CONDITIONS
316+
>
317+
> APPENDIX: How to apply the Apache License to your work.
318+
>
319+
> To apply the Apache License to your work, attach the following
320+
> boilerplate notice, with the fields enclosed by brackets "[]"
321+
> replaced with your own identifying information. (Don't include
322+
> the brackets!) The text should be enclosed in the appropriate
323+
> comment syntax for the file format. We also recommend that a
324+
> file or class name and description of purpose be included on the
325+
> same "printed page" as the copyright notice for easier
326+
> identification within third-party archives.
327+
>
328+
> Copyright 2017 Vercel, Inc.
329+
>
330+
> Licensed under the Apache License, Version 2.0 (the "License");
331+
> you may not use this file except in compliance with the License.
332+
> You may obtain a copy of the License at
333+
>
334+
> http://www.apache.org/licenses/LICENSE-2.0
335+
>
336+
> Unless required by applicable law or agreed to in writing, software
337+
> distributed under the License is distributed on an "AS IS" BASIS,
338+
> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
339+
> See the License for the specific language governing permissions and
340+
> limitations under the License.
341+
342+
---------------------------------------
343+
344+
## @vitest/utils
345+
License: MIT
346+
Repository: https://github.com/vitest-dev/vitest
347+
348+
> MIT License
349+
>
350+
> Copyright (c) 2021-Present VoidZero Inc. and Vitest contributors
351+
>
352+
> Permission is hereby granted, free of charge, to any person obtaining a copy
353+
> of this software and associated documentation files (the "Software"), to deal
354+
> in the Software without restriction, including without limitation the rights
355+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
356+
> copies of the Software, and to permit persons to whom the Software is
357+
> furnished to do so, subject to the following conditions:
358+
>
359+
> The above copyright notice and this permission notice shall be included in all
360+
> copies or substantial portions of the Software.
361+
>
362+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
363+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
364+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
365+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
366+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
367+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
368+
> SOFTWARE.
369+
370+
---------------------------------------
371+
135372
## anymatch
136373
License: ISC
137374
By: Elan Shanker

packages/vite/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@
9191
"@rollup/plugin-alias": "^6.0.0",
9292
"@rollup/plugin-dynamic-import-vars": "2.1.4",
9393
"@rollup/pluginutils": "^5.3.0",
94+
"@vercel/detect-agent": "^1.1.0",
9495
"@types/escape-html": "^1.0.4",
9596
"@types/pnpapi": "^0.0.5",
97+
"@vitest/utils": "4.1.0-beta.5",
9698
"@vitejs/devtools": "^0.0.0-alpha.32",
9799
"artichokie": "^0.4.2",
98100
"baseline-browser-mapping": "^2.10.0",

packages/vite/src/client/client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
normalizeModuleRunnerTransport,
1111
} from '../shared/moduleRunnerTransport'
1212
import { createHMRHandler } from '../shared/hmrHandler'
13+
import { setupForwardConsoleHandler } from '../shared/forwardConsole'
1314
import { ErrorOverlay, cspNonce, overlayId } from './overlay'
1415
import '@vite/env'
1516

@@ -24,6 +25,7 @@ declare const __HMR_BASE__: string
2425
declare const __HMR_TIMEOUT__: number
2526
declare const __HMR_ENABLE_OVERLAY__: boolean
2627
declare const __WS_TOKEN__: string
28+
declare const __SERVER_FORWARD_CONSOLE__: any
2729
declare const __BUNDLED_DEV__: boolean
2830

2931
console.debug('[vite] connecting...')
@@ -43,6 +45,7 @@ const base = __BASE__ || '/'
4345
const hmrTimeout = __HMR_TIMEOUT__
4446
const wsToken = __WS_TOKEN__
4547
const isBundleMode = __BUNDLED_DEV__
48+
const forwardConsole = __SERVER_FORWARD_CONSOLE__
4649

4750
const transport = normalizeModuleRunnerTransport(
4851
(() => {
@@ -196,6 +199,8 @@ const hmrClient = new HMRClient(
196199
)
197200
transport.connect!(createHMRHandler(handleMessage))
198201

202+
setupForwardConsoleHandler(transport, forwardConsole)
203+
199204
async function handleMessage(payload: HotPayload) {
200205
switch (payload.type) {
201206
case 'connected':

packages/vite/src/node/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ export async function resolveConfig(
16981698
)
16991699
: ''
17001700

1701-
const server = resolveServerOptions(resolvedRoot, config.server, logger)
1701+
const server = await resolveServerOptions(resolvedRoot, config.server, logger)
17021702

17031703
const builder = resolveBuilderOptions(config.builder)
17041704

packages/vite/src/node/plugins/clientInjections.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ async function createClientConfigValueReplacer(
112112
const hmrEnableOverlayReplacement = escapeReplacement(overlay)
113113
const hmrConfigNameReplacement = escapeReplacement(hmrConfigName)
114114
const wsTokenReplacement = escapeReplacement(config.webSocketToken)
115+
const serverForwardConsoleReplacement = escapeReplacement(
116+
config.server.forwardConsole as any,
117+
)
115118
const bundleDevReplacement = escapeReplacement(
116119
config.experimental.bundledDev || false,
117120
)
@@ -130,6 +133,7 @@ async function createClientConfigValueReplacer(
130133
.replace(`__HMR_ENABLE_OVERLAY__`, hmrEnableOverlayReplacement)
131134
.replace(`__HMR_CONFIG_NAME__`, hmrConfigNameReplacement)
132135
.replace(`__WS_TOKEN__`, wsTokenReplacement)
136+
.replace(`__SERVER_FORWARD_CONSOLE__`, serverForwardConsoleReplacement)
133137
.replaceAll(`__BUNDLED_DEV__`, bundleDevReplacement)
134138
}
135139

0 commit comments

Comments
 (0)