Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 29, 2022
1 parent 1b2bd80 commit 2bce6d5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 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
57 changes: 22 additions & 35 deletions packages/vite/src/modes/global/dev.ts
@@ -1,32 +1,24 @@
import type { Plugin, Update, ViteDevServer, ResolvedConfig as ViteResolvedConfig } from 'vite'
import type { UnocssPluginContext } from '@unocss/core'
import { cssIdRE } from '@unocss/core'
import { LAYER_MARK_ALL, RESOLVED_ID_RE, getPath, resolveId, resolveLayer } from '../../integration'
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 = ''

const tasks: Promise<any>[] = []
const entries = new Set<string>()
const cssModules = new Set<string>()

let invalidateTimer: any
let lastUpdate = Date.now()
let lastServed = 0
let resolved = false
let resolvedWarnTimer: any

function getCssLikeFiles() {
return [
...entries.keys(),
...cssModules.keys(),
]
}

function configResolved(config: ViteResolvedConfig) {
base = config.base || ''
if (base === '/')
Expand All @@ -35,8 +27,7 @@ export function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter
base = base.slice(0, base.length - 1)
}

function invalidate(timer = 10) {
const ids = getCssLikeFiles()
function invalidate(timer = 10, ids: Set<string> = entries) {
for (const server of servers) {
for (const id of ids) {
const mod = server.moduleGraph.getModuleById(id)
Expand All @@ -46,30 +37,27 @@ export function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter
}
}
clearTimeout(invalidateTimer)
invalidateTimer = setTimeout(sendUpdate, timer)
invalidateTimer = setTimeout(() => sendUpdate(ids), timer)
}

function sendUpdate() {
function sendUpdate(ids: Set<string>) {
lastUpdate = Date.now()
const ids = getCssLikeFiles()
for (const server of servers) {
server.ws.send({
type: 'update',
updates: Array.from(ids).reduce((prev: Update[], id) => {
const mod = server.moduleGraph.getModuleById(id)
if (mod) {
const { url: assetPath } = mod
prev.push(
{
acceptedPath: assetPath,
path: assetPath,
timestamp: lastUpdate,
type: 'js-update',
},
)
}
return prev
}, []),
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 @@ -91,7 +79,9 @@ export function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter
}
}

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

return [
{
Expand Down Expand Up @@ -131,9 +121,6 @@ export function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter
}
},
async load(id) {
if (!RESOLVED_ID_RE.test(id) && cssIdRE.test(id))
cssModules.add(id)

const layer = resolveLayer(getPath(id))
if (!layer)
return null
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 2bce6d5

Please sign in to comment.