Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css): warn if url rewrite has no importer #8183

Merged
merged 1 commit into from May 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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