Tailwind css not working in Vue web component
Asked Answered
T

3

7

I have a Vue web component. When I build it as a normal Vue component everything worked fine. However, it lost all the Tailwind styling immediately I converted it to a Vue Web Component. Thanks for your help in advance.

tailwind.config.js

module.exports = {
  content: [
    "./index.html",
    "./src/**/*.{vue,js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {
      colors: {
      },
    },
  },
  plugins: [
  ],
}

tailwind.css

@tailwind base;
@tailwind components;
@tailwind utilities;

and my vite.config.js

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue({
    template: {
      compilerOptions: {
        // treat all tags with a dash as custom elements
        isCustomElement: (tag) => tag.includes('-')
      }
    }
  })],
  build: {
    lib: {
      entry: './src/entry.js',
      formats: ['es','cjs'],
      name: 'web-component',
      fileName: (format)=>(format === 'es' ? 'index.js' : 'index.cjs')
    },
    sourcemap: true,
    target: 'esnext',
    minify: false
  }
})
Tripe answered 9/4, 2022 at 18:26 Comment(0)
D
5

Change your tailwind.config.cjs like as below

before the change tailwind.config.cjs

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

after changing tailwind.config.cjs

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    './public/**/*.html',
    './src/**/*.{js,jsx,ts,tsx,vue}',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
Donough answered 27/3, 2023 at 5:43 Comment(1)
Thanks, adding the vue extension to the list of file extensions solved my issue. Cheers!Pelotas
T
1

Anyway, for now, what I have done is to add Tailwind directly to the web component and it works.

<style>
 @import url("../tailwind.css");
</style>
Tripe answered 11/4, 2022 at 6:52 Comment(1)
this does not seem a good way. but this solves my problem too.Westernize
A
0

In src/main.js change import "./assets/main.css"; to your tailwind css file `

Aaberg answered 16/1, 2023 at 9:14 Comment(1)
Don't know why this was downvoted, this was the issue for me, for some reason the 'vue add tailwind' command did not add it automaticallyLeatri

© 2022 - 2024 — McMap. All rights reserved.