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 committed by GitHub
parent 5ac6aa574a
commit 0f684970e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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 { Manifest } from 'vue-bundle-renderer'
import type { ViteBuildContext } from './vite' 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 // Write client manifest for use in vue-bundle-renderer
const clientDist = resolve(ctx.nuxt.options.buildDir, 'dist/client') const clientDist = resolve(ctx.nuxt.options.buildDir, 'dist/client')
const serverDist = resolve(ctx.nuxt.options.buildDir, 'dist/server') const serverDist = resolve(ctx.nuxt.options.buildDir, 'dist/server')
@ -17,7 +17,7 @@ export async function writeManifest (ctx: ViteBuildContext, css: string[] = [])
'@vite/client': { '@vite/client': {
isEntry: true, isEntry: true,
file: '@vite/client', file: '@vite/client',
css, css: [],
module: true, module: true,
resourceType: 'script', resourceType: 'script',
}, },

View File

@ -6,6 +6,7 @@ import { isAbsolute, normalize, resolve } from 'pathe'
import { addDevServerHandler } from '@nuxt/kit' import { addDevServerHandler } from '@nuxt/kit'
import { isFileServingAllowed } from 'vite' import { isFileServingAllowed } from 'vite'
import type { ModuleNode, Plugin as VitePlugin } from 'vite' import type { ModuleNode, Plugin as VitePlugin } from 'vite'
import { getQuery } from 'ufo'
import { normalizeViteManifest } from 'vue-bundle-renderer' import { normalizeViteManifest } from 'vue-bundle-renderer'
import { resolve as resolveModule } from 'mlly' import { resolve as resolveModule } from 'mlly'
import { distDir } from './dirs' import { distDir } from './dirs'
@ -78,13 +79,23 @@ export function registerViteNodeMiddleware (ctx: ViteBuildContext) {
} }
function getManifest (ctx: ViteBuildContext) { function getManifest (ctx: ViteBuildContext) {
const css = Array.from(ctx.ssrServer!.moduleGraph.urlToModuleMap.keys()) const css = new Set<string>()
.filter(i => isCSS(i)) 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({ const manifest = normalizeViteManifest({
'@vite/client': { '@vite/client': {
file: '@vite/client', file: '@vite/client',
css, css: [...css],
module: true, module: true,
isEntry: true, isEntry: true,
}, },