Skip to content

Commit 21f2bf9

Browse files
perf(vite): add plugin hook filter (#19308)
## Summary This PR adds plugin [hook filters](https://vite.dev/guide/api-plugin#hook-filters) to the Vite plugin. They are backwards-compatible, having no impact on older Vite versions as the check for `isPotentialCssRootFile` is still included. It can be dropped once you don't need to support older Vite versions. ## See also e18e/ecosystem-issues#171 --------- Co-authored-by: Jordan Pittman <jordan@cryptica.me>
1 parent 4455051 commit 21f2bf9

File tree

1 file changed

+49
-34
lines changed

1 file changed

+49
-34
lines changed

packages/@tailwindcss-vite/src/index.ts

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,30 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
9797
name: '@tailwindcss/vite:generate:serve',
9898
apply: 'serve',
9999
enforce: 'pre',
100+
transform: {
101+
filter: {
102+
id: {
103+
exclude: [/\/\.vite\//, SPECIAL_QUERY_RE, COMMON_JS_PROXY_RE],
104+
include: [/\.css(?:\?.*)?$/, /&lang\.css/, INLINE_STYLE_ID_RE],
105+
},
106+
},
107+
async handler(src, id) {
108+
if (!isPotentialCssRootFile(id)) return
100109

101-
async transform(src, id, options) {
102-
if (!isPotentialCssRootFile(id)) return
103-
104-
using I = new Instrumentation()
105-
DEBUG && I.start('[@tailwindcss/vite] Generate CSS (serve)')
110+
using I = new Instrumentation()
111+
DEBUG && I.start('[@tailwindcss/vite] Generate CSS (serve)')
106112

107-
let root = roots.get(id)
113+
let root = roots.get(id)
108114

109-
let result = await root.generate(src, (file) => this.addWatchFile(file), I)
110-
if (!result) {
111-
roots.delete(id)
112-
return src
113-
}
115+
let result = await root.generate(src, (file) => this.addWatchFile(file), I)
116+
if (!result) {
117+
roots.delete(id)
118+
return src
119+
}
114120

115-
DEBUG && I.end('[@tailwindcss/vite] Generate CSS (serve)')
116-
return result
121+
DEBUG && I.end('[@tailwindcss/vite] Generate CSS (serve)')
122+
return result
123+
},
117124
},
118125
},
119126

@@ -123,31 +130,39 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
123130
apply: 'build',
124131
enforce: 'pre',
125132

126-
async transform(src, id) {
127-
if (!isPotentialCssRootFile(id)) return
133+
transform: {
134+
filter: {
135+
id: {
136+
exclude: [/\/\.vite\//, SPECIAL_QUERY_RE, COMMON_JS_PROXY_RE],
137+
include: [/\.css(?:\?.*)?$/, /&lang\.css/, INLINE_STYLE_ID_RE],
138+
},
139+
},
140+
async handler(src, id) {
141+
if (!isPotentialCssRootFile(id)) return
128142

129-
using I = new Instrumentation()
130-
DEBUG && I.start('[@tailwindcss/vite] Generate CSS (build)')
143+
using I = new Instrumentation()
144+
DEBUG && I.start('[@tailwindcss/vite] Generate CSS (build)')
131145

132-
let root = roots.get(id)
146+
let root = roots.get(id)
133147

134-
let result = await root.generate(src, (file) => this.addWatchFile(file), I)
135-
if (!result) {
136-
roots.delete(id)
137-
return src
138-
}
139-
DEBUG && I.end('[@tailwindcss/vite] Generate CSS (build)')
140-
141-
if (shouldOptimize) {
142-
DEBUG && I.start('[@tailwindcss/vite] Optimize CSS')
143-
result = optimize(result.code, {
144-
minify,
145-
map: result.map,
146-
})
147-
DEBUG && I.end('[@tailwindcss/vite] Optimize CSS')
148-
}
148+
let result = await root.generate(src, (file) => this.addWatchFile(file), I)
149+
if (!result) {
150+
roots.delete(id)
151+
return src
152+
}
153+
DEBUG && I.end('[@tailwindcss/vite] Generate CSS (build)')
154+
155+
if (shouldOptimize) {
156+
DEBUG && I.start('[@tailwindcss/vite] Optimize CSS')
157+
result = optimize(result.code, {
158+
minify,
159+
map: result.map,
160+
})
161+
DEBUG && I.end('[@tailwindcss/vite] Optimize CSS')
162+
}
149163

150-
return result
164+
return result
165+
},
151166
},
152167
},
153168
] satisfies Plugin[]

0 commit comments

Comments
 (0)