Skip to content

Commit

Permalink
chore: improve public assets warning message (#6738)
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed May 29, 2022
1 parent f8c92d1 commit f6bd317
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/vite/src/node/server/middlewares/transform.ts
Expand Up @@ -122,14 +122,27 @@ export function transformMiddleware(
const publicPath = `${publicDir.slice(rootDir.length)}/`
// warn explicit public paths
if (url.startsWith(publicPath)) {
logger.warn(
colors.yellow(
let warning: string

if (isImportRequest(url)) {
const rawUrl = removeImportQuery(url)

warning =
'Assets in public cannot be imported from JavaScript.\n' +
`Instead of ${colors.cyan(
rawUrl
)}, put the file in the src directory, and use ${colors.cyan(
rawUrl.replace(publicPath, '/src/')
)} instead.`
} else {
warning =
`files in the public directory are served at the root path.\n` +
`Instead of ${colors.cyan(url)}, use ${colors.cyan(
url.replace(publicPath, '/')
)}.`
)
)
`Instead of ${colors.cyan(url)}, use ${colors.cyan(
url.replace(publicPath, '/')
)}.`
}

logger.warn(colors.yellow(warning))
}
}

Expand Down

0 comments on commit f6bd317

Please sign in to comment.