Skip to content

Commit

Permalink
fix(css): less @plugin imports of JS files treated as CSS and rebas…
Browse files Browse the repository at this point in the history
…ed (fix #19268) (#19269)
  • Loading branch information
karlvr authored Feb 4, 2025
1 parent 9fc31b6 commit 602b373
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
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 @@ -2776,6 +2776,7 @@ const makeLessWorker = (
filename: string,
dir: string,
rootFile: string,
mime: string | undefined,
) => {
const resolved = await resolvers.less(
environment,
Expand All @@ -2784,6 +2785,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 @@ -2829,7 +2836,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

0 comments on commit 602b373

Please sign in to comment.