Skip to content
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

fix(hmr): fix tailwindcss hmr does not work (vite) #755

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

molvqingtai
Copy link

@molvqingtai molvqingtai commented Jul 18, 2023

FIxed: Fix the problem that tailwindcss jit did not trigger the hmr update
Todo: Fix the problem that the index.css?inline syntax does not work in hmr

Fix: #600 #671 #609 #512

Tests to be added...

import index.css?inline still cannot implement HMR in contentScripts, because it is static data, but manually refreshing the page can already work, Do you have any good ideas?

At present(After merged this PR), there is only one compromise way to fully realize HMR:

createShadowRoot.ts

import { type ReactNode } from 'react'
import { createRoot, type Root } from 'react-dom/client'

export const createElement = <T extends Element>(template: string) => {
  return new Range().createContextualFragment(template).firstElementChild as unknown as T
}

export interface RootOptions {
  mode?: ShadowRootMode
  style?: string
  script?: string
  element?: Element
}

const createShadowRoot = (
  name: string,
  options: RootOptions
): Root & { shadowHost: Element; shadowRoot: ShadowRoot; appRoot: Element } => {
  const { mode = 'open', style = '', script = '', element = '' } = options ?? {}
  const shadowHost = createElement(`<${name}></${name}>`)
  const shadowRoot = shadowHost.attachShadow({ mode })
  const appRoot = createElement(`<div id="app"></div>`)
  const appStyle = style && createElement(`<style type="text/css">${style}</style>`)
  const appScript = script && createElement(`<script type="application/javascript">${script}</script>`)
  const reactRoot = createRoot(appRoot)

  shadowRoot.append(appStyle, appRoot, appScript, element)

  return {
    shadowHost,
    shadowRoot,
    appRoot,
    render: (children: ReactNode) => {
      document.body.appendChild(shadowHost)
      return reactRoot.render(children)
    },
    unmount: () => {
      reactRoot.unmount()
      shadowHost.remove()
    }
  }
}

export default createShadowRoot

content.tsx

import React from 'react'
import App from './App'
import createShadowRoot from './createShadowRoot'
import style from './index.css?inline'

void (async () => {
  const root = createShadowRoot(__NAME__, {
    style: __DEV__ ? '' : style,
    mode: __DEV__ ? 'open' : 'closed'
  })
  root.render(
    <React.StrictMode>
      <RemeshRoot store={store}>
        <App />
      </RemeshRoot>
    </React.StrictMode>
  )

  // HMR Hack
  // https://github.com/crxjs/chrome-extension-tools/issues/600
  if (__DEV__) {
    await import('./index.css')
    const styleElement = document.querySelector('[data-vite-dev-id]')!
    root.shadowRoot.insertBefore(styleElement, root.shadowRoot.firstChild)
  }
})()

If it is a development environment, add the out style in head to shadow dom
If it is a production environment, write css string to shadow dom

@changeset-bot
Copy link

changeset-bot bot commented Jul 18, 2023

🦋 Changeset detected

Latest commit: 221dcbc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@crxjs/vite-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jul 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
vite-plugin-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 14, 2024 7:23pm

@molvqingtai molvqingtai changed the title fix(hmr): fix tailwindcss hmr does not work (vite-plugin) fix(hmr): fix tailwindcss hmr does not work (vite) Jul 19, 2023
@molvqingtai molvqingtai changed the title fix(hmr): fix tailwindcss hmr does not work (vite) [WIP] fix(hmr): fix tailwindcss hmr does not work (vite) Jul 19, 2023
@flexchar
Copy link

It'd be so amazing to have this work!

@gary-lo
Copy link

gary-lo commented Aug 18, 2023

+1

@Joabesv
Copy link

Joabesv commented Oct 18, 2023

any updates here? this has been my blocker with vue :/

@molvqingtai molvqingtai changed the title [WIP] fix(hmr): fix tailwindcss hmr does not work (vite) fix(hmr): fix tailwindcss hmr does not work (vite) Oct 18, 2023
@NathanMLu
Copy link

Is it possible to have this merged? It is a blocker for my project as well.

Copy link
Contributor

@jacksteamdev jacksteamdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks amazing. I'd like to merge this after a few tweaks:

Tasks before merging

  • Add e2e test
  • Fix tests

@jacksteamdev jacksteamdev added awaiting-feedback Further information is requested vite-plugin Related to Vite plugin labels Jan 14, 2024
@sgcullen
Copy link

This would be good! :-)

@mrskiro
Copy link

mrskiro commented Oct 7, 2024

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-feedback Further information is requested vite-plugin Related to Vite plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HMR doesn't work with .css?inline (inline css imports)
8 participants