mutable.test.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import postcss from 'postcss'
  2. import fs from 'fs'
  3. import path from 'path'
  4. import { run } from './util/run'
  5. function pluginThatMutatesRules() {
  6. return (root) => {
  7. root.walkRules((rule) => {
  8. rule.nodes
  9. .filter((node) => node.prop === 'background-image')
  10. .forEach((node) => {
  11. node.value = 'url("./bar.png")'
  12. })
  13. return rule
  14. })
  15. }
  16. }
  17. test.only('plugins mutating rules after tailwind doesnt break it', async () => {
  18. let config = {
  19. content: [path.resolve(__dirname, './mutable.test.html')],
  20. theme: {
  21. backgroundImage: {
  22. foo: 'url("./foo.png")',
  23. },
  24. },
  25. plugins: [],
  26. }
  27. function checkResult(result) {
  28. let expectedPath = path.resolve(__dirname, './mutable.test.css')
  29. let expected = fs.readFileSync(expectedPath, 'utf8')
  30. expect(result.css).toMatchFormattedCss(expected)
  31. }
  32. // Verify the first run produces the expected result
  33. let firstRun = await run('@tailwind utilities', config)
  34. checkResult(firstRun)
  35. // Outside of the context of tailwind jit more postcss plugins may operate on the AST:
  36. // In this case we have a plugin that mutates rules directly
  37. await postcss([pluginThatMutatesRules()]).process(firstRun, {
  38. from: path.resolve(__filename),
  39. })
  40. // Verify subsequent runs don't produce mutated rules
  41. let secondRun = await run('@tailwind utilities', config)
  42. checkResult(secondRun)
  43. })