Skip to content

Commit

Permalink
feat: handle js css import hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 3, 2020
1 parent a3bb973 commit 538198c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ socket.addEventListener('message', ({ data }) => {
`[vite] ${path} style${index > 0 ? `#${index}` : ``} updated.`
)
break
case 'vue-style-remove':
case 'style-update':
updateStyle(id, `${path}?raw&t=${timestamp}`)
console.log(`[vite] ${path} updated.`)
break
case 'style-remove':
const link = document.getElementById(`vite-css-${id}`)
if (link) {
document.head.removeChild(link)
Expand Down Expand Up @@ -65,9 +69,12 @@ socket.addEventListener('message', ({ data }) => {
socket.addEventListener('close', () => {
console.log(`[vite] server connection lost. polling for restart...`)
setInterval(() => {
new WebSocket(`${socketProtocol}://${location.host}`).addEventListener('open', () => {
location.reload()
})
new WebSocket(`${socketProtocol}://${location.host}`).addEventListener(
'open',
() => {
location.reload()
}
)
}, 1000)
})

Expand Down
16 changes: 15 additions & 1 deletion src/node/serverPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isImportRequest } from './utils'
import { hmrClientId } from './serverPluginHmr'
import hash_sum from 'hash-sum'

export const cssPlugin: Plugin = ({ app }) => {
export const cssPlugin: Plugin = ({ app, watcher, resolver }) => {
app.use(async (ctx, next) => {
await next()
// handle .css imports
Expand All @@ -26,4 +26,18 @@ updateStyle(${id}, ${rawPath})
`.trim()
}
})

// handle hmr
watcher.on('change', (file) => {
if (file.endsWith('.css')) {
const publicPath = resolver.fileToRequest(file)
const id = hash_sum(publicPath)
watcher.send({
type: 'style-update',
id,
path: publicPath,
timestamp: Date.now()
})
}
})
}
14 changes: 11 additions & 3 deletions src/node/serverPluginHmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ export const hmrClientId = '@hmr'
export const hmrClientPublicPath = `/${hmrClientId}`

interface HMRPayload {
type: string
type:
| 'vue-rerender'
| 'vue-reload'
| 'vue-style-update'
| 'js-update'
| 'style-update'
| 'style-remove'
| 'full-reload'
| 'custom'
timestamp: number
path?: string
id?: string
Expand Down Expand Up @@ -178,7 +186,7 @@ export const hmrPlugin: Plugin = ({ root, app, server, watcher, resolver }) => {
nextStyles.forEach((_, i) => {
if (!prevStyles[i] || !isEqual(prevStyles[i], nextStyles[i])) {
send({
type: 'vue-style-update',
type: 'style-update',
path: publicPath,
index: i,
id: `${styleId}-${i}`,
Expand All @@ -191,7 +199,7 @@ export const hmrPlugin: Plugin = ({ root, app, server, watcher, resolver }) => {
// stale styles always need to be removed
prevStyles.slice(nextStyles.length).forEach((_, i) => {
send({
type: 'vue-style-remove',
type: 'style-remove',
path: publicPath,
id: `${styleId}-${i + nextStyles.length}`,
timestamp
Expand Down

0 comments on commit 538198c

Please sign in to comment.