MCPcopy
hub / github.com/vitejs/vite / UrlRewritePostcssPlugin

Function UrlRewritePostcssPlugin

packages/vite/src/node/plugins/css.ts:2028–2084  ·  view source on GitHub ↗
(opts)

Source from the content-addressed store, hash-verified

2026 deps: Set<string>
2027 logger: Logger
2028}> = (opts) => {
2029 if (!opts) {
2030 throw new Error('base or replace is required')
2031 }
2032
2033 return {
2034 postcssPlugin: 'vite-url-rewrite',
2035 Once(root) {
2036 const promises: Promise<void>[] = []
2037 root.walkDecls((declaration) => {
2038 const importer = declaration.source?.input.file
2039 if (!importer) {
2040 opts.logger.warnOnce(
2041 '\nA PostCSS plugin did not pass the `from` option to `postcss.parse`. ' +
2042 'This may cause imported assets to be incorrectly transformed. ' +
2043 "If you've recently added a PostCSS plugin that raised this warning, " +
2044 'please contact the package author to fix the issue.',
2045 )
2046 }
2047 const isCssUrl = cssUrlRE.test(declaration.value)
2048 const isCssImageSet = cssImageSetRE.test(declaration.value)
2049 if (isCssUrl || isCssImageSet) {
2050 const replacerForDeclaration = async (rawUrl: string) => {
2051 const [newUrl, resolvedId] = await opts.resolver(rawUrl, importer)
2052 if (resolvedId) {
2053 opts.deps.add(resolvedId)
2054 }
2055 return newUrl
2056 }
2057 if (isCssUrl && isCssImageSet) {
2058 promises.push(
2059 rewriteCssUrls(declaration.value, replacerForDeclaration)
2060 .then((url) => rewriteCssImageSet(url, replacerForDeclaration))
2061 .then((url) => {
2062 declaration.value = url
2063 }),
2064 )
2065 } else {
2066 const rewriterToUse = isCssImageSet
2067 ? rewriteCssImageSet
2068 : rewriteCssUrls
2069 promises.push(
2070 rewriterToUse(declaration.value, replacerForDeclaration).then(
2071 (url) => {
2072 declaration.value = url
2073 },
2074 ),
2075 )
2076 }
2077 }
2078 })
2079 if (promises.length) {
2080 return Promise.all(promises) as any
2081 }
2082 },
2083 }
2084}
2085UrlRewritePostcssPlugin.postcss = true

Callers 1

compilePostCSSFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected