Skip to content

Commit

Permalink
fix(vite): update transformed module when config changed (#1163)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
TrickyPi and antfu committed Jun 29, 2022
1 parent e9334c9 commit 945f90a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/types.ts
Expand Up @@ -500,6 +500,9 @@ export interface UnocssPluginContext<Config extends UserConfig = UserConfig> {
tokens: Set<string>
/** Map for all module's raw content */
modules: BetterMap<string, string>
/** Module IDs that been affected by UnoCSS */
affectedModules: Set<string>

filter: (code: string, id: string) => boolean
extract: (code: string, id?: string) => Promise<void>

Expand Down
2 changes: 2 additions & 0 deletions packages/shared-integration/src/context.ts
Expand Up @@ -22,6 +22,7 @@ export function createContext<Config extends UserConfig<any> = UserConfig<any>>(

const modules = new BetterMap<string, string>()
const tokens = new Set<string>()
const affectedModules = new Set<string>()

let ready = reloadConfig()

Expand Down Expand Up @@ -97,6 +98,7 @@ export function createContext<Config extends UserConfig<any> = UserConfig<any>>(
},
tokens,
modules,
affectedModules,
invalidate,
onInvalidate(fn: () => void) {
invalidations.push(fn)
Expand Down
36 changes: 23 additions & 13 deletions packages/vite/src/modes/global/dev.ts
@@ -1,11 +1,12 @@
import type { Plugin, ViteDevServer, ResolvedConfig as ViteResolvedConfig } from 'vite'
import type { Plugin, Update, ViteDevServer, ResolvedConfig as ViteResolvedConfig } from 'vite'
import type { UnocssPluginContext } from '@unocss/core'
import { notNull } from '@unocss/core'
import { LAYER_MARK_ALL, getPath, resolveId, resolveLayer } from '../../integration'

const WARN_TIMEOUT = 20000
const WS_EVENT_PREFIX = 'unocss:hmr'

export function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[] {
export function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[] {
const servers: ViteDevServer[] = []
let base = ''

Expand All @@ -26,30 +27,37 @@ export function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter
base = base.slice(0, base.length - 1)
}

function invalidate(timer = 10) {
function invalidate(timer = 10, ids: Set<string> = entries) {
for (const server of servers) {
for (const id of entries) {
for (const id of ids) {
const mod = server.moduleGraph.getModuleById(id)
if (!mod)
continue
server!.moduleGraph.invalidateModule(mod)
}
}
clearTimeout(invalidateTimer)
invalidateTimer = setTimeout(sendUpdate, timer)
invalidateTimer = setTimeout(() => sendUpdate(ids), timer)
}

function sendUpdate() {
function sendUpdate(ids: Set<string>) {
lastUpdate = Date.now()
for (const server of servers) {
server.ws.send({
type: 'update',
updates: Array.from(entries).map(i => ({
acceptedPath: i,
path: i,
timestamp: lastUpdate,
type: 'js-update',
})),
updates: Array.from(ids)
.map((id) => {
const mod = server.moduleGraph.getModuleById(id)
if (!mod)
return null
return <Update>{
acceptedPath: mod.url,
path: mod.url,
timestamp: lastUpdate,
type: 'js-update',
}
})
.filter(notNull),
})
}
}
Expand All @@ -71,7 +79,9 @@ export function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter
}
}

onInvalidate(invalidate)
onInvalidate(() => {
invalidate(0, new Set([...entries, ...affectedModules]))
})

return [
{
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/transformers.ts
Expand Up @@ -24,6 +24,7 @@ export function initTransformerPlugins(ctx: UnocssPluginContext): Plugin[] {
}

if (s.hasChanged()) {
ctx.affectedModules.add(id)
return {
code: s.toString(),
map: s.generateMap({ hires: true, source: id }),
Expand Down

0 comments on commit 945f90a

Please sign in to comment.