-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can you provide a working example of postcss-lit with tailwindcss? #56
Comments
After failing to get postcss-lit working with rollup to transform css within static styles = css`` (which vite uses in case that's relevant), I have also come to the conclusion in this comment: #23 (comment) As a side note, I think there might be a fundamental problem with using Tailwind CSS with web components or Lit. In a typical tailwind.config.js file, you manage css purging with content: ['./src/**/*...
What you'll find when you look in the bundled file for a component, is that its css contains px-1, px-2, px-3... even though the component is only using one of those classes in its html.
If we just bundle all components into one js bundle and share the Tailwind css to prevent deploying duplicate Tailwind css, then we aren't taking advantage of the scalable nature of lazy loading many small independent web components.
After thinking about things, if it's hard to use PostCSS syntax inside of a Lit file, it might also be hard to purge Tailwind css for similar reasons. It seems like just using PostCSS (not Tailwind, so no purging) in css files imported into Lit components makes the most sense for now. |
I got inspired by: ChatGPT helped me come up with the following simple concept. vite.config.ts: import postcss from 'postcss'
import tailwindcss from 'tailwindcss'
import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'
function viteLitTailwind() {
return {
name: 'vite-lit-tailwind',
async transform(code, id) {
if (!id.endsWith('.ts')) return null
const match = code.match(/css`([\s\S]*?@tailwind\s+\w+;[\s\S]*?)`/)
if (match) {
try {
const result = await postcss([
tailwindcss({
content: [id]
})
]).process(match[1], { from: undefined, to: undefined })
if (result.css) {
return code.replace(match[0], `css\`${result.css.replace(/`/g, '\\`')}\``)
}
} catch (error) {
this.error('Error processing with Tailwind CSS: ', error)
}
}
return null
}
}
}
export default defineConfig({
plugins: [tsconfigPaths(), viteLitTailwind()]
}) lit-web-component.ts: render() {
return html`
<p class="px-2">Hello World</p>
`
}
static styles = css`
@tailwind utilities;
` It runs tailwindcss for each Lit component that contains a Tailwind directive (ex. Here's how I could imagine using this:
|
in the projects i've used tailwind in, we basically split the styles out in all non-trivial cases i.e. we have this: // my-element.ts
class MyElement extends LitElement {
static styles = [commonStyles];
}
// my-other-element.ts
class MyOtherElement extends LitElement {
static styles = [
commonStyles,
css`some local styles that don't use tailwind`
];
}
// common-styles.ts
export const commonStyles = css`
/* some tailwind mixins */
`; the point being to share stylesheets across elements often. although tbh we have mostly moved into css files now |
Regarding just Tailwind though, you either have the Tailwind directives (ex. |
Ive been trying to get tailwindcss working with lit and postcss-lit seems to offer a way to be able to do this but the examples on the internet fail for me.
If you could show an example of how to get this setup Id appreciate it.
The text was updated successfully, but these errors were encountered: