Skip to content

Commit 243e881

Browse files
authored
Resolve purge paths relative to the current working directory (#4655)
* resolve purge paths relative to cwd * simplify test
1 parent 38a71d8 commit 243e881

10 files changed

+22
-31
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5252
- Improve animation value parsing ([#4250](https://github.com/tailwindlabs/tailwindcss/pull/4250))
5353
- Ignore unknown object types when hashing config ([82f4eaa](https://github.com/tailwindlabs/tailwindcss/commit/82f4eaa6832ef8a4e3fd90869e7068efdf6e34f2))
5454
- Ensure variants are grouped properly for plugins with order-dependent utilities ([#4273](https://github.com/tailwindlabs/tailwindcss/pull/4273))
55-
- Resolve purge paths relative to `tailwind.config.js` instead of the current working directory ([#4214](https://github.com/tailwindlabs/tailwindcss/pull/4214))
5655
- JIT: Fix temp file storage when node temp directories are kept on a different drive than the project itself ([#4044](https://github.com/tailwindlabs/tailwindcss/pull/4044))
5756
- Support border-opacity utilities alongside default `border` utility ([#4277](https://github.com/tailwindlabs/tailwindcss/pull/4277))
5857
- JIT: Fix source maps for expanded `@tailwind` directives ([2f15411](https://github.com/tailwindlabs/tailwindcss/commit/2f1541123dea29d8a2ab0f1411bf60c79eeb96b4))

src/index.js

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

9393
return {
9494
postcssPlugin: 'tailwindcss',
95-
plugins: [...plugins, processTailwindFeatures(getConfig, resolvedConfigPath), formatCSS],
95+
plugins: [...plugins, processTailwindFeatures(getConfig), formatCSS],
9696
}
9797
}
9898

src/jit/lib/setupTrackingContext.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let configPathCache = new LRU({ maxSize: 100 })
2121

2222
let candidateFilesCache = new WeakMap()
2323

24-
function getCandidateFiles(context, userConfigPath, tailwindConfig) {
24+
function getCandidateFiles(context, tailwindConfig) {
2525
if (candidateFilesCache.has(context)) {
2626
return candidateFilesCache.get(context)
2727
}
@@ -30,10 +30,9 @@ function getCandidateFiles(context, userConfigPath, tailwindConfig) {
3030
? tailwindConfig.purge
3131
: tailwindConfig.purge.content
3232

33-
let basePath = userConfigPath === null ? process.cwd() : path.dirname(userConfigPath)
3433
let candidateFiles = purgeContent
3534
.filter((item) => typeof item === 'string')
36-
.map((purgePath) => normalizePath(path.resolve(basePath, purgePath)))
35+
.map((purgePath) => normalizePath(path.resolve(purgePath)))
3736

3837
return candidateFilesCache.set(context, candidateFiles).get(context)
3938
}
@@ -172,7 +171,7 @@ export default function setupTrackingContext(configOrPath) {
172171
contextDependencies
173172
)
174173

175-
let candidateFiles = getCandidateFiles(context, userConfigPath, tailwindConfig)
174+
let candidateFiles = getCandidateFiles(context, tailwindConfig)
176175

177176
// If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
178177
// to be dependencies of the context. Can reuse the context even if they change.

src/jit/lib/setupWatchingContext.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function getConfigDependencies(context) {
142142

143143
let candidateFilesCache = new WeakMap()
144144

145-
function getCandidateFiles(context, userConfigPath, tailwindConfig) {
145+
function getCandidateFiles(context, tailwindConfig) {
146146
if (candidateFilesCache.has(context)) {
147147
return candidateFilesCache.get(context)
148148
}
@@ -151,10 +151,9 @@ function getCandidateFiles(context, userConfigPath, tailwindConfig) {
151151
? tailwindConfig.purge
152152
: tailwindConfig.purge.content
153153

154-
let basePath = userConfigPath === null ? process.cwd() : path.dirname(userConfigPath)
155154
let candidateFiles = purgeContent
156155
.filter((item) => typeof item === 'string')
157-
.map((purgePath) => normalizePath(path.resolve(basePath, purgePath)))
156+
.map((purgePath) => normalizePath(path.resolve(purgePath)))
158157

159158
return candidateFilesCache.set(context, candidateFiles).get(context)
160159
}
@@ -282,7 +281,7 @@ export default function setupWatchingContext(configOrPath) {
282281
contextDependencies
283282
)
284283

285-
let candidateFiles = getCandidateFiles(context, userConfigPath, tailwindConfig)
284+
let candidateFiles = getCandidateFiles(context, tailwindConfig)
286285
let contextConfigDependencies = getConfigDependencies(context)
287286

288287
for (let file of configDependencies) {

src/lib/purgeUnusedStyles.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ function getTransformer(config, fileExtension) {
4848
return transformers[fileExtension] || transformers.DEFAULT || ((content) => content)
4949
}
5050

51-
export default function purgeUnusedUtilities(
52-
config,
53-
configChanged,
54-
resolvedConfigPath,
55-
registerDependency
56-
) {
51+
export default function purgeUnusedUtilities(config, configChanged, registerDependency) {
5752
const purgeEnabled = _.get(
5853
config,
5954
'purge.enabled',
@@ -130,9 +125,7 @@ export default function purgeUnusedUtilities(
130125
Array.isArray(config.purge) ? config.purge : config.purge.content || purgeOptions.content || []
131126
).map((item) => {
132127
if (typeof item === 'string') {
133-
return normalizePath(
134-
path.resolve(resolvedConfigPath ? path.dirname(resolvedConfigPath) : process.cwd(), item)
135-
)
128+
return normalizePath(path.resolve(item))
136129
}
137130
return item
138131
})

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, resolvedConfigPath) {
27+
export default function (getConfig) {
2828
return function (css, result) {
2929
const config = getConfig()
3030
const configChanged = hash(previousConfig) !== hash(config)
@@ -73,7 +73,7 @@ export default function (getConfig, resolvedConfigPath) {
7373
substituteScreenAtRules({ tailwindConfig: config }),
7474
substituteClassApplyAtRules(config, getProcessedPlugins, configChanged),
7575
applyImportantConfiguration(config),
76-
purgeUnusedStyles(config, configChanged, resolvedConfigPath, registerDependency),
76+
purgeUnusedStyles(config, configChanged, registerDependency),
7777
]).process(css, { from: _.get(css, 'source.input.file') })
7878
}
7979
}

tests/fixtures/custom-purge-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
purge: ['./*.html'],
2+
purge: ['./tests/fixtures/*.html'],
33
theme: {
44
extend: {
55
colors: {

tests/jit/relative-purge-paths.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/jit/relative-purge-paths.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ function run(input, config = {}) {
1010
}
1111

1212
test('relative purge paths', () => {
13+
let config = {
14+
purge: ['./tests/jit/relative-purge-paths.test.html'],
15+
mode: 'jit',
16+
corePlugins: { preflight: false },
17+
theme: {},
18+
plugins: [],
19+
}
20+
1321
let css = `
1422
@tailwind base;
1523
@tailwind components;
1624
@tailwind utilities;
1725
`
1826

19-
return run(css, path.resolve(__dirname, './relative-purge-paths.config.js')).then((result) => {
27+
return run(css, config).then((result) => {
2028
let expectedPath = path.resolve(__dirname, './relative-purge-paths.test.css')
2129
let expected = fs.readFileSync(expectedPath, 'utf8')
2230

tests/purgeUnusedStyles.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ test('purges unused classes', () => {
105105
)
106106
})
107107

108-
test('purge patterns are resolved relative to the config file', () => {
108+
test('purge patterns are resolved relative to the current working directory', () => {
109109
return inProduction(
110110
suppressConsoleLogs(() => {
111111
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)

0 commit comments

Comments
 (0)