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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-cooks-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crxjs/vite-plugin": patch
---

[WIP] fix(hmr): fix `tailwindcss` hmr does not work (vite)
28 changes: 28 additions & 0 deletions packages/vite-plugin/src/node/findCSSImportDeps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ModuleNode } from 'vite'

export const CSS_LANGS_RE =
/\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/

export const isCSSRequest = (request: string): boolean =>
CSS_LANGS_RE.test(request)

/** Find all CSS importers of a given node. */
const findCSSImportDeps = (
node: ModuleNode,
selfAccepting = true,
): Set<ModuleNode> => {
const addDeps = (importers: Set<ModuleNode>, deps = new Set<ModuleNode>()) =>
[...importers].reduce((deps, node): Set<ModuleNode> => {
if (deps.has(node)) return deps
if (selfAccepting && !node.isSelfAccepting) return deps
if (isCSSRequest(node.url)) {
return addDeps(node.importers, new Set([...deps, node]))
} else {
return deps
}
}, deps)

return addDeps(node.importers)
}

export default findCSSImportDeps
7 changes: 1 addition & 6 deletions packages/vite-plugin/src/node/isImporter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { ModuleNode } from 'vite'

export type FullModuleNode = ModuleNode & {
file: string
importers: Set<FullModuleNode>
}

/** Determine if a changed file was imported by a file */
export function isImporter(file: string) {
const seen = new Set<ModuleNode>()
const pred = (changedNode: ModuleNode): changedNode is FullModuleNode => {
const pred = (changedNode: ModuleNode) => {
seen.add(changedNode)

if (changedNode.file === file) return true
Expand Down
40 changes: 28 additions & 12 deletions packages/vite-plugin/src/node/plugin-hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { _debug } from './helpers'
import { isImporter } from './isImporter'
import { isAbsolute, join } from './path'
import type { CrxHMRPayload, CrxPluginFn, ManifestFiles } from './types'
import findCSSImportDeps from './findCSSImportDeps'

const debug = _debug('hmr')

Expand Down Expand Up @@ -79,25 +80,36 @@ export const pluginHMR: CrxPluginFn = () => {
)
}
},

closeBundle() {
subs.unsubscribe()
},

// background changes require a full extension reload
handleHotUpdate({ modules, server }) {
const { root } = server.config
const updateFiles = modules
.filter((module) => module.isSelfAccepting)
.map((module) => prefix('/', module.url))

const relFiles = new Set<string>()
for (const m of modules)
if (m.id?.startsWith(root)) {
relFiles.add(m.id.slice(server.config.root.length))
}
/**
* Additionally check for CSS importers, since a PostCSS plugin like
* Tailwind JIT may register any file as a dependency to a CSS file.
* https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/hmr.ts#L266
*/
const additionalCSSFiles = modules
.flatMap((module) => [...findCSSImportDeps(module)])
.map((module) => module.url)

// check if changed file is a background dependency
if (inputManifestFiles.background.length) {
const background = prefix('/', inputManifestFiles.background[0])
// check if changed file is a background dependency
const isDependency = modules.some(
isImporter(join(server.config.root, background)),
)
if (
relFiles.has(background) ||
modules.some(isImporter(join(server.config.root, background)))
updateFiles.includes(background) ||
additionalCSSFiles.length ||
isDependency
) {
debug('sending runtime reload')
server.ws.send(crxRuntimeReload)
Expand All @@ -107,11 +119,15 @@ export const pluginHMR: CrxPluginFn = () => {
for (const [key, script] of contentScripts)
if (key === script.id) {
// check if changed file is a content script dependency
const isDependency = modules.some(
isImporter(join(server.config.root, script.id)),
)
if (
relFiles.has(script.id) ||
modules.some(isImporter(join(server.config.root, script.id)))
updateFiles.includes(script.id) ||
additionalCSSFiles.length ||
isDependency
) {
relFiles.forEach((relFile) => update(relFile))
new Set([...updateFiles, ...additionalCSSFiles]).forEach(update)
}
}
},
Expand Down
Loading
Loading