2022-09-19 12:34:50 +00:00
|
|
|
import { virtual } from 'virtual:file'
|
2020-12-23 04:07:57 +00:00
|
|
|
import { foo as depFoo, nestedFoo } from './hmrDep'
|
2022-09-01 06:33:06 +00:00
|
|
|
import './importing-updated'
|
2022-12-04 20:07:25 +00:00
|
|
|
import './file-delete-restore'
|
2023-02-15 18:39:16 +00:00
|
|
|
import './optional-chaining/parent'
|
2023-10-19 09:49:40 +00:00
|
|
|
import './intermediate-file-delete'
|
2023-11-08 08:01:37 +00:00
|
|
|
import './circular'
|
2023-10-17 16:39:08 +00:00
|
|
|
import logo from './logo.svg'
|
2024-11-06 09:28:44 +00:00
|
|
|
import logoNoInline from './logo-no-inline.svg'
|
2023-10-25 14:58:01 +00:00
|
|
|
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)
|
2022-09-19 12:34:50 +00:00
|
|
|
text('.virtual', virtual)
|
2023-10-25 14:58:01 +00:00
|
|
|
text('.soft-invalidation', softInvalidationMsg)
|
2024-11-06 09:28:44 +00:00
|
|
|
setImgSrc('#logo', logo)
|
|
|
|
setImgSrc('#logo-no-inline', logoNoInline)
|
2020-12-11 01:39:52 +00:00
|
|
|
|
2022-10-14 09:02:34 +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)
|
|
|
|
}
|
|
|
|
|
2023-10-17 16:39:08 +00:00
|
|
|
import.meta.hot.accept('./logo.svg', (newUrl) => {
|
2024-11-06 09:28:44 +00:00
|
|
|
setImgSrc('#logo', newUrl.default)
|
2023-10-17 16:39:08 +00:00
|
|
|
console.log('Logo updated', newUrl.default)
|
|
|
|
})
|
|
|
|
|
2024-11-06 09:28:44 +00:00
|
|
|
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
|
|
|
})
|
2020-12-29 21:29:51 +00:00
|
|
|
|
2022-11-03 19:05:53 +00:00
|
|
|
import.meta.hot.on('vite:afterUpdate', (event) => {
|
|
|
|
console.log(`>>> vite:afterUpdate -- ${event.type}`)
|
|
|
|
})
|
|
|
|
|
2021-06-25 18:06:44 +00:00
|
|
|
import.meta.hot.on('vite:beforeUpdate', (event) => {
|
|
|
|
console.log(`>>> vite:beforeUpdate -- ${event.type}`)
|
2022-01-26 05:24:44 +00:00
|
|
|
|
|
|
|
const cssUpdate = event.updates.find(
|
|
|
|
(update) =>
|
2023-12-03 12:37:17 +00:00
|
|
|
update.type === 'css-update' && update.path.includes('global.css'),
|
2022-01-26 05:24:44 +00:00
|
|
|
)
|
|
|
|
if (cssUpdate) {
|
2022-06-09 12:22:05 +00:00
|
|
|
text(
|
|
|
|
'.css-prev',
|
|
|
|
(document.querySelector('.global-css') as HTMLLinkElement).href,
|
|
|
|
)
|
|
|
|
|
2022-11-03 19:05:53 +00:00
|
|
|
// Wait until the tag has been swapped out, which includes the time taken
|
|
|
|
// to download and parse the new stylesheet. Assert the swapped link.
|
2022-06-09 12:22:05 +00:00
|
|
|
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,
|
|
|
|
})
|
2022-01-26 05:24:44 +00:00
|
|
|
}
|
2021-06-25 18:06:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
import.meta.hot.on('vite:error', (event) => {
|
2022-11-25 15:49:08 +00:00
|
|
|
console.log(`>>> vite:error -- ${event.err.message}`)
|
2021-06-25 18:06:44 +00:00
|
|
|
})
|
|
|
|
|
2022-09-28 21:11:42 +00:00
|
|
|
import.meta.hot.on('vite:invalidate', ({ path }) => {
|
|
|
|
console.log(`>>> vite:invalidate -- ${path}`)
|
|
|
|
})
|
|
|
|
|
2022-03-26 17:08:36 +00:00
|
|
|
import.meta.hot.on('custom:foo', ({ msg }) => {
|
2020-12-29 21:29:51 +00:00
|
|
|
text('.custom', msg)
|
|
|
|
})
|
2022-03-26 14:42:07 +00:00
|
|
|
|
2023-10-11 14:57:37 +00:00
|
|
|
import.meta.hot.on('custom:remove', removeCb)
|
|
|
|
|
2022-03-26 14:42:07 +00:00
|
|
|
// send custom event to server to calculate 1 + 2
|
2022-03-26 17:08:36 +00:00
|
|
|
import.meta.hot.send('custom:remote-add', { a: 1, b: 2 })
|
|
|
|
import.meta.hot.on('custom:remote-add-result', ({ result }) => {
|
2022-03-26 14:42:07 +00:00
|
|
|
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
|
|
|
|
}
|
2023-10-11 14:57:37 +00:00
|
|
|
|
2024-11-06 09:28:44 +00:00
|
|
|
function setImgSrc(el, src) {
|
|
|
|
;(document.querySelector(el) as HTMLImageElement).src = src
|
2023-10-17 16:39:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 14:57:37 +00:00
|
|
|
function removeCb({ msg }) {
|
|
|
|
text('.toRemove', msg)
|
|
|
|
import.meta.hot.off('custom:remove', removeCb)
|
|
|
|
}
|