From 4c1dc6a74b31b50198dc414634acf6f5578de16e Mon Sep 17 00:00:00 2001 From: wangkai53 Date: Sun, 6 Mar 2022 01:14:21 +0800 Subject: [PATCH] fix(css plugin): fix the problem caused by calling vite-url-rewrite after user-defined plugin The `vite-url-rewrite`plugin uses the input.file property to replace the url and put it in the last execution of postcssPlugins. The problem now is that some postcss plugins that users are currently using may make the input.file property disappear, resulting in vite not being able to handle the url correctly and packaging the result successfully. For example https://github.com/saionjisekai/px2rem-postcss/blob/1edc6763ad2fe8288ac6a8bda482319f006d8bb6/lib/index.js#L15 --- packages/vite/src/node/plugins/css.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 85eec565ebb86a..008d3793948ce7 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -671,6 +671,12 @@ async function compileCSS( const postcssPlugins = postcssConfig && postcssConfig.plugins ? postcssConfig.plugins.slice() : [] + postcssPlugins.unshift( + UrlRewritePostcssPlugin({ + replacer: urlReplacer + }) as Postcss.Plugin + ) + if (needInlineImport) { const isHTMLProxy = htmlProxyRE.test(id) postcssPlugins.unshift( @@ -692,11 +698,6 @@ async function compileCSS( }) ) } - postcssPlugins.push( - UrlRewritePostcssPlugin({ - replacer: urlReplacer - }) as Postcss.Plugin - ) if (isModule) { postcssPlugins.unshift(