From 5f60aa5a481a7a7b8f40fe91ea39df8b8683cc45 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 23 May 2024 16:48:27 +0100 Subject: [PATCH] fix(vite): suppress warning about unresolved public assets (#27328) --- packages/vite/src/plugins/public-dirs.ts | 29 ++++++++++++++---------- packages/vite/src/utils/logger.ts | 8 +++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/plugins/public-dirs.ts b/packages/vite/src/plugins/public-dirs.ts index 29e4a305ea..442b87e6e5 100644 --- a/packages/vite/src/plugins/public-dirs.ts +++ b/packages/vite/src/plugins/public-dirs.ts @@ -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 } +} diff --git a/packages/vite/src/utils/logger.ts b/packages/vite/src/utils/logger.ts index 1de733dfea..25a89551c3 100644 --- a/packages/vite/src/utils/logger.ts +++ b/packages/vite/src/utils/logger.ts @@ -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