important-boolean.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import fs from 'fs'
  2. import path from 'path'
  3. import * as sharedState from '../src/lib/sharedState'
  4. import { run, html, css, defaults } from './util/run'
  5. test('important boolean', () => {
  6. let config = {
  7. important: true,
  8. darkMode: 'class',
  9. content: [
  10. {
  11. raw: html`
  12. <div class="container"></div>
  13. <div class="btn"></div>
  14. <div class="animate-spin"></div>
  15. <div class="custom-util"></div>
  16. <div class="custom-component"></div>
  17. <div class="custom-important-component"></div>
  18. <div class="font-bold"></div>
  19. <div class="md:hover:text-right"></div>
  20. <div class="motion-safe:hover:text-center"></div>
  21. <div class="dark:focus:text-left"></div>
  22. <div class="group-hover:focus-within:text-left"></div>
  23. <div class="rtl:active:text-center"></div>
  24. `,
  25. },
  26. ],
  27. corePlugins: { preflight: false },
  28. plugins: [
  29. function ({ addComponents, addUtilities }) {
  30. addComponents(
  31. {
  32. '.btn': {
  33. button: 'yes',
  34. },
  35. },
  36. { respectImportant: true }
  37. )
  38. addComponents(
  39. {
  40. '@font-face': {
  41. 'font-family': 'Inter',
  42. },
  43. '@page': {
  44. margin: '1cm',
  45. },
  46. },
  47. { respectImportant: true }
  48. )
  49. addUtilities(
  50. {
  51. '.custom-util': {
  52. button: 'no',
  53. },
  54. },
  55. { respectImportant: false }
  56. )
  57. },
  58. ],
  59. }
  60. let input = css`
  61. @tailwind base;
  62. @tailwind components;
  63. @layer components {
  64. .custom-component {
  65. @apply font-bold;
  66. }
  67. .custom-important-component {
  68. @apply text-center !important;
  69. }
  70. }
  71. @tailwind utilities;
  72. `
  73. return run(input, config).then((result) => {
  74. expect(result.css).toMatchFormattedCss(css`
  75. ${defaults}
  76. .container {
  77. width: 100%;
  78. }
  79. @media (min-width: 640px) {
  80. .container {
  81. max-width: 640px;
  82. }
  83. }
  84. @media (min-width: 768px) {
  85. .container {
  86. max-width: 768px;
  87. }
  88. }
  89. @media (min-width: 1024px) {
  90. .container {
  91. max-width: 1024px;
  92. }
  93. }
  94. @media (min-width: 1280px) {
  95. .container {
  96. max-width: 1280px;
  97. }
  98. }
  99. @media (min-width: 1536px) {
  100. .container {
  101. max-width: 1536px;
  102. }
  103. }
  104. .btn {
  105. button: yes !important;
  106. }
  107. @font-face {
  108. font-family: Inter;
  109. }
  110. @page {
  111. margin: 1cm;
  112. }
  113. .custom-component {
  114. font-weight: 700;
  115. }
  116. .custom-important-component {
  117. text-align: center !important;
  118. }
  119. @keyframes spin {
  120. to {
  121. transform: rotate(360deg);
  122. }
  123. }
  124. .animate-spin {
  125. animation: 1s linear infinite spin !important;
  126. }
  127. .font-bold {
  128. font-weight: 700 !important;
  129. }
  130. .custom-util {
  131. button: no;
  132. }
  133. .group:hover .group-hover\:focus-within\:text-left:focus-within {
  134. text-align: left !important;
  135. }
  136. :is([dir='rtl'] .rtl\:active\:text-center:active) {
  137. text-align: center !important;
  138. }
  139. @media (prefers-reduced-motion: no-preference) {
  140. .motion-safe\:hover\:text-center:hover {
  141. text-align: center !important;
  142. }
  143. }
  144. :is(.dark .dark\:focus\:text-left:focus) {
  145. text-align: left !important;
  146. }
  147. @media (min-width: 768px) {
  148. .md\:hover\:text-right:hover {
  149. text-align: right !important;
  150. }
  151. }
  152. `)
  153. })
  154. })
  155. // This is in a describe block so we can use `afterEach` :)
  156. describe('duplicate elision', () => {
  157. let filePath = path.resolve(__dirname, './important-boolean-duplicates.test.html')
  158. afterEach(async () => await fs.promises.unlink(filePath))
  159. test('important rules are not duplicated when rebuilding', async () => {
  160. let config = {
  161. important: true,
  162. content: [filePath],
  163. }
  164. await fs.promises.writeFile(
  165. config.content[0],
  166. html`
  167. <div class="ml-2"></div>
  168. <div class="ml-4"></div>
  169. `
  170. )
  171. let input = css`
  172. @tailwind utilities;
  173. `
  174. let result = await run(input, config)
  175. let allContexts = Array.from(sharedState.contextMap.values())
  176. let context = allContexts[allContexts.length - 1]
  177. let ruleCacheSize1 = context.ruleCache.size
  178. expect(result.css).toMatchFormattedCss(css`
  179. .ml-2 {
  180. margin-left: 0.5rem !important;
  181. }
  182. .ml-4 {
  183. margin-left: 1rem !important;
  184. }
  185. `)
  186. await fs.promises.writeFile(
  187. config.content[0],
  188. html`
  189. <div class="ml-2"></div>
  190. <div class="ml-6"></div>
  191. `
  192. )
  193. result = await run(input, config)
  194. let ruleCacheSize2 = context.ruleCache.size
  195. expect(result.css).toMatchFormattedCss(css`
  196. .ml-2 {
  197. margin-left: 0.5rem !important;
  198. }
  199. .ml-4 {
  200. margin-left: 1rem !important;
  201. }
  202. .ml-6 {
  203. margin-left: 1.5rem !important;
  204. }
  205. `)
  206. // The rule cache was effectively doubling in size previously
  207. // because the rule cache was never de-duped
  208. // This ensures this behavior doesn't return
  209. expect(ruleCacheSize2 - ruleCacheSize1).toBeLessThan(10)
  210. })
  211. })