tailwindcss 0.0.0-insiders.ba2fd32 → 0.0.0-insiders.bbafc8e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/CHANGELOG.md +292 -3
  2. package/LICENSE +1 -2
  3. package/README.md +8 -4
  4. package/colors.js +2 -1
  5. package/defaultConfig.js +2 -1
  6. package/defaultTheme.js +2 -1
  7. package/lib/cli.js +96 -61
  8. package/lib/constants.js +1 -1
  9. package/lib/corePluginList.js +4 -0
  10. package/lib/corePlugins.js +399 -374
  11. package/lib/css/preflight.css +6 -5
  12. package/lib/featureFlags.js +7 -8
  13. package/lib/index.js +12 -3
  14. package/lib/lib/cacheInvalidation.js +69 -0
  15. package/lib/lib/collapseAdjacentRules.js +14 -1
  16. package/lib/lib/collapseDuplicateDeclarations.js +54 -3
  17. package/lib/lib/defaultExtractor.js +44 -0
  18. package/lib/lib/detectNesting.js +17 -2
  19. package/lib/lib/evaluateTailwindFunctions.js +10 -6
  20. package/lib/lib/expandApplyAtRules.js +264 -59
  21. package/lib/lib/expandTailwindAtRules.js +63 -46
  22. package/lib/lib/generateRules.js +181 -43
  23. package/lib/lib/normalizeTailwindDirectives.js +10 -2
  24. package/lib/lib/partitionApplyAtRules.js +53 -0
  25. package/lib/lib/resolveDefaultsAtRules.js +32 -14
  26. package/lib/lib/setupContextUtils.js +152 -153
  27. package/lib/lib/setupTrackingContext.js +8 -7
  28. package/lib/lib/sharedState.js +38 -5
  29. package/lib/lib/substituteScreenAtRules.js +7 -4
  30. package/{nesting → lib/postcss-plugins/nesting}/README.md +0 -0
  31. package/lib/postcss-plugins/nesting/index.js +17 -0
  32. package/lib/postcss-plugins/nesting/plugin.js +85 -0
  33. package/lib/processTailwindFeatures.js +10 -2
  34. package/lib/util/buildMediaQuery.js +13 -24
  35. package/lib/util/cloneNodes.js +12 -1
  36. package/lib/util/color.js +23 -8
  37. package/lib/util/createPlugin.js +1 -2
  38. package/lib/util/createUtilityPlugin.js +6 -10
  39. package/lib/util/dataTypes.js +25 -3
  40. package/lib/util/defaults.js +6 -0
  41. package/lib/util/flattenColorPalette.js +1 -3
  42. package/lib/util/formatVariantSelector.js +93 -9
  43. package/lib/util/isValidArbitraryValue.js +64 -0
  44. package/lib/util/log.js +10 -6
  45. package/lib/util/nameClass.js +1 -0
  46. package/lib/util/normalizeConfig.js +54 -22
  47. package/lib/util/normalizeScreens.js +59 -0
  48. package/lib/util/parseBoxShadowValue.js +117 -0
  49. package/lib/util/pluginUtils.js +18 -24
  50. package/lib/util/prefixSelector.js +4 -5
  51. package/lib/util/resolveConfig.js +52 -31
  52. package/lib/util/resolveConfigPath.js +1 -2
  53. package/lib/util/toColorValue.js +1 -2
  54. package/lib/util/toPath.js +6 -1
  55. package/lib/util/transformThemeValue.js +4 -8
  56. package/nesting/index.js +2 -12
  57. package/package.json +28 -33
  58. package/peers/index.js +6970 -7268
  59. package/plugin.js +2 -1
  60. package/resolveConfig.js +2 -1
  61. package/src/cli.js +62 -15
  62. package/src/corePluginList.js +1 -1
  63. package/src/corePlugins.js +329 -255
  64. package/src/css/preflight.css +6 -5
  65. package/src/featureFlags.js +3 -3
  66. package/src/index.js +14 -6
  67. package/src/lib/cacheInvalidation.js +52 -0
  68. package/src/lib/collapseAdjacentRules.js +16 -1
  69. package/src/lib/collapseDuplicateDeclarations.js +66 -1
  70. package/src/lib/defaultExtractor.js +50 -0
  71. package/src/lib/detectNesting.js +22 -3
  72. package/src/lib/evaluateTailwindFunctions.js +6 -3
  73. package/src/lib/expandApplyAtRules.js +285 -56
  74. package/src/lib/expandTailwindAtRules.js +75 -35
  75. package/src/lib/generateRules.js +154 -26
  76. package/src/lib/normalizeTailwindDirectives.js +7 -1
  77. package/src/lib/partitionApplyAtRules.js +52 -0
  78. package/src/lib/resolveDefaultsAtRules.js +35 -10
  79. package/src/lib/setupContextUtils.js +133 -103
  80. package/src/lib/setupTrackingContext.js +8 -7
  81. package/src/lib/sharedState.js +42 -4
  82. package/src/lib/substituteScreenAtRules.js +6 -3
  83. package/src/postcss-plugins/nesting/README.md +42 -0
  84. package/src/postcss-plugins/nesting/index.js +13 -0
  85. package/src/postcss-plugins/nesting/plugin.js +80 -0
  86. package/src/processTailwindFeatures.js +12 -2
  87. package/src/util/buildMediaQuery.js +14 -18
  88. package/src/util/cloneNodes.js +14 -1
  89. package/src/util/color.js +20 -7
  90. package/src/util/dataTypes.js +24 -6
  91. package/src/util/defaults.js +6 -0
  92. package/src/util/formatVariantSelector.js +92 -1
  93. package/src/util/isValidArbitraryValue.js +61 -0
  94. package/src/util/log.js +10 -6
  95. package/src/util/nameClass.js +1 -1
  96. package/src/util/normalizeConfig.js +24 -3
  97. package/src/util/normalizeScreens.js +45 -0
  98. package/src/util/parseBoxShadowValue.js +119 -0
  99. package/src/util/pluginUtils.js +3 -1
  100. package/src/util/prefixSelector.js +7 -5
  101. package/src/util/resolveConfig.js +41 -1
  102. package/src/util/toPath.js +23 -1
  103. package/stubs/defaultConfig.stub.js +27 -11
  104. package/lib/lib/setupWatchingContext.js +0 -288
  105. package/nesting/plugin.js +0 -41
  106. package/src/lib/setupWatchingContext.js +0 -311
@@ -5,7 +5,11 @@ import isPlainObject from '../util/isPlainObject'
5
5
  import prefixSelector from '../util/prefixSelector'
6
6
  import { updateAllClasses } from '../util/pluginUtils'
7
7
  import log from '../util/log'
8
+ import * as sharedState from './sharedState'
8
9
  import { formatVariantSelector, finalizeSelector } from '../util/formatVariantSelector'
10
+ import { asClass } from '../util/nameClass'
11
+ import { normalize } from '../util/dataTypes'
12
+ import isValidArbitraryValue from '../util/isValidArbitraryValue'
9
13
 
10
14
  let classNameParser = selectorParser((selectors) => {
11
15
  return selectors.first.filter(({ type }) => type === 'class').pop().value
@@ -22,33 +26,33 @@ function getClassNameFromSelector(selector) {
22
26
  // Example with dynamic classes:
23
27
  // ['grid-cols', '[[linename],1fr,auto]']
24
28
  // ['grid', 'cols-[[linename],1fr,auto]']
25
- function* candidatePermutations(candidate, lastIndex = Infinity) {
26
- if (lastIndex < 0) {
27
- return
28
- }
29
+ function* candidatePermutations(candidate) {
30
+ let lastIndex = Infinity
29
31
 
30
- let dashIdx
32
+ while (lastIndex >= 0) {
33
+ let dashIdx
31
34
 
32
- if (lastIndex === Infinity && candidate.endsWith(']')) {
33
- let bracketIdx = candidate.indexOf('[')
35
+ if (lastIndex === Infinity && candidate.endsWith(']')) {
36
+ let bracketIdx = candidate.indexOf('[')
34
37
 
35
- // If character before `[` isn't a dash or a slash, this isn't a dynamic class
36
- // eg. string[]
37
- dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
38
- } else {
39
- dashIdx = candidate.lastIndexOf('-', lastIndex)
40
- }
38
+ // If character before `[` isn't a dash or a slash, this isn't a dynamic class
39
+ // eg. string[]
40
+ dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
41
+ } else {
42
+ dashIdx = candidate.lastIndexOf('-', lastIndex)
43
+ }
41
44
 
42
- if (dashIdx < 0) {
43
- return
44
- }
45
+ if (dashIdx < 0) {
46
+ break
47
+ }
45
48
 
46
- let prefix = candidate.slice(0, dashIdx)
47
- let modifier = candidate.slice(dashIdx + 1)
49
+ let prefix = candidate.slice(0, dashIdx)
50
+ let modifier = candidate.slice(dashIdx + 1)
48
51
 
49
- yield [prefix, modifier]
52
+ yield [prefix, modifier]
50
53
 
51
- yield* candidatePermutations(candidate, dashIdx - 1)
54
+ lastIndex = dashIdx - 1
55
+ }
52
56
  }
53
57
 
54
58
  function applyPrefix(matches, context) {
@@ -60,9 +64,23 @@ function applyPrefix(matches, context) {
60
64
  let [meta] = match
61
65
  if (meta.options.respectPrefix) {
62
66
  let container = postcss.root({ nodes: [match[1].clone()] })
67
+ let classCandidate = match[1].raws.tailwind.classCandidate
68
+
63
69
  container.walkRules((r) => {
64
- r.selector = prefixSelector(context.tailwindConfig.prefix, r.selector)
70
+ // If this is a negative utility with a dash *before* the prefix we
71
+ // have to ensure that the generated selector matches the candidate
72
+
73
+ // Not doing this will cause `-tw-top-1` to generate the class `.tw--top-1`
74
+ // The disconnect between candidate <-> class can cause @apply to hard crash.
75
+ let shouldPrependNegative = classCandidate.startsWith('-')
76
+
77
+ r.selector = prefixSelector(
78
+ context.tailwindConfig.prefix,
79
+ r.selector,
80
+ shouldPrependNegative
81
+ )
65
82
  })
83
+
66
84
  match[1] = container.nodes[0]
67
85
  }
68
86
  }
@@ -70,7 +88,7 @@ function applyPrefix(matches, context) {
70
88
  return matches
71
89
  }
72
90
 
73
- function applyImportant(matches) {
91
+ function applyImportant(matches, classCandidate) {
74
92
  if (matches.length === 0) {
75
93
  return matches
76
94
  }
@@ -80,7 +98,10 @@ function applyImportant(matches) {
80
98
  let container = postcss.root({ nodes: [rule.clone()] })
81
99
  container.walkRules((r) => {
82
100
  r.selector = updateAllClasses(r.selector, (className) => {
83
- return `!${className}`
101
+ if (className === classCandidate) {
102
+ return `!${className}`
103
+ }
104
+ return className
84
105
  })
85
106
  r.walkDecls((d) => (d.important = true))
86
107
  })
@@ -109,6 +130,11 @@ function applyVariant(variant, matches, context) {
109
130
  let result = []
110
131
 
111
132
  for (let [meta, rule] of matches) {
133
+ // Don't generate variants for user css
134
+ if (meta.layer === 'user') {
135
+ continue
136
+ }
137
+
112
138
  let container = postcss.root({ nodes: [rule.clone()] })
113
139
 
114
140
  for (let [variantSort, variantFunction] of variantFunctionTuples) {
@@ -208,6 +234,12 @@ function applyVariant(variant, matches, context) {
208
234
  })
209
235
  }
210
236
 
237
+ // This tracks the originating layer for the variant
238
+ // For example:
239
+ // .sm:underline {} is a variant of something in the utilities layer
240
+ // .sm:container {} is a variant of the container component
241
+ clone.nodes[0].raws.tailwind = { ...clone.nodes[0].raws.tailwind, parentLayer: meta.layer }
242
+
211
243
  let withOffset = [
212
244
  {
213
245
  ...meta,
@@ -245,11 +277,92 @@ function parseRules(rule, cache, options = {}) {
245
277
  return [cache.get(rule), options]
246
278
  }
247
279
 
280
+ const IS_VALID_PROPERTY_NAME = /^[a-z_-]/
281
+
282
+ function isValidPropName(name) {
283
+ return IS_VALID_PROPERTY_NAME.test(name)
284
+ }
285
+
286
+ /**
287
+ * @param {string} declaration
288
+ * @returns {boolean}
289
+ */
290
+ function looksLikeUri(declaration) {
291
+ // Quick bailout for obvious non-urls
292
+ // This doesn't support schemes that don't use a leading // but that's unlikely to be a problem
293
+ if (!declaration.includes('://')) {
294
+ return false
295
+ }
296
+
297
+ try {
298
+ const url = new URL(declaration)
299
+ return url.scheme !== '' && url.host !== ''
300
+ } catch (err) {
301
+ // Definitely not a valid url
302
+ return false
303
+ }
304
+ }
305
+
306
+ function isParsableCssValue(property, value) {
307
+ // We don't want to to treat [https://example.com] as a custom property
308
+ // Even though, according to the CSS grammar, it's a totally valid CSS declaration
309
+ // So we short-circuit here by checking if the custom property looks like a url
310
+ if (looksLikeUri(`${property}:${value}`)) {
311
+ return false
312
+ }
313
+
314
+ try {
315
+ postcss.parse(`a{${property}:${value}}`).toResult()
316
+ return true
317
+ } catch (err) {
318
+ return false
319
+ }
320
+ }
321
+
322
+ function extractArbitraryProperty(classCandidate, context) {
323
+ let [, property, value] = classCandidate.match(/^\[([a-zA-Z0-9-_]+):(\S+)\]$/) ?? []
324
+
325
+ if (value === undefined) {
326
+ return null
327
+ }
328
+
329
+ if (!isValidPropName(property)) {
330
+ return null
331
+ }
332
+
333
+ if (!isValidArbitraryValue(value)) {
334
+ return null
335
+ }
336
+
337
+ let normalized = normalize(value)
338
+
339
+ if (!isParsableCssValue(property, normalized)) {
340
+ return null
341
+ }
342
+
343
+ return [
344
+ [
345
+ { sort: context.arbitraryPropertiesSort, layer: 'utilities' },
346
+ () => ({
347
+ [asClass(classCandidate)]: {
348
+ [property]: normalized,
349
+ },
350
+ }),
351
+ ],
352
+ ]
353
+ }
354
+
248
355
  function* resolveMatchedPlugins(classCandidate, context) {
249
356
  if (context.candidateRuleMap.has(classCandidate)) {
250
357
  yield [context.candidateRuleMap.get(classCandidate), 'DEFAULT']
251
358
  }
252
359
 
360
+ yield* (function* (arbitraryPropertyRule) {
361
+ if (arbitraryPropertyRule !== null) {
362
+ yield [arbitraryPropertyRule, 'DEFAULT']
363
+ }
364
+ })(extractArbitraryProperty(classCandidate, context))
365
+
253
366
  let candidatePrefix = classCandidate
254
367
  let negative = false
255
368
 
@@ -268,15 +381,26 @@ function* resolveMatchedPlugins(classCandidate, context) {
268
381
  for (let [prefix, modifier] of candidatePermutations(candidatePrefix)) {
269
382
  if (context.candidateRuleMap.has(prefix)) {
270
383
  yield [context.candidateRuleMap.get(prefix), negative ? `-${modifier}` : modifier]
271
- return
272
384
  }
273
385
  }
274
386
  }
275
387
 
276
388
  function splitWithSeparator(input, separator) {
389
+ if (input === sharedState.NOT_ON_DEMAND) {
390
+ return [sharedState.NOT_ON_DEMAND]
391
+ }
392
+
277
393
  return input.split(new RegExp(`\\${separator}(?![^[]*\\])`, 'g'))
278
394
  }
279
395
 
396
+ function* recordCandidates(matches, classCandidate) {
397
+ for (const match of matches) {
398
+ match[1].raws.tailwind = { ...match[1].raws.tailwind, classCandidate }
399
+
400
+ yield match
401
+ }
402
+ }
403
+
280
404
  function* resolveMatches(candidate, context) {
281
405
  let separator = context.tailwindConfig.separator
282
406
  let [classCandidate, ...variants] = splitWithSeparator(candidate, separator).reverse()
@@ -388,10 +512,12 @@ function* resolveMatches(candidate, context) {
388
512
  continue
389
513
  }
390
514
 
391
- matches = applyPrefix(matches.flat(), context)
515
+ matches = matches.flat()
516
+ matches = Array.from(recordCandidates(matches, classCandidate))
517
+ matches = applyPrefix(matches, context)
392
518
 
393
519
  if (important) {
394
- matches = applyImportant(matches, context)
520
+ matches = applyImportant(matches, classCandidate)
395
521
  }
396
522
 
397
523
  for (let variant of variants) {
@@ -399,6 +525,8 @@ function* resolveMatches(candidate, context) {
399
525
  }
400
526
 
401
527
  for (let match of matches) {
528
+ match[1].raws.tailwind = { ...match[1].raws.tailwind, candidate }
529
+
402
530
  // Apply final format selector
403
531
  if (match[0].collectedFormats) {
404
532
  let finalFormat = formatVariantSelector('&', ...match[0].collectedFormats)
@@ -3,8 +3,13 @@ import log from '../util/log'
3
3
  export default function normalizeTailwindDirectives(root) {
4
4
  let tailwindDirectives = new Set()
5
5
  let layerDirectives = new Set()
6
+ let applyDirectives = new Set()
6
7
 
7
8
  root.walkAtRules((atRule) => {
9
+ if (atRule.name === 'apply') {
10
+ applyDirectives.add(atRule)
11
+ }
12
+
8
13
  if (atRule.name === 'import') {
9
14
  if (atRule.params === '"tailwindcss/base"' || atRule.params === "'tailwindcss/base'") {
10
15
  atRule.name = 'tailwind'
@@ -44,6 +49,7 @@ export default function normalizeTailwindDirectives(root) {
44
49
  log.warn(`${atRule.name}-at-rule-deprecated`, [
45
50
  `The \`@${atRule.name}\` directive has been deprecated in Tailwind CSS v3.0.`,
46
51
  `Use \`@layer utilities\` or \`@layer components\` instead.`,
52
+ 'https://tailwindcss.com/docs/upgrade-guide#replace-variants-with-layer',
47
53
  ])
48
54
  }
49
55
  layerDirectives.add(atRule)
@@ -74,5 +80,5 @@ export default function normalizeTailwindDirectives(root) {
74
80
  }
75
81
  }
76
82
 
77
- return tailwindDirectives
83
+ return { tailwindDirectives, applyDirectives }
78
84
  }
@@ -0,0 +1,52 @@
1
+ function partitionRules(root) {
2
+ if (!root.walkAtRules) return
3
+
4
+ let applyParents = new Set()
5
+
6
+ root.walkAtRules('apply', (rule) => {
7
+ applyParents.add(rule.parent)
8
+ })
9
+
10
+ if (applyParents.size === 0) {
11
+ return
12
+ }
13
+
14
+ for (let rule of applyParents) {
15
+ let nodeGroups = []
16
+ let lastGroup = []
17
+
18
+ for (let node of rule.nodes) {
19
+ if (node.type === 'atrule' && node.name === 'apply') {
20
+ if (lastGroup.length > 0) {
21
+ nodeGroups.push(lastGroup)
22
+ lastGroup = []
23
+ }
24
+ nodeGroups.push([node])
25
+ } else {
26
+ lastGroup.push(node)
27
+ }
28
+ }
29
+
30
+ if (lastGroup.length > 0) {
31
+ nodeGroups.push(lastGroup)
32
+ }
33
+
34
+ if (nodeGroups.length === 1) {
35
+ continue
36
+ }
37
+
38
+ for (let group of [...nodeGroups].reverse()) {
39
+ let clone = rule.clone({ nodes: [] })
40
+ clone.append(group)
41
+ rule.after(clone)
42
+ }
43
+
44
+ rule.remove()
45
+ }
46
+ }
47
+
48
+ export default function expandApplyAtRules() {
49
+ return (root) => {
50
+ partitionRules(root)
51
+ }
52
+ }
@@ -71,6 +71,8 @@ function extractElementSelector(selector) {
71
71
  export default function resolveDefaultsAtRules({ tailwindConfig }) {
72
72
  return (root) => {
73
73
  let variableNodeMap = new Map()
74
+
75
+ /** @type {Set<import('postcss').AtRule>} */
74
76
  let universals = new Set()
75
77
 
76
78
  root.walkAtRules('defaults', (rule) => {
@@ -90,31 +92,54 @@ export default function resolveDefaultsAtRules({ tailwindConfig }) {
90
92
  })
91
93
 
92
94
  for (let universal of universals) {
93
- let selectors = new Set()
95
+ /** @type {Map<string, Set<string>>} */
96
+ let selectorGroups = new Map()
94
97
 
95
98
  let rules = variableNodeMap.get(universal.params) ?? []
96
99
 
97
100
  for (let rule of rules) {
98
101
  for (let selector of extractElementSelector(rule.selector)) {
102
+ // If selector contains a vendor prefix after a pseudo element or class,
103
+ // we consider them separately because merging the declarations into
104
+ // a single rule will cause browsers that do not understand the
105
+ // vendor prefix to throw out the whole rule
106
+ let selectorGroupName =
107
+ selector.includes(':-') || selector.includes('::-') ? selector : '__DEFAULT__'
108
+
109
+ let selectors = selectorGroups.get(selectorGroupName) ?? new Set()
110
+ selectorGroups.set(selectorGroupName, selectors)
111
+
99
112
  selectors.add(selector)
100
113
  }
101
114
  }
102
115
 
103
- if (selectors.size === 0) {
104
- universal.remove()
105
- continue
106
- }
116
+ if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {
117
+ if (selectorGroups.size === 0) {
118
+ universal.remove()
119
+ continue
120
+ }
107
121
 
108
- let universalRule = postcss.rule()
122
+ for (let [, selectors] of selectorGroups) {
123
+ let universalRule = postcss.rule({
124
+ source: universal.source,
125
+ })
109
126
 
110
- if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {
111
- universalRule.selectors = [...selectors]
127
+ universalRule.selectors = [...selectors]
128
+
129
+ universalRule.append(universal.nodes.map((node) => node.clone()))
130
+ universal.before(universalRule)
131
+ }
112
132
  } else {
133
+ let universalRule = postcss.rule({
134
+ source: universal.source,
135
+ })
136
+
113
137
  universalRule.selectors = ['*', '::before', '::after']
138
+
139
+ universalRule.append(universal.nodes)
140
+ universal.before(universalRule)
114
141
  }
115
142
 
116
- universalRule.append(universal.nodes)
117
- universal.before(universalRule)
118
143
  universal.remove()
119
144
  }
120
145
  }