fix(vite): omit css ?raw from head when in dev mode (#27940)

This commit is contained in:
Daniel Roe 2024-07-01 10:30:09 +01:00
parent 164b32c223
commit 0cd98e1164
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
2 changed files with 16 additions and 5 deletions

View File

@ -8,7 +8,7 @@ import { normalizeViteManifest } from 'vue-bundle-renderer'
import type { Manifest } from 'vue-bundle-renderer'
import type { ViteBuildContext } from './vite'
export async function writeManifest (ctx: ViteBuildContext, css: string[] = []) {
export async function writeManifest (ctx: ViteBuildContext) {
// Write client manifest for use in vue-bundle-renderer
const clientDist = resolve(ctx.nuxt.options.buildDir, 'dist/client')
const serverDist = resolve(ctx.nuxt.options.buildDir, 'dist/server')
@ -17,7 +17,7 @@ export async function writeManifest (ctx: ViteBuildContext, css: string[] = [])
'@vite/client': {
isEntry: true,
file: '@vite/client',
css,
css: [],
module: true,
resourceType: 'script',
},

View File

@ -6,6 +6,7 @@ import { isAbsolute, normalize, resolve } from 'pathe'
import { addDevServerHandler } from '@nuxt/kit'
import { isFileServingAllowed } from 'vite'
import type { ModuleNode, Plugin as VitePlugin } from 'vite'
import { getQuery } from 'ufo'
import { normalizeViteManifest } from 'vue-bundle-renderer'
import { resolve as resolveModule } from 'mlly'
import { distDir } from './dirs'
@ -78,13 +79,23 @@ export function registerViteNodeMiddleware (ctx: ViteBuildContext) {
}
function getManifest (ctx: ViteBuildContext) {
const css = Array.from(ctx.ssrServer!.moduleGraph.urlToModuleMap.keys())
.filter(i => isCSS(i))
const css = new Set<string>()
for (const key of ctx.ssrServer!.moduleGraph.urlToModuleMap.keys()) {
if (isCSS(key)) {
const query = getQuery(key)
if ('raw' in query) { continue }
const importers = ctx.ssrServer!.moduleGraph.urlToModuleMap.get(key)?.importers
if (importers && [...importers].every(i => i.id && 'raw' in getQuery(i.id))) {
continue
}
css.add(key)
}
}
const manifest = normalizeViteManifest({
'@vite/client': {
file: '@vite/client',
css,
css: [...css],
module: true,
isEntry: true,
},