source-maps.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import postcss from 'postcss'
  2. import { runWithSourceMaps as run, html, css, map } from './util/run'
  3. import { parseSourceMaps } from './util/source-maps'
  4. it('apply generates source maps', async () => {
  5. let config = {
  6. content: [
  7. {
  8. raw: html`
  9. <div class="with-declaration"></div>
  10. <div class="with-comment"></div>
  11. <div class="just-apply"></div>
  12. `,
  13. },
  14. ],
  15. corePlugins: { preflight: false },
  16. }
  17. let input = css`
  18. .with-declaration {
  19. background-color: red;
  20. @apply h-4 w-4 bg-green-500;
  21. }
  22. .with-comment {
  23. /* sourcemap will work here too */
  24. @apply h-4 w-4 bg-red-500;
  25. }
  26. .just-apply {
  27. @apply h-4 w-4 bg-black;
  28. }
  29. `
  30. let result = await run(input, config)
  31. let { sources, annotations } = parseSourceMaps(result)
  32. // All CSS generated by Tailwind CSS should be annotated with source maps
  33. // And always be able to point to the original source file
  34. expect(sources).not.toContain('<no source>')
  35. expect(sources.length).toBe(1)
  36. expect(annotations).toMatchSnapshot()
  37. })
  38. it('preflight + base have source maps', async () => {
  39. let config = {
  40. content: [],
  41. }
  42. let input = css`
  43. @tailwind base;
  44. `
  45. let result = await run(input, config)
  46. let { sources, annotations } = parseSourceMaps(result)
  47. // All CSS generated by Tailwind CSS should be annotated with source maps
  48. // And always be able to point to the original source file
  49. expect(sources).not.toContain('<no source>')
  50. expect(sources.length).toBe(1)
  51. expect(annotations).toMatchSnapshot()
  52. })
  53. it('utilities have source maps', async () => {
  54. let config = {
  55. content: [{ raw: `text-red-500` }],
  56. }
  57. let input = css`
  58. @tailwind utilities;
  59. `
  60. let result = await run(input, config)
  61. let { sources, annotations } = parseSourceMaps(result)
  62. // All CSS generated by Tailwind CSS should be annotated with source maps
  63. // And always be able to point to the original source file
  64. expect(sources).not.toContain('<no source>')
  65. expect(sources.length).toBe(1)
  66. expect(annotations).toStrictEqual(['2:4 -> 1:0', '2:4-23 -> 2:4-24', '2:4 -> 3:4', '2:23 -> 4:0'])
  67. })
  68. it('components have source maps', async () => {
  69. let config = {
  70. content: [{ raw: `container` }],
  71. }
  72. let input = css`
  73. @tailwind components;
  74. `
  75. let result = await run(input, config)
  76. let { sources, annotations } = parseSourceMaps(result)
  77. // All CSS generated by Tailwind CSS should be annotated with source maps
  78. // And always be able to point to the original source file
  79. expect(sources).not.toContain('<no source>')
  80. expect(sources.length).toBe(1)
  81. expect(annotations).toMatchSnapshot()
  82. })
  83. it('source maps for layer rules are not rewritten to point to @tailwind directives', async () => {
  84. let config = {
  85. content: [{ raw: `font-normal foo hover:foo` }],
  86. }
  87. let utilitiesFile = postcss.parse(
  88. css`
  89. @tailwind utilities;
  90. `,
  91. { from: 'components.css', map: { prev: map } }
  92. )
  93. let mainCssFile = postcss.parse(
  94. css`
  95. @layer utilities {
  96. .foo {
  97. background-color: red;
  98. }
  99. }
  100. `,
  101. { from: 'input.css', map: { prev: map } }
  102. )
  103. // Just pretend that there's an @import in `mainCssFile` that imports the nodes from `utilitiesFile`
  104. let input = postcss.root({
  105. nodes: [...utilitiesFile.nodes, ...mainCssFile.nodes],
  106. source: mainCssFile.source,
  107. })
  108. let result = await run(input, config)
  109. let { sources, annotations } = parseSourceMaps(result)
  110. // All CSS generated by Tailwind CSS should be annotated with source maps
  111. // And always be able to point to the original source file
  112. expect(sources).not.toContain('<no source>')
  113. // And we should see that the source map for the layer rule is not rewritten
  114. // to point to the @tailwind directive but instead points to the original
  115. expect(sources.length).toBe(2)
  116. expect(sources).toEqual(['components.css', 'input.css'])
  117. expect(annotations).toMatchSnapshot()
  118. })