Skip to content

Commit 0506812

Browse files
authored
perf: avoid computing paths on each request (#15318)
1 parent 746a1da commit 0506812

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ export function transformMiddleware(
4848
server: ViteDevServer,
4949
): Connect.NextHandleFunction {
5050
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
51+
52+
// check if public dir is inside root dir
53+
const { root } = server.config
54+
const publicDir = normalizePath(server.config.publicDir)
55+
const publicDirInRoot = publicDir.startsWith(withTrailingSlash(root))
56+
const publicPath = `${publicDir.slice(root.length)}/`
57+
5158
return async function viteTransformMiddleware(req, res, next) {
5259
if (req.method !== 'GET' || knownIgnoreList.has(req.url!)) {
5360
return next()
@@ -123,43 +130,8 @@ export function transformMiddleware(
123130
}
124131
}
125132

126-
// check if public dir is inside root dir
127-
const publicDir = normalizePath(server.config.publicDir)
128-
const rootDir = normalizePath(server.config.root)
129-
if (publicDir.startsWith(withTrailingSlash(rootDir))) {
130-
const publicPath = `${publicDir.slice(rootDir.length)}/`
131-
// warn explicit public paths
132-
if (url.startsWith(withTrailingSlash(publicPath))) {
133-
let warning: string
134-
135-
if (isImportRequest(url)) {
136-
const rawUrl = removeImportQuery(url)
137-
if (urlRE.test(url)) {
138-
warning =
139-
`Assets in the public directory are served at the root path.\n` +
140-
`Instead of ${colors.cyan(rawUrl)}, use ${colors.cyan(
141-
rawUrl.replace(publicPath, '/'),
142-
)}.`
143-
} else {
144-
warning =
145-
'Assets in public directory cannot be imported from JavaScript.\n' +
146-
`If you intend to import that asset, put the file in the src directory, and use ${colors.cyan(
147-
rawUrl.replace(publicPath, '/src/'),
148-
)} instead of ${colors.cyan(rawUrl)}.\n` +
149-
`If you intend to use the URL of that asset, use ${colors.cyan(
150-
injectQuery(rawUrl.replace(publicPath, '/'), 'url'),
151-
)}.`
152-
}
153-
} else {
154-
warning =
155-
`Files in the public directory are served at the root path.\n` +
156-
`Instead of ${colors.cyan(url)}, use ${colors.cyan(
157-
url.replace(publicPath, '/'),
158-
)}.`
159-
}
160-
161-
server.config.logger.warn(colors.yellow(warning))
162-
}
133+
if (publicDirInRoot && url.startsWith(publicPath)) {
134+
warnAboutExplicitPublicPathInUrl(url)
163135
}
164136

165137
if (
@@ -265,4 +237,36 @@ export function transformMiddleware(
265237

266238
next()
267239
}
240+
241+
function warnAboutExplicitPublicPathInUrl(url: string) {
242+
let warning: string
243+
244+
if (isImportRequest(url)) {
245+
const rawUrl = removeImportQuery(url)
246+
if (urlRE.test(url)) {
247+
warning =
248+
`Assets in the public directory are served at the root path.\n` +
249+
`Instead of ${colors.cyan(rawUrl)}, use ${colors.cyan(
250+
rawUrl.replace(publicPath, '/'),
251+
)}.`
252+
} else {
253+
warning =
254+
'Assets in public directory cannot be imported from JavaScript.\n' +
255+
`If you intend to import that asset, put the file in the src directory, and use ${colors.cyan(
256+
rawUrl.replace(publicPath, '/src/'),
257+
)} instead of ${colors.cyan(rawUrl)}.\n` +
258+
`If you intend to use the URL of that asset, use ${colors.cyan(
259+
injectQuery(rawUrl.replace(publicPath, '/'), 'url'),
260+
)}.`
261+
}
262+
} else {
263+
warning =
264+
`Files in the public directory are served at the root path.\n` +
265+
`Instead of ${colors.cyan(url)}, use ${colors.cyan(
266+
url.replace(publicPath, '/'),
267+
)}.`
268+
}
269+
270+
server.config.logger.warn(colors.yellow(warning))
271+
}
268272
}

0 commit comments

Comments
 (0)