tailwindcss 0.0.0-insiders.ca1dfd6 → 0.0.0-insiders.cb6e45a

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 (190) hide show
  1. package/CHANGELOG.md +526 -2
  2. package/LICENSE +1 -2
  3. package/README.md +13 -6
  4. package/colors.d.ts +3 -0
  5. package/colors.js +2 -1
  6. package/defaultConfig.d.ts +3 -0
  7. package/defaultConfig.js +2 -1
  8. package/defaultTheme.d.ts +4 -0
  9. package/defaultTheme.js +2 -1
  10. package/lib/cli/build/deps.js +54 -0
  11. package/lib/cli/build/index.js +44 -0
  12. package/lib/cli/build/plugin.js +335 -0
  13. package/lib/cli/build/utils.js +78 -0
  14. package/lib/cli/build/watching.js +113 -0
  15. package/lib/cli/help/index.js +71 -0
  16. package/lib/cli/index.js +18 -0
  17. package/lib/cli/init/index.js +46 -0
  18. package/lib/cli/shared.js +12 -0
  19. package/lib/cli-peer-dependencies.js +20 -7
  20. package/lib/cli.js +91 -569
  21. package/lib/constants.js +27 -20
  22. package/lib/corePluginList.js +16 -4
  23. package/lib/corePlugins.js +2247 -1873
  24. package/lib/css/preflight.css +20 -9
  25. package/lib/featureFlags.js +37 -23
  26. package/lib/index.js +28 -12
  27. package/lib/lib/cacheInvalidation.js +90 -0
  28. package/lib/lib/collapseAdjacentRules.js +27 -9
  29. package/lib/lib/collapseDuplicateDeclarations.js +83 -0
  30. package/lib/lib/content.js +167 -0
  31. package/lib/lib/defaultExtractor.js +236 -0
  32. package/lib/lib/detectNesting.js +21 -3
  33. package/lib/lib/evaluateTailwindFunctions.js +117 -50
  34. package/lib/lib/expandApplyAtRules.js +439 -160
  35. package/lib/lib/expandTailwindAtRules.js +141 -151
  36. package/lib/lib/findAtConfigPath.js +44 -0
  37. package/lib/lib/generateRules.js +572 -105
  38. package/lib/lib/getModuleDependencies.js +11 -8
  39. package/lib/lib/normalizeTailwindDirectives.js +45 -33
  40. package/lib/lib/offsets.js +217 -0
  41. package/lib/lib/partitionApplyAtRules.js +56 -0
  42. package/lib/lib/regex.js +60 -0
  43. package/lib/lib/resolveDefaultsAtRules.js +114 -81
  44. package/lib/lib/setupContextUtils.js +777 -385
  45. package/lib/lib/setupTrackingContext.js +43 -71
  46. package/lib/lib/sharedState.js +49 -18
  47. package/lib/lib/substituteScreenAtRules.js +13 -8
  48. package/{nesting → lib/postcss-plugins/nesting}/README.md +2 -2
  49. package/lib/postcss-plugins/nesting/index.js +19 -0
  50. package/lib/postcss-plugins/nesting/plugin.js +87 -0
  51. package/lib/processTailwindFeatures.js +36 -21
  52. package/lib/public/colors.js +250 -252
  53. package/lib/public/create-plugin.js +6 -4
  54. package/lib/public/default-config.js +7 -5
  55. package/lib/public/default-theme.js +7 -5
  56. package/lib/public/resolve-config.js +8 -5
  57. package/lib/util/bigSign.js +4 -1
  58. package/lib/util/buildMediaQuery.js +18 -24
  59. package/lib/util/cloneDeep.js +7 -6
  60. package/lib/util/cloneNodes.js +21 -3
  61. package/lib/util/color.js +54 -40
  62. package/lib/util/configurePlugins.js +5 -2
  63. package/lib/util/createPlugin.js +6 -6
  64. package/lib/util/createUtilityPlugin.js +11 -13
  65. package/lib/util/dataTypes.js +137 -100
  66. package/lib/util/defaults.js +10 -1
  67. package/lib/util/escapeClassName.js +7 -4
  68. package/lib/util/escapeCommas.js +5 -2
  69. package/lib/util/flattenColorPalette.js +9 -12
  70. package/lib/util/formatVariantSelector.js +285 -0
  71. package/lib/util/getAllConfigs.js +27 -8
  72. package/lib/util/hashConfig.js +6 -3
  73. package/lib/util/isKeyframeRule.js +5 -2
  74. package/lib/util/isPlainObject.js +5 -2
  75. package/lib/util/isValidArbitraryValue.js +72 -0
  76. package/lib/util/log.js +44 -22
  77. package/lib/util/nameClass.js +20 -8
  78. package/lib/util/negateValue.js +23 -6
  79. package/lib/util/normalizeConfig.js +267 -0
  80. package/lib/util/normalizeScreens.js +170 -0
  81. package/lib/util/parseAnimationValue.js +42 -40
  82. package/lib/util/parseBoxShadowValue.js +84 -0
  83. package/lib/util/parseDependency.js +38 -56
  84. package/lib/util/parseGlob.js +34 -0
  85. package/lib/util/parseObjectStyles.js +11 -8
  86. package/lib/util/pluginUtils.js +171 -189
  87. package/lib/util/prefixSelector.js +10 -10
  88. package/lib/util/removeAlphaVariables.js +29 -0
  89. package/lib/util/resolveConfig.js +92 -126
  90. package/lib/util/resolveConfigPath.js +11 -9
  91. package/lib/util/responsive.js +8 -5
  92. package/lib/util/splitAtTopLevelOnly.js +43 -0
  93. package/lib/util/tap.js +4 -1
  94. package/lib/util/toColorValue.js +5 -3
  95. package/lib/util/toPath.js +24 -3
  96. package/lib/util/transformThemeValue.js +49 -31
  97. package/lib/util/validateConfig.js +24 -0
  98. package/lib/util/validateFormalSyntax.js +24 -0
  99. package/lib/util/withAlphaVariable.js +23 -15
  100. package/nesting/index.js +2 -12
  101. package/package.json +48 -46
  102. package/peers/index.js +14817 -12471
  103. package/plugin.d.ts +11 -0
  104. package/plugin.js +2 -1
  105. package/resolveConfig.d.ts +3 -0
  106. package/resolveConfig.js +2 -1
  107. package/scripts/create-plugin-list.js +2 -2
  108. package/scripts/generate-types.js +105 -0
  109. package/scripts/release-channel.js +18 -0
  110. package/scripts/release-notes.js +21 -0
  111. package/scripts/type-utils.js +27 -0
  112. package/src/cli/build/deps.js +56 -0
  113. package/src/cli/build/index.js +45 -0
  114. package/src/cli/build/plugin.js +397 -0
  115. package/src/cli/build/utils.js +76 -0
  116. package/src/cli/build/watching.js +134 -0
  117. package/src/cli/help/index.js +70 -0
  118. package/src/cli/index.js +3 -0
  119. package/src/cli/init/index.js +50 -0
  120. package/src/cli/shared.js +5 -0
  121. package/src/cli-peer-dependencies.js +7 -1
  122. package/src/cli.js +26 -565
  123. package/src/corePluginList.js +1 -1
  124. package/src/corePlugins.js +905 -615
  125. package/src/css/preflight.css +20 -9
  126. package/src/featureFlags.js +26 -10
  127. package/src/index.js +19 -6
  128. package/src/lib/cacheInvalidation.js +52 -0
  129. package/src/lib/collapseAdjacentRules.js +21 -2
  130. package/src/lib/collapseDuplicateDeclarations.js +93 -0
  131. package/src/lib/content.js +205 -0
  132. package/src/lib/defaultExtractor.js +211 -0
  133. package/src/lib/detectNesting.js +22 -3
  134. package/src/lib/evaluateTailwindFunctions.js +84 -10
  135. package/src/lib/expandApplyAtRules.js +488 -153
  136. package/src/lib/expandTailwindAtRules.js +94 -106
  137. package/src/lib/findAtConfigPath.js +50 -0
  138. package/src/lib/generateRules.js +589 -67
  139. package/src/lib/normalizeTailwindDirectives.js +10 -3
  140. package/src/lib/offsets.js +270 -0
  141. package/src/lib/partitionApplyAtRules.js +52 -0
  142. package/src/lib/regex.js +74 -0
  143. package/src/lib/resolveDefaultsAtRules.js +98 -60
  144. package/src/lib/setupContextUtils.js +737 -246
  145. package/src/lib/setupTrackingContext.js +16 -54
  146. package/src/lib/sharedState.js +44 -7
  147. package/src/lib/substituteScreenAtRules.js +6 -3
  148. package/src/postcss-plugins/nesting/README.md +42 -0
  149. package/src/postcss-plugins/nesting/index.js +13 -0
  150. package/src/postcss-plugins/nesting/plugin.js +80 -0
  151. package/src/processTailwindFeatures.js +17 -2
  152. package/src/public/colors.js +4 -9
  153. package/src/util/buildMediaQuery.js +14 -16
  154. package/src/util/cloneNodes.js +19 -2
  155. package/src/util/color.js +31 -14
  156. package/src/util/createUtilityPlugin.js +2 -2
  157. package/src/util/dataTypes.js +57 -21
  158. package/src/util/defaults.js +6 -0
  159. package/src/util/formatVariantSelector.js +319 -0
  160. package/src/util/getAllConfigs.js +19 -0
  161. package/src/util/isValidArbitraryValue.js +61 -0
  162. package/src/util/log.js +23 -22
  163. package/src/util/nameClass.js +6 -2
  164. package/src/util/negateValue.js +14 -4
  165. package/src/util/normalizeConfig.js +282 -0
  166. package/src/util/normalizeScreens.js +140 -0
  167. package/src/util/parseBoxShadowValue.js +72 -0
  168. package/src/util/parseDependency.js +37 -42
  169. package/src/util/parseGlob.js +24 -0
  170. package/src/util/pluginUtils.js +144 -152
  171. package/src/util/prefixSelector.js +7 -8
  172. package/src/util/removeAlphaVariables.js +24 -0
  173. package/src/util/resolveConfig.js +82 -81
  174. package/src/util/splitAtTopLevelOnly.js +45 -0
  175. package/src/util/toPath.js +23 -1
  176. package/src/util/transformThemeValue.js +33 -8
  177. package/src/util/validateConfig.js +13 -0
  178. package/src/util/validateFormalSyntax.js +34 -0
  179. package/src/util/withAlphaVariable.js +1 -1
  180. package/stubs/defaultConfig.stub.js +173 -93
  181. package/stubs/simpleConfig.stub.js +1 -1
  182. package/types/config.d.ts +361 -0
  183. package/types/generated/.gitkeep +0 -0
  184. package/types/generated/colors.d.ts +276 -0
  185. package/types/generated/corePluginList.d.ts +1 -0
  186. package/types/generated/default-theme.d.ts +342 -0
  187. package/types/index.d.ts +7 -0
  188. package/lib/lib/setupWatchingContext.js +0 -284
  189. package/nesting/plugin.js +0 -41
  190. package/src/lib/setupWatchingContext.js +0 -306
@@ -0,0 +1,50 @@
1
+ // @ts-check
2
+
3
+ import fs from 'fs'
4
+ import path from 'path'
5
+
6
+ export function init(args, configs) {
7
+ let messages = []
8
+
9
+ let tailwindConfigLocation = path.resolve(args['_'][1] ?? `./${configs.tailwind}`)
10
+ if (fs.existsSync(tailwindConfigLocation)) {
11
+ messages.push(`${path.basename(tailwindConfigLocation)} already exists.`)
12
+ } else {
13
+ let stubFile = fs.readFileSync(
14
+ args['--full']
15
+ ? path.resolve(__dirname, '../../../stubs/defaultConfig.stub.js')
16
+ : path.resolve(__dirname, '../../../stubs/simpleConfig.stub.js'),
17
+ 'utf8'
18
+ )
19
+
20
+ // Change colors import
21
+ stubFile = stubFile.replace('../colors', 'tailwindcss/colors')
22
+
23
+ fs.writeFileSync(tailwindConfigLocation, stubFile, 'utf8')
24
+
25
+ messages.push(`Created Tailwind CSS config file: ${path.basename(tailwindConfigLocation)}`)
26
+ }
27
+
28
+ if (args['--postcss']) {
29
+ let postcssConfigLocation = path.resolve(`./${configs.postcss}`)
30
+ if (fs.existsSync(postcssConfigLocation)) {
31
+ messages.push(`${path.basename(postcssConfigLocation)} already exists.`)
32
+ } else {
33
+ let stubFile = fs.readFileSync(
34
+ path.resolve(__dirname, '../../../stubs/defaultPostCssConfig.stub.js'),
35
+ 'utf8'
36
+ )
37
+
38
+ fs.writeFileSync(postcssConfigLocation, stubFile, 'utf8')
39
+
40
+ messages.push(`Created PostCSS config file: ${path.basename(postcssConfigLocation)}`)
41
+ }
42
+ }
43
+
44
+ if (messages.length > 0) {
45
+ console.log()
46
+ for (let message of messages) {
47
+ console.log(message)
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,5 @@
1
+ // @ts-check
2
+
3
+ export const env = {
4
+ DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
5
+ }
@@ -1,4 +1,10 @@
1
- export let postcss = require('postcss')
1
+ export function lazyPostcss() {
2
+ return require('postcss')
3
+ }
4
+
5
+ export function lazyPostcssImport() {
6
+ return require('postcss-import')
7
+ }
2
8
 
3
9
  export function lazyAutoprefixer() {
4
10
  return require('autoprefixer')
package/src/cli.js CHANGED
@@ -1,113 +1,35 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { postcss, lazyCssnano, lazyAutoprefixer } from '../peers/index.js'
4
-
5
- import chokidar from 'chokidar'
6
3
  import path from 'path'
7
4
  import arg from 'arg'
8
5
  import fs from 'fs'
9
- import postcssrc from 'postcss-load-config'
10
- import { cosmiconfig } from 'cosmiconfig'
11
- import loadPlugins from 'postcss-load-config/src/plugins' // Little bit scary, looking at private/internal API
12
- import tailwind from './processTailwindFeatures'
13
- import resolveConfigInternal from '../resolveConfig'
14
- import fastGlob from 'fast-glob'
15
- import getModuleDependencies from './lib/getModuleDependencies'
16
- import log from './util/log'
17
- import packageJson from '../package.json'
18
- import normalizePath from 'normalize-path'
19
6
 
20
- let env = {
21
- DEBUG: process.env.DEBUG !== undefined,
22
- }
7
+ import { build } from './cli/build'
8
+ import { help } from './cli/help'
9
+ import { init } from './cli/init'
23
10
 
24
- // ---
11
+ function isESM() {
12
+ const pkgPath = path.resolve('./package.json')
25
13
 
26
- function indentRecursive(node, indent = 0) {
27
- node.each &&
28
- node.each((child, i) => {
29
- if (!child.raws.before || !child.raws.before.trim() || child.raws.before.includes('\n')) {
30
- child.raws.before = `\n${node.type !== 'rule' && i > 0 ? '\n' : ''}${' '.repeat(indent)}`
31
- }
32
- child.raws.after = `\n${' '.repeat(indent)}`
33
- indentRecursive(child, indent + 1)
34
- })
35
- }
36
-
37
- function formatNodes(root) {
38
- indentRecursive(root)
39
- if (root.first) {
40
- root.first.raws.before = ''
14
+ try {
15
+ let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
16
+ return pkg.type && pkg.type === 'module'
17
+ } catch (err) {
18
+ return false
41
19
  }
42
20
  }
43
21
 
44
- function help({ message, usage, commands, options }) {
45
- let indent = 2
46
-
47
- // Render header
48
- console.log()
49
- console.log(`${packageJson.name} v${packageJson.version}`)
50
-
51
- // Render message
52
- if (message) {
53
- console.log()
54
- for (let msg of message.split('\n')) {
55
- console.log(msg)
22
+ let configs = isESM()
23
+ ? {
24
+ tailwind: 'tailwind.config.cjs',
25
+ postcss: 'postcss.config.cjs',
56
26
  }
57
- }
58
-
59
- // Render usage
60
- if (usage && usage.length > 0) {
61
- console.log()
62
- console.log('Usage:')
63
- for (let example of usage) {
64
- console.log(' '.repeat(indent), example)
65
- }
66
- }
67
-
68
- // Render commands
69
- if (commands && commands.length > 0) {
70
- console.log()
71
- console.log('Commands:')
72
- for (let command of commands) {
73
- console.log(' '.repeat(indent), command)
27
+ : {
28
+ tailwind: 'tailwind.config.js',
29
+ postcss: 'postcss.config.js',
74
30
  }
75
- }
76
-
77
- // Render options
78
- if (options) {
79
- let groupedOptions = {}
80
- for (let [key, value] of Object.entries(options)) {
81
- if (typeof value === 'object') {
82
- groupedOptions[key] = { ...value, flags: [key] }
83
- } else {
84
- groupedOptions[value].flags.push(key)
85
- }
86
- }
87
-
88
- console.log()
89
- console.log('Options:')
90
- for (let { flags, description, deprecated } of Object.values(groupedOptions)) {
91
- if (deprecated) continue
92
31
 
93
- if (flags.length === 1) {
94
- console.log(
95
- ' '.repeat(indent + 4 /* 4 = "-i, ".length */),
96
- flags.slice().reverse().join(', ').padEnd(20, ' '),
97
- description
98
- )
99
- } else {
100
- console.log(
101
- ' '.repeat(indent),
102
- flags.slice().reverse().join(', ').padEnd(24, ' '),
103
- description
104
- )
105
- }
106
- }
107
- }
108
-
109
- console.log()
110
- }
32
+ // ---
111
33
 
112
34
  function oneOf(...options) {
113
35
  return Object.assign(
@@ -129,8 +51,8 @@ let commands = {
129
51
  init: {
130
52
  run: init,
131
53
  args: {
132
- '--full': { type: Boolean, description: 'Initialize a full `tailwind.config.js` file' },
133
- '--postcss': { type: Boolean, description: 'Initialize a `postcss.config.js` file' },
54
+ '--full': { type: Boolean, description: `Initialize a full \`${configs.tailwind}\` file` },
55
+ '--postcss': { type: Boolean, description: `Initialize a \`${configs.postcss}\` file` },
134
56
  '-f': '--full',
135
57
  '-p': '--postcss',
136
58
  },
@@ -141,6 +63,10 @@ let commands = {
141
63
  '--input': { type: String, description: 'Input file' },
142
64
  '--output': { type: String, description: 'Output file' },
143
65
  '--watch': { type: Boolean, description: 'Watch for changes and rebuild as needed' },
66
+ '--poll': {
67
+ type: Boolean,
68
+ description: 'Use polling instead of filesystem events when watching',
69
+ },
144
70
  '--content': {
145
71
  type: String,
146
72
  description: 'Content paths to use for removing unused classes',
@@ -167,6 +93,7 @@ let commands = {
167
93
  '-o': '--output',
168
94
  '-m': '--minify',
169
95
  '-w': '--watch',
96
+ '-p': '--poll',
170
97
  },
171
98
  },
172
99
  }
@@ -293,470 +220,4 @@ if (args['--help']) {
293
220
  process.exit(0)
294
221
  }
295
222
 
296
- run()
297
-
298
- // ---
299
-
300
- function init() {
301
- let messages = []
302
-
303
- let tailwindConfigLocation = path.resolve(args['_'][1] ?? './tailwind.config.js')
304
- if (fs.existsSync(tailwindConfigLocation)) {
305
- messages.push(`${path.basename(tailwindConfigLocation)} already exists.`)
306
- } else {
307
- let stubFile = fs.readFileSync(
308
- args['--full']
309
- ? path.resolve(__dirname, '../stubs/defaultConfig.stub.js')
310
- : path.resolve(__dirname, '../stubs/simpleConfig.stub.js'),
311
- 'utf8'
312
- )
313
-
314
- // Change colors import
315
- stubFile = stubFile.replace('../colors', 'tailwindcss/colors')
316
-
317
- fs.writeFileSync(tailwindConfigLocation, stubFile, 'utf8')
318
-
319
- messages.push(`Created Tailwind CSS config file: ${path.basename(tailwindConfigLocation)}`)
320
- }
321
-
322
- if (args['--postcss']) {
323
- let postcssConfigLocation = path.resolve('./postcss.config.js')
324
- if (fs.existsSync(postcssConfigLocation)) {
325
- messages.push(`${path.basename(postcssConfigLocation)} already exists.`)
326
- } else {
327
- let stubFile = fs.readFileSync(
328
- path.resolve(__dirname, '../stubs/defaultPostCssConfig.stub.js'),
329
- 'utf8'
330
- )
331
-
332
- fs.writeFileSync(postcssConfigLocation, stubFile, 'utf8')
333
-
334
- messages.push(`Created PostCSS config file: ${path.basename(postcssConfigLocation)}`)
335
- }
336
- }
337
-
338
- if (messages.length > 0) {
339
- console.log()
340
- for (let message of messages) {
341
- console.log(message)
342
- }
343
- }
344
- }
345
-
346
- async function build() {
347
- let input = args['--input']
348
- let output = args['--output']
349
- let shouldWatch = args['--watch']
350
- let includePostCss = args['--postcss']
351
-
352
- // TODO: Deprecate this in future versions
353
- if (!input && args['_'][1]) {
354
- console.error('[deprecation] Running tailwindcss without -i, please provide an input file.')
355
- input = args['--input'] = args['_'][1]
356
- }
357
-
358
- if (input && !fs.existsSync((input = path.resolve(input)))) {
359
- console.error(`Specified input file ${args['--input']} does not exist.`)
360
- process.exit(9)
361
- }
362
-
363
- if (args['--config'] && !fs.existsSync((args['--config'] = path.resolve(args['--config'])))) {
364
- console.error(`Specified config file ${args['--config']} does not exist.`)
365
- process.exit(9)
366
- }
367
-
368
- let configPath = args['--config']
369
- ? args['--config']
370
- : ((defaultPath) => (fs.existsSync(defaultPath) ? defaultPath : null))(
371
- path.resolve('./tailwind.config.js')
372
- )
373
-
374
- async function loadPostCssPlugins() {
375
- let customPostCssPath = typeof args['--postcss'] === 'string' ? args['--postcss'] : undefined
376
- let { plugins: configPlugins } = customPostCssPath
377
- ? await (async () => {
378
- let file = path.resolve(customPostCssPath)
379
-
380
- // Implementation, see: https://unpkg.com/browse/postcss-load-config@3.0.1/src/index.js
381
- let { config = {} } = await cosmiconfig('postcss').load(file)
382
- if (typeof config === 'function') {
383
- config = config()
384
- } else {
385
- config = Object.assign({}, config)
386
- }
387
-
388
- if (!config.plugins) {
389
- config.plugins = []
390
- }
391
-
392
- return { plugins: loadPlugins(config, file) }
393
- })()
394
- : await postcssrc()
395
-
396
- let configPluginTailwindIdx = configPlugins.findIndex((plugin) => {
397
- if (typeof plugin === 'function' && plugin.name === 'tailwindcss') {
398
- return true
399
- }
400
-
401
- if (typeof plugin === 'object' && plugin !== null && plugin.postcssPlugin === 'tailwindcss') {
402
- return true
403
- }
404
-
405
- return false
406
- })
407
-
408
- let beforePlugins =
409
- configPluginTailwindIdx === -1 ? [] : configPlugins.slice(0, configPluginTailwindIdx)
410
- let afterPlugins =
411
- configPluginTailwindIdx === -1
412
- ? configPlugins
413
- : configPlugins.slice(configPluginTailwindIdx + 1)
414
-
415
- return [beforePlugins, afterPlugins]
416
- }
417
-
418
- function resolveConfig() {
419
- let config = configPath ? require(configPath) : {}
420
- let resolvedConfig = resolveConfigInternal(config)
421
-
422
- if (args['--purge']) {
423
- log.warn(['The `--purge` flag has been deprecated.', 'Please use `--content` instead.'])
424
- if (!args['--content']) {
425
- args['--content'] = ['--purge']
426
- }
427
- }
428
-
429
- if (args['--content']) {
430
- resolvedConfig.content = args['--content'].split(/(?<!{[^}]+),/)
431
- }
432
-
433
- return resolvedConfig
434
- }
435
-
436
- function extractContent(config) {
437
- return config.content.content.concat(config.content.safelist)
438
- }
439
-
440
- function extractFileGlobs(config) {
441
- return extractContent(config)
442
- .filter((file) => {
443
- // Strings in this case are files / globs. If it is something else,
444
- // like an object it's probably a raw content object. But this object
445
- // is not watchable, so let's remove it.
446
- return typeof file === 'string'
447
- })
448
- .map((glob) => normalizePath(glob))
449
- }
450
-
451
- function extractRawContent(config) {
452
- return extractContent(config).filter((file) => {
453
- return typeof file === 'object' && file !== null
454
- })
455
- }
456
-
457
- function getChangedContent(config) {
458
- let changedContent = []
459
-
460
- // Resolve globs from the content config
461
- let globs = extractFileGlobs(config)
462
- let files = fastGlob.sync(globs)
463
-
464
- for (let file of files) {
465
- changedContent.push({
466
- content: fs.readFileSync(path.resolve(file), 'utf8'),
467
- extension: path.extname(file),
468
- })
469
- }
470
-
471
- // Resolve raw content in the tailwind config
472
- for (let { raw: content, extension = 'html' } of extractRawContent(config)) {
473
- changedContent.push({ content, extension })
474
- }
475
-
476
- return changedContent
477
- }
478
-
479
- async function buildOnce() {
480
- let config = resolveConfig()
481
- let changedContent = getChangedContent(config)
482
-
483
- let tailwindPlugin = () => {
484
- return {
485
- postcssPlugin: 'tailwindcss',
486
- Once(root, { result }) {
487
- tailwind(({ createContext }) => {
488
- return () => {
489
- return createContext(config, changedContent)
490
- }
491
- })(root, result)
492
- },
493
- }
494
- }
495
-
496
- tailwindPlugin.postcss = true
497
-
498
- let [beforePlugins, afterPlugins] = includePostCss ? await loadPostCssPlugins() : [[], []]
499
-
500
- let plugins = [
501
- ...beforePlugins,
502
- tailwindPlugin,
503
- !args['--minify'] && formatNodes,
504
- ...afterPlugins,
505
- !args['--no-autoprefixer'] &&
506
- (() => {
507
- // Try to load a local `autoprefixer` version first
508
- try {
509
- return require('autoprefixer')
510
- } catch {}
511
-
512
- return lazyAutoprefixer()
513
- })(),
514
- args['--minify'] &&
515
- (() => {
516
- let options = { preset: ['default', { cssDeclarationSorter: false }] }
517
-
518
- // Try to load a local `cssnano` version first
519
- try {
520
- return require('cssnano')
521
- } catch {}
522
-
523
- return lazyCssnano()(options)
524
- })(),
525
- ].filter(Boolean)
526
-
527
- let processor = postcss(plugins)
528
-
529
- function processCSS(css) {
530
- let start = process.hrtime.bigint()
531
- return Promise.resolve()
532
- .then(() => (output ? fs.promises.mkdir(path.dirname(output), { recursive: true }) : null))
533
- .then(() => processor.process(css, { from: input, to: output }))
534
- .then((result) => {
535
- if (!output) {
536
- return process.stdout.write(result.css)
537
- }
538
- return Promise.all(
539
- [
540
- fs.promises.writeFile(output, result.css, () => true),
541
- result.map && fs.writeFile(output + '.map', result.map.toString(), () => true),
542
- ].filter(Boolean)
543
- )
544
- })
545
- .then(() => {
546
- let end = process.hrtime.bigint()
547
- console.error()
548
- console.error('Done in', (end - start) / BigInt(1e6) + 'ms.')
549
- })
550
- }
551
-
552
- let css = input
553
- ? fs.readFileSync(path.resolve(input), 'utf8')
554
- : '@tailwind base; @tailwind components; @tailwind utilities'
555
- return processCSS(css)
556
- }
557
-
558
- let context = null
559
-
560
- async function startWatcher() {
561
- let changedContent = []
562
- let configDependencies = []
563
- let contextDependencies = new Set()
564
- let watcher = null
565
-
566
- function refreshConfig() {
567
- env.DEBUG && console.time('Module dependencies')
568
- for (let file of configDependencies) {
569
- delete require.cache[require.resolve(file)]
570
- }
571
-
572
- if (configPath) {
573
- configDependencies = getModuleDependencies(configPath).map(({ file }) => file)
574
-
575
- for (let dependency of configDependencies) {
576
- contextDependencies.add(dependency)
577
- }
578
- }
579
- env.DEBUG && console.timeEnd('Module dependencies')
580
-
581
- return resolveConfig()
582
- }
583
-
584
- let [beforePlugins, afterPlugins] = includePostCss ? await loadPostCssPlugins() : [[], []]
585
-
586
- let plugins = [
587
- ...beforePlugins,
588
- '__TAILWIND_PLUGIN_POSITION__',
589
- !args['--minify'] && formatNodes,
590
- ...afterPlugins,
591
- !args['--no-autoprefixer'] &&
592
- (() => {
593
- // Try to load a local `autoprefixer` version first
594
- try {
595
- return require('autoprefixer')
596
- } catch {}
597
-
598
- return lazyAutoprefixer()
599
- })(),
600
- args['--minify'] &&
601
- (() => {
602
- let options = { preset: ['default', { cssDeclarationSorter: false }] }
603
-
604
- // Try to load a local `cssnano` version first
605
- try {
606
- return require('cssnano')
607
- } catch {}
608
-
609
- return lazyCssnano()(options)
610
- })(),
611
- ].filter(Boolean)
612
-
613
- async function rebuild(config) {
614
- env.DEBUG && console.time('Finished in')
615
-
616
- let tailwindPlugin = () => {
617
- return {
618
- postcssPlugin: 'tailwindcss',
619
- Once(root, { result }) {
620
- env.DEBUG && console.time('Compiling CSS')
621
- tailwind(({ createContext }) => {
622
- console.error()
623
- console.error('Rebuilding...')
624
-
625
- return () => {
626
- if (context !== null) {
627
- context.changedContent = changedContent.splice(0)
628
- return context
629
- }
630
-
631
- env.DEBUG && console.time('Creating context')
632
- context = createContext(config, changedContent.splice(0))
633
- env.DEBUG && console.timeEnd('Creating context')
634
- return context
635
- }
636
- })(root, result)
637
- env.DEBUG && console.timeEnd('Compiling CSS')
638
- },
639
- }
640
- }
641
-
642
- tailwindPlugin.postcss = true
643
-
644
- let tailwindPluginIdx = plugins.indexOf('__TAILWIND_PLUGIN_POSITION__')
645
- let copy = plugins.slice()
646
- copy.splice(tailwindPluginIdx, 1, tailwindPlugin)
647
- let processor = postcss(copy)
648
-
649
- function processCSS(css) {
650
- let start = process.hrtime.bigint()
651
- return Promise.resolve()
652
- .then(() =>
653
- output ? fs.promises.mkdir(path.dirname(output), { recursive: true }) : null
654
- )
655
- .then(() => processor.process(css, { from: input, to: output }))
656
- .then(async (result) => {
657
- for (let message of result.messages) {
658
- if (message.type === 'dependency') {
659
- contextDependencies.add(message.file)
660
- }
661
- }
662
- watcher.add([...contextDependencies])
663
-
664
- if (!output) {
665
- return process.stdout.write(result.css)
666
- }
667
-
668
- await Promise.all(
669
- [
670
- fs.promises.writeFile(output, result.css, () => true),
671
- result.map && fs.writeFile(output + '.map', result.map.toString(), () => true),
672
- ].filter(Boolean)
673
- )
674
- })
675
- .then(() => {
676
- let end = process.hrtime.bigint()
677
- console.error('Done in', (end - start) / BigInt(1e6) + 'ms.')
678
- })
679
- .catch((err) => {
680
- if (err.name === 'CssSyntaxError') {
681
- console.error(err.toString())
682
- } else {
683
- console.error(err)
684
- }
685
- })
686
- }
687
-
688
- let css = input
689
- ? fs.readFileSync(path.resolve(input), 'utf8')
690
- : '@tailwind base; @tailwind components; @tailwind utilities'
691
- let result = await processCSS(css)
692
- env.DEBUG && console.timeEnd('Finished in')
693
- return result
694
- }
695
-
696
- let config = refreshConfig(configPath)
697
-
698
- if (input) {
699
- contextDependencies.add(path.resolve(input))
700
- }
701
-
702
- watcher = chokidar.watch([...contextDependencies, ...extractFileGlobs(config)], {
703
- ignoreInitial: true,
704
- })
705
-
706
- let chain = Promise.resolve()
707
-
708
- watcher.on('change', async (file) => {
709
- if (contextDependencies.has(file)) {
710
- env.DEBUG && console.time('Resolve config')
711
- context = null
712
- config = refreshConfig(configPath)
713
- env.DEBUG && console.timeEnd('Resolve config')
714
-
715
- env.DEBUG && console.time('Watch new files')
716
- let globs = extractFileGlobs(config)
717
- watcher.add(configDependencies)
718
- watcher.add(globs)
719
- env.DEBUG && console.timeEnd('Watch new files')
720
-
721
- chain = chain.then(async () => {
722
- changedContent.push(...getChangedContent(config))
723
- await rebuild(config)
724
- })
725
- } else {
726
- chain = chain.then(async () => {
727
- changedContent.push({
728
- content: fs.readFileSync(path.resolve(file), 'utf8'),
729
- extension: path.extname(file),
730
- })
731
-
732
- await rebuild(config)
733
- })
734
- }
735
- })
736
-
737
- watcher.on('add', async (file) => {
738
- chain = chain.then(async () => {
739
- changedContent.push({
740
- content: fs.readFileSync(path.resolve(file), 'utf8'),
741
- extension: path.extname(file),
742
- })
743
-
744
- await rebuild(config)
745
- })
746
- })
747
-
748
- chain = chain.then(() => {
749
- changedContent.push(...getChangedContent(config))
750
- return rebuild(config)
751
- })
752
- }
753
-
754
- if (shouldWatch) {
755
- /* Abort the watcher if stdin is closed to avoid zombie processes */
756
- process.stdin.on('end', () => process.exit(0))
757
- process.stdin.resume()
758
- startWatcher()
759
- } else {
760
- buildOnce()
761
- }
762
- }
223
+ run(args, configs)