Skip to content

feat: add ignore option to tailwind plugin #17482

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/@tailwindcss-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ const SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/
const COMMON_JS_PROXY_RE = /\?commonjs-proxy/
const INLINE_STYLE_ID_RE = /[?&]index\=\d+\.css$/

export default function tailwindcss(): Plugin[] {
export interface TailwindPluginOptions {
ignore?: (string | RegExp | ((id: string) => boolean))[]
}

export default function tailwindcss(pluginOptions?: TailwindPluginOptions): Plugin[] {
let servers: ViteDevServer[] = []
let config: ResolvedConfig | null = null

let ignoredFiles = pluginOptions?.ignore??[];
let isSSR = false
let minify = false

Expand Down Expand Up @@ -61,7 +65,7 @@ export default function tailwindcss(): Plugin[] {
enforce: 'pre',

async transform(src, id, options) {
if (!isPotentialCssRootFile(id)) return
if (!isPotentialCssRootFile(id, ignoredFiles)) return

using I = new Instrumentation()
DEBUG && I.start('[@tailwindcss/vite] Generate CSS (serve)')
Expand All @@ -86,7 +90,7 @@ export default function tailwindcss(): Plugin[] {
enforce: 'pre',

async transform(src, id) {
if (!isPotentialCssRootFile(id)) return
if (!isPotentialCssRootFile(id, ignoredFiles)) return

using I = new Instrumentation()
DEBUG && I.start('[@tailwindcss/vite] Generate CSS (build)')
Expand Down Expand Up @@ -115,8 +119,21 @@ function getExtension(id: string) {
return path.extname(filename).slice(1)
}

function isPotentialCssRootFile(id: string) {
function isPotentialCssRootFile(id: string, ignoredFiles: Exclude<TailwindPluginOptions["ignore"], undefined>) {
if (id.includes('/.vite/')) return
let isIgnoredPattern = ignoredFiles.some(matcher => {
if(matcher instanceof RegExp){
return matcher.test(id);
}
if(matcher instanceof Function){
return matcher(id);
}

return matcher.includes(matcher)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what to do in this case if it's a string, or if you want to support this use-case at all, maybe it would make sense to offload it to the user with the matcher function above

});
// User defined ignore pattern
if(isIgnoredPattern) return

let extension = getExtension(id)
let isCssFile =
(extension === 'css' || id.includes('&lang.css') || id.match(INLINE_STYLE_ID_RE)) &&
Expand Down