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): less @plugin imports of JS files treated as CSS and rebased (fix #19268) #19269

Merged
merged 3 commits into from
Feb 4, 2025
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
14 changes: 13 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2752,6 +2752,7 @@ const makeLessWorker = (
filename: string,
dir: string,
rootFile: string,
mime: string | undefined,
) => {
const resolved = await resolvers.less(
environment,
Expand All @@ -2760,6 +2761,12 @@ const makeLessWorker = (
)
if (!resolved) return undefined

// don't rebase URLs in JavaScript plugins
if (mime === 'application/javascript') {
const file = path.resolve(resolved) // ensure os-specific flashes
return { resolved: file }
}

const result = await rebaseUrls(
environment,
resolved,
Expand Down Expand Up @@ -2805,7 +2812,12 @@ const makeLessWorker = (
opts: any,
env: any,
): Promise<Less.FileLoadResult> {
const result = await viteLessResolve(filename, dir, this.rootFile)
const result = await viteLessResolve(
filename,
dir,
this.rootFile,
opts.mime,
)
if (result) {
return {
filename: path.resolve(result.resolved),
Expand Down
7 changes: 7 additions & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ test('less', async () => {
await untilUpdated(() => getColor(atImport), 'blue')
})

test('less-plugin', async () => {
const body = await page.$('body')
expect(await getBg(body)).toBe(
'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wIAAgEBAFPIhPsAAAAASUVORK5CYII=")',
)
})

test('stylus', async () => {
const imported = await page.$('.stylus')
const additionalData = await page.$('.stylus-additional-data')
Expand Down
5 changes: 5 additions & 0 deletions playground/css/less-plugin.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@plugin "less-plugin/test.js";

body {
background-image: test();
}
5 changes: 5 additions & 0 deletions playground/css/less-plugin/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
functions.add('test', function test() {
const transparentPng =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wIAAgEBAFPIhPsAAAAASUVORK5CYII='
return `url(${transparentPng})`
})
1 change: 1 addition & 0 deletions playground/css/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './imported.css'
import './sugarss.sss'
import './sass.scss'
import './less.less'
import './less-plugin.less'
import './stylus.styl'
import './manual-chunk.css'

Expand Down