Skip to content

Commit 0858450

Browse files
authored
feat(css): warn if url rewrite has no importer (#8183)
1 parent 102b678 commit 0858450

File tree

1 file changed

+13
-2
lines changed
  • packages/vite/src/node/plugins

1 file changed

+13
-2
lines changed

packages/vite/src/node/plugins/css.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
processSrcSet
4343
} from '../utils'
4444
import { emptyCssComments } from '../utils'
45+
import type { Logger } from '../logger'
4546
import { addToHTMLProxyTransformResult } from './html'
4647
import {
4748
assetUrlRE,
@@ -748,7 +749,8 @@ async function compileCSS(
748749
}
749750
postcssPlugins.push(
750751
UrlRewritePostcssPlugin({
751-
replacer: urlReplacer
752+
replacer: urlReplacer,
753+
logger: config.logger
752754
}) as PostCSS.Plugin
753755
)
754756

@@ -980,6 +982,7 @@ const cssImageSetRE = /(?<=image-set\()((?:[\w\-]+\([^\)]*\)|[^)])*)(?=\))/
980982

981983
const UrlRewritePostcssPlugin: PostCSS.PluginCreator<{
982984
replacer: CssUrlReplacer
985+
logger: Logger
983986
}> = (opts) => {
984987
if (!opts) {
985988
throw new Error('base or replace is required')
@@ -990,11 +993,19 @@ const UrlRewritePostcssPlugin: PostCSS.PluginCreator<{
990993
Once(root) {
991994
const promises: Promise<void>[] = []
992995
root.walkDecls((declaration) => {
996+
const importer = declaration.source?.input.file
997+
if (!importer) {
998+
opts.logger.warnOnce(
999+
'\nA PostCSS plugin did not pass the `from` option to `postcss.parse`. ' +
1000+
'This may cause imported assets to be incorrectly transformed. ' +
1001+
"If you've recently added a PostCSS plugin that raised this warning, " +
1002+
'please contact the package author to fix the issue.'
1003+
)
1004+
}
9931005
const isCssUrl = cssUrlRE.test(declaration.value)
9941006
const isCssImageSet = cssImageSetRE.test(declaration.value)
9951007
if (isCssUrl || isCssImageSet) {
9961008
const replacerForDeclaration = (rawUrl: string) => {
997-
const importer = declaration.source?.input.file
9981009
return opts.replacer(rawUrl, importer)
9991010
}
10001011
const rewriterToUse = isCssImageSet

0 commit comments

Comments
 (0)