tailwindcss 0.0.0-insiders.eebf529 → 0.0.0-insiders.ef2ebb2

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 (227) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +15 -7
  3. package/colors.d.ts +3 -0
  4. package/defaultConfig.d.ts +3 -0
  5. package/defaultTheme.d.ts +4 -0
  6. package/index.css +5 -0
  7. package/lib/cli/build/index.js +57 -0
  8. package/lib/cli/build/plugin.js +433 -0
  9. package/lib/cli/build/utils.js +88 -0
  10. package/lib/cli/build/watching.js +182 -0
  11. package/lib/cli/help/index.js +73 -0
  12. package/lib/cli/index.js +231 -0
  13. package/lib/cli/init/index.js +63 -0
  14. package/lib/cli.js +1 -704
  15. package/lib/corePluginList.js +13 -3
  16. package/lib/corePlugins.js +2399 -1867
  17. package/lib/css/preflight.css +17 -9
  18. package/lib/featureFlags.js +50 -26
  19. package/lib/index.js +1 -31
  20. package/lib/lib/cacheInvalidation.js +92 -0
  21. package/lib/lib/collapseAdjacentRules.js +30 -10
  22. package/lib/lib/collapseDuplicateDeclarations.js +60 -4
  23. package/lib/lib/content.js +207 -0
  24. package/lib/lib/defaultExtractor.js +243 -0
  25. package/lib/lib/detectNesting.js +21 -10
  26. package/lib/lib/evaluateTailwindFunctions.js +117 -50
  27. package/lib/lib/expandApplyAtRules.js +467 -161
  28. package/lib/lib/expandTailwindAtRules.js +200 -133
  29. package/lib/lib/findAtConfigPath.js +46 -0
  30. package/lib/lib/generateRules.js +550 -200
  31. package/lib/lib/getModuleDependencies.js +88 -37
  32. package/lib/lib/handleImportAtRules.js +50 -0
  33. package/lib/lib/load-config.js +42 -0
  34. package/lib/lib/normalizeTailwindDirectives.js +36 -38
  35. package/lib/lib/offsets.js +306 -0
  36. package/lib/lib/partitionApplyAtRules.js +58 -0
  37. package/lib/lib/regex.js +74 -0
  38. package/lib/lib/remap-bitfield.js +89 -0
  39. package/lib/lib/resolveDefaultsAtRules.js +98 -58
  40. package/lib/lib/setupContextUtils.js +767 -325
  41. package/lib/lib/setupTrackingContext.js +71 -75
  42. package/lib/lib/sharedState.js +70 -10
  43. package/lib/lib/substituteScreenAtRules.js +14 -10
  44. package/lib/plugin.js +166 -0
  45. package/{nesting → lib/postcss-plugins/nesting}/README.md +2 -2
  46. package/lib/postcss-plugins/nesting/index.js +21 -0
  47. package/lib/postcss-plugins/nesting/plugin.js +89 -0
  48. package/lib/processTailwindFeatures.js +39 -26
  49. package/lib/public/colors.js +272 -246
  50. package/lib/public/create-plugin.js +9 -5
  51. package/lib/public/default-config.js +10 -6
  52. package/lib/public/default-theme.js +10 -6
  53. package/lib/public/load-config.js +12 -0
  54. package/lib/public/resolve-config.js +11 -6
  55. package/lib/util/applyImportantSelector.js +36 -0
  56. package/lib/util/bigSign.js +6 -1
  57. package/lib/util/buildMediaQuery.js +13 -6
  58. package/lib/util/cloneDeep.js +9 -6
  59. package/lib/util/cloneNodes.js +23 -3
  60. package/lib/util/color.js +70 -38
  61. package/lib/util/colorNames.js +752 -0
  62. package/lib/util/configurePlugins.js +7 -2
  63. package/lib/util/createPlugin.js +8 -6
  64. package/lib/util/createUtilityPlugin.js +16 -16
  65. package/lib/util/dataTypes.js +182 -111
  66. package/lib/util/defaults.js +14 -3
  67. package/lib/util/escapeClassName.js +13 -8
  68. package/lib/util/escapeCommas.js +7 -2
  69. package/lib/util/flattenColorPalette.js +11 -12
  70. package/lib/util/formatVariantSelector.js +228 -151
  71. package/lib/util/getAllConfigs.js +33 -12
  72. package/lib/util/hashConfig.js +9 -4
  73. package/lib/util/isKeyframeRule.js +7 -2
  74. package/lib/util/isPlainObject.js +7 -2
  75. package/lib/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +25 -15
  76. package/lib/util/log.js +44 -13
  77. package/lib/util/nameClass.js +27 -10
  78. package/lib/util/negateValue.js +25 -8
  79. package/lib/util/normalizeConfig.js +167 -67
  80. package/lib/util/normalizeScreens.js +128 -11
  81. package/lib/util/parseAnimationValue.js +44 -40
  82. package/lib/util/parseBoxShadowValue.js +34 -23
  83. package/lib/util/parseDependency.js +39 -55
  84. package/lib/util/parseGlob.js +36 -0
  85. package/lib/util/parseObjectStyles.js +15 -10
  86. package/lib/util/pluginUtils.js +157 -69
  87. package/lib/util/prefixSelector.js +30 -12
  88. package/lib/util/pseudoElements.js +209 -0
  89. package/lib/util/removeAlphaVariables.js +31 -0
  90. package/lib/util/resolveConfig.js +101 -79
  91. package/lib/util/resolveConfigPath.js +30 -12
  92. package/lib/util/responsive.js +11 -6
  93. package/lib/util/splitAtTopLevelOnly.js +51 -0
  94. package/lib/util/tap.js +6 -1
  95. package/lib/util/toColorValue.js +7 -3
  96. package/lib/util/toPath.js +26 -3
  97. package/lib/util/transformThemeValue.js +40 -30
  98. package/lib/util/validateConfig.js +48 -0
  99. package/lib/util/validateFormalSyntax.js +26 -0
  100. package/lib/util/withAlphaVariable.js +27 -15
  101. package/lib/value-parser/LICENSE +22 -0
  102. package/lib/value-parser/README.md +3 -0
  103. package/lib/value-parser/index.d.js +2 -0
  104. package/lib/value-parser/index.js +22 -0
  105. package/lib/value-parser/parse.js +259 -0
  106. package/lib/value-parser/stringify.js +38 -0
  107. package/lib/value-parser/unit.js +86 -0
  108. package/lib/value-parser/walk.js +16 -0
  109. package/loadConfig.d.ts +4 -0
  110. package/loadConfig.js +2 -0
  111. package/nesting/index.js +2 -12
  112. package/package.json +63 -62
  113. package/plugin.d.ts +11 -0
  114. package/resolveConfig.d.ts +12 -0
  115. package/scripts/generate-types.js +105 -0
  116. package/scripts/release-channel.js +18 -0
  117. package/scripts/release-notes.js +21 -0
  118. package/scripts/type-utils.js +27 -0
  119. package/src/cli/build/index.js +53 -0
  120. package/src/cli/build/plugin.js +476 -0
  121. package/src/cli/build/utils.js +76 -0
  122. package/src/cli/build/watching.js +229 -0
  123. package/src/cli/help/index.js +70 -0
  124. package/src/cli/index.js +217 -0
  125. package/src/cli/init/index.js +79 -0
  126. package/src/cli.js +1 -766
  127. package/src/corePluginList.js +1 -1
  128. package/src/corePlugins.js +800 -296
  129. package/src/css/preflight.css +17 -9
  130. package/src/featureFlags.js +20 -6
  131. package/src/index.js +1 -34
  132. package/src/lib/cacheInvalidation.js +52 -0
  133. package/src/lib/collapseAdjacentRules.js +21 -2
  134. package/src/lib/collapseDuplicateDeclarations.js +66 -1
  135. package/src/lib/content.js +240 -0
  136. package/src/lib/defaultExtractor.js +217 -0
  137. package/src/lib/detectNesting.js +9 -1
  138. package/src/lib/evaluateTailwindFunctions.js +83 -9
  139. package/src/lib/expandApplyAtRules.js +515 -153
  140. package/src/lib/expandTailwindAtRules.js +147 -86
  141. package/src/lib/findAtConfigPath.js +48 -0
  142. package/src/lib/generateRules.js +541 -147
  143. package/src/lib/getModuleDependencies.js +70 -30
  144. package/src/lib/handleImportAtRules.js +34 -0
  145. package/src/lib/load-config.ts +31 -0
  146. package/src/lib/normalizeTailwindDirectives.js +5 -26
  147. package/src/lib/offsets.js +373 -0
  148. package/src/lib/partitionApplyAtRules.js +52 -0
  149. package/src/lib/regex.js +74 -0
  150. package/src/lib/remap-bitfield.js +82 -0
  151. package/src/lib/resolveDefaultsAtRules.js +59 -17
  152. package/src/lib/setupContextUtils.js +693 -176
  153. package/src/lib/setupTrackingContext.js +52 -65
  154. package/src/lib/sharedState.js +54 -7
  155. package/src/plugin.js +141 -0
  156. package/src/postcss-plugins/nesting/README.md +42 -0
  157. package/src/postcss-plugins/nesting/index.js +13 -0
  158. package/src/postcss-plugins/nesting/plugin.js +80 -0
  159. package/src/processTailwindFeatures.js +12 -2
  160. package/src/public/colors.js +22 -0
  161. package/src/public/default-config.js +1 -1
  162. package/src/public/default-theme.js +2 -2
  163. package/src/public/load-config.js +2 -0
  164. package/src/util/applyImportantSelector.js +27 -0
  165. package/src/util/buildMediaQuery.js +5 -3
  166. package/src/util/cloneNodes.js +19 -2
  167. package/src/util/color.js +44 -12
  168. package/src/util/colorNames.js +150 -0
  169. package/src/util/dataTypes.js +67 -23
  170. package/src/util/defaults.js +6 -0
  171. package/src/util/formatVariantSelector.js +264 -144
  172. package/src/util/getAllConfigs.js +21 -2
  173. package/src/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +1 -1
  174. package/src/util/log.js +35 -7
  175. package/src/util/nameClass.js +4 -0
  176. package/src/util/negateValue.js +11 -3
  177. package/src/util/normalizeConfig.js +86 -7
  178. package/src/util/normalizeScreens.js +99 -4
  179. package/src/util/parseBoxShadowValue.js +4 -3
  180. package/src/util/parseDependency.js +37 -42
  181. package/src/util/parseGlob.js +24 -0
  182. package/src/util/pluginUtils.js +118 -23
  183. package/src/util/prefixSelector.js +30 -10
  184. package/src/util/pseudoElements.js +167 -0
  185. package/src/util/removeAlphaVariables.js +24 -0
  186. package/src/util/resolveConfig.js +74 -26
  187. package/src/util/resolveConfigPath.js +12 -1
  188. package/src/util/splitAtTopLevelOnly.js +52 -0
  189. package/src/util/toPath.js +23 -1
  190. package/src/util/transformThemeValue.js +13 -3
  191. package/src/util/validateConfig.js +36 -0
  192. package/src/util/validateFormalSyntax.js +34 -0
  193. package/src/util/withAlphaVariable.js +1 -1
  194. package/src/value-parser/LICENSE +22 -0
  195. package/src/value-parser/README.md +3 -0
  196. package/src/value-parser/index.d.ts +177 -0
  197. package/src/value-parser/index.js +28 -0
  198. package/src/value-parser/parse.js +303 -0
  199. package/src/value-parser/stringify.js +41 -0
  200. package/src/value-parser/unit.js +118 -0
  201. package/src/value-parser/walk.js +18 -0
  202. package/stubs/.gitignore +1 -0
  203. package/stubs/.prettierrc.json +6 -0
  204. package/stubs/{defaultConfig.stub.js → config.full.js} +220 -167
  205. package/stubs/{simpleConfig.stub.js → config.simple.js} +1 -1
  206. package/stubs/{defaultPostCssConfig.stub.js → postcss.config.cjs} +0 -1
  207. package/stubs/postcss.config.js +5 -0
  208. package/stubs/tailwind.config.cjs +2 -0
  209. package/stubs/tailwind.config.js +2 -0
  210. package/stubs/tailwind.config.ts +3 -0
  211. package/types/config.d.ts +369 -0
  212. package/types/generated/.gitkeep +0 -0
  213. package/types/generated/colors.d.ts +298 -0
  214. package/types/generated/corePluginList.d.ts +1 -0
  215. package/types/generated/default-theme.d.ts +372 -0
  216. package/types/index.d.ts +7 -0
  217. package/CHANGELOG.md +0 -1853
  218. package/lib/cli-peer-dependencies.js +0 -15
  219. package/lib/constants.js +0 -37
  220. package/lib/lib/setupWatchingContext.js +0 -288
  221. package/nesting/plugin.js +0 -41
  222. package/peers/index.js +0 -76107
  223. package/scripts/install-integrations.js +0 -27
  224. package/scripts/rebuildFixtures.js +0 -68
  225. package/src/cli-peer-dependencies.js +0 -9
  226. package/src/constants.js +0 -17
  227. package/src/lib/setupWatchingContext.js +0 -311
@@ -1,21 +1,17 @@
1
- import fs from 'fs'
2
- import path from 'path'
1
+ // @ts-check
3
2
 
4
- import fastGlob from 'fast-glob'
5
- import LRU from 'quick-lru'
6
- import normalizePath from 'normalize-path'
3
+ import fs from 'fs'
4
+ import LRU from '@alloc/quick-lru'
7
5
 
8
6
  import hash from '../util/hashConfig'
9
- import getModuleDependencies from '../lib/getModuleDependencies'
10
-
11
7
  import resolveConfig from '../public/resolve-config'
12
-
13
8
  import resolveConfigPath from '../util/resolveConfigPath'
14
-
15
- import { env } from './sharedState'
16
-
17
9
  import { getContext, getFileModifiedMap } from './setupContextUtils'
18
10
  import parseDependency from '../util/parseDependency'
11
+ import { validateConfig } from '../util/validateConfig.js'
12
+ import { parseCandidateFiles, resolvedChangedContent } from './content.js'
13
+ import { loadConfig } from '../lib/load-config'
14
+ import getModuleDependencies from './getModuleDependencies'
19
15
 
20
16
  let configPathCache = new LRU({ maxSize: 100 })
21
17
 
@@ -26,9 +22,7 @@ function getCandidateFiles(context, tailwindConfig) {
26
22
  return candidateFilesCache.get(context)
27
23
  }
28
24
 
29
- let candidateFiles = tailwindConfig.content.files
30
- .filter((item) => typeof item === 'string')
31
- .map((contentPath) => normalizePath(contentPath))
25
+ let candidateFiles = parseCandidateFiles(context, tailwindConfig)
32
26
 
33
27
  return candidateFilesCache.set(context, candidateFiles).get(context)
34
28
  }
@@ -41,7 +35,7 @@ function getTailwindConfig(configOrPath) {
41
35
  let [prevConfig, prevConfigHash, prevDeps, prevModified] =
42
36
  configPathCache.get(userConfigPath) || []
43
37
 
44
- let newDeps = getModuleDependencies(userConfigPath).map((dep) => dep.file)
38
+ let newDeps = getModuleDependencies(userConfigPath)
45
39
 
46
40
  let modified = false
47
41
  let newModified = new Map()
@@ -62,48 +56,18 @@ function getTailwindConfig(configOrPath) {
62
56
  for (let file of newDeps) {
63
57
  delete require.cache[file]
64
58
  }
65
- let newConfig = resolveConfig(require(userConfigPath))
59
+ let newConfig = validateConfig(resolveConfig(loadConfig(userConfigPath)))
66
60
  let newHash = hash(newConfig)
67
61
  configPathCache.set(userConfigPath, [newConfig, newHash, newDeps, newModified])
68
62
  return [newConfig, userConfigPath, newHash, newDeps]
69
63
  }
70
64
 
71
65
  // It's a plain object, not a path
72
- let newConfig = resolveConfig(
73
- configOrPath.config === undefined ? configOrPath : configOrPath.config
74
- )
66
+ let newConfig = resolveConfig(configOrPath?.config ?? configOrPath ?? {})
75
67
 
76
- return [newConfig, null, hash(newConfig), []]
77
- }
78
-
79
- function resolvedChangedContent(context, candidateFiles, fileModifiedMap) {
80
- let changedContent = context.tailwindConfig.content.files
81
- .filter((item) => typeof item.raw === 'string')
82
- .map(({ raw, extension = 'html' }) => ({ content: raw, extension }))
83
-
84
- for (let changedFile of resolveChangedFiles(candidateFiles, fileModifiedMap)) {
85
- let content = fs.readFileSync(changedFile, 'utf8')
86
- let extension = path.extname(changedFile).slice(1)
87
- changedContent.push({ content, extension })
88
- }
89
- return changedContent
90
- }
68
+ newConfig = validateConfig(newConfig)
91
69
 
92
- function resolveChangedFiles(candidateFiles, fileModifiedMap) {
93
- let changedFiles = new Set()
94
- env.DEBUG && console.time('Finding changed files')
95
- let files = fastGlob.sync(candidateFiles)
96
- for (let file of files) {
97
- let prevModified = fileModifiedMap.has(file) ? fileModifiedMap.get(file) : -Infinity
98
- let modified = fs.statSync(file).mtimeMs
99
-
100
- if (modified > prevModified) {
101
- changedFiles.add(file)
102
- fileModifiedMap.set(file, modified)
103
- }
104
- }
105
- env.DEBUG && console.timeEnd('Finding changed files')
106
- return changedFiles
70
+ return [newConfig, null, hash(newConfig), []]
107
71
  }
108
72
 
109
73
  // DISABLE_TOUCH = TRUE
@@ -119,11 +83,12 @@ export default function setupTrackingContext(configOrPath) {
119
83
 
120
84
  let contextDependencies = new Set(configDependencies)
121
85
 
122
- // If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
123
- // to be dependencies of the context. Can reuse the context even if they change.
124
- // We may want to think about `@layer` being part of this trigger too, but it's tough
125
- // because it's impossible for a layer in one file to end up in the actual @tailwind rule
126
- // in another file since independent sources are effectively isolated.
86
+ // If there are no @tailwind or @apply rules, we don't consider this CSS
87
+ // file or its dependencies to be dependencies of the context. Can reuse
88
+ // the context even if they change. We may want to think about `@layer`
89
+ // being part of this trigger too, but it's tough because it's impossible
90
+ // for a layer in one file to end up in the actual @tailwind rule in
91
+ // another file since independent sources are effectively isolated.
127
92
  if (tailwindDirectives.size > 0) {
128
93
  // Add current css file as a context dependencies.
129
94
  contextDependencies.add(result.opts.from)
@@ -136,7 +101,7 @@ export default function setupTrackingContext(configOrPath) {
136
101
  }
137
102
  }
138
103
 
139
- let [context] = getContext(
104
+ let [context, , mTimesToCommit] = getContext(
140
105
  root,
141
106
  result,
142
107
  tailwindConfig,
@@ -145,30 +110,45 @@ export default function setupTrackingContext(configOrPath) {
145
110
  contextDependencies
146
111
  )
147
112
 
113
+ let fileModifiedMap = getFileModifiedMap(context)
114
+
148
115
  let candidateFiles = getCandidateFiles(context, tailwindConfig)
149
116
 
150
- // If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
151
- // to be dependencies of the context. Can reuse the context even if they change.
117
+ // If there are no @tailwind or @apply rules, we don't consider this CSS file or it's
118
+ // dependencies to be dependencies of the context. Can reuse the context even if they change.
152
119
  // We may want to think about `@layer` being part of this trigger too, but it's tough
153
120
  // because it's impossible for a layer in one file to end up in the actual @tailwind rule
154
121
  // in another file since independent sources are effectively isolated.
155
122
  if (tailwindDirectives.size > 0) {
156
- let fileModifiedMap = getFileModifiedMap(context)
157
-
158
123
  // Add template paths as postcss dependencies.
159
- for (let fileOrGlob of candidateFiles) {
160
- let dependency = parseDependency(fileOrGlob)
161
- if (dependency) {
124
+ for (let contentPath of candidateFiles) {
125
+ for (let dependency of parseDependency(contentPath)) {
162
126
  registerDependency(dependency)
163
127
  }
164
128
  }
165
129
 
166
- for (let changedContent of resolvedChangedContent(
130
+ let [changedContent, contentMTimesToCommit] = resolvedChangedContent(
167
131
  context,
168
132
  candidateFiles,
169
133
  fileModifiedMap
170
- )) {
171
- context.changedContent.push(changedContent)
134
+ )
135
+
136
+ for (let content of changedContent) {
137
+ context.changedContent.push(content)
138
+ }
139
+
140
+ // Add the mtimes of the content files to the commit list
141
+ // We can overwrite the existing values because unconditionally
142
+ // This is because:
143
+ // 1. Most of the files here won't be in the map yet
144
+ // 2. If they are that means it's a context dependency
145
+ // and we're reading this after the context. This means
146
+ // that the mtime we just read is strictly >= the context
147
+ // mtime. Unless the user / os is doing something weird
148
+ // in which the mtime would be going backwards. If that
149
+ // happens there's already going to be problems.
150
+ for (let [path, mtime] of contentMTimesToCommit.entries()) {
151
+ mTimesToCommit.set(path, mtime)
172
152
  }
173
153
  }
174
154
 
@@ -176,6 +156,13 @@ export default function setupTrackingContext(configOrPath) {
176
156
  registerDependency({ type: 'dependency', file })
177
157
  }
178
158
 
159
+ // "commit" the new modified time for all context deps
160
+ // We do this here because we want content tracking to
161
+ // read the "old" mtime even when it's a context dependency.
162
+ for (let [path, mtime] of mTimesToCommit.entries()) {
163
+ fileModifiedMap.set(path, mtime)
164
+ }
165
+
179
166
  return context
180
167
  }
181
168
  }
@@ -1,10 +1,57 @@
1
- export const env = {
2
- TAILWIND_MODE: process.env.TAILWIND_MODE,
3
- NODE_ENV: process.env.NODE_ENV,
4
- DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
5
- TAILWIND_DISABLE_TOUCH: process.env.TAILWIND_DISABLE_TOUCH !== undefined,
6
- TAILWIND_TOUCH_DIR: process.env.TAILWIND_TOUCH_DIR,
7
- }
1
+ export const env =
2
+ typeof process !== 'undefined'
3
+ ? {
4
+ NODE_ENV: process.env.NODE_ENV,
5
+ DEBUG: resolveDebug(process.env.DEBUG),
6
+ }
7
+ : {
8
+ NODE_ENV: 'production',
9
+ DEBUG: false,
10
+ }
11
+
8
12
  export const contextMap = new Map()
9
13
  export const configContextMap = new Map()
10
14
  export const contextSourcesMap = new Map()
15
+ export const sourceHashMap = new Map()
16
+ export const NOT_ON_DEMAND = new String('*')
17
+
18
+ export const NONE = Symbol('__NONE__')
19
+
20
+ export function resolveDebug(debug) {
21
+ if (debug === undefined) {
22
+ return false
23
+ }
24
+
25
+ // Environment variables are strings, so convert to boolean
26
+ if (debug === 'true' || debug === '1') {
27
+ return true
28
+ }
29
+
30
+ if (debug === 'false' || debug === '0') {
31
+ return false
32
+ }
33
+
34
+ // Keep the debug convention into account:
35
+ // DEBUG=* -> This enables all debug modes
36
+ // DEBUG=projectA,projectB,projectC -> This enables debug for projectA, projectB and projectC
37
+ // DEBUG=projectA:* -> This enables all debug modes for projectA (if you have sub-types)
38
+ // DEBUG=projectA,-projectB -> This enables debug for projectA and explicitly disables it for projectB
39
+
40
+ if (debug === '*') {
41
+ return true
42
+ }
43
+
44
+ let debuggers = debug.split(',').map((d) => d.split(':')[0])
45
+
46
+ // Ignoring tailwindcss
47
+ if (debuggers.includes('-tailwindcss')) {
48
+ return false
49
+ }
50
+
51
+ // Including tailwindcss
52
+ if (debuggers.includes('tailwindcss')) {
53
+ return true
54
+ }
55
+
56
+ return false
57
+ }
package/src/plugin.js ADDED
@@ -0,0 +1,141 @@
1
+ import postcss from 'postcss'
2
+ import lightningcss, { Features } from 'lightningcss'
3
+ import browserslist from 'browserslist'
4
+ import setupTrackingContext from './lib/setupTrackingContext'
5
+ import processTailwindFeatures from './processTailwindFeatures'
6
+ import { env } from './lib/sharedState'
7
+ import { findAtConfigPath } from './lib/findAtConfigPath'
8
+ import { handleImportAtRules } from './lib/handleImportAtRules'
9
+
10
+ module.exports = function tailwindcss(configOrPath) {
11
+ return {
12
+ postcssPlugin: 'tailwindcss',
13
+ plugins: [
14
+ env.DEBUG &&
15
+ function (root) {
16
+ console.log('\n')
17
+ console.time('JIT TOTAL')
18
+ return root
19
+ },
20
+ ...handleImportAtRules(),
21
+ function (root, result) {
22
+ // Use the path for the `@config` directive if it exists, otherwise use the
23
+ // path for the file being processed
24
+ configOrPath = findAtConfigPath(root, result) ?? configOrPath
25
+
26
+ let context = setupTrackingContext(configOrPath)
27
+
28
+ if (root.type === 'document') {
29
+ let roots = root.nodes.filter((node) => node.type === 'root')
30
+
31
+ for (const root of roots) {
32
+ if (root.type === 'root') {
33
+ processTailwindFeatures(context)(root, result)
34
+ }
35
+ }
36
+
37
+ return
38
+ }
39
+
40
+ processTailwindFeatures(context)(root, result)
41
+ },
42
+ function lightningCssPlugin(_root, result) {
43
+ let map = result.map ?? result.opts.map
44
+
45
+ let intermediateResult = result.root.toResult({
46
+ map: map ? { inline: true } : false,
47
+ })
48
+
49
+ let intermediateMap = intermediateResult.map?.toJSON?.() ?? map
50
+
51
+ try {
52
+ let includeFeatures = Features.Nesting
53
+ let excludeFeatures = 0
54
+
55
+ let resolvedBrowsersListConfig = browserslist.findConfig(
56
+ result.opts.from ?? process.cwd()
57
+ )?.defaults
58
+ let defaultBrowsersListConfig = require('../package.json').browserslist
59
+ let browsersListConfig = resolvedBrowsersListConfig ?? defaultBrowsersListConfig
60
+
61
+ if (browsersListConfig.join(',') === defaultBrowsersListConfig.join(',')) {
62
+ includeFeatures |=
63
+ Features.ColorFunction | Features.OklabColors | Features.LabColors | Features.P3Colors
64
+
65
+ excludeFeatures |=
66
+ Features.HexAlphaColors |
67
+ Features.LogicalProperties |
68
+ Features.SpaceSeparatedColorNotation
69
+ }
70
+
71
+ let transformed = lightningcss.transform({
72
+ filename: result.opts.from,
73
+ code: Buffer.from(intermediateResult.css),
74
+ minify: false,
75
+ sourceMap: !!intermediateMap,
76
+ targets: lightningcss.browserslistToTargets(browserslist(browsersListConfig)),
77
+ drafts: {
78
+ nesting: true,
79
+ customMedia: true,
80
+ },
81
+ nonStandard: {
82
+ deepSelectorCombinator: true,
83
+ },
84
+ include: includeFeatures,
85
+ exclude: excludeFeatures,
86
+ })
87
+
88
+ let code = transformed.code.toString()
89
+
90
+ // https://postcss.org/api/#sourcemapoptions
91
+ if (intermediateMap && transformed.map != null) {
92
+ let prev = transformed.map.toString()
93
+
94
+ if (typeof intermediateMap === 'object') {
95
+ intermediateMap.prev = prev
96
+ } else {
97
+ code = `${code}\n/*# sourceMappingURL=data:application/json;base64,${Buffer.from(
98
+ prev
99
+ ).toString('base64')} */`
100
+ }
101
+ }
102
+
103
+ result.root = postcss.parse(code, {
104
+ ...result.opts,
105
+ map: intermediateMap,
106
+ })
107
+ } catch (err) {
108
+ if (err.source && typeof process !== 'undefined' && process.env.JEST_WORKER_ID) {
109
+ let lines = err.source.split('\n')
110
+ err = new Error(
111
+ [
112
+ 'Error formatting using Lightning CSS:',
113
+ '',
114
+ ...[
115
+ '```css',
116
+ ...lines.slice(Math.max(err.loc.line - 3, 0), err.loc.line),
117
+ ' '.repeat(err.loc.column - 1) + '^-- ' + err.toString(),
118
+ ...lines.slice(err.loc.line, err.loc.line + 2),
119
+ '```',
120
+ ],
121
+ ].join('\n')
122
+ )
123
+ }
124
+
125
+ if (Error.captureStackTrace) {
126
+ Error.captureStackTrace(err, lightningCssPlugin)
127
+ }
128
+ throw err
129
+ }
130
+ },
131
+ env.DEBUG &&
132
+ function (root) {
133
+ console.timeEnd('JIT TOTAL')
134
+ console.log('\n')
135
+ return root
136
+ },
137
+ ].filter(Boolean),
138
+ }
139
+ }
140
+
141
+ module.exports.postcss = true
@@ -0,0 +1,42 @@
1
+ # tailwindcss/nesting
2
+
3
+ This is a PostCSS plugin that wraps [postcss-nested](https://github.com/postcss/postcss-nested) or [postcss-nesting](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-nesting) and acts as a compatibility layer to make sure your nesting plugin of choice properly understands Tailwind's custom syntax like `@apply` and `@screen`.
4
+
5
+ Add it to your PostCSS configuration, somewhere before Tailwind itself:
6
+
7
+ ```js
8
+ // postcss.config.js
9
+ module.exports = {
10
+ plugins: [
11
+ require('postcss-import'),
12
+ require('tailwindcss/nesting'),
13
+ require('tailwindcss'),
14
+ require('autoprefixer'),
15
+ ]
16
+ }
17
+ ```
18
+
19
+ By default, it uses the [postcss-nested](https://github.com/postcss/postcss-nested) plugin under the hood, which uses a Sass-like syntax and is the plugin that powers nesting support in the [Tailwind CSS plugin API](https://tailwindcss.com/docs/plugins#css-in-js-syntax).
20
+
21
+ If you'd rather use [postcss-nesting](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-nesting) (which is based on the work-in-progress [CSS Nesting](https://drafts.csswg.org/css-nesting-1/) specification), first install the plugin alongside:
22
+
23
+ ```shell
24
+ npm install postcss-nesting
25
+ ```
26
+
27
+ Then pass the plugin itself as an argument to `tailwindcss/nesting` in your PostCSS configuration:
28
+
29
+ ```js
30
+ // postcss.config.js
31
+ module.exports = {
32
+ plugins: [
33
+ require('postcss-import'),
34
+ require('tailwindcss/nesting')(require('postcss-nesting')),
35
+ require('tailwindcss'),
36
+ require('autoprefixer'),
37
+ ]
38
+ }
39
+ ```
40
+
41
+ This can also be helpful if for whatever reason you need to use a very specific version of `postcss-nested` and want to override the version we bundle with `tailwindcss/nesting` itself.
42
+
@@ -0,0 +1,13 @@
1
+ import { nesting } from './plugin'
2
+
3
+ export default Object.assign(
4
+ function (opts) {
5
+ return {
6
+ postcssPlugin: 'tailwindcss/nesting',
7
+ Once(root, { result }) {
8
+ return nesting(opts)(root, result)
9
+ },
10
+ }
11
+ },
12
+ { postcss: true }
13
+ )
@@ -0,0 +1,80 @@
1
+ import postcss from 'postcss'
2
+ import postcssNested from 'postcss-nested'
3
+
4
+ export function nesting(opts = postcssNested) {
5
+ return (root, result) => {
6
+ root.walkAtRules('screen', (rule) => {
7
+ rule.name = 'media'
8
+ rule.params = `screen(${rule.params})`
9
+ })
10
+
11
+ root.walkAtRules('apply', (rule) => {
12
+ rule.before(postcss.decl({ prop: '__apply', value: rule.params, source: rule.source }))
13
+ rule.remove()
14
+ })
15
+
16
+ let plugin = (() => {
17
+ if (
18
+ typeof opts === 'function' ||
19
+ (typeof opts === 'object' && opts?.hasOwnProperty?.('postcssPlugin'))
20
+ ) {
21
+ return opts
22
+ }
23
+
24
+ if (typeof opts === 'string') {
25
+ return require(opts)
26
+ }
27
+
28
+ if (Object.keys(opts).length <= 0) {
29
+ return postcssNested
30
+ }
31
+
32
+ throw new Error('tailwindcss/nesting should be loaded with a nesting plugin.')
33
+ })()
34
+
35
+ postcss([plugin]).process(root, result.opts).sync()
36
+
37
+ root.walkDecls('__apply', (decl) => {
38
+ decl.before(postcss.atRule({ name: 'apply', params: decl.value, source: decl.source }))
39
+ decl.remove()
40
+ })
41
+
42
+ /**
43
+ * Use a private PostCSS API to remove the "clean" flag from the entire AST.
44
+ * This is done because running process() on the AST will set the "clean"
45
+ * flag on all nodes, which we don't want.
46
+ *
47
+ * This causes downstream plugins using the visitor API to be skipped.
48
+ *
49
+ * This is guarded because the PostCSS API is not public
50
+ * and may change in future versions of PostCSS.
51
+ *
52
+ * See https://github.com/postcss/postcss/issues/1712 for more details
53
+ *
54
+ * @param {import('postcss').Node} node
55
+ */
56
+ function markDirty(node) {
57
+ if (!('markDirty' in node)) {
58
+ return
59
+ }
60
+
61
+ // Traverse the tree down to the leaf nodes
62
+ if (node.nodes) {
63
+ node.nodes.forEach((n) => markDirty(n))
64
+ }
65
+
66
+ // If it's a leaf node mark it as dirty
67
+ // We do this here because marking a node as dirty
68
+ // will walk up the tree and mark all parents as dirty
69
+ // resulting in a lot of unnecessary work if we did this
70
+ // for every single node
71
+ if (!node.nodes) {
72
+ node.markDirty()
73
+ }
74
+ }
75
+
76
+ markDirty(root)
77
+
78
+ return root
79
+ }
80
+ }
@@ -6,16 +6,24 @@ import substituteScreenAtRules from './lib/substituteScreenAtRules'
6
6
  import resolveDefaultsAtRules from './lib/resolveDefaultsAtRules'
7
7
  import collapseAdjacentRules from './lib/collapseAdjacentRules'
8
8
  import collapseDuplicateDeclarations from './lib/collapseDuplicateDeclarations'
9
+ import partitionApplyAtRules from './lib/partitionApplyAtRules'
9
10
  import detectNesting from './lib/detectNesting'
10
11
  import { createContext } from './lib/setupContextUtils'
11
12
  import { issueFlagNotices } from './featureFlags'
12
13
 
13
14
  export default function processTailwindFeatures(setupContext) {
14
15
  return function (root, result) {
15
- let tailwindDirectives = normalizeTailwindDirectives(root)
16
+ let { tailwindDirectives, applyDirectives } = normalizeTailwindDirectives(root)
17
+
18
+ detectNesting()(root, result)
19
+
20
+ // Partition apply rules that are found in the css
21
+ // itself.
22
+ partitionApplyAtRules()(root, result)
16
23
 
17
24
  let context = setupContext({
18
25
  tailwindDirectives,
26
+ applyDirectives,
19
27
  registerDependency(dependency) {
20
28
  result.messages.push({
21
29
  plugin: 'tailwindcss',
@@ -36,8 +44,10 @@ export default function processTailwindFeatures(setupContext) {
36
44
 
37
45
  issueFlagNotices(context.tailwindConfig)
38
46
 
39
- detectNesting(context)(root, result)
40
47
  expandTailwindAtRules(context)(root, result)
48
+ // Partition apply rules that are generated by
49
+ // addComponents, addUtilities and so on.
50
+ partitionApplyAtRules()(root, result)
41
51
  expandApplyAtRules(context)(root, result)
42
52
  evaluateTailwindFunctions(context)(root, result)
43
53
  substituteScreenAtRules(context)(root, result)