Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(css): warn if url rewrite has no importer (#8183)
  • Loading branch information
bluwy committed May 16, 2022
1 parent 102b678 commit 0858450
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -42,6 +42,7 @@ import {
processSrcSet
} from '../utils'
import { emptyCssComments } from '../utils'
import type { Logger } from '../logger'
import { addToHTMLProxyTransformResult } from './html'
import {
assetUrlRE,
Expand Down Expand Up @@ -748,7 +749,8 @@ async function compileCSS(
}
postcssPlugins.push(
UrlRewritePostcssPlugin({
replacer: urlReplacer
replacer: urlReplacer,
logger: config.logger
}) as PostCSS.Plugin
)

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

const UrlRewritePostcssPlugin: PostCSS.PluginCreator<{
replacer: CssUrlReplacer
logger: Logger
}> = (opts) => {
if (!opts) {
throw new Error('base or replace is required')
Expand All @@ -990,11 +993,19 @@ const UrlRewritePostcssPlugin: PostCSS.PluginCreator<{
Once(root) {
const promises: Promise<void>[] = []
root.walkDecls((declaration) => {
const importer = declaration.source?.input.file
if (!importer) {
opts.logger.warnOnce(
'\nA PostCSS plugin did not pass the `from` option to `postcss.parse`. ' +
'This may cause imported assets to be incorrectly transformed. ' +
"If you've recently added a PostCSS plugin that raised this warning, " +
'please contact the package author to fix the issue.'
)
}
const isCssUrl = cssUrlRE.test(declaration.value)
const isCssImageSet = cssImageSetRE.test(declaration.value)
if (isCssUrl || isCssImageSet) {
const replacerForDeclaration = (rawUrl: string) => {
const importer = declaration.source?.input.file
return opts.replacer(rawUrl, importer)
}
const rewriterToUse = isCssImageSet
Expand Down

0 comments on commit 0858450

Please sign in to comment.