-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9467e3f
commit 129d19b
Showing
7 changed files
with
92 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## Styling Custom Components with Tailwind | ||
|
||
``` | ||
<StyledComponent /> | ||
``` | ||
|
||
<StyledComponent /> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,16 +1,81 @@ | ||
import tailwind from '@tailwindcss/postcss'; | ||
import postcss from 'postcss'; | ||
import prefixer from 'postcss-prefix-selector'; | ||
|
||
const tailwindBundle = (html: string, config = {}, tailwindConfig = {}) => { | ||
const includes = ['@tailwind components;', '@tailwind utilities;']; | ||
import * as tailwindcss from 'tailwindcss'; | ||
|
||
if ('includeBase' in config && config.includeBase) { | ||
includes.unshift('@tailwind base;'); | ||
// @ts-ignore | ||
import index from 'tailwindcss/index.css'; | ||
// @ts-ignore | ||
import preflight from 'tailwindcss/preflight.css'; | ||
// @ts-ignore | ||
import theme from 'tailwindcss/theme.css'; | ||
// @ts-ignore | ||
import utilities from 'tailwindcss/utilities.css'; | ||
|
||
/* | ||
* @note: This is mostly copied from @tailwindcss/browser | ||
*/ | ||
async function createCompiler() { | ||
const css = ` | ||
@layer theme, base, components, utilities; | ||
@import "tailwindcss/theme.css" layer(theme); | ||
@import "tailwindcss/utilities.css" layer(utilities); | ||
`; | ||
|
||
return await tailwindcss.compile(css, { | ||
base: '/', | ||
loadStylesheet, | ||
loadModule, | ||
}); | ||
} | ||
|
||
async function loadStylesheet(id: string, base: string) { | ||
function load() { | ||
if (id === 'tailwindcss') { | ||
return { | ||
base, | ||
content: index, | ||
}; | ||
} else if (id === 'tailwindcss/preflight' || id === 'tailwindcss/preflight.css' || id === './preflight.css') { | ||
return { | ||
base, | ||
content: preflight, | ||
}; | ||
} else if (id === 'tailwindcss/theme' || id === 'tailwindcss/theme.css' || id === './theme.css') { | ||
return { | ||
base, | ||
content: theme, | ||
}; | ||
} else if (id === 'tailwindcss/utilities' || id === 'tailwindcss/utilities.css' || id === './utilities.css') { | ||
return { | ||
base, | ||
content: utilities, | ||
}; | ||
} | ||
|
||
throw new Error(`The browser build does not support @import for "${id}"`); | ||
} | ||
|
||
const plugins = [tailwind({ ...tailwindConfig, content: [{ raw: html, extension: 'html' }] })]; | ||
try { | ||
let sheet = load(); | ||
|
||
return sheet; | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
|
||
async function loadModule(): Promise<never> { | ||
throw new Error(`The browser build does not support plugins or config files.`); | ||
} | ||
|
||
async function tailwindBundle(string: string, { prefix }: { prefix: string }) { | ||
const compiler = await createCompiler(); | ||
const newClasses = new Set<string>(string.split(/[^a-zA-Z0-9_-]+/)); | ||
const css = compiler.build(Array.from(newClasses)); | ||
|
||
return postcss(plugins).process(includes.join('\n'), { from: undefined }); | ||
}; | ||
return await postcss([prefixer({ prefix })]).process(css, { from: undefined }); | ||
} | ||
|
||
export default tailwindBundle; |
This file contains 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 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