Skip to content

Commit ea5fdeb

Browse files
authored
refactor: normalize publicDir when resolving config (#15360)
1 parent 6ace32b commit ea5fdeb

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

packages/vite/src/node/config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,11 @@ export async function resolveConfig(
651651
const { publicDir } = config
652652
const resolvedPublicDir =
653653
publicDir !== false && publicDir !== ''
654-
? path.resolve(
655-
resolvedRoot,
656-
typeof publicDir === 'string' ? publicDir : 'public',
654+
? normalizePath(
655+
path.resolve(
656+
resolvedRoot,
657+
typeof publicDir === 'string' ? publicDir : 'public',
658+
),
657659
)
658660
: ''
659661

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { ResolveFn } from '../'
77
import {
88
injectQuery,
99
isParentDirectory,
10-
normalizePath,
1110
slash,
1211
transformStableResult,
1312
} from '../utils'
@@ -28,7 +27,7 @@ import { tryFsResolve } from './resolve'
2827
* ```
2928
*/
3029
export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
31-
const normalizedPublicDir = normalizePath(config.publicDir)
30+
const { publicDir } = config
3231
let assetResolver: ResolveFn
3332

3433
const fsResolveOptions: InternalResolveOptions = {
@@ -117,7 +116,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
117116
file = await assetResolver(url, id)
118117
file ??=
119118
url[0] === '/'
120-
? slash(path.join(config.publicDir, url))
119+
? slash(path.join(publicDir, url))
121120
: slash(path.resolve(path.dirname(id), url))
122121
}
123122

@@ -126,9 +125,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
126125
let builtUrl: string | undefined
127126
if (file) {
128127
try {
129-
if (isParentDirectory(normalizedPublicDir, file)) {
130-
const publicPath =
131-
'/' + path.posix.relative(normalizedPublicDir, file)
128+
if (isParentDirectory(publicDir, file)) {
129+
const publicPath = '/' + path.posix.relative(publicDir, file)
132130
builtUrl = await fileToUrl(publicPath, config, this)
133131
} else {
134132
builtUrl = await fileToUrl(file, config, this)

packages/vite/src/node/publicDir.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function checkPublicFile(
5656
}
5757

5858
const publicFile = normalizePath(path.join(publicDir, fileName))
59-
if (!publicFile.startsWith(withTrailingSlash(normalizePath(publicDir)))) {
59+
if (!publicFile.startsWith(withTrailingSlash(publicDir))) {
6060
// can happen if URL starts with '../'
6161
return
6262
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -650,17 +650,15 @@ export async function _createServer(
650650
}
651651
}
652652

653-
const normalizedPublicDir = normalizePath(config.publicDir)
653+
const { publicDir } = config
654654

655655
const onFileAddUnlink = async (file: string, isUnlink: boolean) => {
656656
file = normalizePath(file)
657657
await container.watchChange(file, { event: isUnlink ? 'delete' : 'create' })
658658

659-
if (config.publicDir && publicFiles) {
660-
if (file.startsWith(normalizedPublicDir)) {
661-
publicFiles[isUnlink ? 'delete' : 'add'](
662-
file.slice(normalizedPublicDir.length),
663-
)
659+
if (publicDir && publicFiles) {
660+
if (file.startsWith(publicDir)) {
661+
publicFiles[isUnlink ? 'delete' : 'add'](file.slice(publicDir.length))
664662
}
665663
}
666664
await handleFileAddUnlink(file, server, isUnlink)
@@ -761,7 +759,7 @@ export async function _createServer(
761759
// serve static files under /public
762760
// this applies before the transform middleware so that these files are served
763761
// as-is without transforms.
764-
if (config.publicDir) {
762+
if (publicDir) {
765763
middlewares.use(servePublicMiddleware(server, publicFiles))
766764
}
767765

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ export function transformMiddleware(
5050
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
5151

5252
// check if public dir is inside root dir
53-
const { root } = server.config
54-
const publicDir = normalizePath(server.config.publicDir)
53+
const { root, publicDir } = server.config
5554
const publicDirInRoot = publicDir.startsWith(withTrailingSlash(root))
5655
const publicPath = `${publicDir.slice(root.length)}/`
5756

0 commit comments

Comments
 (0)