Skip to content

Commit

Permalink
refactor: normalize publicDir when resolving config (#15360)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Jan 8, 2024
1 parent 6ace32b commit ea5fdeb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
8 changes: 5 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,11 @@ export async function resolveConfig(
const { publicDir } = config
const resolvedPublicDir =
publicDir !== false && publicDir !== ''
? path.resolve(
resolvedRoot,
typeof publicDir === 'string' ? publicDir : 'public',
? normalizePath(
path.resolve(
resolvedRoot,
typeof publicDir === 'string' ? publicDir : 'public',
),
)
: ''

Expand Down
10 changes: 4 additions & 6 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { ResolveFn } from '../'
import {
injectQuery,
isParentDirectory,
normalizePath,
slash,
transformStableResult,
} from '../utils'
Expand All @@ -28,7 +27,7 @@ import { tryFsResolve } from './resolve'
* ```
*/
export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const normalizedPublicDir = normalizePath(config.publicDir)
const { publicDir } = config
let assetResolver: ResolveFn

const fsResolveOptions: InternalResolveOptions = {
Expand Down Expand Up @@ -117,7 +116,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
file = await assetResolver(url, id)
file ??=
url[0] === '/'
? slash(path.join(config.publicDir, url))
? slash(path.join(publicDir, url))
: slash(path.resolve(path.dirname(id), url))
}

Expand All @@ -126,9 +125,8 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
let builtUrl: string | undefined
if (file) {
try {
if (isParentDirectory(normalizedPublicDir, file)) {
const publicPath =
'/' + path.posix.relative(normalizedPublicDir, file)
if (isParentDirectory(publicDir, file)) {
const publicPath = '/' + path.posix.relative(publicDir, file)
builtUrl = await fileToUrl(publicPath, config, this)
} else {
builtUrl = await fileToUrl(file, config, this)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/publicDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function checkPublicFile(
}

const publicFile = normalizePath(path.join(publicDir, fileName))
if (!publicFile.startsWith(withTrailingSlash(normalizePath(publicDir)))) {
if (!publicFile.startsWith(withTrailingSlash(publicDir))) {
// can happen if URL starts with '../'
return
}
Expand Down
12 changes: 5 additions & 7 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,17 +650,15 @@ export async function _createServer(
}
}

const normalizedPublicDir = normalizePath(config.publicDir)
const { publicDir } = config

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

if (config.publicDir && publicFiles) {
if (file.startsWith(normalizedPublicDir)) {
publicFiles[isUnlink ? 'delete' : 'add'](
file.slice(normalizedPublicDir.length),
)
if (publicDir && publicFiles) {
if (file.startsWith(publicDir)) {
publicFiles[isUnlink ? 'delete' : 'add'](file.slice(publicDir.length))
}
}
await handleFileAddUnlink(file, server, isUnlink)
Expand Down Expand Up @@ -761,7 +759,7 @@ export async function _createServer(
// serve static files under /public
// this applies before the transform middleware so that these files are served
// as-is without transforms.
if (config.publicDir) {
if (publicDir) {
middlewares.use(servePublicMiddleware(server, publicFiles))
}

Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export function transformMiddleware(
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`

// check if public dir is inside root dir
const { root } = server.config
const publicDir = normalizePath(server.config.publicDir)
const { root, publicDir } = server.config
const publicDirInRoot = publicDir.startsWith(withTrailingSlash(root))
const publicPath = `${publicDir.slice(root.length)}/`

Expand Down

0 comments on commit ea5fdeb

Please sign in to comment.