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 Nov 23, 2021
1 parent faae331 commit cd5f7e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
16 changes: 16 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Expand Up @@ -356,3 +356,19 @@ test('minify css', async () => {
expect(cssFile).toMatch('rgba(')
expect(cssFile).not.toMatch('#ffff00b3')
})


test('?raw', async () => {;
expect(await page.textContent('.raw-imported-css')).toBe(
require('fs').readFileSync(require.resolve('../imported.css'), 'utf-8')
)
expect(await page.textContent('.raw-imported-sass')).toBe(
require('fs').readFileSync(require.resolve('../sass.scss'), 'utf-8')
)
expect(await page.textContent('.raw-imported-less')).toBe(
require('fs').readFileSync(require.resolve('../less.less'), 'utf-8')
)
expect(await page.textContent('.raw-imported-stylus')).toBe(
require('fs').readFileSync(require.resolve('../stylus.styl'), 'utf-8')
)
});
6 changes: 6 additions & 0 deletions packages/playground/css/index.html
Expand Up @@ -104,6 +104,12 @@ <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>
<pre class="raw-imported-sass"></pre>
<pre class="raw-imported-less"></pre>
<pre class="raw-imported-stylus"></pre>
</div>

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

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

import rawSass from './sass.scss?raw'
text('.raw-imported-sass', rawSass)

import rawLess from './less.less?raw'
text('.raw-imported-less', rawLess)

import rawStylus from './stylus.styl?raw'
text('.raw-imported-stylus', rawStylus)

import mod from './mod.module.css'
document.querySelector('.modules').classList.add(mod['apply-color'])
text('.modules-code', JSON.stringify(mod, null, 2))
Expand Down
6 changes: 3 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,7 @@ 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 +275,7 @@ 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 cd5f7e1

Please sign in to comment.