11import type { UnoGenerator } from '@unocss/core'
2+ import type { IconsOptions } from '@unocss/preset-icons'
23import { readFile } from 'node:fs/promises'
3- import { createGenerator } from '@unocss/core'
4+ import { createGenerator , mergeDeep } from '@unocss/core'
45import presetIcons from '@unocss/preset-icons'
56import presetUno from '@unocss/preset-uno'
67import MagicString from 'magic-string'
@@ -1361,19 +1362,24 @@ div {
13611362} )
13621363
13631364describe ( 'icon directive' , ( ) => {
1364- const uno = createGenerator ( {
1365- presets : [
1366- presetIcons ( {
1367- collections : {
1368- ph : {
1369- check : `<svg xmlns="http://www.w3.org/2000/svg" fill="currentcolor" viewBox="0 0 24 24"><path d="ph:check"/></svg>` ,
1370- } ,
1365+ function createUno ( iconsOptions ?: IconsOptions ) {
1366+ const defaultOptions = {
1367+ collections : {
1368+ ph : {
1369+ check : `<svg xmlns="http://www.w3.org/2000/svg" fill="currentcolor" viewBox="0 0 24 24"><path d="ph:check"/></svg>` ,
13711370 } ,
1372- } ) ,
1373- ] ,
1374- } )
1371+ } ,
1372+ }
13751373
1376- async function transform ( code : string , _uno : UnoGenerator = uno ) {
1374+ return createGenerator ( {
1375+ presets : [
1376+ presetUno ( ) ,
1377+ presetIcons ( mergeDeep ( defaultOptions , iconsOptions ?? { } ) ) ,
1378+ ] ,
1379+ } )
1380+ }
1381+
1382+ async function transform ( code : string , _uno : UnoGenerator ) {
13771383 const s = new MagicString ( code )
13781384 await transformDirectives ( s , _uno , { } )
13791385 return prettier . format ( s . toString ( ) , {
@@ -1383,24 +1389,46 @@ describe('icon directive', () => {
13831389 }
13841390
13851391 it ( 'icon()' , async ( ) => {
1392+ const uno = createUno ( )
1393+
13861394 const result = await transform (
13871395 `.icon {
13881396 background-image: icon('i-ph-check');
13891397 background-image: icon('i-ph:check', '#fff') no-repeat;
1398+ background-image: icon('i-ph:check', 'theme("colors.red.500")');
13901399 background-image: icon('i-carbon-sun');
1391- background-image: icon('i-carbon:moon', '#fff');
13921400 }` ,
1401+ uno ,
13931402 )
13941403
13951404 expect ( result ) . toMatchInlineSnapshot ( `
13961405 ".icon {
13971406 background-image: url("data:image/svg+xml;utf8,%3Csvg width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' fill='currentcolor' viewBox='0 0 24 24'%3E%3Cpath d='ph:check'/%3E%3C/svg%3E");
1398- background-image: url("data:image/svg+xml;utf8,%3Csvg width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' fill='#fff ' viewBox='0 0 24 24'%3E%3Cpath d='ph:check'/%3E%3C/svg%3E")
1407+ background-image: url("data:image/svg+xml;utf8,%3Csvg width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' fill='%23fff ' viewBox='0 0 24 24'%3E%3Cpath d='ph:check'/%3E%3C/svg%3E")
13991408 no-repeat;
1409+ background-image: url("data:image/svg+xml;utf8,%3Csvg width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' fill='%23ef4444' viewBox='0 0 24 24'%3E%3Cpath d='ph:check'/%3E%3C/svg%3E");
14001410 background-image: url("data:image/svg+xml;utf8,%3Csvg viewBox='0 0 32 32' width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' %3E%3Cpath fill='currentColor' d='M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6M5.394 6.813L6.81 5.399l3.505 3.506L8.9 10.319zM2 15.005h5v2H2zm3.394 10.193L8.9 21.692l1.414 1.414l-3.505 3.506zM15 25.005h2v5h-2zm6.687-1.9l1.414-1.414l3.506 3.506l-1.414 1.414zm3.313-8.1h5v2h-5zm-3.313-6.101l3.506-3.506l1.414 1.414l-3.506 3.506zM15 2.005h2v5h-2z'/%3E%3C/svg%3E");
1401- background-image: url("data:image/svg+xml;utf8,%3Csvg viewBox='0 0 32 32' width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' %3E%3Cpath fill='#fff' d='M13.503 5.414a15.076 15.076 0 0 0 11.593 18.194a11.1 11.1 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1 1 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.07 13.07 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3'/%3E%3C/svg%3E");
14021411 }
14031412 "
14041413 ` )
14051414 } )
1415+
1416+ it ( 'icon() without extra properties' , async ( ) => {
1417+ const uno = createUno ( {
1418+ extraProperties : {
1419+ 'display' : 'inline-block' ,
1420+ 'vertical-align' : 'middle' ,
1421+ } ,
1422+ } )
1423+
1424+ const result = await transform (
1425+ `.icon {
1426+ background-image: icon('i-ph-check');
1427+ }` ,
1428+ uno ,
1429+ )
1430+
1431+ expect ( result ) . not . toContain ( `display='inline-block'` )
1432+ expect ( result ) . not . toContain ( `vertical-align='middle'` )
1433+ } )
14061434} )
0 commit comments