Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
css
config option, don't process CSS by default (#1467)
* feat: add processCss config option, don't process CSS by default * refactor: css option is aligned with other simmilar options * refactor: change is css regexp * refactor: cleanup * Update docs/config/index.md Co-authored-by: Bartosz Gościński <bargosc@gmail.com> * refactor: reverse filters in css Co-authored-by: Bartosz Gościński <bargosc@gmail.com>
- Loading branch information
1 parent
805c0ba
commit 68715b6
Showing
6 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { Plugin as VitePlugin } from 'vite' | ||
import { toArray } from '../../utils' | ||
import type { Vitest } from '../core' | ||
|
||
const cssLangs = '\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)' | ||
const cssLangRE = new RegExp(cssLangs) | ||
|
||
const isCSS = (id: string) => { | ||
return cssLangRE.test(id) | ||
} | ||
|
||
export function CSSEnablerPlugin(ctx: Vitest): VitePlugin { | ||
const shouldProcessCSS = (id: string) => { | ||
const { css } = ctx.config | ||
if (typeof css === 'boolean') | ||
return css | ||
if (toArray(css.exclude).some(re => re.test(id))) | ||
return false | ||
if (toArray(css.include).some(re => re.test(id))) | ||
return true | ||
return false | ||
} | ||
|
||
return { | ||
name: 'vitest:css-enabler', | ||
enforce: 'pre', | ||
transform(code, id) { | ||
if (!isCSS(id)) | ||
return | ||
if (!shouldProcessCSS(id)) | ||
return '' | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters