tailwindcss 3.0.8 → 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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ - Nothing yet!
11
+
12
+ ## [3.0.9] - 2022-01-03
13
+
14
+ ### Fixed
15
+
16
+ - Improve `DEBUG` flag ([#6797](https://github.com/tailwindlabs/tailwindcss/pull/6797), [#6804](https://github.com/tailwindlabs/tailwindcss/pull/6804))
17
+ - Ensure we can use `<` and `>` characters in modifiers ([#6851](https://github.com/tailwindlabs/tailwindcss/pull/6851))
18
+ - Validate `theme()` works in arbitrary values ([#6852](https://github.com/tailwindlabs/tailwindcss/pull/6852))
19
+ - Properly detect `theme()` value usage in arbitrary properties ([#6854](https://github.com/tailwindlabs/tailwindcss/pull/6854))
20
+ - Improve collapsing of duplicate declarations ([#6856](https://github.com/tailwindlabs/tailwindcss/pull/6856))
21
+ - Remove support for `TAILWIND_MODE=watch` ([#6858](https://github.com/tailwindlabs/tailwindcss/pull/6858))
22
+
23
+ ## [3.0.8] - 2021-12-28
24
+
10
25
  ### Fixed
11
26
 
12
27
  - Reduce specificity of `abbr` rule in preflight ([#6671](https://github.com/tailwindlabs/tailwindcss/pull/6671))
@@ -1745,7 +1760,9 @@ No release notes
1745
1760
 
1746
1761
  - Everything!
1747
1762
 
1748
- [unreleased]: https://github.com/tailwindlabs/tailwindcss/compare/v3.0.7...HEAD
1763
+ [unreleased]: https://github.com/tailwindlabs/tailwindcss/compare/v3.0.9...HEAD
1764
+ [3.0.9]: https://github.com/tailwindlabs/tailwindcss/compare/v3.0.8...v3.0.9
1765
+ [3.0.8]: https://github.com/tailwindlabs/tailwindcss/compare/v3.0.7...v3.0.8
1749
1766
  [3.0.7]: https://github.com/tailwindlabs/tailwindcss/compare/v3.0.6...v3.0.7
1750
1767
  [3.0.6]: https://github.com/tailwindlabs/tailwindcss/compare/v3.0.5...v3.0.6
1751
1768
  [3.0.5]: https://github.com/tailwindlabs/tailwindcss/compare/v3.0.4...v3.0.5
package/lib/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  var _setupTrackingContext = _interopRequireDefault(require("./lib/setupTrackingContext"));
3
- var _setupWatchingContext = _interopRequireDefault(require("./lib/setupWatchingContext"));
4
3
  var _processTailwindFeatures = _interopRequireDefault(require("./processTailwindFeatures"));
5
4
  var _sharedState = require("./lib/sharedState");
6
5
  function _interopRequireDefault(obj) {
@@ -18,8 +17,7 @@ module.exports = function tailwindcss(configOrPath) {
18
17
  return root;
19
18
  },
20
19
  function(root, result) {
21
- let setupContext = _sharedState.env.TAILWIND_MODE === 'watch' ? (0, _setupWatchingContext).default(configOrPath) : (0, _setupTrackingContext).default(configOrPath);
22
- (0, _processTailwindFeatures).default(setupContext)(root, result);
20
+ (0, _processTailwindFeatures).default((0, _setupTrackingContext).default(configOrPath))(root, result);
23
21
  },
24
22
  _sharedState.env.DEBUG && function(root) {
25
23
  console.timeEnd('JIT TOTAL');
@@ -8,6 +8,7 @@ function collapseDuplicateDeclarations() {
8
8
  root.walkRules((node)=>{
9
9
  let seen = new Map();
10
10
  let droppable = new Set([]);
11
+ let byProperty = new Map();
11
12
  node.walkDecls((decl)=>{
12
13
  // This could happen if we have nested selectors. In that case the
13
14
  // parent will loop over all its declarations but also the declarations
@@ -17,13 +18,63 @@ function collapseDuplicateDeclarations() {
17
18
  return;
18
19
  }
19
20
  if (seen.has(decl.prop)) {
20
- droppable.add(seen.get(decl.prop));
21
+ // Exact same value as what we have seen so far
22
+ if (seen.get(decl.prop).value === decl.value) {
23
+ // Keep the last one, drop the one we've seen so far
24
+ droppable.add(seen.get(decl.prop));
25
+ // Override the existing one with the new value. This is necessary
26
+ // so that if we happen to have more than one declaration with the
27
+ // same value, that we keep removing the previous one. Otherwise we
28
+ // will only remove the *first* one.
29
+ seen.set(decl.prop, decl);
30
+ return;
31
+ }
32
+ // Not the same value, so we need to check if we can merge it so
33
+ // let's collect it first.
34
+ if (!byProperty.has(decl.prop)) {
35
+ byProperty.set(decl.prop, new Set());
36
+ }
37
+ byProperty.get(decl.prop).add(seen.get(decl.prop));
38
+ byProperty.get(decl.prop).add(decl);
21
39
  }
22
40
  seen.set(decl.prop, decl);
23
41
  });
42
+ // Drop all the duplicate declarations with the exact same value we've
43
+ // already seen so far.
24
44
  for (let decl1 of droppable){
25
45
  decl1.remove();
26
46
  }
47
+ // Analyze the declarations based on its unit, drop all the declarations
48
+ // with the same unit but the last one in the list.
49
+ for (let declarations of byProperty.values()){
50
+ let byUnit = new Map();
51
+ for (let decl of declarations){
52
+ let unit = resolveUnit(decl.value);
53
+ if (unit === null) {
54
+ continue;
55
+ }
56
+ if (!byUnit.has(unit)) {
57
+ byUnit.set(unit, new Set());
58
+ }
59
+ byUnit.get(unit).add(decl);
60
+ }
61
+ for (let declarations1 of byUnit.values()){
62
+ // Get all but the last one
63
+ let removableDeclarations = Array.from(declarations1).slice(0, -1);
64
+ for (let decl of removableDeclarations){
65
+ decl.remove();
66
+ }
67
+ }
68
+ }
27
69
  });
28
70
  };
29
71
  }
72
+ let UNITLESS_NUMBER = Symbol('unitless-number');
73
+ function resolveUnit(input) {
74
+ let result = /^-?\d*.?\d+([\w%]+)?$/g.exec(input);
75
+ if (result) {
76
+ var ref;
77
+ return (ref = result[1]) !== null && ref !== void 0 ? ref : UNITLESS_NUMBER;
78
+ }
79
+ return null;
80
+ }
@@ -15,9 +15,11 @@ const PATTERNS = [
15
15
  /([^<>"'`\s]*\[\w*\("[^'`\s]*"\)\])/.source,
16
16
  /([^<>"'`\s]*\['[^"'`\s]*'\])/.source,
17
17
  /([^<>"'`\s]*\["[^"'`\s]*"\])/.source,
18
+ /([^<>"'`\s]*\[[^<>"'`\s]*:[^\]\s]*\])/.source,
18
19
  /([^<>"'`\s]*\[[^<>"'`\s]*:'[^"'`\s]*'\])/.source,
19
20
  /([^<>"'`\s]*\[[^<>"'`\s]*:"[^"'`\s]*"\])/.source,
20
21
  /([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source,
22
+ /([^"'`\s]*[^<>"'`\s:\\])/.source,
21
23
  /([^<>"'`\s]*[^"'`\s:\\])/.source,
22
24
  ].join('|');
23
25
  const BROAD_MATCH_GLOBAL_REGEXP = new RegExp(PATTERNS, 'g');
@@ -2,13 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
+ exports.resolveDebug = resolveDebug;
5
6
  exports.contextSourcesMap = exports.configContextMap = exports.contextMap = exports.env = void 0;
6
7
  const env = {
7
- TAILWIND_MODE: process.env.TAILWIND_MODE,
8
8
  NODE_ENV: process.env.NODE_ENV,
9
- DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
10
- TAILWIND_DISABLE_TOUCH: process.env.TAILWIND_DISABLE_TOUCH !== undefined,
11
- TAILWIND_TOUCH_DIR: process.env.TAILWIND_TOUCH_DIR
9
+ DEBUG: resolveDebug(process.env.DEBUG)
12
10
  };
13
11
  exports.env = env;
14
12
  const contextMap = new Map();
@@ -17,3 +15,34 @@ const configContextMap = new Map();
17
15
  exports.configContextMap = configContextMap;
18
16
  const contextSourcesMap = new Map();
19
17
  exports.contextSourcesMap = contextSourcesMap;
18
+ function resolveDebug(debug) {
19
+ if (debug === undefined) {
20
+ return false;
21
+ }
22
+ // Environment variables are strings, so convert to boolean
23
+ if (debug === 'true' || debug === '1') {
24
+ return true;
25
+ }
26
+ if (debug === 'false' || debug === '0') {
27
+ return false;
28
+ }
29
+ // Keep the debug convention into account:
30
+ // DEBUG=* -> This enables all debug modes
31
+ // DEBUG=projectA,projectB,projectC -> This enables debug for projectA, projectB and projectC
32
+ // DEBUG=projectA:* -> This enables all debug modes for projectA (if you have sub-types)
33
+ // DEBUG=projectA,-projectB -> This enables debug for projectA and explicitly disables it for projectB
34
+ if (debug === '*') {
35
+ return true;
36
+ }
37
+ let debuggers = debug.split(',').map((d)=>d.split(':')[0]
38
+ );
39
+ // Ignoring tailwindcss
40
+ if (debuggers.includes('-tailwindcss')) {
41
+ return false;
42
+ }
43
+ // Including tailwindcss
44
+ if (debuggers.includes('tailwindcss')) {
45
+ return true;
46
+ }
47
+ return false;
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwindcss",
3
- "version": "3.0.8",
3
+ "version": "3.0.9",
4
4
  "description": "A utility-first CSS framework for rapidly building custom user interfaces.",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -25,7 +25,7 @@
25
25
  "prepublishOnly": "npm install --force && npm run swcify",
26
26
  "style": "eslint .",
27
27
  "pretest": "npm run generate:plugin-list",
28
- "test": "cross-env TAILWIND_MODE=build jest",
28
+ "test": "jest",
29
29
  "test:integrations": "npm run test --prefix ./integrations",
30
30
  "install:integrations": "node scripts/install-integrations.js",
31
31
  "posttest": "npm run style",
@@ -54,7 +54,7 @@
54
54
  "eslint": "^8.4.1",
55
55
  "eslint-config-prettier": "^8.3.0",
56
56
  "eslint-plugin-prettier": "^4.0.0",
57
- "jest": "^27.4.3",
57
+ "jest": "^27.4.5",
58
58
  "jest-diff": "^27.4.2",
59
59
  "postcss": "^8.4.5",
60
60
  "postcss-cli": "^8.3.1",
@@ -85,8 +85,7 @@
85
85
  "postcss-selector-parser": "^6.0.7",
86
86
  "postcss-value-parser": "^4.2.0",
87
87
  "quick-lru": "^5.1.1",
88
- "resolve": "^1.20.0",
89
- "tmp": "^0.2.1"
88
+ "resolve": "^1.20.0"
90
89
  },
91
90
  "browserslist": [
92
91
  "> 1%",
package/src/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import setupTrackingContext from './lib/setupTrackingContext'
2
- import setupWatchingContext from './lib/setupWatchingContext'
3
2
  import processTailwindFeatures from './processTailwindFeatures'
4
3
  import { env } from './lib/sharedState'
5
4
 
@@ -14,12 +13,7 @@ module.exports = function tailwindcss(configOrPath) {
14
13
  return root
15
14
  },
16
15
  function (root, result) {
17
- let setupContext =
18
- env.TAILWIND_MODE === 'watch'
19
- ? setupWatchingContext(configOrPath)
20
- : setupTrackingContext(configOrPath)
21
-
22
- processTailwindFeatures(setupContext)(root, result)
16
+ processTailwindFeatures(setupTrackingContext(configOrPath))(root, result)
23
17
  },
24
18
  env.DEBUG &&
25
19
  function (root) {
@@ -3,6 +3,7 @@ export default function collapseDuplicateDeclarations() {
3
3
  root.walkRules((node) => {
4
4
  let seen = new Map()
5
5
  let droppable = new Set([])
6
+ let byProperty = new Map()
6
7
 
7
8
  node.walkDecls((decl) => {
8
9
  // This could happen if we have nested selectors. In that case the
@@ -14,15 +15,79 @@ export default function collapseDuplicateDeclarations() {
14
15
  }
15
16
 
16
17
  if (seen.has(decl.prop)) {
17
- droppable.add(seen.get(decl.prop))
18
+ // Exact same value as what we have seen so far
19
+ if (seen.get(decl.prop).value === decl.value) {
20
+ // Keep the last one, drop the one we've seen so far
21
+ droppable.add(seen.get(decl.prop))
22
+ // Override the existing one with the new value. This is necessary
23
+ // so that if we happen to have more than one declaration with the
24
+ // same value, that we keep removing the previous one. Otherwise we
25
+ // will only remove the *first* one.
26
+ seen.set(decl.prop, decl)
27
+ return
28
+ }
29
+
30
+ // Not the same value, so we need to check if we can merge it so
31
+ // let's collect it first.
32
+ if (!byProperty.has(decl.prop)) {
33
+ byProperty.set(decl.prop, new Set())
34
+ }
35
+
36
+ byProperty.get(decl.prop).add(seen.get(decl.prop))
37
+ byProperty.get(decl.prop).add(decl)
18
38
  }
19
39
 
20
40
  seen.set(decl.prop, decl)
21
41
  })
22
42
 
43
+ // Drop all the duplicate declarations with the exact same value we've
44
+ // already seen so far.
23
45
  for (let decl of droppable) {
24
46
  decl.remove()
25
47
  }
48
+
49
+ // Analyze the declarations based on its unit, drop all the declarations
50
+ // with the same unit but the last one in the list.
51
+ for (let declarations of byProperty.values()) {
52
+ let byUnit = new Map()
53
+
54
+ for (let decl of declarations) {
55
+ let unit = resolveUnit(decl.value)
56
+ if (unit === null) {
57
+ // We don't have a unit, so should never try and collapse this
58
+ // value. This is because we can't know how to do it in a correct
59
+ // way (e.g.: overrides for older browsers)
60
+ continue
61
+ }
62
+
63
+ if (!byUnit.has(unit)) {
64
+ byUnit.set(unit, new Set())
65
+ }
66
+
67
+ byUnit.get(unit).add(decl)
68
+ }
69
+
70
+ for (let declarations of byUnit.values()) {
71
+ // Get all but the last one
72
+ let removableDeclarations = Array.from(declarations).slice(0, -1)
73
+
74
+ for (let decl of removableDeclarations) {
75
+ decl.remove()
76
+ }
77
+ }
78
+ }
26
79
  })
27
80
  }
28
81
  }
82
+
83
+ let UNITLESS_NUMBER = Symbol('unitless-number')
84
+
85
+ function resolveUnit(input) {
86
+ let result = /^-?\d*.?\d+([\w%]+)?$/g.exec(input)
87
+
88
+ if (result) {
89
+ return result[1] ?? UNITLESS_NUMBER
90
+ }
91
+
92
+ return null
93
+ }
@@ -10,9 +10,11 @@ const PATTERNS = [
10
10
  /([^<>"'`\s]*\[\w*\("[^'`\s]*"\)\])/.source, // bg-[url("..."),url("...")]
11
11
  /([^<>"'`\s]*\['[^"'`\s]*'\])/.source, // `content-['hello']` but not `content-['hello']']`
12
12
  /([^<>"'`\s]*\["[^"'`\s]*"\])/.source, // `content-["hello"]` but not `content-["hello"]"]`
13
+ /([^<>"'`\s]*\[[^<>"'`\s]*:[^\]\s]*\])/.source, // `[attr:value]`
13
14
  /([^<>"'`\s]*\[[^<>"'`\s]*:'[^"'`\s]*'\])/.source, // `[content:'hello']` but not `[content:"hello"]`
14
15
  /([^<>"'`\s]*\[[^<>"'`\s]*:"[^"'`\s]*"\])/.source, // `[content:"hello"]` but not `[content:'hello']`
15
16
  /([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source, // `fill-[#bada55]`, `fill-[#bada55]/50`
17
+ /([^"'`\s]*[^<>"'`\s:\\])/.source, // `<sm:underline`, `md>:font-bold`
16
18
  /([^<>"'`\s]*[^"'`\s:\\])/.source, // `px-1.5`, `uppercase` but not `uppercase:`
17
19
 
18
20
  // Arbitrary properties
@@ -1,10 +1,46 @@
1
1
  export const env = {
2
- TAILWIND_MODE: process.env.TAILWIND_MODE,
3
2
  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,
3
+ DEBUG: resolveDebug(process.env.DEBUG),
7
4
  }
8
5
  export const contextMap = new Map()
9
6
  export const configContextMap = new Map()
10
7
  export const contextSourcesMap = new Map()
8
+
9
+ export function resolveDebug(debug) {
10
+ if (debug === undefined) {
11
+ return false
12
+ }
13
+
14
+ // Environment variables are strings, so convert to boolean
15
+ if (debug === 'true' || debug === '1') {
16
+ return true
17
+ }
18
+
19
+ if (debug === 'false' || debug === '0') {
20
+ return false
21
+ }
22
+
23
+ // Keep the debug convention into account:
24
+ // DEBUG=* -> This enables all debug modes
25
+ // DEBUG=projectA,projectB,projectC -> This enables debug for projectA, projectB and projectC
26
+ // DEBUG=projectA:* -> This enables all debug modes for projectA (if you have sub-types)
27
+ // DEBUG=projectA,-projectB -> This enables debug for projectA and explicitly disables it for projectB
28
+
29
+ if (debug === '*') {
30
+ return true
31
+ }
32
+
33
+ let debuggers = debug.split(',').map((d) => d.split(':')[0])
34
+
35
+ // Ignoring tailwindcss
36
+ if (debuggers.includes('-tailwindcss')) {
37
+ return false
38
+ }
39
+
40
+ // Including tailwindcss
41
+ if (debuggers.includes('tailwindcss')) {
42
+ return true
43
+ }
44
+
45
+ return false
46
+ }
@@ -1,288 +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
- awaitWriteFinish: process.platform === 'win32' ? {
80
- stabilityThreshold: 50,
81
- pollInterval: 10
82
- } : false
83
- });
84
- setWatcher(context, watcher);
85
- watcher.on('add', (file)=>{
86
- let changedFile = _path.default.resolve('.', file);
87
- let content = _fs.default.readFileSync(changedFile, 'utf8');
88
- let extension = _path.default.extname(changedFile).slice(1);
89
- context.changedContent.push({
90
- content,
91
- extension
92
- });
93
- touch(touchFile);
94
- });
95
- watcher.on('change', (file)=>{
96
- // If it was a config dependency, touch the config file to trigger a new context.
97
- // This is not really that clean of a solution but it's the fastest, because we
98
- // can do a very quick check on each build to see if the config has changed instead
99
- // of having to get all of the module dependencies and check every timestamp each
100
- // time.
101
- if (configDependencies.has(file)) {
102
- for (let dependency of configDependencies){
103
- delete require.cache[require.resolve(dependency)];
104
- }
105
- touch(configPath);
106
- } else {
107
- let changedFile = _path.default.resolve('.', file);
108
- let content = _fs.default.readFileSync(changedFile, 'utf8');
109
- let extension = _path.default.extname(changedFile).slice(1);
110
- context.changedContent.push({
111
- content,
112
- extension
113
- });
114
- touch(touchFile);
115
- }
116
- });
117
- watcher.on('unlink', (file)=>{
118
- // Touch the config file if any of the dependencies are deleted.
119
- if (configDependencies.has(file)) {
120
- for (let dependency of configDependencies){
121
- delete require.cache[require.resolve(dependency)];
122
- }
123
- touch(configPath);
124
- }
125
- });
126
- });
127
- }
128
- let configPathCache = new _quickLru.default({
129
- maxSize: 100
130
- });
131
- let configDependenciesCache = new WeakMap();
132
- function getConfigDependencies(context) {
133
- if (!configDependenciesCache.has(context)) {
134
- configDependenciesCache.set(context, new Set());
135
- }
136
- return configDependenciesCache.get(context);
137
- }
138
- let candidateFilesCache = new WeakMap();
139
- function getCandidateFiles(context, tailwindConfig) {
140
- if (candidateFilesCache.has(context)) {
141
- return candidateFilesCache.get(context);
142
- }
143
- let candidateFiles = tailwindConfig.content.files.filter((item)=>typeof item === 'string'
144
- ).map((contentPath)=>(0, _normalizePath).default(contentPath)
145
- );
146
- return candidateFilesCache.set(context, candidateFiles).get(context);
147
- }
148
- // Get the config object based on a path
149
- function getTailwindConfig(configOrPath) {
150
- let userConfigPath = (0, _resolveConfigPath).default(configOrPath);
151
- if (userConfigPath !== null) {
152
- let [prevConfig, prevModified = -Infinity, prevConfigHash] = configPathCache.get(userConfigPath) || [];
153
- let modified = _fs.default.statSync(userConfigPath).mtimeMs;
154
- // It hasn't changed (based on timestamp)
155
- if (modified <= prevModified) {
156
- return [
157
- prevConfig,
158
- userConfigPath,
159
- prevConfigHash,
160
- [
161
- userConfigPath
162
- ]
163
- ];
164
- }
165
- // It has changed (based on timestamp), or first run
166
- delete require.cache[userConfigPath];
167
- let newConfig = (0, _resolveConfig).default(require(userConfigPath));
168
- let newHash = (0, _hashConfig).default(newConfig);
169
- configPathCache.set(userConfigPath, [
170
- newConfig,
171
- modified,
172
- newHash
173
- ]);
174
- return [
175
- newConfig,
176
- userConfigPath,
177
- newHash,
178
- [
179
- userConfigPath
180
- ]
181
- ];
182
- }
183
- // It's a plain object, not a path
184
- let newConfig = (0, _resolveConfig).default(configOrPath.config === undefined ? configOrPath : configOrPath.config);
185
- return [
186
- newConfig,
187
- null,
188
- (0, _hashConfig).default(newConfig),
189
- []
190
- ];
191
- }
192
- function resolvedChangedContent(context, candidateFiles) {
193
- let changedContent = context.tailwindConfig.content.files.filter((item)=>typeof item.raw === 'string'
194
- ).map(({ raw , extension ='html' })=>({
195
- content: raw,
196
- extension
197
- })
198
- );
199
- for (let changedFile of resolveChangedFiles(context, candidateFiles)){
200
- let content = _fs.default.readFileSync(changedFile, 'utf8');
201
- let extension = _path.default.extname(changedFile).slice(1);
202
- changedContent.push({
203
- content,
204
- extension
205
- });
206
- }
207
- return changedContent;
208
- }
209
- let scannedContentCache = new WeakMap();
210
- function resolveChangedFiles(context, candidateFiles) {
211
- let changedFiles = new Set();
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.default.sync(candidateFiles);
217
- for (let file of files){
218
- changedFiles.add(file);
219
- }
220
- scannedContentCache.set(context, true);
221
- }
222
- return changedFiles;
223
- }
224
- function setupWatchingContext(configOrPath) {
225
- return ({ tailwindDirectives , registerDependency })=>{
226
- return (root, result)=>{
227
- let [tailwindConfig, userConfigPath, tailwindConfigHash, configDependencies] = getTailwindConfig(configOrPath);
228
- let contextDependencies = new Set(configDependencies);
229
- // If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
230
- // to be dependencies of the context. Can reuse the context even if they change.
231
- // We may want to think about `@layer` being part of this trigger too, but it's tough
232
- // because it's impossible for a layer in one file to end up in the actual @tailwind rule
233
- // in another file since independent sources are effectively isolated.
234
- if (tailwindDirectives.size > 0) {
235
- // Add current css file as a context dependencies.
236
- contextDependencies.add(result.opts.from);
237
- // Add all css @import dependencies as context dependencies.
238
- for (let message of result.messages){
239
- if (message.type === 'dependency') {
240
- contextDependencies.add(message.file);
241
- }
242
- }
243
- }
244
- let [context, isNewContext] = (0, _setupContextUtils).getContext(root, result, tailwindConfig, userConfigPath, tailwindConfigHash, contextDependencies);
245
- let candidateFiles = getCandidateFiles(context, tailwindConfig);
246
- let contextConfigDependencies = getConfigDependencies(context);
247
- for (let file of configDependencies){
248
- registerDependency({
249
- type: 'dependency',
250
- file
251
- });
252
- }
253
- context.disposables.push((oldContext)=>{
254
- let watcher = getWatcher(oldContext);
255
- if (watcher !== null) {
256
- watcher.close();
257
- }
258
- });
259
- let configPath = getConfigPath(context, configOrPath);
260
- if (configPath !== null) {
261
- for (let dependency of (0, _getModuleDependencies).default(configPath)){
262
- if (dependency.file === configPath) {
263
- continue;
264
- }
265
- contextConfigDependencies.add(dependency.file);
266
- }
267
- }
268
- if (isNewContext) {
269
- rebootWatcher(context, configPath, contextConfigDependencies, candidateFiles);
270
- }
271
- // Register our temp file as a dependency — we write to this file
272
- // to trigger rebuilds.
273
- let touchFile = getTouchFile(context);
274
- if (touchFile) {
275
- registerDependency({
276
- type: 'dependency',
277
- file: touchFile
278
- });
279
- }
280
- if (tailwindDirectives.size > 0) {
281
- for (let changedContent of resolvedChangedContent(context, candidateFiles)){
282
- context.changedContent.push(changedContent);
283
- }
284
- }
285
- return context;
286
- };
287
- };
288
- }
@@ -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
- }