Skip to content

Commit

Permalink
Register PurgeCSS content array as PostCSS dependencies (#4530)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc committed Jun 1, 2021
1 parent 1aedf10 commit 1600fbd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/lib/purgeUnusedStyles.js
Expand Up @@ -3,6 +3,8 @@ import postcss from 'postcss'
import purgecss from '@fullhuman/postcss-purgecss'
import log from '../util/log'
import htmlTags from 'html-tags'
import parseGlob from 'parse-glob'
import path from 'path'

function removeTailwindMarkers(css) {
css.walkAtRules('tailwind', (rule) => rule.remove())
Expand Down Expand Up @@ -33,7 +35,7 @@ export function tailwindExtractor(content) {
return broadMatches.concat(broadMatchesWithoutTrailingSlash).concat(innerMatches)
}

export default function purgeUnusedUtilities(config, configChanged) {
export default function purgeUnusedUtilities(config, configChanged, registerDependency) {
const purgeEnabled = _.get(
config,
'purge.enabled',
Expand All @@ -59,6 +61,23 @@ export default function purgeUnusedUtilities(config, configChanged) {

const { defaultExtractor, ...purgeOptions } = config.purge.options || {}

let content = Array.isArray(config.purge) ? config.purge : config.purge.content

for (let maybeGlob of content) {
let { is, base } = parseGlob(maybeGlob)

if (is.glob) {
// rollup-plugin-postcss does not support dir-dependency messages
// but directories can be watched in the same way as files
registerDependency(
path.resolve(base),
process.env.ROLLUP_WATCH === 'true' ? 'dependency' : 'dir-dependency'
)
} else {
registerDependency(path.resolve(maybeGlob))
}
}

return postcss([
function (css) {
const mode = _.get(config, 'purge.mode', 'layers')
Expand Down Expand Up @@ -104,7 +123,7 @@ export default function purgeUnusedUtilities(config, configChanged) {
},
removeTailwindMarkers,
purgecss({
content: Array.isArray(config.purge) ? config.purge : config.purge.content,
content,
defaultExtractor: (content) => {
const extractor = defaultExtractor || tailwindExtractor
const preserved = [...extractor(content)]
Expand Down
13 changes: 11 additions & 2 deletions src/processTailwindFeatures.js
Expand Up @@ -25,7 +25,7 @@ let processedPlugins = null
let getProcessedPlugins = null

export default function (getConfig) {
return function (css) {
return function (css, result) {
const config = getConfig()
const configChanged = hash(previousConfig) !== hash(config)
previousConfig = config
Expand Down Expand Up @@ -56,6 +56,15 @@ export default function (getConfig) {
}
}

function registerDependency(fileName, type = 'dependency') {
result.messages.push({
type,
plugin: 'tailwindcss',
parent: result.opts.from,
[type === 'dir-dependency' ? 'dir' : 'file']: fileName,
})
}

return postcss([
substituteTailwindAtRules(config, getProcessedPlugins()),
evaluateTailwindFunctions(config),
Expand All @@ -65,7 +74,7 @@ export default function (getConfig) {
substituteScreenAtRules(config),
substituteClassApplyAtRules(config, getProcessedPlugins, configChanged),
applyImportantConfiguration(config),
purgeUnusedStyles(config, configChanged),
purgeUnusedStyles(config, configChanged, registerDependency),
]).process(css, { from: _.get(css, 'source.input.file') })
}
}

0 comments on commit 1600fbd

Please sign in to comment.