Skip to content

Commit 82942e7

Browse files
committed
Very basic AOT matchUtilities support
1 parent bfc6162 commit 82942e7

File tree

2 files changed

+105
-23
lines changed

2 files changed

+105
-23
lines changed

src/util/processPlugins.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ export default function (plugins, config) {
4141
return prefixSelector(config.prefix, selector)
4242
}
4343

44+
function addUtilities(utilities, options) {
45+
const defaultOptions = { variants: [], respectPrefix: true, respectImportant: true }
46+
47+
options = Array.isArray(options)
48+
? Object.assign({}, defaultOptions, { variants: options })
49+
: _.defaults(options, defaultOptions)
50+
51+
const styles = postcss.root({ nodes: parseStyles(utilities) })
52+
53+
styles.walkRules((rule) => {
54+
if (options.respectPrefix && !isKeyframeRule(rule)) {
55+
rule.selector = applyConfiguredPrefix(rule.selector)
56+
}
57+
58+
if (options.respectImportant && config.important) {
59+
rule.__tailwind = {
60+
...rule.__tailwind,
61+
important: config.important,
62+
}
63+
}
64+
})
65+
66+
pluginUtilities.push(
67+
wrapWithLayer(wrapWithVariants(styles.nodes, options.variants), 'utilities')
68+
)
69+
}
70+
4471
const getConfigValue = (path, defaultValue) => (path ? _.get(config, path, defaultValue) : config)
4572

4673
plugins.forEach((plugin) => {
@@ -75,31 +102,17 @@ export default function (plugins, config) {
75102
},
76103
e: escapeClassName,
77104
prefix: applyConfiguredPrefix,
78-
addUtilities: (utilities, options) => {
79-
const defaultOptions = { variants: [], respectPrefix: true, respectImportant: true }
80-
81-
options = Array.isArray(options)
82-
? Object.assign({}, defaultOptions, { variants: options })
83-
: _.defaults(options, defaultOptions)
84-
85-
const styles = postcss.root({ nodes: parseStyles(utilities) })
86-
87-
styles.walkRules((rule) => {
88-
if (options.respectPrefix && !isKeyframeRule(rule)) {
89-
rule.selector = applyConfiguredPrefix(rule.selector)
90-
}
91-
92-
if (options.respectImportant && config.important) {
93-
rule.__tailwind = {
94-
...rule.__tailwind,
95-
important: config.important,
96-
}
97-
}
105+
addUtilities,
106+
matchUtilities: (matches, { values, variants, respectPrefix, respectImportant }) => {
107+
let modifierValues = Object.entries(values)
108+
109+
let result = Object.values(matches).flatMap((utilityFunction) => {
110+
return modifierValues.map(([modifier, value]) => {
111+
return utilityFunction(modifier, { value })
112+
})
98113
})
99114

100-
pluginUtilities.push(
101-
wrapWithLayer(wrapWithVariants(styles.nodes, options.variants), 'utilities')
102-
)
115+
addUtilities(result, { variants, respectPrefix, respectImportant })
103116
},
104117
addComponents: (components, options) => {
105118
const defaultOptions = { variants: [], respectPrefix: true }

tests/processPlugins.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,3 +2483,72 @@ test('animation values are joined when retrieved using the theme function', () =
24832483
}
24842484
`)
24852485
})
2486+
2487+
test('plugins can add utilities using matchUtilities in AOT mode', () => {
2488+
const { components, utilities } = processPlugins(
2489+
[
2490+
function ({ matchUtilities, theme, variants }) {
2491+
matchUtilities(
2492+
{
2493+
'flex-grow': (modifier, { value }) => {
2494+
return {
2495+
[`.flex-grow-${modifier}`]: {
2496+
'flex-grow': value,
2497+
},
2498+
}
2499+
},
2500+
'flex-shrink': (modifier, { value }) => {
2501+
return {
2502+
[`.flex-shrink-${modifier}`]: {
2503+
'flex-shrink': value,
2504+
},
2505+
}
2506+
},
2507+
},
2508+
{
2509+
values: theme('flexyPants'),
2510+
variants: variants('flexyPants'),
2511+
}
2512+
)
2513+
},
2514+
],
2515+
makeConfig({
2516+
theme: {
2517+
flexyPants: {
2518+
0: '0',
2519+
1: '1',
2520+
2: '2',
2521+
},
2522+
},
2523+
variants: {
2524+
flexyPants: ['responsive', 'hover', 'focus'],
2525+
},
2526+
})
2527+
)
2528+
2529+
expect(components.length).toBe(0)
2530+
expect(css(utilities)).toMatchCss(`
2531+
@layer utilities {
2532+
@variants responsive, hover, focus {
2533+
.flex-grow-0 {
2534+
flex-grow: 0
2535+
}
2536+
.flex-grow-1 {
2537+
flex-grow: 1
2538+
}
2539+
.flex-grow-2 {
2540+
flex-grow: 2
2541+
}
2542+
.flex-shrink-0 {
2543+
flex-shrink: 0
2544+
}
2545+
.flex-shrink-1 {
2546+
flex-shrink: 1
2547+
}
2548+
.flex-shrink-2 {
2549+
flex-shrink: 2
2550+
}
2551+
}
2552+
}
2553+
`)
2554+
})

0 commit comments

Comments
 (0)