tailwindcss 3.0.5 → 3.0.9

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.
@@ -1,311 +0,0 @@
1
- import fs from 'fs'
2
- import path from 'path'
3
-
4
- import tmp from 'tmp'
5
- import chokidar from 'chokidar'
6
- import fastGlob from 'fast-glob'
7
- import LRU from 'quick-lru'
8
- import normalizePath from 'normalize-path'
9
-
10
- import hash from '../util/hashConfig'
11
- import log from '../util/log'
12
- import getModuleDependencies from '../lib/getModuleDependencies'
13
- import resolveConfig from '../public/resolve-config'
14
- import resolveConfigPath from '../util/resolveConfigPath'
15
- import { getContext } from './setupContextUtils'
16
-
17
- // This is used to trigger rebuilds. Just updating the timestamp
18
- // is significantly faster than actually writing to the file (10x).
19
-
20
- function touch(filename) {
21
- let time = new Date()
22
-
23
- try {
24
- fs.utimesSync(filename, time, time)
25
- } catch (err) {
26
- fs.closeSync(fs.openSync(filename, 'w'))
27
- }
28
- }
29
-
30
- let watchers = new WeakMap()
31
-
32
- function getWatcher(context) {
33
- if (watchers.has(context)) {
34
- return watchers.get(context)
35
- }
36
-
37
- return null
38
- }
39
-
40
- function setWatcher(context, watcher) {
41
- return watchers.set(context, watcher)
42
- }
43
-
44
- let touchFiles = new WeakMap()
45
-
46
- function getTouchFile(context) {
47
- if (touchFiles.has(context)) {
48
- return touchFiles.get(context)
49
- }
50
-
51
- return null
52
- }
53
-
54
- function setTouchFile(context, touchFile) {
55
- return touchFiles.set(context, touchFile)
56
- }
57
-
58
- let configPaths = new WeakMap()
59
-
60
- function getConfigPath(context, configOrPath) {
61
- if (!configPaths.has(context)) {
62
- configPaths.set(context, resolveConfigPath(configOrPath))
63
- }
64
-
65
- return configPaths.get(context)
66
- }
67
-
68
- function rebootWatcher(context, configPath, configDependencies, candidateFiles) {
69
- let touchFile = getTouchFile(context)
70
-
71
- if (touchFile === null) {
72
- touchFile = tmp.fileSync().name
73
- setTouchFile(context, touchFile)
74
- touch(touchFile)
75
- }
76
-
77
- let watcher = getWatcher(context)
78
-
79
- Promise.resolve(watcher ? watcher.close() : null).then(() => {
80
- log.info([
81
- 'Tailwind CSS is watching for changes...',
82
- 'https://tailwindcss.com/docs/just-in-time-mode#watch-mode-and-one-off-builds',
83
- ])
84
-
85
- watcher = chokidar.watch([...candidateFiles, ...configDependencies], {
86
- ignoreInitial: true,
87
- awaitWriteFinish:
88
- process.platform === 'win32'
89
- ? {
90
- stabilityThreshold: 50,
91
- pollInterval: 10,
92
- }
93
- : false,
94
- })
95
-
96
- setWatcher(context, watcher)
97
-
98
- watcher.on('add', (file) => {
99
- let changedFile = path.resolve('.', file)
100
- let content = fs.readFileSync(changedFile, 'utf8')
101
- let extension = path.extname(changedFile).slice(1)
102
- context.changedContent.push({ content, extension })
103
- touch(touchFile)
104
- })
105
-
106
- watcher.on('change', (file) => {
107
- // If it was a config dependency, touch the config file to trigger a new context.
108
- // This is not really that clean of a solution but it's the fastest, because we
109
- // can do a very quick check on each build to see if the config has changed instead
110
- // of having to get all of the module dependencies and check every timestamp each
111
- // time.
112
- if (configDependencies.has(file)) {
113
- for (let dependency of configDependencies) {
114
- delete require.cache[require.resolve(dependency)]
115
- }
116
- touch(configPath)
117
- } else {
118
- let changedFile = path.resolve('.', file)
119
- let content = fs.readFileSync(changedFile, 'utf8')
120
- let extension = path.extname(changedFile).slice(1)
121
- context.changedContent.push({ content, extension })
122
- touch(touchFile)
123
- }
124
- })
125
-
126
- watcher.on('unlink', (file) => {
127
- // Touch the config file if any of the dependencies are deleted.
128
- if (configDependencies.has(file)) {
129
- for (let dependency of configDependencies) {
130
- delete require.cache[require.resolve(dependency)]
131
- }
132
- touch(configPath)
133
- }
134
- })
135
- })
136
- }
137
-
138
- let configPathCache = new LRU({ maxSize: 100 })
139
-
140
- let configDependenciesCache = new WeakMap()
141
-
142
- function getConfigDependencies(context) {
143
- if (!configDependenciesCache.has(context)) {
144
- configDependenciesCache.set(context, new Set())
145
- }
146
-
147
- return configDependenciesCache.get(context)
148
- }
149
-
150
- let candidateFilesCache = new WeakMap()
151
-
152
- function getCandidateFiles(context, tailwindConfig) {
153
- if (candidateFilesCache.has(context)) {
154
- return candidateFilesCache.get(context)
155
- }
156
-
157
- let candidateFiles = tailwindConfig.content.files
158
- .filter((item) => typeof item === 'string')
159
- .map((contentPath) => normalizePath(contentPath))
160
-
161
- return candidateFilesCache.set(context, candidateFiles).get(context)
162
- }
163
-
164
- // Get the config object based on a path
165
- function getTailwindConfig(configOrPath) {
166
- let userConfigPath = resolveConfigPath(configOrPath)
167
-
168
- if (userConfigPath !== null) {
169
- let [prevConfig, prevModified = -Infinity, prevConfigHash] =
170
- configPathCache.get(userConfigPath) || []
171
- let modified = fs.statSync(userConfigPath).mtimeMs
172
-
173
- // It hasn't changed (based on timestamp)
174
- if (modified <= prevModified) {
175
- return [prevConfig, userConfigPath, prevConfigHash, [userConfigPath]]
176
- }
177
-
178
- // It has changed (based on timestamp), or first run
179
- delete require.cache[userConfigPath]
180
- let newConfig = resolveConfig(require(userConfigPath))
181
- let newHash = hash(newConfig)
182
- configPathCache.set(userConfigPath, [newConfig, modified, newHash])
183
- return [newConfig, userConfigPath, newHash, [userConfigPath]]
184
- }
185
-
186
- // It's a plain object, not a path
187
- let newConfig = resolveConfig(
188
- configOrPath.config === undefined ? configOrPath : configOrPath.config
189
- )
190
-
191
- return [newConfig, null, hash(newConfig), []]
192
- }
193
-
194
- function resolvedChangedContent(context, candidateFiles) {
195
- let changedContent = context.tailwindConfig.content.files
196
- .filter((item) => typeof item.raw === 'string')
197
- .map(({ raw, extension = 'html' }) => ({ content: raw, extension }))
198
-
199
- for (let changedFile of resolveChangedFiles(context, candidateFiles)) {
200
- let content = fs.readFileSync(changedFile, 'utf8')
201
- let extension = path.extname(changedFile).slice(1)
202
- changedContent.push({ content, extension })
203
- }
204
- return changedContent
205
- }
206
-
207
- let scannedContentCache = new WeakMap()
208
-
209
- function resolveChangedFiles(context, candidateFiles) {
210
- let changedFiles = new Set()
211
-
212
- // If we're not set up and watching files ourselves, we need to do
213
- // the work of grabbing all of the template files for candidate
214
- // detection.
215
- if (!scannedContentCache.has(context)) {
216
- let files = fastGlob.sync(candidateFiles)
217
- for (let file of files) {
218
- changedFiles.add(file)
219
- }
220
- scannedContentCache.set(context, true)
221
- }
222
-
223
- return changedFiles
224
- }
225
-
226
- // DISABLE_TOUCH = FALSE
227
-
228
- // Retrieve an existing context from cache if possible (since contexts are unique per
229
- // source path), or set up a new one (including setting up watchers and registering
230
- // plugins) then return it
231
- export default function setupWatchingContext(configOrPath) {
232
- return ({ tailwindDirectives, registerDependency }) => {
233
- return (root, result) => {
234
- let [tailwindConfig, userConfigPath, tailwindConfigHash, configDependencies] =
235
- getTailwindConfig(configOrPath)
236
-
237
- let contextDependencies = new Set(configDependencies)
238
-
239
- // If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
240
- // to be dependencies of the context. Can reuse the context even if they change.
241
- // We may want to think about `@layer` being part of this trigger too, but it's tough
242
- // because it's impossible for a layer in one file to end up in the actual @tailwind rule
243
- // in another file since independent sources are effectively isolated.
244
- if (tailwindDirectives.size > 0) {
245
- // Add current css file as a context dependencies.
246
- contextDependencies.add(result.opts.from)
247
-
248
- // Add all css @import dependencies as context dependencies.
249
- for (let message of result.messages) {
250
- if (message.type === 'dependency') {
251
- contextDependencies.add(message.file)
252
- }
253
- }
254
- }
255
-
256
- let [context, isNewContext] = getContext(
257
- root,
258
- result,
259
- tailwindConfig,
260
- userConfigPath,
261
- tailwindConfigHash,
262
- contextDependencies
263
- )
264
-
265
- let candidateFiles = getCandidateFiles(context, tailwindConfig)
266
- let contextConfigDependencies = getConfigDependencies(context)
267
-
268
- for (let file of configDependencies) {
269
- registerDependency({ type: 'dependency', file })
270
- }
271
-
272
- context.disposables.push((oldContext) => {
273
- let watcher = getWatcher(oldContext)
274
- if (watcher !== null) {
275
- watcher.close()
276
- }
277
- })
278
-
279
- let configPath = getConfigPath(context, configOrPath)
280
-
281
- if (configPath !== null) {
282
- for (let dependency of getModuleDependencies(configPath)) {
283
- if (dependency.file === configPath) {
284
- continue
285
- }
286
-
287
- contextConfigDependencies.add(dependency.file)
288
- }
289
- }
290
-
291
- if (isNewContext) {
292
- rebootWatcher(context, configPath, contextConfigDependencies, candidateFiles)
293
- }
294
-
295
- // Register our temp file as a dependency — we write to this file
296
- // to trigger rebuilds.
297
- let touchFile = getTouchFile(context)
298
- if (touchFile) {
299
- registerDependency({ type: 'dependency', file: touchFile })
300
- }
301
-
302
- if (tailwindDirectives.size > 0) {
303
- for (let changedContent of resolvedChangedContent(context, candidateFiles)) {
304
- context.changedContent.push(changedContent)
305
- }
306
- }
307
-
308
- return context
309
- }
310
- }
311
- }