Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Add support for wildcard utility matching #176

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/corePlugins/padding.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const { asValue, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
module.exports = function ({ matchUtilities, matchWildcards, jit: { theme } }) {
matchWildcards({
p: Object.keys(theme['padding']),
px: Object.keys(theme['padding']),
py: Object.keys(theme['padding']),
pt: Object.keys(theme['padding']),
pr: Object.keys(theme['padding']),
pb: Object.keys(theme['padding']),
pl: Object.keys(theme['padding']),
})

matchUtilities({
p: (modifier, { theme }) => {
let value = asValue(modifier, theme['padding'])
Expand Down
14 changes: 13 additions & 1 deletion src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function* resolveMatchedPlugins(classCandidate, context) {
yield [context.candidateRuleMap.get(classCandidate), 'DEFAULT']
}

let wildcards = /^\{\*\}$/g
let candidatePrefix = classCandidate
let negative = false

Expand All @@ -184,8 +185,19 @@ function* resolveMatchedPlugins(classCandidate, context) {
}

for (let [prefix, modifier] of candidatePermutations(candidatePrefix)) {
let modifiers = [modifier]

if (context.candidateRuleMap.has(prefix)) {
yield [context.candidateRuleMap.get(prefix), negative ? `-${modifier}` : modifier]
let rules = context.candidateRuleMap.get(prefix)

if (wildcards.test(modifier) && context.wildcardModifierList.has(prefix)) {
modifiers = context.wildcardModifierList.get(prefix)
}

for (const modifier of modifiers) {
yield [rules, negative ? `-${modifier}` : modifier]
}

return
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/setupContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,13 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
context.candidateRuleMap.get(prefixedIdentifier).push(...withOffsets)
}
},

matchWildcards: function (modifierMap) {
for (const [prefix, modifiers] of Object.entries(modifierMap)) {
context.wildcardModifierList.set(prefix, modifiers)
}
},

matchUtilities: function (utilities, options) {
let defaultOptions = {
variants: [],
Expand Down Expand Up @@ -773,6 +780,7 @@ function setupContext(configOrPath) {
variantMap: new Map(),
stylesheetCache: null,
fileModifiedMap: new Map(),
wildcardModifierList: new Map(),
}

// ---
Expand Down
108 changes: 108 additions & 0 deletions tests/wildcards.test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
.p-0 {
padding: 0px;
}
.p-1 {
padding: 0.25rem;
}
.p-2 {
padding: 0.5rem;
}
.p-3 {
padding: 0.75rem;
}
.p-4 {
padding: 1rem;
}
.p-5 {
padding: 1.25rem;
}
.p-6 {
padding: 1.5rem;
}
.p-7 {
padding: 1.75rem;
}
.p-8 {
padding: 2rem;
}
.p-9 {
padding: 2.25rem;
}
.p-10 {
padding: 2.5rem;
}
.p-11 {
padding: 2.75rem;
}
.p-12 {
padding: 3rem;
}
.p-14 {
padding: 3.5rem;
}
.p-16 {
padding: 4rem;
}
.p-20 {
padding: 5rem;
}
.p-24 {
padding: 6rem;
}
.p-28 {
padding: 7rem;
}
.p-32 {
padding: 8rem;
}
.p-36 {
padding: 9rem;
}
.p-40 {
padding: 10rem;
}
.p-44 {
padding: 11rem;
}
.p-48 {
padding: 12rem;
}
.p-52 {
padding: 13rem;
}
.p-56 {
padding: 14rem;
}
.p-60 {
padding: 15rem;
}
.p-64 {
padding: 16rem;
}
.p-72 {
padding: 18rem;
}
.p-80 {
padding: 20rem;
}
.p-96 {
padding: 24rem;
}
.p-px {
padding: 1px;
}
.p-0\.5 {
padding: 0.125rem;
}
.p-1\.5 {
padding: 0.375rem;
}
.p-2\.5 {
padding: 0.625rem;
}
.p-3\.5 {
padding: 0.875rem;
}
.p-4 {
padding: 1rem;
}
14 changes: 14 additions & 0 deletions tests/wildcards.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
<link rel="stylesheet" href="./tailwind.css" />
</head>
<body>
<div class="p-{*}"></div>
<div class="p-4"></div>
</body>
</html>
29 changes: 29 additions & 0 deletions tests/wildcards.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const postcss = require('postcss')
const tailwind = require('../src/index.js')
const fs = require('fs')
const path = require('path')

function run(input, config = {}) {
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
}

test('wildcards', () => {
let config = {
darkMode: 'class',
purge: [path.resolve(__dirname, './wildcards.test.html')],
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

let css = `
@tailwind utilities;
`

return run(css, config).then((result) => {
let expectedPath = path.resolve(__dirname, './wildcards.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')

expect(result.css).toMatchCss(expected)
})
})