Skip to content

Commit

Permalink
Show warnings for invalid content config (#7065)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace authored Jan 14, 2022
1 parent 1eb4127 commit bef3838
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Added

- Show warnings for invalid content config ([#7065](https://github.com/tailwindlabs/tailwindcss/pull/7065))

## [3.0.13] - 2022-01-11

Expand Down
9 changes: 9 additions & 0 deletions src/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import LRU from 'quick-lru'
import * as sharedState from './sharedState'
import { generateRules } from './generateRules'
import bigSign from '../util/bigSign'
import log from '../util/log'
import cloneNodes from '../util/cloneNodes'
import { defaultExtractor } from './defaultExtractor'

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

// If we've got a utility layer and no utilities are generated there's likely something wrong
// TODO: Detect utility variants only
if (layerNodes.utilities && utilityNodes.size === 0 && screenNodes.size === 0) {
log.warn('content-problems', [
'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',
])
}

// ---

if (env.DEBUG) {
Expand Down
6 changes: 6 additions & 0 deletions src/util/normalizeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,11 @@ export function normalizeConfig(config) {
}
}

if (config.content.files.length === 0) {
log.warn('content-problems', [
'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',
])
}

return config
}
74 changes: 74 additions & 0 deletions tests/warnings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { html, run, css } from './util/run'

let warn

beforeEach(() => {
let log = require('../src/util/log')
warn = jest.spyOn(log.default, 'warn')
})

afterEach(() => {
warn.mockClear()
})

test('it warns when there is no content key', async () => {
let config = {
corePlugins: { preflight: false },
}

let input = css`
@tailwind base;
`

await run(input, config)

expect(warn).toHaveBeenCalledTimes(1)
expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems'])
})

test('it warns when there is an empty content key', async () => {
let config = {
content: [],
corePlugins: { preflight: false },
}

let input = css`
@tailwind base;
`

await run(input, config)

expect(warn).toHaveBeenCalledTimes(1)
expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems'])
})

test('it warns when there are no utilities generated', async () => {
let config = {
content: [{ raw: html`nothing here matching a utility` }],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

await run(input, config)

expect(warn).toHaveBeenCalledTimes(1)
expect(warn.mock.calls.map((x) => x[0])).toEqual(['content-problems'])
})

it('warnings are not thrown when only variant utilities are generated', async () => {
let config = {
content: [{ raw: html`<div class="sm:underline"></div>` }],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

await run(input, config)

expect(warn).toHaveBeenCalledTimes(0)
})

0 comments on commit bef3838

Please sign in to comment.