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: return correct value when import css file with ?url #5229

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/playground/css/__tests__/css.spec.ts
@@ -1,13 +1,11 @@
import fs from 'fs'
import path from 'path'
import {
editFile,
findAssetFile,
getBg,
getColor,
isBuild,
removeFile,
testDir,
readFile,
untilUpdated
} from '../../testUtils'

Expand Down Expand Up @@ -334,7 +332,20 @@ test('Url separation', async () => {
}
})

test('inlined', async () => {
test('special query', async () => {
// should not insert css
expect(await getColor('.inlined')).toBe('black')
expect(await getColor('.url')).toBe('black')
expect(await getColor('.raw')).toBe('black')
expect(await getColor('.inline')).toBe('black')

const source = readFile('special-query.scss')

expect(await page.textContent('.url-code')).toMatch(
isBuild
? `data:null;base64,${Buffer.from(source).toString(
'base64'
)}`
: '/special-query.scss'
)
expect(await page.textContent('.raw-code')).toMatch(source)
})
10 changes: 8 additions & 2 deletions packages/playground/css/index.html
Expand Up @@ -102,8 +102,14 @@ <h1>CSS</h1>
Url separation preservation: should have valid background-image
</p>

<p class="inlined">Inlined import - this should NOT be red.</p>
<pre class="inlined-code"></pre>
<p class="url">?url import - this should NOT be green</p>
<pre class="url-code"></pre>

<p class="raw">?raw import - this should NOT be green</p>
<pre class="raw-code"></pre>

<p class="inline">?inline import - this should NOT be green</p>
<pre class="inline-code"></pre>
</div>

<script type="module" src="./main.js"></script>
3 changes: 0 additions & 3 deletions packages/playground/css/inlined.css

This file was deleted.

14 changes: 11 additions & 3 deletions packages/playground/css/main.js
Expand Up @@ -63,6 +63,14 @@ if (import.meta.env.DEV) {
import('./async-treeshaken')
}

// inlined
import inlined from './inlined.css?inline'
text('.inlined-code', inlined)
// ?url import
import url from './special-query.scss?url'
text('.url-code', url)

// ?raw import
import raw from './special-query.scss?raw'
text('.raw-code', raw)

// ?inline import
import inline from './special-query.scss?inline'
text('.inline-code', inline)
13 changes: 13 additions & 0 deletions packages/playground/css/special-query.scss
@@ -0,0 +1,13 @@
.inline {
color: green;
}

.url {
color: green;
}

.raw {
& {
color: green;
}
}
14 changes: 11 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -24,7 +24,7 @@ import {
} from 'rollup'
import { dataToEsm } from '@rollup/pluginutils'
import chalk from 'chalk'
import { CLIENT_PUBLIC_PATH } from '../constants'
import { CLIENT_PUBLIC_PATH, SPECIAL_QUERY_RE } from '../constants'
import { ResolveFn, ViteDevServer } from '../'
import {
getAssetFilename,
Expand Down Expand Up @@ -161,7 +161,11 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
},

async transform(raw, id) {
if (!isCSSRequest(id) || commonjsProxyRE.test(id)) {
if (
!isCSSRequest(id) ||
commonjsProxyRE.test(id) ||
SPECIAL_QUERY_RE.test(id)
) {
return
}

Expand Down Expand Up @@ -272,7 +276,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
},

async transform(css, id, ssr) {
if (!isCSSRequest(id) || commonjsProxyRE.test(id)) {
if (
!isCSSRequest(id) ||
commonjsProxyRE.test(id) ||
SPECIAL_QUERY_RE.test(id)
) {
return
}

Expand Down