From eb08f605ddadef99a5d68f55de143e3e47c91618 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 26 Sep 2024 17:22:38 +0900 Subject: [PATCH] fix(css): fix lightningcss dep url resolution with custom root (#18125) --- packages/vite/src/node/plugins/css.ts | 5 ++++- .../__tests__/css-lightningcss-root.spec.ts | 9 +++++++++ playground/css-lightningcss-root/package.json | 14 ++++++++++++++ playground/css-lightningcss-root/root/index.html | 3 +++ playground/css-lightningcss-root/root/main.js | 1 + playground/css-lightningcss-root/root/ok.png | Bin 0 -> 4986 bytes .../css-lightningcss-root/root/url-dep.css | 7 +++++++ playground/css-lightningcss-root/vite.config.js | 8 ++++++++ pnpm-lock.yaml | 6 ++++++ 9 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 playground/css-lightningcss-root/__tests__/css-lightningcss-root.spec.ts create mode 100644 playground/css-lightningcss-root/package.json create mode 100644 playground/css-lightningcss-root/root/index.html create mode 100644 playground/css-lightningcss-root/root/main.js create mode 100644 playground/css-lightningcss-root/root/ok.png create mode 100644 playground/css-lightningcss-root/root/url-dep.css create mode 100644 playground/css-lightningcss-root/vite.config.js diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 6fa5904dd..faa5f10e8 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -3095,7 +3095,10 @@ async function compileLightningCSS( } deps.add(dep.url) if (urlReplacer) { - const replaceUrl = await urlReplacer(dep.url, dep.loc.filePath) + const replaceUrl = await urlReplacer( + dep.url, + toAbsolute(dep.loc.filePath), + ) css = css.replace(dep.placeholder, () => replaceUrl) } else { css = css.replace(dep.placeholder, () => dep.url) diff --git a/playground/css-lightningcss-root/__tests__/css-lightningcss-root.spec.ts b/playground/css-lightningcss-root/__tests__/css-lightningcss-root.spec.ts new file mode 100644 index 000000000..786d17f12 --- /dev/null +++ b/playground/css-lightningcss-root/__tests__/css-lightningcss-root.spec.ts @@ -0,0 +1,9 @@ +import { expect, test } from 'vitest' +import { getBg, isBuild, page, viteTestUrl } from '~utils' + +test('url dependency', async () => { + const css = await page.$('.url-dep') + expect(await getBg(css)).toMatch( + isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`, + ) +}) diff --git a/playground/css-lightningcss-root/package.json b/playground/css-lightningcss-root/package.json new file mode 100644 index 000000000..e1669e89b --- /dev/null +++ b/playground/css-lightningcss-root/package.json @@ -0,0 +1,14 @@ +{ + "name": "@vitejs/test-css-lightningcss-root", + "private": true, + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "debug": "node --inspect-brk ../../packages/vite/bin/vite", + "preview": "vite preview" + }, + "devDependencies": { + "lightningcss": "^1.27.0" + } +} diff --git a/playground/css-lightningcss-root/root/index.html b/playground/css-lightningcss-root/root/index.html new file mode 100644 index 000000000..df2820bbe --- /dev/null +++ b/playground/css-lightningcss-root/root/index.html @@ -0,0 +1,3 @@ +

url() dependency

+
+ diff --git a/playground/css-lightningcss-root/root/main.js b/playground/css-lightningcss-root/root/main.js new file mode 100644 index 000000000..fe93503f5 --- /dev/null +++ b/playground/css-lightningcss-root/root/main.js @@ -0,0 +1 @@ +import './url-dep.css' diff --git a/playground/css-lightningcss-root/root/ok.png b/playground/css-lightningcss-root/root/ok.png new file mode 100644 index 0000000000000000000000000000000000000000..a8d1e52510c41ceeaf3515ee2f594a82557d1685 GIT binary patch literal 4986 zcmZ`*2RK|?*B+e_JxHPrqC^`dI%D+CL?i|g27@8m=!PJK=%R!nQKA!~B)aHPqD6@= zdX1Xs?IXGWz4!UQ|3BwBXT4{y^{#jAwa>H935DsXk^yf40RRA*x|)*SrIo+D1c?bR z?>eFMtN;LT2BoM7Q&&_3!O)IYs3%AOKrJ*Okx2i(;Vu6y%xWZ*nhnJ9i&KruKfWK2 zyFN@q4VQ$YH7MOol~C15pOUIc*gumE7!~CoK~QR@T#8_ck(W0lA{Eo$$I(o}%GP4` z{gywSytz0#03S?ZdM^O!LEBja*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^}uFE%vn zHD_V5NyylkAvO?1gV65Lotd3|o-0Q|;j#VmXF@($H!mlL&@?AELqn+i`TL@xmI`4k z2lzop7xdiL_pI+m;uEW9&v0kSmIUODjY&VJ%7yIjlXD9Q1XNVG53!@(q@}g2*ko!o zJJst~!}YJJtDC|^%7VTPf8&#A);uj*sJ~6zDruM_EL;v^XAgJbL)XSTu`zeBBq%?U z2N_mgK)ENYf0DW>E=AYXF@hEg_+pL8V?hgS>|&g^9L_!QaPdw#%(0FvX|hg51=O(+ zT}uN08)f#XS#M%#R#yi*tImpx3PaW|qQ(YlzSz5T;iHzjk)*PuF-%h(9o+mRG&D3N zC4$SYoje;`%9V)(&Ed=+%H~~oh>%2Q4M+3xX6M9L8{i|vF824%=R&;w2fP57lKV)S b37?V{t|cgkE9S{uec08Nb(D$~%%1%R{RRHc literal 0 HcmV?d00001 diff --git a/playground/css-lightningcss-root/root/url-dep.css b/playground/css-lightningcss-root/root/url-dep.css new file mode 100644 index 000000000..f56470afc --- /dev/null +++ b/playground/css-lightningcss-root/root/url-dep.css @@ -0,0 +1,7 @@ +.url-dep { + background-image: url('./ok.png'); + background-size: cover; + width: 50px; + height: 50px; + border: 1px solid black; +} diff --git a/playground/css-lightningcss-root/vite.config.js b/playground/css-lightningcss-root/vite.config.js new file mode 100644 index 000000000..6e42d3e20 --- /dev/null +++ b/playground/css-lightningcss-root/vite.config.js @@ -0,0 +1,8 @@ +import { defineConfig } from 'vite' + +export default defineConfig({ + root: 'root', + css: { + transformer: 'lightningcss', + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 35d6ea300..00995545b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -598,6 +598,12 @@ importers: specifier: ^1.27.0 version: 1.27.0 + playground/css-lightningcss-root: + devDependencies: + lightningcss: + specifier: ^1.27.0 + version: 1.27.0 + playground/css-no-codesplit: {} playground/css-sourcemap: