Skip to content

Commit

Permalink
Only log purge notice once per process
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Aug 19, 2020
1 parent ef149cf commit 2d090fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
22 changes: 1 addition & 21 deletions src/featureFlags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash'
import chalk from 'chalk'
import log from './util/log'

const featureFlags = {
future: ['removeDeprecatedGapUtilities'],
Expand Down Expand Up @@ -57,27 +58,6 @@ export function issueFlagNotices(config) {
return
}

const log = {
info(messages) {
console.log('')
messages.forEach(message => {
console.log(chalk.bold.cyan('info'), '-', message)
})
},
warn(messages) {
console.log('')
messages.forEach(message => {
console.log(chalk.bold.yellow('warn'), '-', message)
})
},
risk(messages) {
console.log('')
messages.forEach(message => {
console.log(chalk.bold.magenta('risk'), '-', message)
})
},
}

if (futureFlagsEnabled(config).length > 0) {
const changes = futureFlagsEnabled(config)
.map(s => chalk.cyan(s))
Expand Down
29 changes: 10 additions & 19 deletions src/lib/purgeUnusedStyles.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import _ from 'lodash'
import postcss from 'postcss'
import purgecss from '@fullhuman/postcss-purgecss'
import chalk from 'chalk'
import { log } from '../cli/utils'
import * as emoji from '../cli/emoji'
import log from '../util/log'

function removeTailwindMarkers(css) {
css.walkAtRules('tailwind', rule => rule.remove())
Expand All @@ -21,7 +19,7 @@ function removeTailwindMarkers(css) {
})
}

export default function purgeUnusedUtilities(config) {
export default function purgeUnusedUtilities(config, configChanged) {
const purgeEnabled = _.get(
config,
'purge.enabled',
Expand All @@ -34,21 +32,14 @@ export default function purgeUnusedUtilities(config) {

// Skip if `purge: []` since that's part of the default config
if (Array.isArray(config.purge) && config.purge.length === 0) {
log()
log(
emoji.warning,
chalk.yellow(
' Tailwind is not purging unused styles because no template paths have been provided.'
)
)
log(
chalk.white(
' If you have manually configured PurgeCSS outside of Tailwind or are deliberately not\n removing unused styles, set `purge: false` in your Tailwind config file to silence\n this warning.'
)
)
log(
chalk.white('\n https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css')
)
if (configChanged) {
log.warn([
'Tailwind is not purging unused styles because no template paths have been provided.',
'If you have manually configured PurgeCSS outside of Tailwind or are deliberately not removing unused styles, set `purge: false` in your Tailwind config file to silence this warning.',
'https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css',
])
}

return removeTailwindMarkers
}

Expand Down
8 changes: 2 additions & 6 deletions src/processTailwindFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { issueFlagNotices } from './featureFlags.js'

import hash from 'object-hash'

let flagsIssued = null
let previousConfig = null
let processedPlugins = null
let getProcessedPlugins = null
Expand All @@ -29,12 +28,9 @@ export default function(getConfig) {
const configChanged = hash(previousConfig) !== hash(config)
previousConfig = config

if (!flagsIssued || !_.isEqual(flagsIssued, _.pick(config, ['future', 'experimental']))) {
flagsIssued = _.pick(config, ['future', 'experimental'])
if (configChanged) {
issueFlagNotices(config)
}

if (configChanged) {
processedPlugins = processPlugins([...corePlugins(config), ...config.plugins], config)
getProcessedPlugins = function() {
return {
Expand All @@ -55,7 +51,7 @@ export default function(getConfig) {
substituteScreenAtRules(config),
substituteClassApplyAtRules(config, getProcessedPlugins, configChanged),
applyImportantConfiguration(config),
purgeUnusedStyles(config),
purgeUnusedStyles(config, configChanged),
]).process(css, { from: _.get(css, 'source.input.file') })
}
}
22 changes: 22 additions & 0 deletions src/util/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import chalk from 'chalk'

export default {
info(messages) {
console.log('')
messages.forEach(message => {
console.log(chalk.bold.cyan('info'), '-', message)
})
},
warn(messages) {
console.log('')
messages.forEach(message => {
console.log(chalk.bold.yellow('warn'), '-', message)
})
},
risk(messages) {
console.log('')
messages.forEach(message => {
console.log(chalk.bold.magenta('risk'), '-', message)
})
},
}

0 comments on commit 2d090fe

Please sign in to comment.