Skip to content

Commit 4b2482f

Browse files
authored
Warn about invalid globs in content (#6449)
* rewrite invalid globs if we can * warn instead of rewrite * update changelog
1 parent d07553b commit 4b2482f

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-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+
### Added
11+
12+
- Warn about invalid globs in `content` ([#6449](https://github.com/tailwindlabs/tailwindcss/pull/6449))
1113

1214
## [3.0.2] - 2021-12-13
1315

src/util/log.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function log(chalk, messages, key) {
1212
messages.forEach((message) => console.warn(chalk, '-', message))
1313
}
1414

15+
export function dim(input) {
16+
return chalk.dim(input)
17+
}
18+
1519
export default {
1620
info(key, messages) {
1721
log(chalk.bold.cyan('info'), ...(Array.isArray(key) ? [key] : [messages, key]))

src/util/normalizeConfig.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import log from './log'
1+
import log, { dim } from './log'
22

33
export function normalizeConfig(config) {
44
// Quick structure validation
@@ -245,5 +245,18 @@ export function normalizeConfig(config) {
245245
})(),
246246
}
247247

248+
// Validate globs to prevent bogus globs.
249+
// E.g.: `./src/*.{html}` is invalid, the `{html}` should just be `html`
250+
for (let file of config.content.files) {
251+
if (typeof file === 'string' && /{([^,]*?)}/g.test(file)) {
252+
log.warn('invalid-glob-braces', [
253+
`The glob pattern ${dim(file)} in your config is invalid.`,
254+
` Update it to ${dim(file.replace(/{([^,]*?)}/g, '$1'))} to silence this warning.`,
255+
// TODO: Add https://tw.wtf/invalid-glob-braces
256+
])
257+
break
258+
}
259+
}
260+
248261
return config
249262
}

tests/normalize-config.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { normalizeConfig } from '../src/util/normalizeConfig'
12
import { run, css } from './util/run'
3+
import resolveConfig from '../src/public/resolve-config'
24

35
it.each`
46
config
@@ -95,3 +97,45 @@ it('should still be possible to use the "old" v2 config', () => {
9597
`)
9698
})
9799
})
100+
101+
it('should keep content files with globs', () => {
102+
let config = {
103+
content: ['./example-folder/**/*.{html,js}'],
104+
}
105+
106+
expect(normalizeConfig(resolveConfig(config)).content).toEqual({
107+
files: ['./example-folder/**/*.{html,js}'],
108+
extract: {},
109+
transform: {},
110+
})
111+
})
112+
113+
it('should warn when we detect invalid globs with incorrect brace expansion', () => {
114+
let log = require('../src/util/log')
115+
let spy = jest.spyOn(log.default, 'warn')
116+
117+
let config = {
118+
content: [
119+
'./{example-folder}/**/*.{html,js}',
120+
'./{example-folder}/**/*.{html}',
121+
'./example-folder/**/*.{html}',
122+
],
123+
}
124+
125+
// No rewrite happens
126+
expect(normalizeConfig(resolveConfig(config)).content).toEqual({
127+
files: [
128+
'./{example-folder}/**/*.{html,js}',
129+
'./{example-folder}/**/*.{html}',
130+
'./example-folder/**/*.{html}',
131+
],
132+
extract: {},
133+
transform: {},
134+
})
135+
136+
// But a warning should happen
137+
expect(spy).toHaveBeenCalledTimes(2)
138+
expect(spy.mock.calls.map((x) => x[0])).toEqual(['invalid-glob-braces', 'invalid-glob-braces'])
139+
140+
spy.mockClear()
141+
})

0 commit comments

Comments
 (0)