Skip to content

Commit 602b373

Browse files
authored
fix(css): less @plugin imports of JS files treated as CSS and rebased (fix #19268) (#19269)
1 parent 9fc31b6 commit 602b373

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,7 @@ const makeLessWorker = (
27762776
filename: string,
27772777
dir: string,
27782778
rootFile: string,
2779+
mime: string | undefined,
27792780
) => {
27802781
const resolved = await resolvers.less(
27812782
environment,
@@ -2784,6 +2785,12 @@ const makeLessWorker = (
27842785
)
27852786
if (!resolved) return undefined
27862787

2788+
// don't rebase URLs in JavaScript plugins
2789+
if (mime === 'application/javascript') {
2790+
const file = path.resolve(resolved) // ensure os-specific flashes
2791+
return { resolved: file }
2792+
}
2793+
27872794
const result = await rebaseUrls(
27882795
environment,
27892796
resolved,
@@ -2829,7 +2836,12 @@ const makeLessWorker = (
28292836
opts: any,
28302837
env: any,
28312838
): Promise<Less.FileLoadResult> {
2832-
const result = await viteLessResolve(filename, dir, this.rootFile)
2839+
const result = await viteLessResolve(
2840+
filename,
2841+
dir,
2842+
this.rootFile,
2843+
opts.mime,
2844+
)
28332845
if (result) {
28342846
return {
28352847
filename: path.resolve(result.resolved),

playground/css/__tests__/css.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ test('less', async () => {
108108
await untilUpdated(() => getColor(atImport), 'blue')
109109
})
110110

111+
test('less-plugin', async () => {
112+
const body = await page.$('body')
113+
expect(await getBg(body)).toBe(
114+
'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wIAAgEBAFPIhPsAAAAASUVORK5CYII=")',
115+
)
116+
})
117+
111118
test('stylus', async () => {
112119
const imported = await page.$('.stylus')
113120
const additionalData = await page.$('.stylus-additional-data')

playground/css/less-plugin.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@plugin "less-plugin/test.js";
2+
3+
body {
4+
background-image: test();
5+
}

playground/css/less-plugin/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
functions.add('test', function test() {
2+
const transparentPng =
3+
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wIAAgEBAFPIhPsAAAAASUVORK5CYII='
4+
return `url(${transparentPng})`
5+
})

playground/css/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './imported.css'
33
import './sugarss.sss'
44
import './sass.scss'
55
import './less.less'
6+
import './less-plugin.less'
67
import './stylus.styl'
78
import './manual-chunk.css'
89

0 commit comments

Comments
 (0)