tailwindcss 0.0.0-insiders.d2b53cd → 0.0.0-insiders.d2fdf9e

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