This repository was archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
feat: allow custom extractors #125
Merged
adamwathan
merged 1 commit into
tailwindlabs:main
from
kombucha:allow-custom-extractors
Mar 23, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
* { | ||
--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; | ||
} | ||
.bg-white { | ||
--tw-bg-opacity: 1; | ||
background-color: rgba(255, 255, 255, var(--tw-bg-opacity)); | ||
} | ||
.text-indigo-500 { | ||
--tw-text-opacity: 1; | ||
color: rgba(99, 102, 241, var(--tw-text-opacity)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" href="/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Title</title> | ||
<link rel="stylesheet" href="./tailwind.css" /> | ||
</head> | ||
<body> | ||
<div class="text-indigo-500 bg-white">hello world</div> | ||
<span>text-red-500 shouldn't appear in the output</span> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const postcss = require('postcss') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
function run(input, config = {}) { | ||
jest.resetModules() | ||
const tailwind = require('../src/index.js') | ||
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) }) | ||
} | ||
|
||
function customExtractor(content) { | ||
const matches = content.match(/class="([^"]+)"/) | ||
return matches ? matches[1].split(/\s+/) : [] | ||
} | ||
|
||
const css = ` | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
` | ||
const expectedPath = path.resolve(__dirname, './11-custom-extractors.test.css') | ||
const expected = fs.readFileSync(expectedPath, 'utf8') | ||
|
||
test('defaultExtractor', () => { | ||
let config = { | ||
purge: { | ||
content: [path.resolve(__dirname, './11-custom-extractors.test.html')], | ||
options: { | ||
defaultExtractor: customExtractor, | ||
}, | ||
}, | ||
corePlugins: { preflight: false }, | ||
theme: {}, | ||
plugins: [], | ||
} | ||
|
||
return run(css, config).then((result) => { | ||
expect(result.css).toMatchCss(expected) | ||
}) | ||
}) | ||
|
||
test('extractors array', () => { | ||
let config = { | ||
purge: { | ||
content: [path.resolve(__dirname, './11-custom-extractors.test.html')], | ||
options: { | ||
extractors: [ | ||
{ | ||
extractor: customExtractor, | ||
extensions: ['html'], | ||
}, | ||
], | ||
}, | ||
}, | ||
corePlugins: { preflight: false }, | ||
theme: {}, | ||
plugins: [], | ||
} | ||
|
||
return run(css, config).then((result) => { | ||
expect(result.css).toMatchCss(expected) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've noticed that the second test in that file would pass even when it shouldn't, as long as the first one passed. I think it's due to this module sharedState which survives across multiple call in the same process.
That's why I'm calling
resetModules
, which resetsrequire
's cache and gives me a fresh new instance on each run.