Register PurgeCSS content as PostCSS dependencies (#4543)

* add `glob` property to `dir-dependency` messages

* Add `glob` to `dir-dependency` messages

* register purge content as postcss dependencies
This commit is contained in:
Brad Cornes 2021-06-03 10:41:32 +01:00 • committed by GitHub
parent 1e48728efc
commit a3db3a4700
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 15 deletions

View file

@ -4,6 +4,8 @@ import purgecss from '@fullhuman/postcss-purgecss'
import log from '../util/log'
import htmlTags from 'html-tags'
import path from 'path'
import parseDependency from '../util/parseDependency'
import normalizePath from 'normalize-path'
function removeTailwindMarkers(css) {
css.walkAtRules('tailwind', (rule) => rule.remove())
@ -46,7 +48,12 @@ function getTransformer(config, fileExtension) {
return transformers[fileExtension] || transformers.DEFAULT || ((content) => content)
}
export default function purgeUnusedUtilities(config, configChanged, resolvedConfigPath) {
export default function purgeUnusedUtilities(
config,
configChanged,
resolvedConfigPath,
registerDependency
) {
const purgeEnabled = _.get(
config,
'purge.enabled',
@ -115,6 +122,21 @@ export default function purgeUnusedUtilities(config, configChanged, resolvedConf
}
})
let content = (
Array.isArray(config.purge) ? config.purge : config.purge.content || purgeOptions.content || []
).map((item) => {
if (typeof item === 'string') {
return normalizePath(
path.resolve(resolvedConfigPath ? path.dirname(resolvedConfigPath) : process.cwd(), item)
)
}
return item
})
for (let fileOrGlob of content.filter((item) => typeof item === 'string')) {
registerDependency(parseDependency(fileOrGlob))
}
return postcss([
function (css) {
const mode = _.get(config, 'purge.mode', 'layers')
@ -166,18 +188,7 @@ export default function purgeUnusedUtilities(config, configChanged, resolvedConf
},
extractors: fileSpecificExtractors,
...purgeOptions,
content: (Array.isArray(config.purge)
? config.purge
: config.purge.content || purgeOptions.content || []
).map((item) => {
if (typeof item === 'string') {
return path.resolve(
resolvedConfigPath ? path.dirname(resolvedConfigPath) : process.cwd(),
item
)
}
return item
}),
content,
}),
])
}

View file

@ -25,7 +25,7 @@ let processedPlugins = null
let getProcessedPlugins = null
export default function (getConfig, resolvedConfigPath) {
return function (css) {
return function (css, result) {
const config = getConfig()
const configChanged = hash(previousConfig) !== hash(config)
previousConfig = config
@ -56,6 +56,14 @@ export default function (getConfig, resolvedConfigPath) {
}
}
function registerDependency(dependency) {
result.messages.push({
plugin: 'tailwindcss',
parent: result.opts.from,
...dependency,
})
}
return postcss([
substituteTailwindAtRules(config, getProcessedPlugins()),
evaluateTailwindFunctions({ tailwindConfig: config }),
@ -65,7 +73,7 @@ export default function (getConfig, resolvedConfigPath) {
substituteScreenAtRules({ tailwindConfig: config }),
substituteClassApplyAtRules(config, getProcessedPlugins, configChanged),
applyImportantConfiguration(config),
purgeUnusedStyles(config, configChanged, resolvedConfigPath),
purgeUnusedStyles(config, configChanged, resolvedConfigPath, registerDependency),
]).process(css, { from: _.get(css, 'source.input.file') })
}
}