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