Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit b7fba16

Browse files
authored
Support Svelte 'class:' syntax (#183)
1 parent 9658133 commit b7fba16

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

src/lib/expandTailwindAtRules.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ let contentMatchCache = sharedState.contentMatchCache
1212
const BROAD_MATCH_GLOBAL_REGEXP = /[^<>"'`\s]*[^<>"'`\s:]/g
1313
const INNER_MATCH_GLOBAL_REGEXP = /[^<>"'`\s.(){}[\]#=%]*[^<>"'`\s.(){}[\]#=%:]/g
1414

15-
function defaultJitExtractor(content) {
16-
let broadMatches = content.match(BROAD_MATCH_GLOBAL_REGEXP) || []
17-
let innerMatches = content.match(INNER_MATCH_GLOBAL_REGEXP) || []
15+
function getDefaultExtractor(fileExtension) {
16+
return function (content) {
17+
if (fileExtension === 'svelte') {
18+
content = content.replace(/\sclass:/g, ' ')
19+
}
20+
let broadMatches = content.match(BROAD_MATCH_GLOBAL_REGEXP) || []
21+
let innerMatches = content.match(INNER_MATCH_GLOBAL_REGEXP) || []
1822

19-
return [...broadMatches, ...innerMatches]
23+
return [...broadMatches, ...innerMatches]
24+
}
2025
}
2126

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

2531
if (!purgeOptions) {
26-
return defaultJitExtractor
32+
return getDefaultExtractor(fileExtension)
2733
}
2834

29-
const fileExtension = path.extname(fileName).slice(1)
3035
const fileSpecificExtractor = (purgeOptions.extractors || []).find((extractor) =>
3136
extractor.extensions.includes(fileExtension)
3237
)
@@ -35,7 +40,7 @@ function getExtractor(fileName, tailwindConfig) {
3540
return fileSpecificExtractor.extractor
3641
}
3742

38-
return purgeOptions.defaultExtractor || defaultJitExtractor
43+
return purgeOptions.defaultExtractor || getDefaultExtractor(fileExtension)
3944
}
4045

4146
// Scans template contents for possible classes. This is a hot path on initial build but

tests/svelte-syntax.test.css

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
* {
2+
--tw-shadow: 0 0 #0000;
3+
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
4+
--tw-ring-offset-width: 0px;
5+
--tw-ring-offset-color: #fff;
6+
--tw-ring-color: rgba(59, 130, 246, 0.5);
7+
--tw-ring-offset-shadow: 0 0 #0000;
8+
--tw-ring-shadow: 0 0 #0000;
9+
}
10+
@media (min-width: 1024px) {
11+
.lg\:hover\:bg-blue-500:hover {
12+
--tw-bg-opacity: 1;
13+
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
14+
}
15+
}

tests/svelte-syntax.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const postcss = require('postcss')
2+
const tailwind = require('../src/index.js')
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
function run(input, config = {}) {
7+
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
8+
}
9+
10+
test('basic usage', () => {
11+
let config = {
12+
purge: [path.resolve(__dirname, './svelte-syntax.test.svelte')],
13+
corePlugins: { preflight: false },
14+
theme: {},
15+
plugins: [],
16+
}
17+
18+
let css = `
19+
@tailwind base;
20+
@tailwind components;
21+
@tailwind utilities;
22+
`
23+
24+
return run(css, config).then((result) => {
25+
let expectedPath = path.resolve(__dirname, './svelte-syntax.test.css')
26+
let expected = fs.readFileSync(expectedPath, 'utf8')
27+
28+
expect(result.css).toMatchCss(expected)
29+
})
30+
})

tests/svelte-syntax.test.svelte

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let current = 'foo'
3+
</script>
4+
5+
<button class:lg:hover:bg-blue-500={current === 'foo'}>Click me</button>

0 commit comments

Comments
 (0)