Skip to content

Commit

Permalink
[JIT] Add support for "raw" purge content (#4272)
Browse files Browse the repository at this point in the history
* add support for "raw" purge content

* add support for raw content extensions
  • Loading branch information
bradlc authored and adamwathan committed May 7, 2021
1 parent dfbeb1e commit 73fe57a
Show file tree
Hide file tree
Showing 5 changed files with 1,010 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/jit/lib/expandTailwindAtRules.js
Expand Up @@ -25,9 +25,12 @@ function getDefaultExtractor(fileExtension) {
}
}

function getExtractor(fileName, tailwindConfig) {
function getExtractor(tailwindConfig, fileExtension) {
const purgeOptions = tailwindConfig && tailwindConfig.purge && tailwindConfig.purge.options
const fileExtension = path.extname(fileName).slice(1)

if (!fileExtension) {
return (purgeOptions && purgeOptions.defaultExtractor) || getDefaultExtractor()
}

if (!purgeOptions) {
return getDefaultExtractor(fileExtension)
Expand Down Expand Up @@ -208,11 +211,16 @@ export default function expandTailwindAtRules(context, registerDependency) {
env.DEBUG && console.time('Reading changed files')
for (let file of context.changedFiles) {
let content = fs.readFileSync(file, 'utf8')
let extractor = getExtractor(file, context.tailwindConfig)
let extractor = getExtractor(context.tailwindConfig, path.extname(file).slice(1))
getClassCandidates(content, extractor, contentMatchCache, candidates, seen)
}
env.DEBUG && console.timeEnd('Reading changed files')

for (let { content, extension } of context.rawContent) {
let extractor = getExtractor(context.tailwindConfig, extension)
getClassCandidates(content, extractor, contentMatchCache, candidates, seen)
}

// ---

// Generate the actual CSS
Expand Down
14 changes: 10 additions & 4 deletions src/jit/lib/setupContext.js
Expand Up @@ -767,6 +767,10 @@ export default function setupContext(configOrPath) {

process.env.DEBUG && console.log('Setting up new context...')

let purgeContent = Array.isArray(tailwindConfig.purge)
? tailwindConfig.purge
: tailwindConfig.purge.content

let context = {
changedFiles: new Set(),
ruleCache: new Set(),
Expand All @@ -781,10 +785,12 @@ export default function setupContext(configOrPath) {
configPath: userConfigPath,
tailwindConfig: tailwindConfig,
configDependencies: new Set(),
candidateFiles: (Array.isArray(tailwindConfig.purge)
? tailwindConfig.purge
: tailwindConfig.purge.content
).map((path) => normalizePath(path)),
candidateFiles: purgeContent
.filter((item) => typeof item === 'string')
.map((path) => normalizePath(path)),
rawContent: purgeContent
.filter((item) => typeof item.raw === 'string')
.map(({ raw, extension }) => ({ content: raw, extension })),
variantMap: new Map(),
stylesheetCache: null,
fileModifiedMap: new Map(),
Expand Down

0 comments on commit 73fe57a

Please sign in to comment.