From 151099628310d27c0b526b31ba74b8b258fb59b5 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Sun, 16 Apr 2023 02:24:59 +0800 Subject: [PATCH] fix(build): do not warn when URL in CSS is externalized (#12873) --- packages/vite/src/node/build.ts | 4 ++-- packages/vite/src/node/plugins/css.ts | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 5b098377764b27..26f80c9b37a72c 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -947,12 +947,12 @@ async function cjsSsrResolveExternal( } } -function resolveUserExternal( +export function resolveUserExternal( user: ExternalOption, id: string, parentId: string | undefined, isResolved: boolean, -) { +): boolean | null | void { if (typeof user === 'function') { return user(id, parentId, isResolved) } else if (Array.isArray(user)) { diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index dd49feb4c744d0..7366c1b093799d 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -26,7 +26,7 @@ import type { RawSourceMap } from '@ampproject/remapping' import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap' import type { ModuleNode } from '../server/moduleGraph' import type { ResolveFn, ViteDevServer } from '../' -import { toOutputFilePathInCss } from '../build' +import { resolveUserExternal, toOutputFilePathInCss } from '../build' import { CLIENT_PUBLIC_PATH, CSS_LANGS_RE, @@ -230,10 +230,21 @@ export function cssPlugin(config: ResolvedConfig): Plugin { return fileToUrl(resolved, config, this) } if (config.command === 'build') { - // #9800 If we cannot resolve the css url, leave a warning. - config.logger.warnOnce( - `\n${url} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`, - ) + const isExternal = config.build.rollupOptions.external + ? resolveUserExternal( + config.build.rollupOptions.external, + url, // use URL as id since id could not be resolved + id, + false, + ) + : false + + if (!isExternal) { + // #9800 If we cannot resolve the css url, leave a warning. + config.logger.warnOnce( + `\n${url} referenced in ${id} didn't resolve at build time, it will remain unchanged to be resolved at runtime`, + ) + } } return url }