@@ -7,7 +7,10 @@ const configPath = path.resolve(__dirname, './context-reuse.tailwind.config.js')
7
7
const { css } = require ( './util/run.js' )
8
8
9
9
function run ( input , config = { } , from = null ) {
10
- from = from || path . resolve ( __filename )
10
+ from = [
11
+ `${ path . resolve ( __filename ) } ?test=${ expect . getState ( ) . currentTestName } ` ,
12
+ from
13
+ ] . join ( '&' )
11
14
12
15
return postcss ( tailwind ( config ) ) . process ( input , { from } )
13
16
}
@@ -26,16 +29,14 @@ afterEach(async () => {
26
29
} )
27
30
28
31
it ( 're-uses the context across multiple files with the same config' , async ( ) => {
29
- let from = path . resolve ( __filename )
30
-
31
32
let results = [
32
- await run ( `@tailwind utilities;` , configPath , `${ from } ? id=1` ) ,
33
+ await run ( `@tailwind utilities;` , configPath , `id=1` ) ,
33
34
34
35
// Using @apply directives should still re-use the context
35
36
// They depend on the config but do not the other way around
36
- await run ( `body { @apply bg-blue-400; }` , configPath , `${ from } ? id=2` ) ,
37
- await run ( `body { @apply text-red-400; }` , configPath , `${ from } ? id=3` ) ,
38
- await run ( `body { @apply mb-4; }` , configPath , `${ from } ? id=4` ) ,
37
+ await run ( `body { @apply bg-blue-400; }` , configPath , `id=2` ) ,
38
+ await run ( `body { @apply text-red-400; }` , configPath , `id=3` ) ,
39
+ await run ( `body { @apply mb-4; }` , configPath , `id=4` ) ,
39
40
]
40
41
41
42
let dependencies = results . map ( ( result ) => {
@@ -85,3 +86,78 @@ it('re-uses the context across multiple files with the same config', async () =>
85
86
// And none of this should have resulted in multiple contexts being created
86
87
expect ( sharedState . contextSourcesMap . size ) . toBe ( 1 )
87
88
} )
89
+
90
+ it ( 'updates layers when any CSS containing @tailwind directives changes' , async ( ) => {
91
+ let result
92
+
93
+ // Compile the initial version once
94
+ let input = css `
95
+ @tailwind utilities;
96
+ @layer utilities {
97
+ .custom-utility {
98
+ color : orange;
99
+ }
100
+ }
101
+ `
102
+
103
+ result = await run ( input , configPath , `id=1` )
104
+
105
+ expect ( result . css ) . toMatchFormattedCss ( css `
106
+ .only\:custom-utility : only-child {
107
+ color : orange;
108
+ }
109
+ ` )
110
+
111
+ // Save the file with a change
112
+ input = css `
113
+ @tailwind utilities;
114
+ @layer utilities {
115
+ .custom-utility {
116
+ color : blue;
117
+ }
118
+ }
119
+ `
120
+
121
+ result = await run ( input , configPath , `id=1` )
122
+
123
+ expect ( result . css ) . toMatchFormattedCss ( css `
124
+ .only\:custom-utility : only-child {
125
+ color : blue;
126
+ }
127
+ ` )
128
+ } )
129
+
130
+ it ( 'invalidates the context when any CSS containing @tailwind directives changes' , async ( ) => {
131
+ sharedState . contextInvalidationCount = 0
132
+ sharedState . sourceHashMap . clear ( )
133
+
134
+ // Save the file a handful of times with no changes
135
+ // This builds the context at most once
136
+ for ( let n = 0 ; n < 5 ; n ++ ) {
137
+ await run ( `@tailwind utilities;` , configPath , `id=1` )
138
+ }
139
+
140
+ expect ( sharedState . contextInvalidationCount ) . toBe ( 1 )
141
+
142
+ // Save the file twice with a change
143
+ // This should rebuild the context again but only once
144
+ await run ( `@tailwind utilities; .foo {}` , configPath , `id=1` )
145
+ await run ( `@tailwind utilities; .foo {}` , configPath , `id=1` )
146
+
147
+ expect ( sharedState . contextInvalidationCount ) . toBe ( 2 )
148
+
149
+ // Save the file twice with a content but not length change
150
+ // This should rebuild the context two more times
151
+ await run ( `@tailwind utilities; .bar {}` , configPath , `id=1` )
152
+ await run ( `@tailwind utilities; .baz {}` , configPath , `id=1` )
153
+
154
+ expect ( sharedState . contextInvalidationCount ) . toBe ( 4 )
155
+
156
+ // Save a file with a change that does not affect the context
157
+ // No invalidation should occur
158
+ await run ( `.foo { @apply mb-1; }` , configPath , `id=2` )
159
+ await run ( `.foo { @apply mb-1; }` , configPath , `id=2` )
160
+ await run ( `.foo { @apply mb-1; }` , configPath , `id=2` )
161
+
162
+ expect ( sharedState . contextInvalidationCount ) . toBe ( 4 )
163
+ } )
0 commit comments