Skip to content

Commit cd5cb00

Browse files
reininkadamwathan
andauthored
Implement one-time logging to prevent duplicate warnings (tailwindlabs#5661)
Co-Authored-By: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
1 parent 61f881f commit cd5cb00

File tree

8 files changed

+38
-52
lines changed

8 files changed

+38
-52
lines changed

src/cli.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,10 @@ async function build() {
420420
let resolvedConfig = resolveConfigInternal(config)
421421

422422
if (args['--purge']) {
423-
log.warn(['The `--purge` flag has been deprecated.', 'Please use `--content` instead.'])
423+
log.warn('purge-flag-deprecated', [
424+
'The `--purge` flag has been deprecated.',
425+
'Please use `--content` instead.',
426+
])
424427
if (!args['--content']) {
425428
args['--content'] = args['--purge']
426429
}

src/corePlugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export default {
296296
let mode = config('darkMode', 'media')
297297
if (mode === false) {
298298
mode = 'media'
299-
log.warn([
299+
log.warn('darkmode-false', [
300300
'`darkMode` is set to `false` in your config.',
301301
'This will behave just like the `media` value.',
302302
])

src/featureFlags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function issueFlagNotices(config) {
3838
.map((s) => chalk.yellow(s))
3939
.join(', ')
4040

41-
log.warn([
41+
log.warn('experimental-flags-enabled', [
4242
`You have enabled experimental features: ${changes}`,
4343
'Experimental features are not covered by semver, may introduce breaking changes, and can change at any time.',
4444
])

src/lib/normalizeTailwindDirectives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function normalizeTailwindDirectives(root) {
4141

4242
if (['layer', 'responsive', 'variants'].includes(atRule.name)) {
4343
if (['responsive', 'variants'].includes(atRule.name)) {
44-
log.warn([
44+
log.warn(`${atRule.name}-at-rule-deprecated`, [
4545
`'@${atRule.name}' is deprecated, use '@layer utilities' or '@layer components' instead.`,
4646
])
4747
}

src/lib/setupContextUtils.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,6 @@ function registerPlugins(plugins, context) {
559559
)
560560
}
561561

562-
//
563-
let warnedAbout = new Set([])
564562
context.safelist = function () {
565563
let safelist = (context.tailwindConfig.safelist ?? []).filter(Boolean)
566564
if (safelist.length <= 0) return []
@@ -575,14 +573,11 @@ function registerPlugins(plugins, context) {
575573
}
576574

577575
if (value instanceof RegExp) {
578-
if (!warnedAbout.has('root-regex')) {
579-
log.warn([
580-
// TODO: Improve this warning message
581-
'RegExp in the safelist option is not supported.',
582-
'Please use the object syntax instead: https://tailwindcss.com/docs/...',
583-
])
584-
warnedAbout.add('root-regex')
585-
}
576+
log.warn('root-regex', [
577+
// TODO: Improve this warning message
578+
'RegExp in the safelist option is not supported.',
579+
'Please use the object syntax instead: https://tailwindcss.com/docs/...',
580+
])
586581
continue
587582
}
588583

src/public/colors.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import log from '../util/log'
22

3-
let warned = []
4-
53
function warn({ version, from, to }) {
6-
if (!warned.includes(from)) {
7-
log.warn([
8-
`As of Tailwind CSS ${version}, \`${from}\` has been renamed to \`${to}\`.`,
9-
'Please update your color palette to eliminate this warning.',
10-
])
11-
warned.push(from)
12-
}
4+
log.warn(`${from}-color-rename`, [
5+
`As of Tailwind CSS ${version}, \`${from}\` has been renamed to \`${to}\`.`,
6+
'Please update your color palette to eliminate this warning.',
7+
])
138
}
149

1510
export default {

src/util/log.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import chalk from 'chalk'
22

3-
export default {
4-
info(messages) {
5-
if (process.env.JEST_WORKER_ID !== undefined) return
3+
let alreadyShown = new Set()
64

7-
console.warn('')
8-
messages.forEach((message) => {
9-
console.warn(chalk.bold.cyan('info'), '-', message)
10-
})
11-
},
12-
warn(messages) {
13-
if (process.env.JEST_WORKER_ID !== undefined) return
5+
function log(chalk, messages, key) {
6+
if (process.env.JEST_WORKER_ID !== undefined) return
147

15-
console.warn('')
16-
messages.forEach((message) => {
17-
console.warn(chalk.bold.yellow('warn'), '-', message)
18-
})
19-
},
20-
risk(messages) {
21-
if (process.env.JEST_WORKER_ID !== undefined) return
8+
if (key && alreadyShown.has(key)) return
9+
if (key) alreadyShown.add(key)
10+
11+
console.warn('')
12+
messages.forEach((message) => console.warn(chalk, '-', message))
13+
}
2214

23-
console.warn('')
24-
messages.forEach((message) => {
25-
console.warn(chalk.bold.magenta('risk'), '-', message)
26-
})
15+
export default {
16+
info(key, messages) {
17+
log(chalk.bold.cyan('info'), ...(Array.isArray(key) ? [key] : [messages, key]))
18+
},
19+
warn(key, messages) {
20+
log(chalk.bold.yellow('warn'), ...(Array.isArray(key) ? [key] : [messages, key]))
21+
},
22+
risk(key, messages) {
23+
log(chalk.bold.magenta('risk'), ...(Array.isArray(key) ? [key] : [messages, key]))
2724
},
2825
}

src/util/resolveConfig.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,11 @@ export default function resolveConfig(configs) {
222222
)
223223
}
224224

225-
let warnedAbout = new Set()
226225
function normalizeConfig(config) {
227-
if (!warnedAbout.has('purge-deprecation') && config.hasOwnProperty('purge')) {
228-
log.warn([
229-
'The `purge` option in your tailwind.config.js file has been deprecated.',
230-
'Please rename this to `content` instead.',
231-
])
232-
warnedAbout.add('purge-deprecation')
233-
}
226+
log.warn('purge-deprecation', [
227+
'The `purge` option in your tailwind.config.js file has been deprecated.',
228+
'Please rename this to `content` instead.',
229+
])
234230

235231
config.content = {
236232
content: (() => {

0 commit comments

Comments
 (0)