vite/playground/hmr/hmr.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
4.2 KiB
TypeScript
Raw Normal View History

import { virtual } from 'virtual:file'
2020-12-23 04:07:57 +00:00
import { foo as depFoo, nestedFoo } from './hmrDep'
import './importing-updated'
import './file-delete-restore'
import './optional-chaining/parent'
import './intermediate-file-delete'
import './circular'
import logo from './logo.svg'
import logoNoInline from './logo-no-inline.svg'
import { msg as softInvalidationMsg } from './soft-invalidation'
2020-12-11 01:39:52 +00:00
2020-12-23 04:07:57 +00:00
export const foo = 1
text('.app', foo)
text('.dep', depFoo)
text('.nested', nestedFoo)
text('.virtual', virtual)
text('.soft-invalidation', softInvalidationMsg)
setImgSrc('#logo', logo)
setImgSrc('#logo-no-inline', logoNoInline)
2020-12-11 01:39:52 +00:00
const btn = document.querySelector('.virtual-update') as HTMLButtonElement
btn.onclick = () => {
if (import.meta.hot) {
import.meta.hot.send('virtual:increment')
}
}
2020-12-11 01:39:52 +00:00
if (import.meta.hot) {
import.meta.hot.accept(({ foo }) => {
2020-12-23 04:07:57 +00:00
console.log('(self-accepting 1) foo is now:', foo)
2020-12-11 01:39:52 +00:00
})
import.meta.hot.accept(({ foo }) => {
2020-12-23 04:07:57 +00:00
console.log('(self-accepting 2) foo is now:', foo)
2020-12-11 01:39:52 +00:00
})
2020-12-23 04:07:57 +00:00
const handleDep = (type, newFoo, newNestedFoo) => {
console.log(`(${type}) foo is now: ${newFoo}`)
console.log(`(${type}) nested foo is now: ${newNestedFoo}`)
text('.dep', newFoo)
text('.nested', newNestedFoo)
}
import.meta.hot.accept('./logo.svg', (newUrl) => {
setImgSrc('#logo', newUrl.default)
console.log('Logo updated', newUrl.default)
})
import.meta.hot.accept('./logo-no-inline.svg', (newUrl) => {
setImgSrc('#logo-no-inline', newUrl.default)
console.log('Logo-no-inline updated', newUrl.default)
})
2020-12-23 04:07:57 +00:00
import.meta.hot.accept('./hmrDep', ({ foo, nestedFoo }) => {
handleDep('single dep', foo, nestedFoo)
2020-12-11 01:39:52 +00:00
})
2020-12-23 04:07:57 +00:00
import.meta.hot.accept(['./hmrDep'], ([{ foo, nestedFoo }]) => {
handleDep('multi deps', foo, nestedFoo)
2020-12-11 01:39:52 +00:00
})
import.meta.hot.dispose(() => {
2020-12-11 06:20:58 +00:00
console.log(`foo was:`, foo)
2020-12-11 01:39:52 +00:00
})
import.meta.hot.on('vite:afterUpdate', (event) => {
console.log(`>>> vite:afterUpdate -- ${event.type}`)
})
import.meta.hot.on('vite:beforeUpdate', (event) => {
console.log(`>>> vite:beforeUpdate -- ${event.type}`)
const cssUpdate = event.updates.find(
(update) =>
update.type === 'css-update' && update.path.includes('global.css'),
)
if (cssUpdate) {
text(
'.css-prev',
(document.querySelector('.global-css') as HTMLLinkElement).href,
)
// Wait until the tag has been swapped out, which includes the time taken
// to download and parse the new stylesheet. Assert the swapped link.
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (
node.nodeType === Node.ELEMENT_NODE &&
(node as Element).tagName === 'LINK'
) {
text('.link-tag-added', 'yes')
}
})
mutation.removedNodes.forEach((node) => {
if (
node.nodeType === Node.ELEMENT_NODE &&
(node as Element).tagName === 'LINK'
) {
text('.link-tag-removed', 'yes')
text(
'.css-post',
(document.querySelector('.global-css') as HTMLLinkElement).href,
)
}
})
})
})
observer.observe(document.querySelector('#style-tags-wrapper'), {
childList: true,
})
}
})
import.meta.hot.on('vite:error', (event) => {
console.log(`>>> vite:error -- ${event.err.message}`)
})
import.meta.hot.on('vite:invalidate', ({ path }) => {
console.log(`>>> vite:invalidate -- ${path}`)
})
import.meta.hot.on('custom:foo', ({ msg }) => {
text('.custom', msg)
})
import.meta.hot.on('custom:remove', removeCb)
// send custom event to server to calculate 1 + 2
import.meta.hot.send('custom:remote-add', { a: 1, b: 2 })
import.meta.hot.on('custom:remote-add-result', ({ result }) => {
text('.custom-communication', result)
})
2020-12-11 01:39:52 +00:00
}
2020-12-23 04:07:57 +00:00
function text(el, text) {
document.querySelector(el).textContent = text
}
function setImgSrc(el, src) {
;(document.querySelector(el) as HTMLImageElement).src = src
}
function removeCb({ msg }) {
text('.toRemove', msg)
import.meta.hot.off('custom:remove', removeCb)
}