Skip to content

Commit a0b2de7

Browse files
committed
add support for raw content extensions
1 parent 906be69 commit a0b2de7

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

src/jit/lib/expandTailwindAtRules.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ function getDefaultExtractor(fileExtension) {
2525
}
2626
}
2727

28-
function getExtractor(tailwindConfig, fileName) {
28+
function getExtractor(tailwindConfig, fileExtension) {
2929
const purgeOptions = tailwindConfig && tailwindConfig.purge && tailwindConfig.purge.options
3030

31-
if (!fileName) {
31+
if (!fileExtension) {
3232
return (purgeOptions && purgeOptions.defaultExtractor) || getDefaultExtractor()
3333
}
3434

35-
const fileExtension = path.extname(fileName).slice(1)
36-
3735
if (!purgeOptions) {
3836
return getDefaultExtractor(fileExtension)
3937
}
@@ -213,13 +211,13 @@ export default function expandTailwindAtRules(context, registerDependency) {
213211
env.DEBUG && console.time('Reading changed files')
214212
for (let file of context.changedFiles) {
215213
let content = fs.readFileSync(file, 'utf8')
216-
let extractor = getExtractor(context.tailwindConfig, file)
214+
let extractor = getExtractor(context.tailwindConfig, path.extname(file).slice(1))
217215
getClassCandidates(content, extractor, contentMatchCache, candidates, seen)
218216
}
219217
env.DEBUG && console.timeEnd('Reading changed files')
220218

221-
for (let content of context.rawContent) {
222-
let extractor = getExtractor(context.tailwindConfig)
219+
for (let { content, extension } of context.rawContent) {
220+
let extractor = getExtractor(context.tailwindConfig, extension)
223221
getClassCandidates(content, extractor, contentMatchCache, candidates, seen)
224222
}
225223

src/jit/lib/setupContext.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,9 @@ export default function setupContext(configOrPath) {
788788
candidateFiles: purgeContent
789789
.filter((item) => typeof item === 'string')
790790
.map((path) => normalizePath(path)),
791-
rawContent: purgeContent.filter((item) => typeof item.raw === 'string').map(({ raw }) => raw),
791+
rawContent: purgeContent
792+
.filter((item) => typeof item.raw === 'string')
793+
.map(({ raw, extension }) => ({ content: raw, extension })),
792794
variantMap: new Map(),
793795
stylesheetCache: null,
794796
fileModifiedMap: new Map(),

tests/jit/raw-content.test.js

+55-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import postcss from 'postcss'
22
import fs from 'fs'
33
import path from 'path'
4-
import tailwind from '../../src/jit/index.js'
54

6-
function run(input, config = {}) {
5+
beforeEach(() => {
6+
jest.resetModules()
7+
})
8+
9+
function run(tailwind, input, config = {}) {
710
return postcss(tailwind(config)).process(input, {
811
from: path.resolve(__filename),
912
})
1013
}
1114

1215
test('raw content', () => {
16+
let tailwind = require('../../src/jit/index.js').default
17+
1318
let config = {
1419
mode: 'jit',
1520
purge: [{ raw: fs.readFileSync(path.resolve(__dirname, './raw-content.test.html'), 'utf8') }],
@@ -24,10 +29,57 @@ test('raw content', () => {
2429
@tailwind utilities;
2530
`
2631

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

3136
expect(result.css).toMatchFormattedCss(expected)
3237
})
3338
})
39+
40+
test('raw content with extension', () => {
41+
let tailwind = require('../../src/jit/index.js').default
42+
43+
let config = {
44+
mode: 'jit',
45+
purge: {
46+
content: [
47+
{
48+
raw: fs.readFileSync(path.resolve(__dirname, './raw-content.test.html'), 'utf8'),
49+
extension: 'html',
50+
},
51+
],
52+
options: {
53+
extractors: [
54+
{
55+
extractor: () => [],
56+
extensions: ['html'],
57+
},
58+
],
59+
},
60+
},
61+
corePlugins: { preflight: false },
62+
theme: {},
63+
plugins: [],
64+
}
65+
66+
let css = `
67+
@tailwind base;
68+
@tailwind components;
69+
@tailwind utilities;
70+
`
71+
72+
return run(tailwind, css, config).then((result) => {
73+
expect(result.css).toMatchFormattedCss(`
74+
* {
75+
--tw-shadow: 0 0 #0000;
76+
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
77+
--tw-ring-offset-width: 0px;
78+
--tw-ring-offset-color: #fff;
79+
--tw-ring-color: rgba(59, 130, 246, 0.5);
80+
--tw-ring-offset-shadow: 0 0 #0000;
81+
--tw-ring-shadow: 0 0 #0000;
82+
}
83+
`)
84+
})
85+
})

0 commit comments

Comments
 (0)