vue-web-component-wrapper icon indicating copy to clipboard operation
vue-web-component-wrapper copied to clipboard

Tailwind CSS styles are not updating with HMR

Open chiragparekh opened this issue 1 year ago • 4 comments

Hi I am using this library with shadow root and HMR is working but tailwind styles are not updating.

This is the main.js file

import { defineCustomElement as VueDefineCustomElement, h, createApp, getCurrentInstance } from "vue"
import { createWebComponent } from "vue-web-component-wrapper"
import styles from "./assets/styles/index.scss?inline"
import App from "./App.vue"

createWebComponent({
  rootComponent: App,
  elementName: "element",
  plugins: {
    install(GivenVue) {
      const Vue = GivenVue
    },
  },
  cssFrameworkStyles: styles,
  VueDefineCustomElement,
  h,
  createApp,
  getCurrentInstance,
  disableStyleRemoval: false,
})

This is the index.scss file

@import "./tailwind.css";
@import "./custom.scss";

This is the vite config file

import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import { viteVueCE } from "unplugin-vue-ce"
import { PluginOption } from "vite"
import { resolve } from "path"
import tailwindcss from "tailwindcss"

export default defineConfig(({ mode }) => {
  const plugins = [
    vue({
      customElement: true,
    }),
    viteVueCE({
      isESCSS: true,
    }) as PluginOption,
  ]

  return {
    plugins,
    css: {
      postcss: {
        plugins: [tailwindcss()],
      },
    },
    resolve: {
      alias: [
        {
          find: "@",
          replacement: resolve(__dirname, "./src"),
        },
      ],
    },
    define: {
      global: {},
    },
  }
})

chiragparekh avatar Sep 16 '24 09:09 chiragparekh

I’m experiencing a similar issue where Tailwind CSS classes do not update with HMR unless I reload the site. However, if I add a class that has already been used elsewhere in the application, it updates correctly with HMR.

Interestingly, this issue resolves when I disable the Shadow DOM by setting disableShadowDOM: true. However, this workaround defeats the purpose of using the Shadow DOM in my project.

MohamedKamalOthman avatar Sep 17 '24 16:09 MohamedKamalOthman

@MohamedKamalOthman I have tried with disableShadowDOM: true but it didn't work for me. My idea was to conditionally toggle value for that attribute like if its in development mode then disable Shadow Dom but unfortunately it didn't work for me.

chiragparekh avatar Sep 17 '24 18:09 chiragparekh

The HMR cannot update the module directly with the shadow DOM; therefore, I would suggest using a workaround solution add this custom plugin to the plugins array of vite config file

plugins: [
    // ... other plugins ...
    {
      name: 'force-reload',
      handleHotUpdate({ file, server }) {
        // Force full page reload for all .vue file changes
        if (file.endsWith('.vue')) {
          server.ws.send({ type: 'full-reload' })
          return []
        }
      },
    },
  ],

EranGrin avatar Sep 17 '24 21:09 EranGrin

@EranGrin Thanks for now I have used the code that you have provided for full page reload.

chiragparekh avatar Sep 18 '24 10:09 chiragparekh

I added this workaround to the docs and closing this PR

EranGrin avatar Dec 16 '24 22:12 EranGrin