Skip to content

Commit bef3838

Browse files
Show warnings for invalid content config (#7065)
1 parent 1eb4127 commit bef3838

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
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+
- Show warnings for invalid content config ([#7065](https://github.com/tailwindlabs/tailwindcss/pull/7065))
1113

1214
## [3.0.13] - 2022-01-11
1315

src/lib/expandTailwindAtRules.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import LRU from 'quick-lru'
22
import * as sharedState from './sharedState'
33
import { generateRules } from './generateRules'
44
import bigSign from '../util/bigSign'
5+
import log from '../util/log'
56
import cloneNodes from '../util/cloneNodes'
67
import { defaultExtractor } from './defaultExtractor'
78

@@ -227,6 +228,14 @@ export default function expandTailwindAtRules(context) {
227228
root.append(cloneNodes([...screenNodes], root.source))
228229
}
229230

231+
// If we've got a utility layer and no utilities are generated there's likely something wrong
232+
// TODO: Detect utility variants only
233+
if (layerNodes.utilities && utilityNodes.size === 0 && screenNodes.size === 0) {
234+
log.warn('content-problems', [
235+
'No utilities were generated there is likely a problem with the `content` key in the tailwind config. For more information see the documentation: https://tailwindcss.com/docs/content-configuration',
236+
])
237+
}
238+
230239
// ---
231240

232241
if (env.DEBUG) {

src/util/normalizeConfig.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,11 @@ export function normalizeConfig(config) {
258258
}
259259
}
260260

261+
if (config.content.files.length === 0) {
262+
log.warn('content-problems', [
263+
'The `content` key is missing or empty. Please populate the content key as Tailwind generates utilities on-demand based on the files that use them. For more information see the documentation: https://tailwindcss.com/docs/content-configuration',
264+
])
265+
}
266+
261267
return config
262268
}

tests/warnings.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { html, run, css } from './util/run'
2+
3+
let warn
4+
5+
beforeEach(() => {
6+
let log = require('../src/util/log')
7+
warn = jest.spyOn(log.default, 'warn')
8+
})
9+
10+
afterEach(() => {
11+
warn.mockClear()
12+
})
13+
14+
test('it warns when there is no content key', async () => {
15+
let config = {
16+
corePlugins: { preflight: false },
17+
}
18+
19+
let input = css`
20+
@tailwind base;
21+
`
22+
23+
await run(input, config)
24+
25+
expect(warn).toHaveBeenCalledTimes(1)
26+
expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems'])
27+
})
28+
29+
test('it warns when there is an empty content key', async () => {
30+
let config = {
31+
content: [],
32+
corePlugins: { preflight: false },
33+
}
34+
35+
let input = css`
36+
@tailwind base;
37+
`
38+
39+
await run(input, config)
40+
41+
expect(warn).toHaveBeenCalledTimes(1)
42+
expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems'])
43+
})
44+
45+
test('it warns when there are no utilities generated', async () => {
46+
let config = {
47+
content: [{ raw: html`nothing here matching a utility` }],
48+
corePlugins: { preflight: false },
49+
}
50+
51+
let input = css`
52+
@tailwind utilities;
53+
`
54+
55+
await run(input, config)
56+
57+
expect(warn).toHaveBeenCalledTimes(1)
58+
expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems'])
59+
})
60+
61+
it('warnings are not thrown when only variant utilities are generated', async () => {
62+
let config = {
63+
content: [{ raw: html`<div class="sm:underline"></div>` }],
64+
corePlugins: { preflight: false },
65+
}
66+
67+
let input = css`
68+
@tailwind utilities;
69+
`
70+
71+
await run(input, config)
72+
73+
expect(warn).toHaveBeenCalledTimes(0)
74+
})

0 commit comments

Comments
 (0)