Skip to content

Commit 4a9307c

Browse files
committed
refactor: improve public file import warning
1 parent d9c3fdb commit 4a9307c

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

packages/vite/src/client/overlay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class ErrorOverlay extends HTMLElement {
140140
if (hasFrame) {
141141
this.text('.frame', err.frame!.trim())
142142
}
143-
this.text('.stack', err.stack.replace(codeframeRE, '').trim(), true)
143+
this.text('.stack', err.stack, true)
144144

145145
this.root.querySelector('.window')!.addEventListener('click', (e) => {
146146
e.stopPropagation()

packages/vite/src/node/server/middlewares/error.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function prepareError(err: Error | RollupError): ErrorPayload['err'] {
1111
// properties, since some errors may attach full objects (e.g. PostCSS)
1212
return {
1313
message: strip(err.message),
14-
stack: strip(err.stack || ''),
14+
stack: strip(cleanStack(err.stack || '')),
1515
id: (err as RollupError).id,
1616
frame: strip((err as RollupError).frame || ''),
1717
plugin: (err as RollupError).plugin,
@@ -20,18 +20,31 @@ export function prepareError(err: Error | RollupError): ErrorPayload['err'] {
2020
}
2121
}
2222

23+
export function buildErrorMessage(err: RollupError, args: string[] = []) {
24+
if (err.plugin) args.push(` Plugin: ${chalk.magenta(err.plugin)}`)
25+
if (err.id) args.push(` File: ${chalk.cyan(err.id)}`)
26+
if (err.frame) args.push(chalk.yellow(pad(err.frame)))
27+
if (err.stack) args.push(pad(cleanStack(err.stack)))
28+
return args.join('\n')
29+
}
30+
31+
function cleanStack(stack: string) {
32+
return stack
33+
.split(/\n/g)
34+
.filter((l) => /^\s*at/.test(l))
35+
.join('\n')
36+
}
37+
2338
export function errorMiddleware(
2439
server: ViteDevServer
2540
): Connect.ErrorHandleFunction {
2641
// note the 4 args must be kept for connect to treat this as error middleware
2742
return (err: RollupError, _req, res, _next) => {
28-
const args = [chalk.red(`Internal server error: ${err.message}`)]
29-
if (err.plugin) args.push(` Plugin: ${chalk.magenta(err.plugin)}`)
30-
if (err.id) args.push(` File: ${chalk.cyan(err.id)}`)
31-
if (err.frame) args.push(chalk.yellow(pad(err.frame)))
32-
if (err.stack) args.push(pad(err.stack))
43+
const msg = buildErrorMessage(err, [
44+
chalk.red(`Internal server error: ${err.message}`)
45+
])
3346

34-
server.config.logger.error(args.join('\n'), {
47+
server.config.logger.error(msg, {
3548
clear: true,
3649
timestamp: true
3750
})

packages/vite/src/node/server/middlewares/static.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ export function serveStaticMiddleware(
1616
return (req, res, next) => {
1717
let url = req.url!
1818

19-
// skip import request
20-
if (isImportRequest(url)) {
21-
return next()
22-
}
23-
2419
// only serve the file if it's not an html request
2520
// so that html requests can fallthrough to our html middleware for
2621
// special processing
@@ -30,7 +25,12 @@ export function serveStaticMiddleware(
3025
) {
3126
return next()
3227
}
33-
28+
29+
// skip import request
30+
if (isImportRequest(url)) {
31+
return next()
32+
}
33+
3434
// #1426
3535
url = req.url = decodeURIComponent(url)
3636

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import merge from 'merge-source-map'
5555
import MagicString from 'magic-string'
5656
import { FSWatcher } from 'chokidar'
5757
import {
58-
pad,
5958
createDebugger,
6059
generateCodeFrame,
6160
normalizePath,
@@ -65,6 +64,7 @@ import {
6564
} from '../utils'
6665
import chalk from 'chalk'
6766
import { ResolvedConfig } from '../config'
67+
import { buildErrorMessage } from './middlewares/error'
6868

6969
export interface PluginContainerOptions {
7070
cwd?: string
@@ -248,12 +248,10 @@ export async function createPluginContainer(
248248
position?: number | { column: number; line: number }
249249
) {
250250
const err = formatError(e, position, this)
251-
const args = [chalk.yellow(`warning: ${err.message}`)]
252-
if (err.plugin) args.push(` Plugin: ${chalk.magenta(err.plugin)}`)
253-
if (err.id) args.push(` File: ${chalk.cyan(err.id)}`)
254-
if (err.frame) args.push(chalk.yellow(pad(err.frame)))
255-
if (err.stack) args.push(pad(err.stack))
256-
logger.warn(args.join('\n'), {
251+
const msg = buildErrorMessage(err, [
252+
chalk.yellow(`warning: ${err.message}`)
253+
])
254+
logger.warn(msg, {
257255
clear: true,
258256
timestamp: true
259257
})

packages/vite/src/node/server/transformRequest.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
removeTimestampQuery,
1313
timeFrom
1414
} from '../utils'
15+
import { checkPublicFile } from '../plugins/asset'
1516

1617
const debugLoad = createDebugger('vite:load')
1718
const debugTransform = createDebugger('vite:transform')
@@ -76,9 +77,12 @@ export async function transformRequest(
7677
}
7778
}
7879
if (code == null) {
79-
throw new Error(
80-
`Failed to load url ${url} (resolved id: ${id}). Does the file exist?`
81-
)
80+
const msg = checkPublicFile(url, root)
81+
? `This file is in /public and will be copied as-is during build without ` +
82+
`going through the plugin transforms, and therefore should not be ` +
83+
`imported from source code. It can only be referenced via HTML tags.`
84+
: `Does the file exist?`
85+
throw new Error(`Failed to load url ${url} (resolved id: ${id}). ${msg}`)
8286
}
8387

8488
// ensure module in graph after successful load

0 commit comments

Comments
 (0)