Skip to content

Commit

Permalink
simplify resolving of purge/content information
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Sep 1, 2021
1 parent df48ea1 commit 7bf4685
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 227 deletions.
70 changes: 1 addition & 69 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,72 +20,6 @@ let env = {
DEBUG: process.env.DEBUG !== undefined,
}

let warned = false
function resolveContentPaths(config) {
if (config.hasOwnProperty('purge') && !warned) {
log.warn([
'The `purge` option in your tailwind.config.js file has been deprecated.',
'Please rename this to `content` instead.',
])
warned = true
}

if (Array.isArray(config.content)) {
return config.content
}

if (Array.isArray(config.content?.content)) {
return config.content.content
}

// TODO: Drop this in a future version
if (Array.isArray(config.purge)) {
return config.purge
}

if (Array.isArray(config.purge?.content)) {
return config.purge.content
}

return []
}

function resolveSafelistPaths(config) {
if (config.hasOwnProperty('purge') && !warned) {
log.warn([
'The `purge` option in your tailwind.config.js file has been deprecated.',
'Please rename this to `content` instead.',
])
warned = true
}

let [key, content] = (() => {
if (Array.isArray(config.content?.safelist)) {
return ['content.safelist', config.content.safelist]
}

if (Array.isArray(config.purge?.safelist)) {
return ['purge.safelist', config.purge.safelist]
}

return [null, []]
})()

return content.map((content) => {
if (typeof content === 'string') {
return { raw: content, extension: 'html' }
}

if (content instanceof RegExp) {
throw new Error(`Values inside '${key}' can only be of type 'string', found 'regex'.`)
}

throw new Error(
`Values inside '${key}' can only be of type 'string', found '${typeof content}'.`
)
})
}

// ---

function indentRecursive(node, indent = 0) {
Expand Down Expand Up @@ -498,9 +432,7 @@ async function build() {
}

function extractContent(config) {
let result = resolveContentPaths(config).concat(resolveSafelistPaths(config))
console.log({ result })
return result
return config.content.content.concat(config.content.safelist)
}

function extractFileGlobs(config) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const builtInTransformers = {
}

function getExtractor(tailwindConfig, fileExtension) {
let extractors = tailwindConfig?.content?.extract || tailwindConfig?.purge?.extract || {}
let contentOptions = tailwindConfig?.content?.options || tailwindConfig?.purge?.options || {}
let extractors = tailwindConfig.content.extract
let contentOptions = tailwindConfig.content.options

if (typeof extractors === 'function') {
extractors = {
Expand All @@ -56,7 +56,7 @@ function getExtractor(tailwindConfig, fileExtension) {
}

function getTransformer(tailwindConfig, fileExtension) {
let transformers = tailwindConfig?.content?.transform || tailwindConfig?.purge?.transform || {}
let transformers = tailwindConfig.content.transform

if (typeof transformers === 'function') {
transformers = {
Expand Down
73 changes: 3 additions & 70 deletions src/lib/setupTrackingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,6 @@ import { env } from './sharedState'

import { getContext, getFileModifiedMap } from './setupContextUtils'
import parseDependency from '../util/parseDependency'
import log from '../util/log'

let warned = false
function resolveContentPaths(config) {
if (config.hasOwnProperty('purge') && !warned) {
log.warn([
'The `purge` option in your tailwind.config.js file has been deprecated.',
'Please rename this to `content` instead.',
])
warned = true
}

if (Array.isArray(config.content)) {
return config.content
}

if (Array.isArray(config.content?.content)) {
return config.content.content
}

// TODO: Drop this in a future version
if (Array.isArray(config.purge)) {
return config.purge
}

if (Array.isArray(config.purge?.content)) {
return config.purge.content
}

return []
}

function resolveSafelistPaths(config) {
if (config.hasOwnProperty('purge') && !warned) {
log.warn([
'The `purge` option in your tailwind.config.js file has been deprecated.',
'Please rename this to `content` instead.',
])
warned = true
}

let [key, content] = (() => {
if (Array.isArray(config.content?.safelist)) {
return ['content.safelist', config.content.safelist]
}

if (Array.isArray(config.purge?.safelist)) {
return ['purge.safelist', config.purge.safelist]
}

return [null, []]
})()

return content.map((content) => {
if (typeof content === 'string') {
return { raw: content, extension: 'html' }
}

if (content instanceof RegExp) {
throw new Error(`Values inside '${key}' can only be of type 'string', found 'regex'.`)
}

throw new Error(
`Values inside '${key}' can only be of type 'string', found '${typeof content}'.`
)
})
}

let configPathCache = new LRU({ maxSize: 100 })

Expand All @@ -93,7 +26,7 @@ function getCandidateFiles(context, tailwindConfig) {
return candidateFilesCache.get(context)
}

let candidateFiles = resolveContentPaths(tailwindConfig)
let candidateFiles = tailwindConfig.content.content
.filter((item) => typeof item === 'string')
.map((contentPath) => normalizePath(path.resolve(contentPath)))

Expand Down Expand Up @@ -144,9 +77,9 @@ function getTailwindConfig(configOrPath) {
}

function resolvedChangedContent(context, candidateFiles, fileModifiedMap) {
let changedContent = resolveContentPaths(context.tailwindConfig)
let changedContent = context.tailwindConfig.content.content
.filter((item) => typeof item.raw === 'string')
.concat(resolveSafelistPaths(context.tailwindConfig))
.concat(context.tailwindConfig.content.safelist)
.map(({ raw, extension }) => ({ content: raw, extension }))

for (let changedFile of resolveChangedFiles(candidateFiles, fileModifiedMap)) {
Expand Down
72 changes: 3 additions & 69 deletions src/lib/setupWatchingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,6 @@ import resolveConfig from '../../resolveConfig'
import resolveConfigPath from '../util/resolveConfigPath'
import { getContext } from './setupContextUtils'

let warned = false
function resolveContentPaths(config) {
if (config.hasOwnProperty('purge') && !warned) {
log.warn([
'The `purge` option in your tailwind.config.js file has been deprecated.',
'Please rename this to `content` instead.',
])
warned = true
}

if (Array.isArray(config.content)) {
return config.content
}

if (Array.isArray(config.content?.content)) {
return config.content.content
}

// TODO: Drop this in a future version
if (Array.isArray(config.purge)) {
return config.purge
}

if (Array.isArray(config.purge?.content)) {
return config.purge.content
}

return []
}

function resolveSafelistPaths(config) {
if (config.hasOwnProperty('purge') && !warned) {
log.warn([
'The `purge` option in your tailwind.config.js file has been deprecated.',
'Please rename this to `content` instead.',
])
warned = true
}

let [key, content] = (() => {
if (Array.isArray(config.content?.safelist)) {
return ['content.safelist', config.content.safelist]
}

if (Array.isArray(config.purge?.safelist)) {
return ['purge.safelist', config.purge.safelist]
}

return [null, []]
})()

return content.map((content) => {
if (typeof content === 'string') {
return { raw: content, extension: 'html' }
}

if (content instanceof RegExp) {
throw new Error(`Values inside '${key}' can only be of type 'string', found 'regex'.`)
}

throw new Error(
`Values inside '${key}' can only be of type 'string', found '${typeof content}'.`
)
})
}

// This is used to trigger rebuilds. Just updating the timestamp
// is significantly faster than actually writing to the file (10x).

Expand Down Expand Up @@ -213,7 +147,7 @@ function getCandidateFiles(context, tailwindConfig) {
return candidateFilesCache.get(context)
}

let candidateFiles = resolveContentPaths(tailwindConfig)
let candidateFiles = tailwindConfig.content.content
.filter((item) => typeof item === 'string')
.map((contentPath) => normalizePath(path.resolve(contentPath)))

Expand Down Expand Up @@ -251,9 +185,9 @@ function getTailwindConfig(configOrPath) {
}

function resolvedChangedContent(context, candidateFiles) {
let changedContent = resolveContentPaths(context.tailwindConfig)
let changedContent = context.tailwindConfig.content.content
.filter((item) => typeof item.raw === 'string')
.concat(resolveSafelistPaths(context.tailwindConfig))
.concat(context.tailwindConfig.content.safelist)
.map(({ raw, extension }) => ({ content: raw, extension }))

for (let changedFile of resolveChangedFiles(context, candidateFiles)) {
Expand Down
Loading

0 comments on commit 7bf4685

Please sign in to comment.