Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register PurgeCSS content array as PostCSS dependencies #4530

Merged
merged 1 commit into from Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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') })
}
}