apply.test.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { run, html, css, defaults } from './util/run'
  4. test('@apply', () => {
  5. let config = {
  6. darkMode: 'class',
  7. content: [path.resolve(__dirname, './apply.test.html')],
  8. }
  9. let input = css`
  10. @tailwind components;
  11. @tailwind utilities;
  12. @layer components {
  13. .basic-example {
  14. @apply rounded-md bg-blue-500 px-4 py-2;
  15. }
  16. .class-order {
  17. @apply p-8 px-3 py-7 pt-4 pr-1;
  18. }
  19. .with-additional-properties {
  20. font-weight: 500;
  21. @apply text-right;
  22. }
  23. .variants {
  24. @apply font-semibold hover:font-bold focus:font-medium lg:font-light xl:focus:font-black;
  25. }
  26. .only-variants {
  27. @apply hover:font-bold focus:font-medium lg:font-light xl:focus:font-black;
  28. }
  29. .apply-group-variant {
  30. @apply group-hover:text-center lg:group-hover:text-left;
  31. }
  32. .apply-dark-variant {
  33. @apply dark:text-center dark:hover:text-right lg:dark:text-left;
  34. }
  35. .apply-custom-utility {
  36. @apply custom-util hover:custom-util lg:custom-util xl:focus:custom-util;
  37. }
  38. .multiple,
  39. .selectors {
  40. @apply rounded-md bg-blue-500 px-4 py-2;
  41. }
  42. .multiple-variants,
  43. .selectors-variants {
  44. @apply hover:text-center active:text-right lg:focus:text-left;
  45. }
  46. .multiple-group,
  47. .selectors-group {
  48. @apply group-hover:text-center lg:group-hover:text-left;
  49. }
  50. .complex-utilities {
  51. @apply ordinal tabular-nums shadow-lg hover:shadow-xl focus:diagonal-fractions;
  52. }
  53. .use-base-only-a {
  54. @apply font-bold;
  55. }
  56. .use-base-only-b {
  57. @apply use-base-only-a font-normal;
  58. }
  59. .use-dependant-only-a {
  60. @apply font-bold;
  61. }
  62. .use-dependant-only-b {
  63. @apply use-dependant-only-a font-normal;
  64. }
  65. .btn {
  66. @apply rounded py-2 px-4 font-bold;
  67. }
  68. .btn-blue {
  69. @apply btn bg-blue-500 text-white hover:bg-blue-700;
  70. }
  71. .recursive-apply-a {
  72. @apply font-black sm:font-thin;
  73. }
  74. .recursive-apply-b {
  75. @apply recursive-apply-a font-semibold md:font-extralight;
  76. }
  77. .recursive-apply-c {
  78. @apply recursive-apply-b font-bold lg:font-light;
  79. }
  80. .use-with-other-properties-base {
  81. color: green;
  82. @apply font-bold;
  83. }
  84. .use-with-other-properties-component {
  85. @apply use-with-other-properties-base;
  86. }
  87. .add-sibling-properties {
  88. padding: 2rem;
  89. @apply px-4 hover:px-2 lg:px-10 xl:focus:px-1;
  90. padding-top: 3px;
  91. @apply use-with-other-properties-base;
  92. }
  93. h1 {
  94. @apply text-2xl sm:text-3xl lg:text-2xl;
  95. }
  96. h2 {
  97. @apply text-2xl;
  98. @apply lg:text-2xl;
  99. @apply sm:text-2xl;
  100. }
  101. .important-modifier {
  102. @apply !rounded-md px-4;
  103. }
  104. .important-modifier-variant {
  105. @apply px-4 hover:!rounded-md;
  106. }
  107. }
  108. @layer utilities {
  109. .custom-util {
  110. custom: stuff;
  111. }
  112. .foo {
  113. @apply animate-spin;
  114. }
  115. .bar {
  116. @apply animate-pulse !important;
  117. }
  118. }
  119. `
  120. return run(input, config).then((result) => {
  121. let expectedPath = path.resolve(__dirname, './apply.test.css')
  122. let expected = fs.readFileSync(expectedPath, 'utf8')
  123. expect(result.css).toMatchFormattedCss(expected)
  124. })
  125. })
  126. test('@apply error with unknown utility', async () => {
  127. let config = {
  128. darkMode: 'class',
  129. content: [path.resolve(__dirname, './apply.test.html')],
  130. }
  131. let input = css`
  132. @tailwind components;
  133. @tailwind utilities;
  134. @layer components {
  135. .foo {
  136. @apply a-utility-that-does-not-exist;
  137. }
  138. }
  139. `
  140. await expect(run(input, config)).rejects.toThrowError('class does not exist')
  141. })
  142. test('@apply error with nested @screen', async () => {
  143. let config = {
  144. darkMode: 'class',
  145. content: [path.resolve(__dirname, './apply.test.html')],
  146. }
  147. let input = css`
  148. @tailwind components;
  149. @tailwind utilities;
  150. @layer components {
  151. .foo {
  152. @screen md {
  153. @apply text-black;
  154. }
  155. }
  156. }
  157. `
  158. await expect(run(input, config)).rejects.toThrowError(
  159. '@apply is not supported within nested at-rules like @screen'
  160. )
  161. })
  162. test('@apply error with nested @anyatrulehere', async () => {
  163. let config = {
  164. darkMode: 'class',
  165. content: [path.resolve(__dirname, './apply.test.html')],
  166. }
  167. let input = css`
  168. @tailwind components;
  169. @tailwind utilities;
  170. @layer components {
  171. .foo {
  172. @genie {
  173. @apply text-black;
  174. }
  175. }
  176. }
  177. `
  178. await expect(run(input, config)).rejects.toThrowError(
  179. '@apply is not supported within nested at-rules like @genie'
  180. )
  181. })
  182. test('@apply error when using .group utility', async () => {
  183. let config = {
  184. darkMode: 'class',
  185. content: [{ raw: '<div class="foo"></div>' }],
  186. }
  187. let input = css`
  188. @tailwind components;
  189. @tailwind utilities;
  190. @layer components {
  191. .foo {
  192. @apply group;
  193. }
  194. }
  195. `
  196. await expect(run(input, config)).rejects.toThrowError(
  197. `@apply should not be used with the 'group' utility`
  198. )
  199. })
  200. test('@apply error when using a prefixed .group utility', async () => {
  201. let config = {
  202. prefix: 'tw-',
  203. darkMode: 'class',
  204. content: [{ raw: html`<div class="foo"></div>` }],
  205. }
  206. let input = css`
  207. @tailwind components;
  208. @tailwind utilities;
  209. @layer components {
  210. .foo {
  211. @apply tw-group;
  212. }
  213. }
  214. `
  215. await expect(run(input, config)).rejects.toThrowError(
  216. `@apply should not be used with the 'tw-group' utility`
  217. )
  218. })
  219. test('@apply error when using .peer utility', async () => {
  220. let config = {
  221. darkMode: 'class',
  222. content: [{ raw: '<div class="foo"></div>' }],
  223. }
  224. let input = css`
  225. @tailwind components;
  226. @tailwind utilities;
  227. @layer components {
  228. .foo {
  229. @apply peer;
  230. }
  231. }
  232. `
  233. await expect(run(input, config)).rejects.toThrowError(
  234. `@apply should not be used with the 'peer' utility`
  235. )
  236. })
  237. test('@apply error when using a prefixed .peer utility', async () => {
  238. let config = {
  239. prefix: 'tw-',
  240. darkMode: 'class',
  241. content: [{ raw: html`<div class="foo"></div>` }],
  242. }
  243. let input = css`
  244. @tailwind components;
  245. @tailwind utilities;
  246. @layer components {
  247. .foo {
  248. @apply tw-peer;
  249. }
  250. }
  251. `
  252. await expect(run(input, config)).rejects.toThrowError(
  253. `@apply should not be used with the 'tw-peer' utility`
  254. )
  255. })
  256. test('@apply classes from outside a @layer', async () => {
  257. let config = {
  258. content: [{ raw: html`<div class="foo bar baz font-bold"></div>` }],
  259. }
  260. let input = css`
  261. @tailwind components;
  262. @tailwind utilities;
  263. .foo {
  264. @apply font-bold;
  265. }
  266. .bar {
  267. @apply foo text-red-500 hover:text-green-500;
  268. }
  269. .baz {
  270. @apply bar underline;
  271. }
  272. .keep-me-even-though-I-am-not-used-in-content {
  273. color: green;
  274. }
  275. `
  276. await run(input, config).then((result) => {
  277. return expect(result.css).toMatchFormattedCss(css`
  278. .font-bold {
  279. font-weight: 700;
  280. }
  281. .foo {
  282. font-weight: 700;
  283. }
  284. .bar {
  285. --tw-text-opacity: 1;
  286. color: rgb(239 68 68 / var(--tw-text-opacity));
  287. font-weight: 700;
  288. }
  289. .bar:hover {
  290. --tw-text-opacity: 1;
  291. color: rgb(34 197 94 / var(--tw-text-opacity));
  292. }
  293. .baz {
  294. text-decoration-line: underline;
  295. --tw-text-opacity: 1;
  296. color: rgb(239 68 68 / var(--tw-text-opacity));
  297. font-weight: 700;
  298. }
  299. .baz:hover {
  300. --tw-text-opacity: 1;
  301. color: rgb(34 197 94 / var(--tw-text-opacity));
  302. }
  303. .keep-me-even-though-I-am-not-used-in-content {
  304. color: green;
  305. }
  306. `)
  307. })
  308. })
  309. test('@applying classes from outside a @layer respects the source order', async () => {
  310. let config = {
  311. content: [{ raw: html`<div class="foo bar baz container font-bold"></div>` }],
  312. }
  313. let input = css`
  314. .baz {
  315. @apply bar underline;
  316. }
  317. @tailwind components;
  318. .keep-me-even-though-I-am-not-used-in-content {
  319. color: green;
  320. }
  321. @tailwind utilities;
  322. .foo {
  323. @apply font-bold;
  324. }
  325. .bar {
  326. @apply no-underline;
  327. }
  328. `
  329. await run(input, config).then((result) => {
  330. return expect(result.css).toMatchFormattedCss(css`
  331. .baz {
  332. text-decoration-line: underline;
  333. text-decoration-line: none;
  334. }
  335. .container {
  336. width: 100%;
  337. }
  338. @media (min-width: 640px) {
  339. .container {
  340. max-width: 640px;
  341. }
  342. }
  343. @media (min-width: 768px) {
  344. .container {
  345. max-width: 768px;
  346. }
  347. }
  348. @media (min-width: 1024px) {
  349. .container {
  350. max-width: 1024px;
  351. }
  352. }
  353. @media (min-width: 1280px) {
  354. .container {
  355. max-width: 1280px;
  356. }
  357. }
  358. @media (min-width: 1536px) {
  359. .container {
  360. max-width: 1536px;
  361. }
  362. }
  363. .keep-me-even-though-I-am-not-used-in-content {
  364. color: green;
  365. }
  366. .font-bold {
  367. font-weight: 700;
  368. }
  369. .foo {
  370. font-weight: 700;
  371. }
  372. .bar {
  373. text-decoration-line: none;
  374. }
  375. `)
  376. })
  377. })
  378. it('should remove duplicate properties when using apply with similar properties', () => {
  379. let config = {
  380. content: [{ raw: 'foo' }],
  381. }
  382. let input = css`
  383. @tailwind utilities;
  384. .foo {
  385. @apply absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 transform;
  386. }
  387. `
  388. return run(input, config).then((result) => {
  389. expect(result.css).toMatchFormattedCss(css`
  390. .foo {
  391. position: absolute;
  392. top: 50%;
  393. left: 50%;
  394. --tw-translate-x: -50%;
  395. --tw-translate-y: -50%;
  396. transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate))
  397. skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x))
  398. scaleY(var(--tw-scale-y));
  399. }
  400. `)
  401. })
  402. })
  403. it('should apply all the definitions of a class', () => {
  404. let config = {
  405. content: [{ raw: html`<div class="foo"></div>` }],
  406. plugins: [],
  407. }
  408. let input = css`
  409. @tailwind components;
  410. @tailwind utilities;
  411. @layer utilities {
  412. .aspect-w-1 {
  413. position: relative;
  414. }
  415. .aspect-w-1 {
  416. --tw-aspect-w: 1;
  417. }
  418. }
  419. @layer components {
  420. .foo {
  421. @apply aspect-w-1;
  422. }
  423. }
  424. `
  425. return run(input, config).then((result) => {
  426. return expect(result.css).toMatchFormattedCss(css`
  427. .foo {
  428. position: relative;
  429. --tw-aspect-w: 1;
  430. }
  431. `)
  432. })
  433. })
  434. it('should throw when trying to apply a direct circular dependency', () => {
  435. let config = {
  436. content: [{ raw: html`<div class="foo"></div>` }],
  437. plugins: [],
  438. }
  439. let input = css`
  440. @tailwind components;
  441. @tailwind utilities;
  442. @layer components {
  443. .foo:not(.text-red-500) {
  444. @apply text-red-500;
  445. }
  446. }
  447. `
  448. return run(input, config).catch((err) => {
  449. expect(err.reason).toBe(
  450. 'You cannot `@apply` the `text-red-500` utility here because it creates a circular dependency.'
  451. )
  452. })
  453. })
  454. it('should throw when trying to apply an indirect circular dependency', () => {
  455. let config = {
  456. content: [{ raw: html`<div class="a"></div>` }],
  457. plugins: [],
  458. }
  459. let input = css`
  460. @tailwind components;
  461. @tailwind utilities;
  462. @layer components {
  463. .a {
  464. @apply b;
  465. }
  466. .b {
  467. @apply c;
  468. }
  469. .c {
  470. @apply a;
  471. }
  472. }
  473. `
  474. return run(input, config).catch((err) => {
  475. expect(err.reason).toBe(
  476. 'You cannot `@apply` the `a` utility here because it creates a circular dependency.'
  477. )
  478. })
  479. })
  480. it('should not throw when the selector is different (but contains the base partially)', () => {
  481. let config = {
  482. content: [{ raw: html`<div class="bg-gray-500"></div>` }],
  483. plugins: [],
  484. }
  485. let input = css`
  486. @tailwind components;
  487. @tailwind utilities;
  488. .focus\:bg-gray-500 {
  489. @apply bg-gray-500;
  490. }
  491. `
  492. return run(input, config).then((result) => {
  493. expect(result.css).toMatchFormattedCss(css`
  494. .bg-gray-500 {
  495. --tw-bg-opacity: 1;
  496. background-color: rgb(107 114 128 / var(--tw-bg-opacity));
  497. }
  498. .focus\:bg-gray-500 {
  499. --tw-bg-opacity: 1;
  500. background-color: rgb(107 114 128 / var(--tw-bg-opacity));
  501. }
  502. `)
  503. })
  504. })
  505. it('should throw when trying to apply an indirect circular dependency with a modifier (1)', () => {
  506. let config = {
  507. content: [{ raw: html`<div class="a"></div>` }],
  508. plugins: [],
  509. }
  510. let input = css`
  511. @tailwind components;
  512. @tailwind utilities;
  513. @layer components {
  514. .a {
  515. @apply b;
  516. }
  517. .b {
  518. @apply c;
  519. }
  520. .c {
  521. @apply hover:a;
  522. }
  523. }
  524. `
  525. return run(input, config).catch((err) => {
  526. expect(err.reason).toBe(
  527. 'You cannot `@apply` the `hover:a` utility here because it creates a circular dependency.'
  528. )
  529. })
  530. })
  531. it('should throw when trying to apply an indirect circular dependency with a modifier (2)', () => {
  532. let config = {
  533. content: [{ raw: html`<div class="a"></div>` }],
  534. plugins: [],
  535. }
  536. let input = css`
  537. @tailwind components;
  538. @tailwind utilities;
  539. @layer components {
  540. .a {
  541. @apply b;
  542. }
  543. .b {
  544. @apply hover:c;
  545. }
  546. .c {
  547. @apply a;
  548. }
  549. }
  550. `
  551. return run(input, config).catch((err) => {
  552. expect(err.reason).toBe(
  553. 'You cannot `@apply` the `a` utility here because it creates a circular dependency.'
  554. )
  555. })
  556. })
  557. it('should not throw when the circular dependency is part of a different selector (1)', () => {
  558. let config = {
  559. content: [{ raw: html`<div class="c"></div>` }],
  560. plugins: [],
  561. }
  562. let input = css`
  563. @tailwind utilities;
  564. @layer utilities {
  565. html.dark .a,
  566. .b {
  567. color: red;
  568. }
  569. }
  570. html.dark .c {
  571. @apply b;
  572. }
  573. `
  574. return run(input, config).then((result) => {
  575. expect(result.css).toMatchFormattedCss(css`
  576. html.dark .c {
  577. color: red;
  578. }
  579. `)
  580. })
  581. })
  582. it('should not throw when the circular dependency is part of a different selector (2)', () => {
  583. let config = {
  584. content: [{ raw: html`<div class="c"></div>` }],
  585. plugins: [],
  586. }
  587. let input = css`
  588. @tailwind utilities;
  589. @layer utilities {
  590. html.dark .a,
  591. .b {
  592. color: red;
  593. }
  594. }
  595. html.dark .c {
  596. @apply hover:b;
  597. }
  598. `
  599. return run(input, config).then((result) => {
  600. expect(result.css).toMatchFormattedCss(css`
  601. html.dark .c:hover {
  602. color: red;
  603. }
  604. `)
  605. })
  606. })
  607. it('should throw when the circular dependency is part of the same selector', () => {
  608. let config = {
  609. content: [{ raw: html`<div class="c"></div>` }],
  610. plugins: [],
  611. }
  612. let input = css`
  613. @tailwind utilities;
  614. @layer utilities {
  615. html.dark .a,
  616. html.dark .b {
  617. color: red;
  618. }
  619. }
  620. html.dark .c {
  621. @apply hover:b;
  622. }
  623. `
  624. return run(input, config).catch((err) => {
  625. expect(err.reason).toBe(
  626. 'You cannot `@apply` the `hover:b` utility here because it creates a circular dependency.'
  627. )
  628. })
  629. })
  630. it('rules with vendor prefixes are still separate when optimizing defaults rules', () => {
  631. let config = {
  632. experimental: { optimizeUniversalDefaults: true },
  633. content: [{ raw: html`<div class="border"></div>` }],
  634. corePlugins: { preflight: false },
  635. }
  636. let input = css`
  637. @tailwind base;
  638. @tailwind components;
  639. @tailwind utilities;
  640. @layer components {
  641. input[type='range']::-moz-range-thumb {
  642. @apply border;
  643. }
  644. }
  645. `
  646. return run(input, config).then((result) => {
  647. return expect(result.css).toMatchFormattedCss(css`
  648. input[type='range']::-moz-range-thumb {
  649. border-width: 1px;
  650. }
  651. .border {
  652. border-width: 1px;
  653. }
  654. `)
  655. })
  656. })
  657. it('should be possible to apply user css', () => {
  658. let config = {
  659. content: [{ raw: html`<div></div>` }],
  660. plugins: [],
  661. }
  662. let input = css`
  663. @tailwind components;
  664. @tailwind utilities;
  665. .foo {
  666. color: red;
  667. }
  668. .bar {
  669. @apply foo;
  670. }
  671. `
  672. return run(input, config).then((result) => {
  673. return expect(result.css).toMatchFormattedCss(css`
  674. .foo {
  675. color: red;
  676. }
  677. .bar {
  678. color: red;
  679. }
  680. `)
  681. })
  682. })
  683. it('should not be possible to apply user css with variants', () => {
  684. let config = {
  685. content: [{ raw: html`<div></div>` }],
  686. plugins: [],
  687. }
  688. let input = css`
  689. @tailwind components;
  690. @tailwind utilities;
  691. .foo {
  692. color: red;
  693. }
  694. .bar {
  695. @apply hover:foo;
  696. }
  697. `
  698. return run(input, config).catch((err) => {
  699. expect(err.reason).toBe(
  700. 'The `hover:foo` class does not exist. If `hover:foo` is a custom class, make sure it is defined within a `@layer` directive.'
  701. )
  702. })
  703. })
  704. it('should not apply unrelated siblings when applying something from within atrules', () => {
  705. let config = {
  706. content: [{ raw: html`<div class="foo bar something-unrelated"></div>` }],
  707. plugins: [],
  708. }
  709. let input = css`
  710. @tailwind components;
  711. @tailwind utilities;
  712. @layer components {
  713. .foo {
  714. font-weight: bold;
  715. @apply bar;
  716. }
  717. .bar {
  718. color: green;
  719. }
  720. @supports (a: b) {
  721. .bar {
  722. color: blue;
  723. }
  724. .something-unrelated {
  725. color: red;
  726. }
  727. }
  728. }
  729. `
  730. return run(input, config).then((result) => {
  731. expect(result.css).toMatchFormattedCss(css`
  732. .foo {
  733. font-weight: bold;
  734. color: green;
  735. }
  736. @supports (a: b) {
  737. .foo {
  738. color: blue;
  739. }
  740. }
  741. .bar {
  742. color: green;
  743. }
  744. @supports (a: b) {
  745. .bar {
  746. color: blue;
  747. }
  748. .something-unrelated {
  749. color: red;
  750. }
  751. }
  752. `)
  753. })
  754. })
  755. it('should be possible to apply user css without tailwind directives', () => {
  756. let config = {
  757. content: [{ raw: html`<div class="foo"></div>` }],
  758. plugins: [],
  759. }
  760. let input = css`
  761. .bop {
  762. color: red;
  763. }
  764. .bar {
  765. background-color: blue;
  766. }
  767. .foo {
  768. @apply bar bop absolute;
  769. }
  770. `
  771. return run(input, config).then((result) => {
  772. return expect(result.css).toMatchFormattedCss(css`
  773. .bop {
  774. color: red;
  775. }
  776. .bar {
  777. background-color: blue;
  778. }
  779. .foo {
  780. position: absolute;
  781. color: red;
  782. background-color: blue;
  783. }
  784. `)
  785. })
  786. })
  787. it('should be possible to apply a class from another rule with multiple selectors (2 classes)', () => {
  788. let config = {
  789. content: [{ raw: html`<div class="c"></div>` }],
  790. plugins: [],
  791. }
  792. let input = css`
  793. @tailwind utilities;
  794. @layer utilities {
  795. .a,
  796. .b {
  797. @apply underline;
  798. }
  799. .c {
  800. @apply b;
  801. }
  802. }
  803. `
  804. return run(input, config).then((result) => {
  805. return expect(result.css).toMatchFormattedCss(css`
  806. .c {
  807. text-decoration-line: underline;
  808. }
  809. `)
  810. })
  811. })
  812. it('should be possible to apply a class from another rule with multiple selectors (1 class, 1 tag)', () => {
  813. let config = {
  814. content: [{ raw: html`<div class="c"></div>` }],
  815. plugins: [],
  816. }
  817. let input = css`
  818. @tailwind utilities;
  819. @layer utilities {
  820. span,
  821. .b {
  822. @apply underline;
  823. }
  824. .c {
  825. @apply b;
  826. }
  827. }
  828. `
  829. return run(input, config).then((result) => {
  830. return expect(result.css).toMatchFormattedCss(css`
  831. span,
  832. .b {
  833. text-decoration-line: underline;
  834. }
  835. .c {
  836. text-decoration-line: underline;
  837. }
  838. `)
  839. })
  840. })
  841. it('should be possible to apply a class from another rule with multiple selectors (1 class, 1 id)', () => {
  842. let config = {
  843. content: [{ raw: html`<div class="c"></div>` }],
  844. plugins: [],
  845. }
  846. let input = css`
  847. @tailwind utilities;
  848. @layer utilities {
  849. #a,
  850. .b {
  851. @apply underline;
  852. }
  853. .c {
  854. @apply b;
  855. }
  856. }
  857. `
  858. return run(input, config).then((result) => {
  859. return expect(result.css).toMatchFormattedCss(css`
  860. #a,
  861. .b {
  862. text-decoration-line: underline;
  863. }
  864. .c {
  865. text-decoration-line: underline;
  866. }
  867. `)
  868. })
  869. })
  870. describe('multiple instances', () => {
  871. it('should be possible to apply multiple "instances" of the same class', () => {
  872. let config = {
  873. content: [{ raw: html`` }],
  874. plugins: [],
  875. corePlugins: { preflight: false },
  876. }
  877. let input = css`
  878. .a {
  879. @apply b;
  880. }
  881. .b {
  882. @apply uppercase;
  883. }
  884. .b {
  885. color: red;
  886. }
  887. `
  888. return run(input, config).then((result) => {
  889. return expect(result.css).toMatchFormattedCss(css`
  890. .a {
  891. text-transform: uppercase;
  892. color: red;
  893. }
  894. .b {
  895. text-transform: uppercase;
  896. color: red;
  897. }
  898. `)
  899. })
  900. })
  901. it('should be possible to apply a combination of multiple "instances" of the same class', () => {
  902. let config = {
  903. content: [{ raw: html`` }],
  904. plugins: [],
  905. corePlugins: { preflight: false },
  906. }
  907. let input = css`
  908. .a {
  909. @apply b;
  910. }
  911. .b {
  912. @apply uppercase;
  913. color: red;
  914. }
  915. `
  916. return run(input, config).then((result) => {
  917. return expect(result.css).toMatchFormattedCss(css`
  918. .a {
  919. text-transform: uppercase;
  920. color: red;
  921. }
  922. .b {
  923. text-transform: uppercase;
  924. color: red;
  925. }
  926. `)
  927. })
  928. })
  929. it('should generate the same output, even if it was used in a @layer', () => {
  930. let config = {
  931. content: [{ raw: html`<div class="a b"></div>` }],
  932. plugins: [],
  933. corePlugins: { preflight: false },
  934. }
  935. let input = css`
  936. @tailwind components;
  937. @layer components {
  938. .a {
  939. @apply b;
  940. }
  941. .b {
  942. @apply uppercase;
  943. color: red;
  944. }
  945. }
  946. `
  947. return run(input, config).then((result) => {
  948. return expect(result.css).toMatchFormattedCss(css`
  949. .a {
  950. text-transform: uppercase;
  951. color: red;
  952. }
  953. .b {
  954. text-transform: uppercase;
  955. color: red;
  956. }
  957. `)
  958. })
  959. })
  960. it('should be possible to apply a combination of multiple "instances" of the same class (defined in a layer)', () => {
  961. let config = {
  962. content: [{ raw: html`<div class="a b"></div>` }],
  963. plugins: [],
  964. corePlugins: { preflight: false },
  965. }
  966. let input = css`
  967. @tailwind components;
  968. @layer components {
  969. .a {
  970. color: red;
  971. @apply b;
  972. color: blue;
  973. }
  974. .b {
  975. @apply text-green-500;
  976. text-decoration: underline;
  977. }
  978. }
  979. `
  980. return run(input, config).then((result) => {
  981. return expect(result.css).toMatchFormattedCss(css`
  982. .a {
  983. color: red;
  984. --tw-text-opacity: 1;
  985. color: rgb(34 197 94 / var(--tw-text-opacity));
  986. text-decoration: underline;
  987. color: blue;
  988. }
  989. .b {
  990. --tw-text-opacity: 1;
  991. color: rgb(34 197 94 / var(--tw-text-opacity));
  992. text-decoration: underline;
  993. }
  994. `)
  995. })
  996. })
  997. it('should properly maintain the order', () => {
  998. let config = {
  999. content: [{ raw: html`` }],
  1000. plugins: [],
  1001. corePlugins: { preflight: false },
  1002. }
  1003. let input = css`
  1004. h2 {
  1005. @apply text-xl;
  1006. @apply lg:text-3xl;
  1007. @apply sm:text-2xl;
  1008. }
  1009. `
  1010. return run(input, config).then((result) => {
  1011. return expect(result.css).toMatchFormattedCss(css`
  1012. h2 {
  1013. font-size: 1.25rem;
  1014. line-height: 1.75rem;
  1015. }
  1016. @media (min-width: 1024px) {
  1017. h2 {
  1018. font-size: 1.875rem;
  1019. line-height: 2.25rem;
  1020. }
  1021. }
  1022. @media (min-width: 640px) {
  1023. h2 {
  1024. font-size: 1.5rem;
  1025. line-height: 2rem;
  1026. }
  1027. }
  1028. `)
  1029. })
  1030. })
  1031. })
  1032. it('apply can emit defaults in isolated environments without @tailwind directives', () => {
  1033. let config = {
  1034. experimental: { optimizeUniversalDefaults: true },
  1035. content: [{ raw: html`<div class="foo"></div>` }],
  1036. }
  1037. let input = css`
  1038. .foo {
  1039. @apply focus:rotate-90;
  1040. }
  1041. `
  1042. return run(input, config).then((result) => {
  1043. return expect(result.css).toMatchFormattedCss(css`
  1044. .foo:focus {
  1045. --tw-rotate: 90deg;
  1046. transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate))
  1047. skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x))
  1048. scaleY(var(--tw-scale-y));
  1049. }
  1050. `)
  1051. })
  1052. })
  1053. it('apply does not emit defaults in isolated environments without optimizeUniversalDefaults', () => {
  1054. let config = {
  1055. experimental: { optimizeUniversalDefaults: false },
  1056. content: [{ raw: html`<div class="foo"></div>` }],
  1057. corePlugins: { preflight: false },
  1058. }
  1059. let input = css`
  1060. @tailwind base;
  1061. .foo {
  1062. @apply focus:rotate-90;
  1063. }
  1064. `
  1065. return run(input, config).then((result) => {
  1066. return expect(result.css).toMatchFormattedCss(css`
  1067. ${defaults}
  1068. .foo:focus {
  1069. --tw-rotate: 90deg;
  1070. transform: translate(var(--tw-translate-x), var(--tw-translate-y)) rotate(var(--tw-rotate))
  1071. skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x))
  1072. scaleY(var(--tw-scale-y));
  1073. }
  1074. `)
  1075. })
  1076. })
  1077. it('should work outside of layer', async () => {
  1078. let config = {
  1079. content: [{ raw: html`<div class="input-text"></div>` }],
  1080. corePlugins: { preflight: false },
  1081. }
  1082. let input = css`
  1083. .input-text {
  1084. @apply bg-white;
  1085. background-color: red;
  1086. }
  1087. `
  1088. let result
  1089. result = await run(input, config)
  1090. expect(result.css).toMatchFormattedCss(css`
  1091. .input-text {
  1092. --tw-bg-opacity: 1;
  1093. background-color: rgb(255 255 255 / var(--tw-bg-opacity));
  1094. background-color: red;
  1095. }
  1096. `)
  1097. result = await run(input, config)
  1098. expect(result.css).toMatchFormattedCss(css`
  1099. .input-text {
  1100. --tw-bg-opacity: 1;
  1101. background-color: rgb(255 255 255 / var(--tw-bg-opacity));
  1102. background-color: red;
  1103. }
  1104. `)
  1105. })
  1106. it('should work in layer', async () => {
  1107. let config = {
  1108. content: [{ raw: html`<div class="input-text"></div>` }],
  1109. corePlugins: { preflight: false },
  1110. }
  1111. let input = css`
  1112. @tailwind components;
  1113. @layer components {
  1114. .input-text {
  1115. @apply bg-white;
  1116. background-color: red;
  1117. }
  1118. }
  1119. `
  1120. await run(input, config)
  1121. const result = await run(input, config)
  1122. expect(result.css).toMatchFormattedCss(css`
  1123. .input-text {
  1124. --tw-bg-opacity: 1;
  1125. background-color: rgb(255 255 255 / var(--tw-bg-opacity));
  1126. background-color: red;
  1127. }
  1128. `)
  1129. })
  1130. it('apply partitioning works with media queries', async () => {
  1131. let config = {
  1132. content: [{ raw: html`` }],
  1133. corePlugins: { preflight: false },
  1134. }
  1135. let input = css`
  1136. @tailwind base;
  1137. @layer base {
  1138. html,
  1139. body {
  1140. @apply text-green-600;
  1141. font-size: 1rem;
  1142. }
  1143. @media print {
  1144. html,
  1145. body {
  1146. @apply text-red-600;
  1147. font-size: 2rem;
  1148. }
  1149. }
  1150. }
  1151. `
  1152. await run(input, config)
  1153. const result = await run(input, config)
  1154. expect(result.css).toMatchFormattedCss(css`
  1155. html,
  1156. body {
  1157. --tw-text-opacity: 1;
  1158. color: rgb(22 163 74 / var(--tw-text-opacity));
  1159. font-size: 1rem;
  1160. }
  1161. @media print {
  1162. html,
  1163. body {
  1164. --tw-text-opacity: 1;
  1165. color: rgb(220 38 38 / var(--tw-text-opacity));
  1166. font-size: 2rem;
  1167. }
  1168. }
  1169. ${defaults}
  1170. `)
  1171. })
  1172. it('should be possible to use apply in plugins', async () => {
  1173. let config = {
  1174. content: [{ raw: html`<div class="a b"></div>` }],
  1175. corePlugins: { preflight: false },
  1176. plugins: [
  1177. function ({ addComponents }) {
  1178. addComponents({
  1179. '.a': {
  1180. color: 'red',
  1181. },
  1182. '.b': {
  1183. '@apply a': {},
  1184. color: 'blue',
  1185. },
  1186. })
  1187. },
  1188. ],
  1189. }
  1190. return run('@tailwind components', config).then((result) => {
  1191. expect(result.css).toMatchFormattedCss(css`
  1192. .a {
  1193. color: red;
  1194. }
  1195. .b {
  1196. color: red;
  1197. color: blue;
  1198. }
  1199. `)
  1200. })
  1201. })
  1202. it('should apply using the updated user CSS when the source has changed', async () => {
  1203. let config = {
  1204. content: [{ raw: html`<div></div>` }],
  1205. plugins: [],
  1206. }
  1207. let inputBefore = css`
  1208. .foo {
  1209. color: green;
  1210. }
  1211. .bar {
  1212. @apply foo;
  1213. }
  1214. `
  1215. let inputAfter = css`
  1216. .foo {
  1217. color: red;
  1218. }
  1219. .bar {
  1220. @apply foo;
  1221. }
  1222. `
  1223. let result = await run(inputBefore, config)
  1224. expect(result.css).toMatchFormattedCss(css`
  1225. .foo {
  1226. color: green;
  1227. }
  1228. .bar {
  1229. color: green;
  1230. }
  1231. `)
  1232. result = await run(inputAfter, config)
  1233. expect(result.css).toMatchFormattedCss(css`
  1234. .foo {
  1235. color: red;
  1236. }
  1237. .bar {
  1238. color: red;
  1239. }
  1240. `)
  1241. })
  1242. it('apply + layer utilities + selector variants (like group) + important selector', async () => {
  1243. let config = {
  1244. important: '#myselector',
  1245. content: [{ raw: html`<div class="custom-utility"></div>` }],
  1246. plugins: [],
  1247. }
  1248. let input = css`
  1249. @tailwind utilities;
  1250. @layer utilities {
  1251. .custom-utility {
  1252. @apply font-normal group-hover:underline;
  1253. }
  1254. }
  1255. `
  1256. let result = await run(input, config)
  1257. expect(result.css).toMatchFormattedCss(css`
  1258. #myselector .custom-utility {
  1259. font-weight: 400;
  1260. }
  1261. #myselector .group:hover .custom-utility {
  1262. text-decoration-line: underline;
  1263. }
  1264. `)
  1265. })
  1266. it('apply + user CSS + selector variants (like group) + important selector (1)', async () => {
  1267. let config = {
  1268. important: '#myselector',
  1269. content: [{ raw: html`<div class="custom-utility"></div>` }],
  1270. plugins: [],
  1271. }
  1272. let input = css`
  1273. .custom-utility {
  1274. @apply font-normal group-hover:underline;
  1275. }
  1276. `
  1277. let result = await run(input, config)
  1278. expect(result.css).toMatchFormattedCss(css`
  1279. .custom-utility {
  1280. font-weight: 400;
  1281. }
  1282. .group:hover .custom-utility {
  1283. text-decoration-line: underline;
  1284. }
  1285. `)
  1286. })
  1287. it('apply + user CSS + selector variants (like group) + important selector (2)', async () => {
  1288. let config = {
  1289. important: '#myselector',
  1290. content: [{ raw: html`<div class="custom-utility"></div>` }],
  1291. plugins: [],
  1292. }
  1293. let input = css`
  1294. #myselector .custom-utility {
  1295. @apply font-normal group-hover:underline;
  1296. }
  1297. `
  1298. let result = await run(input, config)
  1299. expect(result.css).toMatchFormattedCss(css`
  1300. #myselector .custom-utility {
  1301. font-weight: 400;
  1302. }
  1303. .group:hover #myselector .custom-utility {
  1304. text-decoration-line: underline;
  1305. }
  1306. `)
  1307. })
  1308. it('can apply user utilities that start with a dash', async () => {
  1309. let config = {
  1310. content: [{ raw: html`<div class="foo-1 -foo-1 new-class"></div>` }],
  1311. plugins: [],
  1312. }
  1313. let input = css`
  1314. @tailwind utilities;
  1315. @layer utilities {
  1316. .foo-1 {
  1317. margin: 10px;
  1318. }
  1319. .-foo-1 {
  1320. margin: -15px;
  1321. }
  1322. .new-class {
  1323. @apply -foo-1;
  1324. }
  1325. }
  1326. `
  1327. let result = await run(input, config)
  1328. expect(result.css).toMatchFormattedCss(css`
  1329. .foo-1 {
  1330. margin: 10px;
  1331. }
  1332. .-foo-1 {
  1333. margin: -15px;
  1334. }
  1335. .new-class {
  1336. margin: -15px;
  1337. }
  1338. `)
  1339. })
  1340. it('can apply joined classes when using elements', async () => {
  1341. let config = {
  1342. content: [{ raw: html`<div class="foo-1 -foo-1 new-class"></div>` }],
  1343. plugins: [],
  1344. }
  1345. let input = css`
  1346. .foo.bar {
  1347. color: red;
  1348. }
  1349. .bar.foo {
  1350. color: green;
  1351. }
  1352. header:nth-of-type(odd) {
  1353. @apply foo;
  1354. }
  1355. main {
  1356. @apply foo bar;
  1357. }
  1358. footer {
  1359. @apply bar;
  1360. }
  1361. `
  1362. let result = await run(input, config)
  1363. expect(result.css).toMatchFormattedCss(css`
  1364. .foo.bar {
  1365. color: red;
  1366. }
  1367. .bar.foo {
  1368. color: green;
  1369. }
  1370. header.bar:nth-of-type(odd) {
  1371. color: red;
  1372. color: green;
  1373. }
  1374. main.bar {
  1375. color: red;
  1376. }
  1377. main.foo {
  1378. color: red;
  1379. }
  1380. main.bar {
  1381. color: green;
  1382. }
  1383. main.foo {
  1384. color: green;
  1385. }
  1386. footer.foo {
  1387. color: red;
  1388. color: green;
  1389. }
  1390. `)
  1391. })
  1392. it('should not replace multiple instances of the same class in a single selector', async () => {
  1393. // NOTE: This test is non-normative and is not part of the spec of how `@apply` works per-se
  1394. // It describes how it currently works because the "correct" way produces a combinatorial explosion
  1395. // of selectors that is not easily doable
  1396. let config = {
  1397. content: [{ raw: html`<div class="foo-1 -foo-1 new-class"></div>` }],
  1398. plugins: [],
  1399. }
  1400. let input = css`
  1401. .foo + .foo {
  1402. color: blue;
  1403. }
  1404. .bar + .bar {
  1405. color: fuchsia;
  1406. }
  1407. header {
  1408. @apply foo;
  1409. }
  1410. main {
  1411. @apply foo bar;
  1412. }
  1413. footer {
  1414. @apply bar;
  1415. }
  1416. `
  1417. let result = await run(input, config)
  1418. expect(result.css).toMatchFormattedCss(css`
  1419. .foo + .foo {
  1420. color: blue;
  1421. }
  1422. .bar + .bar {
  1423. color: fuchsia;
  1424. }
  1425. header + .foo {
  1426. color: blue;
  1427. }
  1428. main + .foo {
  1429. color: blue;
  1430. }
  1431. main + .bar {
  1432. color: fuchsia;
  1433. }
  1434. footer + .bar {
  1435. color: fuchsia;
  1436. }
  1437. `)
  1438. })