normalize-config.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { normalizeConfig } from '../src/util/normalizeConfig'
  2. import { run, css } from './util/run'
  3. import resolveConfig from '../src/public/resolve-config'
  4. it.each`
  5. config
  6. ${{ purge: [{ raw: 'text-center' }] }}
  7. ${{ purge: { content: [{ raw: 'text-center' }] } }}
  8. ${{ content: { 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: { 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. it.each`
  33. config
  34. ${{ content: [{ raw: 'text-center' }], purge: { extract: () => ['font-bold'] } }}
  35. ${{ content: [{ raw: 'text-center' }], purge: { extract: { DEFAULT: () => ['font-bold'] } } }}
  36. ${{ content: [{ raw: 'text-center' }], purge: { options: { defaultExtractor: () => ['font-bold'] } } }}
  37. ${{ content: [{ raw: 'text-center' }], purge: { options: { extractors: [{ extractor: () => ['font-bold'], extensions: ['html'] }] } } }}
  38. ${{ content: [{ raw: 'text-center' }], purge: { extract: { html: () => ['font-bold'] } } }}
  39. `('should normalize extractors $config', ({ config }) => {
  40. return run('@tailwind utilities', config).then((result) => {
  41. return expect(result.css).toMatchFormattedCss(css`
  42. .font-bold {
  43. font-weight: 700;
  44. }
  45. `)
  46. })
  47. })
  48. it('should still be possible to use the "old" v2 config', () => {
  49. let config = {
  50. purge: {
  51. content: [
  52. { raw: 'text-svelte', extension: 'svelte' },
  53. { raw: '# My Big Heading', extension: 'md' },
  54. ],
  55. options: {
  56. defaultExtractor(content) {
  57. return content.split(' ').concat(['font-bold'])
  58. },
  59. },
  60. extract: {
  61. svelte(content) {
  62. return content.replace('svelte', 'center').split(' ')
  63. },
  64. },
  65. transform: {
  66. md() {
  67. return 'text-4xl'
  68. },
  69. },
  70. },
  71. theme: {
  72. extends: {},
  73. },
  74. variants: {
  75. extends: {},
  76. },
  77. }
  78. return run('@tailwind utilities', config).then((result) => {
  79. return expect(result.css).toMatchFormattedCss(css`
  80. .text-center {
  81. text-align: center;
  82. }
  83. .text-4xl {
  84. font-size: 2.25rem;
  85. line-height: 2.5rem;
  86. }
  87. .font-bold {
  88. font-weight: 700;
  89. }
  90. `)
  91. })
  92. })
  93. it('should keep content files with globs', () => {
  94. let config = {
  95. content: ['./example-folder/**/*.{html,js}'],
  96. }
  97. expect(normalizeConfig(resolveConfig(config)).content).toEqual({
  98. files: ['./example-folder/**/*.{html,js}'],
  99. extract: {},
  100. transform: {},
  101. })
  102. })
  103. it('should warn when we detect invalid globs with incorrect brace expansion', () => {
  104. let log = require('../src/util/log')
  105. let spy = jest.spyOn(log.default, 'warn')
  106. let config = {
  107. content: [
  108. './{example-folder}/**/*.{html,js}',
  109. './{example-folder}/**/*.{html}',
  110. './example-folder/**/*.{html}',
  111. ],
  112. }
  113. // No rewrite happens
  114. expect(normalizeConfig(resolveConfig(config)).content).toEqual({
  115. files: [
  116. './{example-folder}/**/*.{html,js}',
  117. './{example-folder}/**/*.{html}',
  118. './example-folder/**/*.{html}',
  119. ],
  120. extract: {},
  121. transform: {},
  122. })
  123. // But a warning should happen
  124. expect(spy).toHaveBeenCalledTimes(2)
  125. expect(spy.mock.calls.map((x) => x[0])).toEqual(['invalid-glob-braces', 'invalid-glob-braces'])
  126. spy.mockClear()
  127. })