Skip to content

Commit

Permalink
feat: support importing css with ?raw
Browse files Browse the repository at this point in the history
  • Loading branch information
stygian-desolator committed Dec 26, 2021
1 parent a96bdd9 commit eb1d12c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/playground/assets/__tests__/assets.spec.ts
Expand Up @@ -183,6 +183,16 @@ test('?url import', async () => {
)
})

test('?url import on css', async () => {
const src = readFile('css/icons.css')
const txt = await page.textContent('.url-css')
expect(txt).toEqual(
isBuild
? `data:text/css;base64,${Buffer.from(src).toString('base64')}`
: '/foo/css/icons.css'
)
})

describe('unicode url', () => {
test('from js import', async () => {
const src = readFile('テスト-測試-white space.js')
Expand Down
6 changes: 6 additions & 0 deletions packages/playground/assets/index.html
Expand Up @@ -133,6 +133,9 @@ <h2>?raw import</h2>
<h2>?url import</h2>
<code class="url"></code>

<h2>?url import with css</h2>
<code class="url-css"></code>

<h2>new URL('...', import.meta.url)</h2>
<img class="import-meta-url-img" />
<code class="import-meta-url"></code>
Expand Down Expand Up @@ -176,6 +179,9 @@ <h2>new URL(`./${dynamic}`, import.meta.url)</h2>
import unicodeUrl from './テスト-測試-white space.js?url'
text('.unicode-url', unicodeUrl)

import cssUrl from './css/icons.css?url'
text('.url-css', cssUrl)

// const url = new URL('non_existent_file.png', import.meta.url)
const metaUrl = new URL('./nested/asset.png', import.meta.url)
text('.import-meta-url', metaUrl)
Expand Down
8 changes: 8 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Expand Up @@ -356,3 +356,11 @@ test('minify css', async () => {
expect(cssFile).toMatch('rgba(')
expect(cssFile).not.toMatch('#ffff00b3')
})

test('?raw', async () => {
const rawImportCss = await page.$('.raw-imported-css')

expect(await rawImportCss.textContent()).toBe(
require('fs').readFileSync(require.resolve('../raw-imported.css'), 'utf-8')
)
})
3 changes: 3 additions & 0 deletions packages/playground/css/index.html
Expand Up @@ -104,6 +104,9 @@ <h1>CSS</h1>

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

<p>Raw Support</p>
<pre class="raw-imported-css"></pre>
</div>

<script type="module" src="./main.js"></script>
3 changes: 3 additions & 0 deletions packages/playground/css/main.js
Expand Up @@ -12,6 +12,9 @@ text('.imported-less', less)
import stylus from './stylus.styl'
text('.imported-stylus', stylus)

import rawCss from './raw-imported.css?raw'
text('.raw-imported-css', rawCss)

import mod from './mod.module.css'
document.querySelector('.modules').classList.add(mod['apply-color'])
text('.modules-code', JSON.stringify(mod, null, 2))
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/raw-imported.css
@@ -0,0 +1,3 @@
.raw-imported {
color: yellow;
}
14 changes: 11 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -24,7 +24,7 @@ import type {
} 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 type { 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 @@ -275,7 +279,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
},

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

Expand Down

0 comments on commit eb1d12c

Please sign in to comment.