From 9e06b81973cf298aeb0a1f48c67a496bf984c663 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Tue, 14 Sep 2021 15:16:04 +0800 Subject: [PATCH] fix: avoid wrong warning of explicit public paths (#4927) --- .../src/node/server/middlewares/transform.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index 26fb1a0a6793cd..a68308f391e363 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -114,20 +114,22 @@ export function transformMiddleware( } } - const publicPath = - normalizePath(server.config.publicDir).slice( - server.config.root.length - ) + '/' - // warn explicit public paths - if (url.startsWith(publicPath)) { - logger.warn( - chalk.yellow( - `files in the public directory are served at the root path.\n` + - `Instead of ${chalk.cyan(url)}, use ${chalk.cyan( - url.replace(publicPath, '/') - )}.` + // check if public dir is inside root dir + const publicDir = normalizePath(server.config.publicDir) + const rootDir = normalizePath(server.config.root) + if (publicDir.startsWith(rootDir)) { + const publicPath = `${publicDir.slice(rootDir.length)}/` + // warn explicit public paths + if (url.startsWith(publicPath)) { + logger.warn( + chalk.yellow( + `files in the public directory are served at the root path.\n` + + `Instead of ${chalk.cyan(url)}, use ${chalk.cyan( + url.replace(publicPath, '/') + )}.` + ) ) - ) + } } if (