Skip to content

Commit

Permalink
add support for raw content extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc committed May 7, 2021
1 parent 906be69 commit a0b2de7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/jit/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ function getDefaultExtractor(fileExtension) {
}
}

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

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

const fileExtension = path.extname(fileName).slice(1)

if (!purgeOptions) {
return getDefaultExtractor(fileExtension)
}
Expand Down Expand Up @@ -213,13 +211,13 @@ 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(context.tailwindConfig, file)
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 of context.rawContent) {
let extractor = getExtractor(context.tailwindConfig)
for (let { content, extension } of context.rawContent) {
let extractor = getExtractor(context.tailwindConfig, extension)
getClassCandidates(content, extractor, contentMatchCache, candidates, seen)
}

Expand Down
4 changes: 3 additions & 1 deletion src/jit/lib/setupContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,9 @@ export default function setupContext(configOrPath) {
candidateFiles: purgeContent
.filter((item) => typeof item === 'string')
.map((path) => normalizePath(path)),
rawContent: purgeContent.filter((item) => typeof item.raw === 'string').map(({ raw }) => raw),
rawContent: purgeContent
.filter((item) => typeof item.raw === 'string')
.map(({ raw, extension }) => ({ content: raw, extension })),
variantMap: new Map(),
stylesheetCache: null,
fileModifiedMap: new Map(),
Expand Down
58 changes: 55 additions & 3 deletions tests/jit/raw-content.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import postcss from 'postcss'
import fs from 'fs'
import path from 'path'
import tailwind from '../../src/jit/index.js'

function run(input, config = {}) {
beforeEach(() => {
jest.resetModules()
})

function run(tailwind, input, config = {}) {
return postcss(tailwind(config)).process(input, {
from: path.resolve(__filename),
})
}

test('raw content', () => {
let tailwind = require('../../src/jit/index.js').default

let config = {
mode: 'jit',
purge: [{ raw: fs.readFileSync(path.resolve(__dirname, './raw-content.test.html'), 'utf8') }],
Expand All @@ -24,10 +29,57 @@ test('raw content', () => {
@tailwind utilities;
`

return run(css, config).then((result) => {
return run(tailwind, css, config).then((result) => {
let expectedPath = path.resolve(__dirname, './raw-content.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')

expect(result.css).toMatchFormattedCss(expected)
})
})

test('raw content with extension', () => {
let tailwind = require('../../src/jit/index.js').default

let config = {
mode: 'jit',
purge: {
content: [
{
raw: fs.readFileSync(path.resolve(__dirname, './raw-content.test.html'), 'utf8'),
extension: 'html',
},
],
options: {
extractors: [
{
extractor: () => [],
extensions: ['html'],
},
],
},
},
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

let css = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(tailwind, css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
* {
--tw-shadow: 0 0 #0000;
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
--tw-ring-offset-width: 0px;
--tw-ring-offset-color: #fff;
--tw-ring-color: rgba(59, 130, 246, 0.5);
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
}
`)
})
})

0 comments on commit a0b2de7

Please sign in to comment.