mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 00:52:01 +00:00
fix(vite): suppress warning about unresolved public assets (#27328)
This commit is contained in:
parent
47c47c9b42
commit
5f60aa5a48
@ -6,19 +6,10 @@ import { dirname, relative } from 'pathe'
|
||||
import MagicString from 'magic-string'
|
||||
|
||||
const PREFIX = 'virtual:public?'
|
||||
const CSS_URL_RE = /url\((\/[^)]+)\)/g
|
||||
|
||||
export const VitePublicDirsPlugin = createUnplugin((options: { sourcemap?: boolean }) => {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
const { resolveFromPublicAssets } = useResolveFromPublicAssets()
|
||||
|
||||
return {
|
||||
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 }
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import { logger } from '@nuxt/kit'
|
||||
import { hasTTY, isCI } from 'std-env'
|
||||
import clear from 'clear'
|
||||
import type { NuxtOptions } from '@nuxt/schema'
|
||||
import { useResolveFromPublicAssets } from '../plugins/public-dirs'
|
||||
|
||||
let duplicateCount = 0
|
||||
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 clearScreen = canClearScreen ? clear : () => {}
|
||||
|
||||
const { resolveFromPublicAssets } = useResolveFromPublicAssets()
|
||||
|
||||
function output (type: vite.LogType, msg: string, options: vite.LogErrorOptions = {}) {
|
||||
if (typeof msg === 'string' && !process.env.DEBUG) {
|
||||
// TODO: resolve upstream in Vite
|
||||
// Hide sourcemap warnings related to node_modules
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user