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

fix(css): referencing aliased svg asset with lightningcss enabled errored #18819

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3177,7 +3177,6 @@ async function compileLightningCSS(
css = css.replace(dep.placeholder, () => dep.url)
break
}
deps.add(dep.url)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line added non-resolved URLs to deps. But deps should only include file paths.

That URL was passed to fileToDevUrl here.

await fileToDevUrl(
this.environment,
file,
/* skipBase */ true,
),

Then, this fs.readFile was called for svg files.
const content = await fsp.readFile(file)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the postcss transformer, we didn't add url() referenced assets to deps.

postcssPlugins.push(
UrlRewritePostcssPlugin({
replacer: urlReplacer,
logger: environment.logger,
}),

So for now, I think we can simply remove this deps.add. In future, maybe we should add url() referenced files to deps as well.

if (urlReplacer) {
const replaceUrl = await urlReplacer(
dep.url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ test('nested css with relative asset', async () => {
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
})

test('aliased asset', async () => {
const bg = await getBg('.css-url-aliased')
expect(bg).toMatch('data:image/svg+xml,')
})
4 changes: 4 additions & 0 deletions playground/css-lightningcss/css-url.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.css-url-aliased {
background: url('@/fragment.svg');
background-size: 10px;
}
4 changes: 4 additions & 0 deletions playground/css-lightningcss/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ <h1>Lightning CSS</h1>

<p>Assets relative to nested CSS</p>
<div class="nested-css-relative-asset"></div>

<div class="css-url-aliased">
<span style="background: #fff">CSS background (aliased)</span>
</div>
</div>

<script type="module" src="./main.js"></script>
1 change: 1 addition & 0 deletions playground/css-lightningcss/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import './minify.css'
import './imported.css'
import mod from './mod.module.css'
import './external-url.css'
import './css-url.css'

document.querySelector('.modules').classList.add(mod['apply-color'])
text('.modules-code', JSON.stringify(mod, null, 2))
Expand Down
22 changes: 22 additions & 0 deletions playground/css-lightningcss/nested/fragment.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions playground/css-lightningcss/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import path from 'node:path'
import { defineConfig } from 'vite'

export default defineConfig({
css: {
transformer: 'lightningcss',
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'nested'),
},
},
build: {
cssTarget: ['chrome61'],
cssMinify: 'lightningcss',
Expand Down