Skip to content

Commit 477dd06

Browse files
authored
Resolve purge paths (#4214)
* resolve purge paths * add test for purgecss pattern resolution * resolve purgecss patterns relative to the config file if there is one * account for raw content when transforming purgecss options * append test name to postcss `from` option in purge tests fixes tests hanging * add test for relative purge path resolution in JIT mode
1 parent a974d0e commit 477dd06

10 files changed

+1018
-39
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports = function (config) {
9797

9898
return {
9999
postcssPlugin: 'tailwindcss',
100-
plugins: [...plugins, processTailwindFeatures(getConfig), formatCSS],
100+
plugins: [...plugins, processTailwindFeatures(getConfig, resolvedConfigPath), formatCSS],
101101
}
102102
}
103103

src/jit/lib/setupContext.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,14 @@ export default function setupContext(configOrPath) {
787787
configDependencies: new Set(),
788788
candidateFiles: purgeContent
789789
.filter((item) => typeof item === 'string')
790-
.map((path) => normalizePath(path)),
790+
.map((purgePath) =>
791+
normalizePath(
792+
path.resolve(
793+
userConfigPath === null ? process.cwd() : path.dirname(userConfigPath),
794+
purgePath
795+
)
796+
)
797+
),
791798
rawContent: purgeContent
792799
.filter((item) => typeof item.raw === 'string')
793800
.map(({ raw, extension }) => ({ content: raw, extension })),

src/lib/purgeUnusedStyles.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import postcss from 'postcss'
33
import purgecss from '@fullhuman/postcss-purgecss'
44
import log from '../util/log'
55
import htmlTags from 'html-tags'
6+
import path from 'path'
67

78
function removeTailwindMarkers(css) {
89
css.walkAtRules('tailwind', (rule) => rule.remove())
@@ -33,7 +34,7 @@ export function tailwindExtractor(content) {
3334
return broadMatches.concat(broadMatchesWithoutTrailingSlash).concat(innerMatches)
3435
}
3536

36-
export default function purgeUnusedUtilities(config, configChanged) {
37+
export default function purgeUnusedUtilities(config, configChanged, resolvedConfigPath) {
3738
const purgeEnabled = _.get(
3839
config,
3940
'purge.enabled',
@@ -104,7 +105,6 @@ export default function purgeUnusedUtilities(config, configChanged) {
104105
},
105106
removeTailwindMarkers,
106107
purgecss({
107-
content: Array.isArray(config.purge) ? config.purge : config.purge.content,
108108
defaultExtractor: (content) => {
109109
const extractor = defaultExtractor || tailwindExtractor
110110
const preserved = [...extractor(content)]
@@ -116,6 +116,18 @@ export default function purgeUnusedUtilities(config, configChanged) {
116116
return preserved
117117
},
118118
...purgeOptions,
119+
content: (Array.isArray(config.purge)
120+
? config.purge
121+
: config.purge.content || purgeOptions.content || []
122+
).map((item) => {
123+
if (typeof item === 'string') {
124+
return path.resolve(
125+
resolvedConfigPath ? path.dirname(resolvedConfigPath) : process.cwd(),
126+
item
127+
)
128+
}
129+
return item
130+
}),
119131
}),
120132
])
121133
}

src/processTailwindFeatures.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let previousConfig = null
2424
let processedPlugins = null
2525
let getProcessedPlugins = null
2626

27-
export default function (getConfig) {
27+
export default function (getConfig, resolvedConfigPath) {
2828
return function (css) {
2929
const config = getConfig()
3030
const configChanged = hash(previousConfig) !== hash(config)
@@ -65,7 +65,7 @@ export default function (getConfig) {
6565
substituteScreenAtRules(config),
6666
substituteClassApplyAtRules(config, getProcessedPlugins, configChanged),
6767
applyImportantConfiguration(config),
68-
purgeUnusedStyles(config, configChanged),
68+
purgeUnusedStyles(config, configChanged, resolvedConfigPath),
6969
]).process(css, { from: _.get(css, 'source.input.file') })
7070
}
7171
}

tests/fixtures/custom-purge-config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
purge: ['./*.html'],
3+
theme: {
4+
extend: {
5+
colors: {
6+
'black!': '#000',
7+
},
8+
spacing: {
9+
1.5: '0.375rem',
10+
'(1/2+8)': 'calc(50% + 2rem)',
11+
},
12+
minHeight: {
13+
'(screen-4)': 'calc(100vh - 1rem)',
14+
},
15+
fontFamily: {
16+
'%#$@': 'Comic Sans',
17+
},
18+
},
19+
},
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
mode: 'jit',
3+
purge: ['./relative-purge-paths.test.html'],
4+
corePlugins: { preflight: false },
5+
theme: {},
6+
plugins: [],
7+
}

0 commit comments

Comments
 (0)