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

Commit d8209e6

Browse files
committed
WIP
1 parent 836ae9a commit d8209e6

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

src/lib/generateRules.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ function applyVariant(variant, matches, { variantMap }) {
3737
let result = []
3838

3939
for (let [{ sort, layer }, rule] of matches) {
40-
let [, , options = {}] = rule
40+
// TODO: Support these options again
41+
// let [, , options = {}] = rule
4142

42-
if (options.respectVariants === false) {
43-
result.push([{ sort, layer }, rule])
44-
continue
45-
}
43+
// if (options.respectVariants === false) {
44+
// result.push([{ sort, layer }, rule])
45+
// continue
46+
// }
4647

4748
let ruleWithVariant = applyThisVariant(rule)
4849

@@ -109,13 +110,16 @@ function generateRules(tailwindConfig, candidates, context) {
109110
let matches = []
110111
let [plugins, modifier] = matchedPlugins
111112

113+
// console.log(plugins, modifier)
114+
112115
for (let [sort, plugin] of plugins) {
113-
if (Array.isArray(plugin)) {
114-
matches.push([sort, plugin])
115-
} else {
116+
console.log(plugin)
117+
if (typeof plugin === 'function') {
116118
for (let result of plugin(modifier, pluginHelpers)) {
117119
matches.push([sort, result])
118120
}
121+
} else {
122+
matches.push([sort, plugin])
119123
}
120124
}
121125

@@ -127,12 +131,7 @@ function generateRules(tailwindConfig, candidates, context) {
127131
allRules.push(matches)
128132
}
129133

130-
return allRules
131-
.flat(1)
132-
.map(([{ sort, layer }, rule]) => [
133-
sort | context.layerOrder[layer],
134-
toPostCssNode(rule, postCssNodeCache),
135-
])
134+
return allRules.flat(1).map(([{ sort, layer }, rule]) => [sort | context.layerOrder[layer], rule])
136135
}
137136

138137
module.exports = generateRules

src/lib/setupContext.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,25 @@ function extractCandidates(node) {
327327
return classes
328328
}
329329

330+
function withIdentifiers(styles) {
331+
return parseLegacyStyles(styles).flatMap((node) => {
332+
let nodeMap = new Map()
333+
let candidates = extractCandidates(node)
334+
335+
// If this isn't "on-demandable", assign it a universal candidate.
336+
if (candidates.length === 0) {
337+
return [['*', node]]
338+
}
339+
340+
return candidates.map((c) => {
341+
if (!nodeMap.has(node)) {
342+
nodeMap.set(node, node)
343+
}
344+
return [c, nodeMap.get(node)]
345+
})
346+
})
347+
}
348+
330349
// { .foo { color: black } }
331350
// => [ ['foo', ['.foo', { color: 'black' }] ]
332351
function toStaticRuleArray(legacyStyles) {
@@ -431,15 +450,25 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
431450
Array.isArray(options) ? { variants: options } : options
432451
)
433452

434-
for (let [identifier, tuple] of toStaticRuleArray(utilities)) {
453+
for (let [identifier, node] of withIdentifiers(utilities)) {
435454
let offset = offsets.utilities++
436455

437456
if (!context.candidateRuleMap.has(identifier)) {
438457
context.candidateRuleMap.set(identifier, [])
439458
}
440459

441-
context.candidateRuleMap.get(identifier).push([{ sort: offset, layer: 'utilities' }, tuple])
460+
context.candidateRuleMap.get(identifier).push([{ sort: offset, layer: 'utilities' }, node])
442461
}
462+
463+
// for (let [identifier, tuple] of toStaticRuleArray(utilities)) {
464+
// let offset = offsets.utilities++
465+
466+
// if (!context.candidateRuleMap.has(identifier)) {
467+
// context.candidateRuleMap.set(identifier, [])
468+
// }
469+
470+
// context.candidateRuleMap.get(identifier).push([{ sort: offset, layer: 'utilities' }, tuple])
471+
// }
443472
},
444473
// ---
445474
jit: {
@@ -488,6 +517,7 @@ function registerPlugins(tailwindConfig, plugins, context) {
488517
let variantList = []
489518
let variantMap = new Map()
490519
let offsets = {
520+
base: 0n,
491521
components: 0n,
492522
utilities: 0n,
493523
}
@@ -508,8 +538,11 @@ function registerPlugins(tailwindConfig, plugins, context) {
508538
}
509539
}
510540

511-
let highestOffset =
512-
offsets.utilities > offsets.components ? offsets.utilities : offsets.components
541+
let highestOffset = ((...args) => args.reduce((m, e) => (e > m ? e : m)))([
542+
offsets.base,
543+
offsets.components,
544+
offsets.utilities,
545+
])
513546
let reservedBits = BigInt(highestOffset.toString(2).length)
514547

515548
context.layerOrder = {

src/pluginUtils.test.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
const postcss = require('postcss')
12
const { updateLastClasses, updateAllClasses, transformRule } = require('./pluginUtils')
23

34
test('transforming simple class rule', () => {
4-
let simpleClass = [
5-
'.bg-black',
6-
{
7-
'background-color': '#000',
8-
},
9-
]
5+
// let simpleClass = [
6+
// '.bg-black',
7+
// {
8+
// 'background-color': '#000',
9+
// },
10+
// ]
11+
12+
// TODO: Make this work
13+
let simpleClass = postcss.rule({
14+
selector: '.bg-black',
15+
nodes: [postcss.decl({ prop: 'background-color', value: '#000' })],
16+
})
17+
18+
console.log(simpleClass)
1019

1120
expect(transformRule(simpleClass, ([sel, rules]) => ['.transformed', rules])).toEqual([
1221
'.transformed',

0 commit comments

Comments
 (0)