tailwindcss 3.2.4 → 3.2.5

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 (100) hide show
  1. package/CHANGELOG.md +44 -1
  2. package/README.md +1 -1
  3. package/lib/cli/build/index.js +5 -1
  4. package/lib/cli/build/plugin.js +39 -34
  5. package/lib/cli/index.js +231 -10
  6. package/lib/cli/init/index.js +2 -2
  7. package/lib/cli.js +4 -226
  8. package/lib/corePlugins.js +45 -27
  9. package/lib/featureFlags.js +8 -8
  10. package/lib/index.js +4 -46
  11. package/lib/lib/collapseAdjacentRules.js +2 -2
  12. package/lib/lib/collapseDuplicateDeclarations.js +2 -2
  13. package/lib/lib/content.js +16 -16
  14. package/lib/lib/defaultExtractor.js +10 -5
  15. package/lib/lib/detectNesting.js +7 -1
  16. package/lib/lib/evaluateTailwindFunctions.js +4 -4
  17. package/lib/lib/expandApplyAtRules.js +2 -2
  18. package/lib/lib/expandTailwindAtRules.js +35 -9
  19. package/lib/lib/findAtConfigPath.js +3 -3
  20. package/lib/lib/generateRules.js +105 -50
  21. package/lib/lib/offsets.js +88 -1
  22. package/lib/lib/remap-bitfield.js +87 -0
  23. package/lib/lib/resolveDefaultsAtRules.js +4 -4
  24. package/lib/lib/setupContextUtils.js +121 -80
  25. package/lib/lib/setupTrackingContext.js +25 -4
  26. package/lib/lib/sharedState.js +19 -1
  27. package/lib/oxide/cli/build/deps.js +81 -0
  28. package/lib/oxide/cli/build/index.js +47 -0
  29. package/lib/oxide/cli/build/plugin.js +364 -0
  30. package/lib/oxide/cli/build/utils.js +77 -0
  31. package/lib/oxide/cli/build/watching.js +177 -0
  32. package/lib/oxide/cli/help/index.js +70 -0
  33. package/lib/oxide/cli/index.js +220 -0
  34. package/lib/oxide/cli/init/index.js +35 -0
  35. package/lib/oxide/cli.js +5 -0
  36. package/lib/oxide/postcss-plugin.js +2 -0
  37. package/lib/plugin.js +98 -0
  38. package/lib/postcss-plugins/nesting/plugin.js +2 -2
  39. package/lib/util/cloneNodes.js +2 -2
  40. package/lib/util/color.js +20 -6
  41. package/lib/util/createUtilityPlugin.js +2 -2
  42. package/lib/util/dataTypes.js +26 -2
  43. package/lib/util/defaults.js +4 -4
  44. package/lib/util/escapeClassName.js +3 -3
  45. package/lib/util/formatVariantSelector.js +171 -105
  46. package/lib/util/getAllConfigs.js +2 -2
  47. package/lib/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +2 -2
  48. package/lib/util/negateValue.js +2 -2
  49. package/lib/util/normalizeConfig.js +22 -22
  50. package/lib/util/pluginUtils.js +38 -40
  51. package/lib/util/prefixSelector.js +22 -8
  52. package/lib/util/resolveConfig.js +8 -10
  53. package/oxide-node-api-shim/index.js +21 -0
  54. package/oxide-node-api-shim/package.json +5 -0
  55. package/package.json +32 -19
  56. package/peers/index.js +61 -42
  57. package/resolveConfig.d.ts +11 -2
  58. package/scripts/swap-engines.js +40 -0
  59. package/src/cli/build/index.js +6 -2
  60. package/src/cli/build/plugin.js +14 -9
  61. package/src/cli/index.js +234 -3
  62. package/src/cli.js +4 -220
  63. package/src/corePlugins.js +31 -3
  64. package/src/index.js +4 -46
  65. package/src/lib/content.js +12 -17
  66. package/src/lib/defaultExtractor.js +9 -3
  67. package/src/lib/detectNesting.js +9 -1
  68. package/src/lib/expandTailwindAtRules.js +37 -6
  69. package/src/lib/generateRules.js +90 -28
  70. package/src/lib/offsets.js +104 -1
  71. package/src/lib/remap-bitfield.js +82 -0
  72. package/src/lib/setupContextUtils.js +97 -55
  73. package/src/lib/setupTrackingContext.js +31 -6
  74. package/src/lib/sharedState.js +17 -0
  75. package/src/oxide/cli/build/deps.ts +91 -0
  76. package/src/oxide/cli/build/index.ts +47 -0
  77. package/src/oxide/cli/build/plugin.ts +436 -0
  78. package/src/oxide/cli/build/utils.ts +74 -0
  79. package/src/oxide/cli/build/watching.ts +225 -0
  80. package/src/oxide/cli/help/index.ts +69 -0
  81. package/src/oxide/cli/index.ts +212 -0
  82. package/src/oxide/cli/init/index.ts +32 -0
  83. package/src/oxide/cli.ts +1 -0
  84. package/src/oxide/postcss-plugin.ts +1 -0
  85. package/src/plugin.js +107 -0
  86. package/src/util/color.js +17 -2
  87. package/src/util/dataTypes.js +29 -4
  88. package/src/util/formatVariantSelector.js +215 -122
  89. package/src/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +1 -1
  90. package/src/util/negateValue.js +1 -1
  91. package/src/util/pluginUtils.js +22 -19
  92. package/src/util/prefixSelector.js +28 -10
  93. package/src/util/resolveConfig.js +0 -2
  94. package/stubs/defaultConfig.stub.js +149 -165
  95. package/types/config.d.ts +7 -2
  96. package/types/generated/default-theme.d.ts +77 -77
  97. package/lib/cli/shared.js +0 -12
  98. package/scripts/install-integrations.js +0 -27
  99. package/scripts/rebuildFixtures.js +0 -68
  100. package/src/cli/shared.js +0 -5
@@ -0,0 +1,436 @@
1
+ import path from 'path'
2
+ import fs from 'fs'
3
+ import postcssrc from 'postcss-load-config'
4
+ import { lilconfig } from 'lilconfig'
5
+ import loadPlugins from 'postcss-load-config/src/plugins' // Little bit scary, looking at private/internal API
6
+ import loadOptions from 'postcss-load-config/src/options' // Little bit scary, looking at private/internal API
7
+
8
+ import tailwind from '../../../processTailwindFeatures'
9
+ import { loadPostcss, loadPostcssImport, lightningcss } from './deps'
10
+ import { formatNodes, drainStdin, outputFile } from './utils'
11
+ import { env } from '../../../lib/sharedState'
12
+ import resolveConfig from '../../../../resolveConfig'
13
+ import getModuleDependencies from '../../../lib/getModuleDependencies'
14
+ import { parseCandidateFiles } from '../../../lib/content'
15
+ import { createWatcher } from './watching'
16
+ import fastGlob from 'fast-glob'
17
+ import { findAtConfigPath } from '../../../lib/findAtConfigPath'
18
+ import log from '../../../util/log'
19
+
20
+ /**
21
+ *
22
+ * @param {string} [customPostCssPath ]
23
+ * @returns
24
+ */
25
+ async function loadPostCssPlugins(customPostCssPath) {
26
+ let config = customPostCssPath
27
+ ? await (async () => {
28
+ let file = path.resolve(customPostCssPath)
29
+
30
+ // Implementation, see: https://unpkg.com/browse/postcss-load-config@3.1.0/src/index.js
31
+ // @ts-ignore
32
+ let { config = {} } = await lilconfig('postcss').load(file)
33
+ if (typeof config === 'function') {
34
+ config = config()
35
+ } else {
36
+ config = Object.assign({}, config)
37
+ }
38
+
39
+ if (!config.plugins) {
40
+ config.plugins = []
41
+ }
42
+
43
+ return {
44
+ file,
45
+ plugins: loadPlugins(config, file),
46
+ options: loadOptions(config, file),
47
+ }
48
+ })()
49
+ : await postcssrc()
50
+
51
+ let configPlugins = config.plugins
52
+
53
+ let configPluginTailwindIdx = configPlugins.findIndex((plugin) => {
54
+ if (typeof plugin === 'function' && plugin.name === 'tailwindcss') {
55
+ return true
56
+ }
57
+
58
+ if (typeof plugin === 'object' && plugin !== null && plugin.postcssPlugin === 'tailwindcss') {
59
+ return true
60
+ }
61
+
62
+ return false
63
+ })
64
+
65
+ let beforePlugins =
66
+ configPluginTailwindIdx === -1 ? [] : configPlugins.slice(0, configPluginTailwindIdx)
67
+ let afterPlugins =
68
+ configPluginTailwindIdx === -1
69
+ ? configPlugins
70
+ : configPlugins.slice(configPluginTailwindIdx + 1)
71
+
72
+ return [beforePlugins, afterPlugins, config.options]
73
+ }
74
+
75
+ function loadBuiltinPostcssPlugins() {
76
+ let postcss = loadPostcss()
77
+ let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '
78
+ return [
79
+ [
80
+ (root) => {
81
+ root.walkAtRules('import', (rule) => {
82
+ if (rule.params.slice(1).startsWith('tailwindcss/')) {
83
+ rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
84
+ rule.remove()
85
+ }
86
+ })
87
+ },
88
+ loadPostcssImport(),
89
+ (root) => {
90
+ root.walkComments((rule) => {
91
+ if (rule.text.startsWith(IMPORT_COMMENT)) {
92
+ rule.after(
93
+ postcss.atRule({
94
+ name: 'import',
95
+ params: rule.text.replace(IMPORT_COMMENT, ''),
96
+ })
97
+ )
98
+ rule.remove()
99
+ }
100
+ })
101
+ },
102
+ ],
103
+ [],
104
+ {},
105
+ ]
106
+ }
107
+
108
+ let state = {
109
+ /** @type {any} */
110
+ context: null,
111
+
112
+ /** @type {ReturnType<typeof createWatcher> | null} */
113
+ watcher: null,
114
+
115
+ /** @type {{content: string, extension: string}[]} */
116
+ changedContent: [],
117
+
118
+ configDependencies: new Set(),
119
+ contextDependencies: new Set(),
120
+
121
+ /** @type {import('../../lib/content.js').ContentPath[]} */
122
+ contentPaths: [],
123
+
124
+ refreshContentPaths() {
125
+ this.contentPaths = parseCandidateFiles(this.context, this.context?.tailwindConfig)
126
+ },
127
+
128
+ get config() {
129
+ return this.context.tailwindConfig
130
+ },
131
+
132
+ get contentPatterns() {
133
+ return {
134
+ all: this.contentPaths.map((contentPath) => contentPath.pattern),
135
+ dynamic: this.contentPaths
136
+ .filter((contentPath) => contentPath.glob !== undefined)
137
+ .map((contentPath) => contentPath.pattern),
138
+ }
139
+ },
140
+
141
+ loadConfig(configPath, content) {
142
+ if (this.watcher && configPath) {
143
+ this.refreshConfigDependencies(configPath)
144
+ }
145
+
146
+ let config = configPath ? require(configPath) : {}
147
+
148
+ // @ts-ignore
149
+ config = resolveConfig(config, { content: { files: [] } })
150
+
151
+ // Override content files if `--content` has been passed explicitly
152
+ if (content?.length > 0) {
153
+ config.content.files = content
154
+ }
155
+
156
+ return config
157
+ },
158
+
159
+ refreshConfigDependencies(configPath) {
160
+ env.DEBUG && console.time('Module dependencies')
161
+
162
+ for (let file of this.configDependencies) {
163
+ delete require.cache[require.resolve(file)]
164
+ }
165
+
166
+ if (configPath) {
167
+ let deps = getModuleDependencies(configPath).map(({ file }) => file)
168
+
169
+ for (let dependency of deps) {
170
+ this.configDependencies.add(dependency)
171
+ }
172
+ }
173
+
174
+ env.DEBUG && console.timeEnd('Module dependencies')
175
+ },
176
+
177
+ readContentPaths() {
178
+ let content = []
179
+
180
+ // Resolve globs from the content config
181
+ // TODO: When we make the postcss plugin async-capable this can become async
182
+ let files = fastGlob.sync(this.contentPatterns.all)
183
+
184
+ for (let file of files) {
185
+ if (env.OXIDE) {
186
+ content.push({
187
+ file,
188
+ extension: path.extname(file).slice(1),
189
+ })
190
+ } else {
191
+ content.push({
192
+ content: fs.readFileSync(path.resolve(file), 'utf8'),
193
+ extension: path.extname(file).slice(1),
194
+ })
195
+ }
196
+ }
197
+
198
+ // Resolve raw content in the tailwind config
199
+ let rawContent = this.config.content.files.filter((file) => {
200
+ return file !== null && typeof file === 'object'
201
+ })
202
+
203
+ for (let { raw: htmlContent, extension = 'html' } of rawContent) {
204
+ content.push({ content: htmlContent, extension })
205
+ }
206
+
207
+ return content
208
+ },
209
+
210
+ getContext({ createContext, cliConfigPath, root, result, content }) {
211
+ if (this.context) {
212
+ this.context.changedContent = this.changedContent.splice(0)
213
+
214
+ return this.context
215
+ }
216
+
217
+ env.DEBUG && console.time('Searching for config')
218
+ let configPath = findAtConfigPath(root, result) ?? cliConfigPath
219
+ env.DEBUG && console.timeEnd('Searching for config')
220
+
221
+ env.DEBUG && console.time('Loading config')
222
+ let config = this.loadConfig(configPath, content)
223
+ env.DEBUG && console.timeEnd('Loading config')
224
+
225
+ env.DEBUG && console.time('Creating context')
226
+ this.context = createContext(config, [])
227
+ Object.assign(this.context, {
228
+ userConfigPath: configPath,
229
+ })
230
+ env.DEBUG && console.timeEnd('Creating context')
231
+
232
+ env.DEBUG && console.time('Resolving content paths')
233
+ this.refreshContentPaths()
234
+ env.DEBUG && console.timeEnd('Resolving content paths')
235
+
236
+ if (this.watcher) {
237
+ env.DEBUG && console.time('Watch new files')
238
+ this.watcher.refreshWatchedFiles()
239
+ env.DEBUG && console.timeEnd('Watch new files')
240
+ }
241
+
242
+ for (let file of this.readContentPaths()) {
243
+ this.context.changedContent.push(file)
244
+ }
245
+
246
+ return this.context
247
+ },
248
+ }
249
+
250
+ export async function createProcessor(args, cliConfigPath) {
251
+ let postcss = loadPostcss()
252
+
253
+ let input = args['--input']
254
+ let output = args['--output']
255
+ let includePostCss = args['--postcss']
256
+ let customPostCssPath = typeof args['--postcss'] === 'string' ? args['--postcss'] : undefined
257
+
258
+ let [beforePlugins, afterPlugins, postcssOptions] = includePostCss
259
+ ? await loadPostCssPlugins(customPostCssPath)
260
+ : loadBuiltinPostcssPlugins()
261
+
262
+ if (args['--purge']) {
263
+ log.warn('purge-flag-deprecated', [
264
+ 'The `--purge` flag has been deprecated.',
265
+ 'Please use `--content` instead.',
266
+ ])
267
+
268
+ if (!args['--content']) {
269
+ args['--content'] = args['--purge']
270
+ }
271
+ }
272
+
273
+ let content = args['--content']?.split(/(?<!{[^}]+),/) ?? []
274
+
275
+ let tailwindPlugin = () => {
276
+ return {
277
+ postcssPlugin: 'tailwindcss',
278
+ Once(root, { result }) {
279
+ env.DEBUG && console.time('Compiling CSS')
280
+ tailwind(({ createContext }) => {
281
+ console.error()
282
+ console.error('Rebuilding...')
283
+
284
+ return () => {
285
+ return state.getContext({
286
+ createContext,
287
+ cliConfigPath,
288
+ root,
289
+ result,
290
+ content,
291
+ })
292
+ }
293
+ })(root, result)
294
+ env.DEBUG && console.timeEnd('Compiling CSS')
295
+ },
296
+ }
297
+ }
298
+
299
+ tailwindPlugin.postcss = true
300
+
301
+ let plugins = [
302
+ ...beforePlugins,
303
+ tailwindPlugin,
304
+ !args['--minify'] && formatNodes,
305
+ ...afterPlugins,
306
+ ].filter(Boolean)
307
+
308
+ /** @type {import('postcss').Processor} */
309
+ // @ts-ignore
310
+ let processor = postcss(plugins)
311
+
312
+ async function readInput() {
313
+ // Piping in data, let's drain the stdin
314
+ if (input === '-') {
315
+ return drainStdin()
316
+ }
317
+
318
+ // Input file has been provided
319
+ if (input) {
320
+ return fs.promises.readFile(path.resolve(input), 'utf8')
321
+ }
322
+
323
+ // No input file provided, fallback to default atrules
324
+ return '@tailwind base; @tailwind components; @tailwind utilities'
325
+ }
326
+
327
+ async function build() {
328
+ let start = process.hrtime.bigint()
329
+
330
+ return readInput()
331
+ .then((css) => processor.process(css, { ...postcssOptions, from: input, to: output }))
332
+ .then((result) => lightningcss(!!args['--minify'], result))
333
+ .then((result) => {
334
+ if (!state.watcher) {
335
+ return result
336
+ }
337
+
338
+ env.DEBUG && console.time('Recording PostCSS dependencies')
339
+ for (let message of result.messages) {
340
+ if (message.type === 'dependency') {
341
+ state.contextDependencies.add(message.file)
342
+ }
343
+ }
344
+ env.DEBUG && console.timeEnd('Recording PostCSS dependencies')
345
+
346
+ // TODO: This needs to be in a different spot
347
+ env.DEBUG && console.time('Watch new files')
348
+ state.watcher.refreshWatchedFiles()
349
+ env.DEBUG && console.timeEnd('Watch new files')
350
+
351
+ return result
352
+ })
353
+ .then((result) => {
354
+ if (!output) {
355
+ process.stdout.write(result.css)
356
+ return
357
+ }
358
+
359
+ return Promise.all([
360
+ outputFile(result.opts.to, result.css),
361
+ result.map && outputFile(result.opts.to + '.map', result.map.toString()),
362
+ ])
363
+ })
364
+ .then(() => {
365
+ let end = process.hrtime.bigint()
366
+ console.error()
367
+ console.error('Done in', (end - start) / BigInt(1e6) + 'ms.')
368
+ })
369
+ .then(
370
+ () => {},
371
+ (err) => {
372
+ // TODO: If an initial build fails we can't easily pick up any PostCSS dependencies
373
+ // that were collected before the error occurred
374
+ // The result is not stored on the error so we have to store it externally
375
+ // and pull the messages off of it here somehow
376
+
377
+ // This results in a less than ideal DX because the watcher will not pick up
378
+ // changes to imported CSS if one of them caused an error during the initial build
379
+ // If you fix it and then save the main CSS file so there's no error
380
+ // The watcher will start watching the imported CSS files and will be
381
+ // resilient to future errors.
382
+
383
+ console.error(err)
384
+ }
385
+ )
386
+ }
387
+
388
+ /**
389
+ * @param {{file: string, content(): Promise<string>, extension: string}[]} changes
390
+ */
391
+ async function parseChanges(changes) {
392
+ return Promise.all(
393
+ changes.map(async (change) => ({
394
+ content: await change.content(),
395
+ extension: change.extension,
396
+ }))
397
+ )
398
+ }
399
+
400
+ if (input !== undefined && input !== '-') {
401
+ state.contextDependencies.add(path.resolve(input))
402
+ }
403
+
404
+ return {
405
+ build,
406
+ watch: async () => {
407
+ state.watcher = createWatcher(args, {
408
+ state,
409
+
410
+ /**
411
+ * @param {{file: string, content(): Promise<string>, extension: string}[]} changes
412
+ */
413
+ async rebuild(changes) {
414
+ let needsNewContext = changes.some((change) => {
415
+ return (
416
+ state.configDependencies.has(change.file) ||
417
+ state.contextDependencies.has(change.file)
418
+ )
419
+ })
420
+
421
+ if (needsNewContext) {
422
+ state.context = null
423
+ } else {
424
+ for (let change of await parseChanges(changes)) {
425
+ state.changedContent.push(change)
426
+ }
427
+ }
428
+
429
+ return build()
430
+ },
431
+ })
432
+
433
+ await build()
434
+ },
435
+ }
436
+ }
@@ -0,0 +1,74 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+
4
+ export function indentRecursive(node, indent = 0) {
5
+ node.each &&
6
+ node.each((child, i) => {
7
+ if (!child.raws.before || !child.raws.before.trim() || child.raws.before.includes('\n')) {
8
+ child.raws.before = `\n${node.type !== 'rule' && i > 0 ? '\n' : ''}${' '.repeat(indent)}`
9
+ }
10
+ child.raws.after = `\n${' '.repeat(indent)}`
11
+ indentRecursive(child, indent + 1)
12
+ })
13
+ }
14
+
15
+ export function formatNodes(root) {
16
+ indentRecursive(root)
17
+ if (root.first) {
18
+ root.first.raws.before = ''
19
+ }
20
+ }
21
+
22
+ /**
23
+ * When rapidly saving files atomically a couple of situations can happen:
24
+ * - The file is missing since the external program has deleted it by the time we've gotten around to reading it from the earlier save.
25
+ * - The file is being written to by the external program by the time we're going to read it and is thus treated as busy because a lock is held.
26
+ *
27
+ * To work around this we retry reading the file a handful of times with a delay between each attempt
28
+ *
29
+ * @param {string} path
30
+ * @param {number} tries
31
+ * @returns {Promise<string | undefined>}
32
+ * @throws {Error} If the file is still missing or busy after the specified number of tries
33
+ */
34
+ export async function readFileWithRetries(path, tries = 5) {
35
+ for (let n = 0; n <= tries; n++) {
36
+ try {
37
+ return await fs.promises.readFile(path, 'utf8')
38
+ } catch (err) {
39
+ if (n !== tries) {
40
+ if (err.code === 'ENOENT' || err.code === 'EBUSY') {
41
+ await new Promise((resolve) => setTimeout(resolve, 10))
42
+
43
+ continue
44
+ }
45
+ }
46
+
47
+ throw err
48
+ }
49
+ }
50
+ }
51
+
52
+ export function drainStdin() {
53
+ return new Promise((resolve, reject) => {
54
+ let result = ''
55
+ process.stdin.on('data', (chunk) => {
56
+ result += chunk
57
+ })
58
+ process.stdin.on('end', () => resolve(result))
59
+ process.stdin.on('error', (err) => reject(err))
60
+ })
61
+ }
62
+
63
+ export async function outputFile(file, newContents) {
64
+ try {
65
+ let currentContents = await fs.promises.readFile(file, 'utf8')
66
+ if (currentContents === newContents) {
67
+ return // Skip writing the file
68
+ }
69
+ } catch {}
70
+
71
+ // Write the file
72
+ await fs.promises.mkdir(path.dirname(file), { recursive: true })
73
+ await fs.promises.writeFile(file, newContents, 'utf8')
74
+ }