Skip to content

Commit 10710b0

Browse files
authored
Improve DEBUG flag (#6797)
* improve DEBUG flag * update changelog
1 parent 07c3e95 commit 10710b0

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Improve `DEBUG` flag ([#6797](https://github.com/tailwindlabs/tailwindcss/pull/6797))
1113

1214
## [3.0.8] - 2021-12-28
1315

src/lib/sharedState.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
11
export const env = {
22
TAILWIND_MODE: process.env.TAILWIND_MODE,
33
NODE_ENV: process.env.NODE_ENV,
4-
DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
4+
DEBUG: resolveDebug(process.env.DEBUG),
55
TAILWIND_DISABLE_TOUCH: process.env.TAILWIND_DISABLE_TOUCH !== undefined,
66
TAILWIND_TOUCH_DIR: process.env.TAILWIND_TOUCH_DIR,
77
}
88
export const contextMap = new Map()
99
export const configContextMap = new Map()
1010
export const contextSourcesMap = new Map()
11+
12+
export function resolveDebug(debug) {
13+
if (debug === undefined) {
14+
return false
15+
}
16+
17+
// Environment variables are strings, so convert to boolean
18+
if (debug === 'true' || debug === '1') {
19+
return true
20+
}
21+
22+
if (debug === 'false' || debug === '0') {
23+
return false
24+
}
25+
26+
// Keep the debug convention into account:
27+
// DEBUG=* -> This enables all debug modes
28+
// DEBUG=projectA,projectB,projectC -> This enables debug for projectA, projectB and projectC
29+
// DEBUG=projectA:* -> This enables all debug modes for projectA (if you have sub-types)
30+
// DEBUG=projectA,-projectB -> This enables debug for projectA and explicitly disables it for projectB
31+
32+
if (debug === '*') {
33+
return true
34+
}
35+
36+
let debuggers = debug.split(',').map((d) => d.split(':')[0])
37+
38+
// Ignoring tailwind / tailwindcss
39+
if (debuggers.includes('-tailwindcss') || debuggers.includes('-tailwind')) {
40+
return false
41+
}
42+
43+
// Definitely including tailwind / tailwindcss
44+
if (debuggers.includes('tailwindcss') || debuggers.includes('tailwind')) {
45+
return true
46+
}
47+
48+
return false
49+
}

tests/shared-state.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { resolveDebug } from '../src/lib/sharedState'
2+
3+
it.each`
4+
value | expected
5+
${'true'} | ${true}
6+
${'1'} | ${true}
7+
${'false'} | ${false}
8+
${'0'} | ${false}
9+
${'*'} | ${true}
10+
${'tailwind'} | ${true}
11+
${'tailwind:*'} | ${true}
12+
${'tailwindcss'} | ${true}
13+
${'tailwindcss:*'} | ${true}
14+
${'other,tailwind'} | ${true}
15+
${'other,tailwind:*'} | ${true}
16+
${'other,tailwindcss'} | ${true}
17+
${'other,tailwindcss:*'} | ${true}
18+
${'other,-tailwind'} | ${false}
19+
${'other,-tailwind:*'} | ${false}
20+
${'other,-tailwindcss'} | ${false}
21+
${'other,-tailwindcss:*'} | ${false}
22+
${'-tailwind'} | ${false}
23+
${'-tailwind:*'} | ${false}
24+
${'-tailwindcss'} | ${false}
25+
${'-tailwindcss:*'} | ${false}
26+
`('should resolve the debug ($value) flag correctly ($expected)', ({ value, expected }) => {
27+
expect(resolveDebug(value)).toBe(expected)
28+
})

0 commit comments

Comments
 (0)