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

Support Svelte 'class:' syntax #183

Merged
merged 1 commit into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ let contentMatchCache = sharedState.contentMatchCache
const BROAD_MATCH_GLOBAL_REGEXP = /[^<>"'`\s]*[^<>"'`\s:]/g
const INNER_MATCH_GLOBAL_REGEXP = /[^<>"'`\s.(){}[\]#=%]*[^<>"'`\s.(){}[\]#=%:]/g

function defaultJitExtractor(content) {
let broadMatches = content.match(BROAD_MATCH_GLOBAL_REGEXP) || []
let innerMatches = content.match(INNER_MATCH_GLOBAL_REGEXP) || []
function getDefaultExtractor(fileExtension) {
return function (content) {
if (fileExtension === 'svelte') {
content = content.replace(/\sclass:/g, ' ')
}
let broadMatches = content.match(BROAD_MATCH_GLOBAL_REGEXP) || []
let innerMatches = content.match(INNER_MATCH_GLOBAL_REGEXP) || []

return [...broadMatches, ...innerMatches]
return [...broadMatches, ...innerMatches]
}
}

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

if (!purgeOptions) {
return defaultJitExtractor
return getDefaultExtractor(fileExtension)
}

const fileExtension = path.extname(fileName).slice(1)
const fileSpecificExtractor = (purgeOptions.extractors || []).find((extractor) =>
extractor.extensions.includes(fileExtension)
)
Expand All @@ -35,7 +40,7 @@ function getExtractor(fileName, tailwindConfig) {
return fileSpecificExtractor.extractor
}

return purgeOptions.defaultExtractor || defaultJitExtractor
return purgeOptions.defaultExtractor || getDefaultExtractor(fileExtension)
}

// Scans template contents for possible classes. This is a hot path on initial build but
Expand Down
15 changes: 15 additions & 0 deletions tests/svelte-syntax.test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
* {
--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;
}
@media (min-width: 1024px) {
.lg\:hover\:bg-blue-500:hover {
--tw-bg-opacity: 1;
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
}
}
30 changes: 30 additions & 0 deletions tests/svelte-syntax.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const postcss = require('postcss')
const tailwind = require('../src/index.js')
const fs = require('fs')
const path = require('path')

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

test('basic usage', () => {
let config = {
purge: [path.resolve(__dirname, './svelte-syntax.test.svelte')],
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

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

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

expect(result.css).toMatchCss(expected)
})
})
5 changes: 5 additions & 0 deletions tests/svelte-syntax.test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let current = 'foo'
</script>

<button class:lg:hover:bg-blue-500={current === 'foo'}>Click me</button>