@@ -11,7 +11,8 @@ import { clearRequireCache } from '@tailwindcss/node/require-cache'
1111import { Scanner } from '@tailwindcss/oxide'
1212import fs from 'node:fs/promises'
1313import path from 'node:path'
14- import type { Plugin , ResolvedConfig , ViteDevServer } from 'vite'
14+ import type { Environment , Plugin , ResolvedConfig , ViteDevServer } from 'vite'
15+ import * as vite from 'vite'
1516
1617const DEBUG = env . DEBUG
1718const SPECIAL_QUERY_RE = / [ ? & ] (?: w o r k e r | s h a r e d w o r k e r | r a w | u r l ) \b /
@@ -28,28 +29,51 @@ export type PluginOptions = {
2829export default function tailwindcss ( opts : PluginOptions = { } ) : Plugin [ ] {
2930 let servers : ViteDevServer [ ] = [ ]
3031 let config : ResolvedConfig | null = null
32+ let rootsByEnv = new DefaultMap < string , Map < string , Root > > ( ( env : string ) => new Map ( ) )
3133
3234 let isSSR = false
3335 let shouldOptimize = true
3436 let minify = true
3537
36- let roots : DefaultMap < string , Root > = new DefaultMap ( ( id ) => {
37- let cssResolver = config ! . createResolver ( {
38- ...config ! . resolve ,
39- extensions : [ '.css' ] ,
40- mainFields : [ 'style' ] ,
41- conditions : [ 'style' , 'development|production' ] ,
42- tryIndex : false ,
43- preferRelative : true ,
44- } )
45- function customCssResolver ( id : string , base : string ) {
46- return cssResolver ( id , base , true , isSSR )
47- }
38+ function createRoot ( env : Environment | null , id : string ) {
39+ type ResolveFn = ( id : string , base : string ) => Promise < string | false | undefined >
40+
41+ let customCssResolver : ResolveFn
42+ let customJsResolver : ResolveFn
43+
44+ if ( ! env ) {
45+ // Older, pre-environment Vite API
46+ // TODO: Can we drop this??
47+ let cssResolver = config ! . createResolver ( {
48+ ...config ! . resolve ,
49+ extensions : [ '.css' ] ,
50+ mainFields : [ 'style' ] ,
51+ conditions : [ 'style' , 'development|production' ] ,
52+ tryIndex : false ,
53+ preferRelative : true ,
54+ } )
55+
56+ let jsResolver = config ! . createResolver ( config ! . resolve )
57+
58+ customCssResolver = ( id : string , base : string ) => cssResolver ( id , base , true , isSSR )
59+ customJsResolver = ( id : string , base : string ) => jsResolver ( id , base , true , isSSR )
60+ } else {
61+ // Newer Vite versions
62+ let cssResolver = vite . createIdResolver ( env . config , {
63+ ...env . config . resolve ,
64+ extensions : [ '.css' ] ,
65+ mainFields : [ 'style' ] ,
66+ conditions : [ 'style' , 'development|production' ] ,
67+ tryIndex : false ,
68+ preferRelative : true ,
69+ } )
4870
49- let jsResolver = config ! . createResolver ( config ! . resolve )
50- function customJsResolver ( id : string , base : string ) {
51- return jsResolver ( id , base , true , isSSR )
71+ let jsResolver = vite . createIdResolver ( env . config , env . config . resolve )
72+
73+ customCssResolver = ( id : string , base : string ) => cssResolver ( env , id , base , true )
74+ customJsResolver = ( id : string , base : string ) => jsResolver ( env , id , base , true )
5275 }
76+
5377 return new Root (
5478 id ,
5579 config ! . root ,
@@ -59,7 +83,7 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
5983 customCssResolver ,
6084 customJsResolver ,
6185 )
62- } )
86+ }
6387
6488 return [
6589 {
@@ -110,7 +134,12 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
110134 using I = new Instrumentation ( )
111135 DEBUG && I . start ( '[@tailwindcss/vite] Generate CSS (serve)' )
112136
137+ let roots = rootsByEnv . get ( this . environment ?. name ?? 'default' )
113138 let root = roots . get ( id )
139+ if ( ! root ) {
140+ root ??= createRoot ( this . environment ?? null , id )
141+ roots . set ( id , root )
142+ }
114143
115144 let result = await root . generate ( src , ( file ) => this . addWatchFile ( file ) , I )
116145 if ( ! result ) {
@@ -129,7 +158,6 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
129158 name : '@tailwindcss/vite:generate:build' ,
130159 apply : 'build' ,
131160 enforce : 'pre' ,
132-
133161 transform : {
134162 filter : {
135163 id : {
@@ -143,7 +171,12 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
143171 using I = new Instrumentation ( )
144172 DEBUG && I . start ( '[@tailwindcss/vite] Generate CSS (build)' )
145173
174+ let roots = rootsByEnv . get ( this . environment ?. name ?? 'default' )
146175 let root = roots . get ( id )
176+ if ( ! root ) {
177+ root ??= createRoot ( this . environment ?? null , id )
178+ roots . set ( id , root )
179+ }
147180
148181 let result = await root . generate ( src , ( file ) => this . addWatchFile ( file ) , I )
149182 if ( ! result ) {
@@ -174,13 +207,15 @@ function getExtension(id: string) {
174207}
175208
176209function isPotentialCssRootFile ( id : string ) {
177- if ( id . includes ( '/.vite/' ) ) return
210+ if ( id . includes ( '/.vite/' ) ) return false
211+
212+ // Don't intercept special static asset resources
213+ if ( SPECIAL_QUERY_RE . test ( id ) ) return false
214+ if ( COMMON_JS_PROXY_RE . test ( id ) ) return false
215+
178216 let extension = getExtension ( id )
179- let isCssFile =
180- ( extension === 'css' || id . includes ( '&lang.css' ) || id . match ( INLINE_STYLE_ID_RE ) ) &&
181- // Don't intercept special static asset resources
182- ! SPECIAL_QUERY_RE . test ( id ) &&
183- ! COMMON_JS_PROXY_RE . test ( id )
217+ let isCssFile = extension === 'css' || id . includes ( '&lang.css' ) || id . match ( INLINE_STYLE_ID_RE )
218+
184219 return isCssFile
185220}
186221
0 commit comments