fix(vite): suppress warning about unresolved public assets (#27328)

This commit is contained in:
Daniel Roe 2024-05-23 16:48:27 +01:00 committed by GitHub
parent 47c47c9b42
commit 5f60aa5a48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 12 deletions

View File

@ -6,19 +6,10 @@ import { dirname, relative } from 'pathe'
import MagicString from 'magic-string' import MagicString from 'magic-string'
const PREFIX = 'virtual:public?' const PREFIX = 'virtual:public?'
const CSS_URL_RE = /url\((\/[^)]+)\)/g
export const VitePublicDirsPlugin = createUnplugin((options: { sourcemap?: boolean }) => { export const VitePublicDirsPlugin = createUnplugin((options: { sourcemap?: boolean }) => {
const nitro = useNitro() const { resolveFromPublicAssets } = useResolveFromPublicAssets()
function resolveFromPublicAssets (id: string) {
for (const dir of nitro.options.publicAssets) {
if (!id.startsWith(withTrailingSlash(dir.baseURL || '/'))) { continue }
const path = id.replace(/[?#].*$/, '').replace(withTrailingSlash(dir.baseURL || '/'), withTrailingSlash(dir.dir))
if (existsSync(path)) {
return id
}
}
}
return { return {
name: 'nuxt:vite-public-dir-resolution', name: 'nuxt:vite-public-dir-resolution',
@ -83,4 +74,18 @@ export const VitePublicDirsPlugin = createUnplugin((options: { sourcemap?: boole
} }
}) })
const CSS_URL_RE = /url\((\/[^)]+)\)/g export function useResolveFromPublicAssets () {
const nitro = useNitro()
function resolveFromPublicAssets (id: string) {
for (const dir of nitro.options.publicAssets) {
if (!id.startsWith(withTrailingSlash(dir.baseURL || '/'))) { continue }
const path = id.replace(/[?#].*$/, '').replace(withTrailingSlash(dir.baseURL || '/'), withTrailingSlash(dir.dir))
if (existsSync(path)) {
return id
}
}
}
return { resolveFromPublicAssets }
}

View File

@ -3,6 +3,7 @@ import { logger } from '@nuxt/kit'
import { hasTTY, isCI } from 'std-env' import { hasTTY, isCI } from 'std-env'
import clear from 'clear' import clear from 'clear'
import type { NuxtOptions } from '@nuxt/schema' import type { NuxtOptions } from '@nuxt/schema'
import { useResolveFromPublicAssets } from '../plugins/public-dirs'
let duplicateCount = 0 let duplicateCount = 0
let lastType: vite.LogType | null = null let lastType: vite.LogType | null = null
@ -26,11 +27,18 @@ export function createViteLogger (config: vite.InlineConfig): vite.Logger {
const canClearScreen = hasTTY && !isCI && config.clearScreen const canClearScreen = hasTTY && !isCI && config.clearScreen
const clearScreen = canClearScreen ? clear : () => {} const clearScreen = canClearScreen ? clear : () => {}
const { resolveFromPublicAssets } = useResolveFromPublicAssets()
function output (type: vite.LogType, msg: string, options: vite.LogErrorOptions = {}) { function output (type: vite.LogType, msg: string, options: vite.LogErrorOptions = {}) {
if (typeof msg === 'string' && !process.env.DEBUG) { if (typeof msg === 'string' && !process.env.DEBUG) {
// TODO: resolve upstream in Vite // TODO: resolve upstream in Vite
// Hide sourcemap warnings related to node_modules // Hide sourcemap warnings related to node_modules
if (msg.startsWith('Sourcemap') && msg.includes('node_modules')) { return } if (msg.startsWith('Sourcemap') && msg.includes('node_modules')) { return }
// Hide warnings about externals produced by https://github.com/vitejs/vite/blob/v5.2.11/packages/vite/src/node/plugins/css.ts#L350-L355
if (msg.includes('didn\'t resolve at build time, it will remain unchanged to be resolved at runtime')) {
const id = msg.trim().match(/^([^ ]+) referenced in/m)?.[1]
if (id && resolveFromPublicAssets(id)) { return }
}
} }
const sameAsLast = lastType === type && lastMsg === msg const sameAsLast = lastType === type && lastMsg === msg