mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 01:45:53 +00:00
fix(vite): don't warm up css deps and normalise urls correctly (#23975)
This commit is contained in:
parent
62b7917d2c
commit
4f017a5538
@ -1,6 +1,33 @@
|
|||||||
import { logger } from '@nuxt/kit'
|
import { logger } from '@nuxt/kit'
|
||||||
|
import { join, normalize, relative } from 'pathe'
|
||||||
|
import { withoutBase } from 'ufo'
|
||||||
|
import { isCSSRequest } from 'vite'
|
||||||
import type { ViteDevServer } from 'vite'
|
import type { ViteDevServer } from 'vite'
|
||||||
|
|
||||||
|
// https://github.com/vitejs/vite/tree/main/packages/vite/src/node/server/warmup.ts#L62-L70
|
||||||
|
function fileToUrl (file: string, root: string) {
|
||||||
|
const url = relative(root, file)
|
||||||
|
// out of root, use /@fs/ prefix
|
||||||
|
if (url[0] === '.') {
|
||||||
|
return join('/@fs/', normalize(file))
|
||||||
|
}
|
||||||
|
// file within root, create root-relative url
|
||||||
|
return '/' + normalize(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
function normaliseURL (url: string, base: string) {
|
||||||
|
// remove any base url
|
||||||
|
url = withoutBase(url, base)
|
||||||
|
// unwrap record
|
||||||
|
if (url.startsWith('/@id/')) {
|
||||||
|
url = url.slice('/@id/'.length).replace('__x00__', '\0')
|
||||||
|
}
|
||||||
|
// strip query
|
||||||
|
url = url.replace(/(\?|&)import=?(?:&|$)/, '').replace(/[?&]$/, '')
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: use built-in warmup logic when we update to vite 5
|
||||||
export async function warmupViteServer (
|
export async function warmupViteServer (
|
||||||
server: ViteDevServer,
|
server: ViteDevServer,
|
||||||
entries: string[],
|
entries: string[],
|
||||||
@ -9,7 +36,12 @@ export async function warmupViteServer (
|
|||||||
const warmedUrls = new Set<String>()
|
const warmedUrls = new Set<String>()
|
||||||
|
|
||||||
const warmup = async (url: string) => {
|
const warmup = async (url: string) => {
|
||||||
if (warmedUrls.has(url)) {
|
url = normaliseURL(url, server.config.base)
|
||||||
|
|
||||||
|
if (warmedUrls.has(url)) { return }
|
||||||
|
const m = await server.moduleGraph.getModuleByUrl(url, isServer)
|
||||||
|
// a module that is already compiled (and can't be warmed up anyway)
|
||||||
|
if (m?.transformResult?.code || m?.ssrTransformResult?.code) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
warmedUrls.add(url)
|
warmedUrls.add(url)
|
||||||
@ -18,10 +50,14 @@ export async function warmupViteServer (
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.debug('Warmup for %s failed with: %s', url, e)
|
logger.debug('Warmup for %s failed with: %s', url, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't warmup CSS file dependencies as they have already all been loaded to produce result
|
||||||
|
if (isCSSRequest(url)) { return }
|
||||||
|
|
||||||
const mod = await server.moduleGraph.getModuleByUrl(url, isServer)
|
const mod = await server.moduleGraph.getModuleByUrl(url, isServer)
|
||||||
const deps = mod?.ssrTransformResult?.deps /* server */ || Array.from(mod?.importedModules /* client */ || []).map(m => m.url)
|
const deps = mod?.ssrTransformResult?.deps /* server */ || Array.from(mod?.importedModules /* client */ || []).map(m => m.url)
|
||||||
await Promise.all(deps.map(m => warmup(m.replace('/@id/__x00__', '\0'))))
|
await Promise.all(deps.map(m => warmup(m)))
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(entries.map(entry => warmup(entry)))
|
await Promise.all(entries.map(entry => warmup(fileToUrl(entry, server.config.root))))
|
||||||
}
|
}
|
||||||
|
@ -192,13 +192,9 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (
|
if (nuxt.options.vite.warmupEntry !== false) {
|
||||||
nuxt.options.vite.warmupEntry !== false &&
|
|
||||||
// https://github.com/nuxt/nuxt/issues/14898
|
|
||||||
!(env.isServer && ctx.nuxt.options.vite.devBundler !== 'legacy')
|
|
||||||
) {
|
|
||||||
const start = Date.now()
|
const start = Date.now()
|
||||||
warmupViteServer(server, [join('/@fs/', ctx.entry)], env.isServer)
|
warmupViteServer(server, [ctx.entry], env.isServer)
|
||||||
.then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`))
|
.then(() => logger.info(`Vite ${env.isClient ? 'client' : 'server'} warmed up in ${Date.now() - start}ms`))
|
||||||
.catch(logger.error)
|
.catch(logger.error)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user