normalize-config.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { normalizeConfig } from '../src/util/normalizeConfig'
  2. import resolveConfig from '../src/public/resolve-config'
  3. import { run, css } from './util/run'
  4. it.each`
  5. config
  6. ${{ purge: [{ raw: 'text-center' }] }}
  7. ${{ purge: { content: [{ raw: 'text-center' }] } }}
  8. ${{ content: { files: [], content: [{ raw: 'text-center' }] } }}
  9. `('should normalize content $config', ({ config }) => {
  10. return run('@tailwind utilities', config).then((result) => {
  11. return expect(result.css).toMatchFormattedCss(css`
  12. .text-center {
  13. text-align: center;
  14. }
  15. `)
  16. })
  17. })
  18. it.each`
  19. config
  20. ${{ purge: { safelist: ['text-center'] } }}
  21. ${{ purge: { options: { safelist: ['text-center'] } } }}
  22. ${{ content: { files: [], safelist: ['text-center'] } }}
  23. `('should normalize safelist $config', ({ config }) => {
  24. return run('@tailwind utilities', config).then((result) => {
  25. return expect(result.css).toMatchFormattedCss(css`
  26. .text-center {
  27. text-align: center;
  28. }
  29. `)
  30. })
  31. })
  32. test.each`
  33. config
  34. ${{
  35. purge: { content: [{ raw: 'text-center' }], extract: () => ['font-bold'] },
  36. }}
  37. ${{
  38. purge: { content: [{ raw: 'text-center' }], extract: { DEFAULT: () => ['font-bold'] } },
  39. }}
  40. ${{
  41. purge: {
  42. content: [{ raw: 'text-center' }],
  43. options: { defaultExtractor: () => ['font-bold'] },
  44. },
  45. }}
  46. ${{
  47. purge: {
  48. content: [{ raw: 'text-center' }],
  49. options: { extractors: [{ extractor: () => ['font-bold'], extensions: ['html'] }] },
  50. },
  51. }}
  52. ${{
  53. purge: {
  54. content: [{ raw: 'text-center' }],
  55. extract: { html: () => ['font-bold'] },
  56. },
  57. }}
  58. `('should normalize extractors $config', ({ config }) => {
  59. return run('@tailwind utilities', config).then((result) => {
  60. return expect(result.css).toMatchFormattedCss(css`
  61. .font-bold {
  62. font-weight: 700;
  63. }
  64. `)
  65. })
  66. })
  67. test('should still be possible to use the "old" v2 config', () => {
  68. let config = {
  69. purge: {
  70. content: [
  71. { raw: 'text-svelte', extension: 'svelte' },
  72. { raw: '# My Big Heading', extension: 'md' },
  73. ],
  74. options: {
  75. defaultExtractor(content) {
  76. return content.split(' ').concat(['font-bold'])
  77. },
  78. },
  79. extract: {
  80. svelte(content) {
  81. return content.replace('svelte', 'center').split(' ')
  82. },
  83. },
  84. transform: {
  85. md() {
  86. return 'text-4xl'
  87. },
  88. },
  89. },
  90. theme: {
  91. extends: {},
  92. },
  93. variants: {
  94. extends: {},
  95. },
  96. }
  97. return run('@tailwind utilities', config).then((result) => {
  98. return expect(result.css).toMatchFormattedCss(css`
  99. .text-center {
  100. text-align: center;
  101. }
  102. .text-4xl {
  103. font-size: 2.25rem;
  104. line-height: 2.5rem;
  105. }
  106. .font-bold {
  107. font-weight: 700;
  108. }
  109. `)
  110. })
  111. })
  112. it('should keep content files with globs', () => {
  113. let config = {
  114. content: ['./example-folder/**/*.{html,js}'],
  115. }
  116. expect(normalizeConfig(resolveConfig(config)).content).toEqual({
  117. files: ['./example-folder/**/*.{html,js}'],
  118. relative: false,
  119. extract: {},
  120. transform: {},
  121. })
  122. })
  123. it('should warn when we detect invalid globs with incorrect brace expansion', () => {
  124. let config = {
  125. content: [
  126. './{example-folder}/**/*.{html,js}',
  127. './{example-folder}/**/*.{html}',
  128. './example-folder/**/*.{html}',
  129. ],
  130. }
  131. let normalizedConfig = normalizeConfig(resolveConfig(config)).content
  132. // No rewrite happens
  133. expect(normalizedConfig).toEqual({
  134. files: [
  135. './{example-folder}/**/*.{html,js}',
  136. './{example-folder}/**/*.{html}',
  137. './example-folder/**/*.{html}',
  138. ],
  139. relative: false,
  140. extract: {},
  141. transform: {},
  142. })
  143. expect().toHaveBeenWarnedWith(['invalid-glob-braces'])
  144. })