|
|
|
import * as svelteInternal from 'svelte/internal' |
|
|
|
|
|
|
|
|
|
const svelteInsert = svelteInternal.insert_hydration || svelteInternal.insert |
|
if (!svelteInsert) { |
|
throw new Error( |
|
'failed to find insert_hydration and insert in svelte/internal' |
|
) |
|
} |
|
|
|
import ErrorOverlay from './overlay.js' |
|
|
|
const removeElement = el => el && el.parentNode && el.parentNode.removeChild(el) |
|
|
|
export const adapter = class ProxyAdapterDom { |
|
constructor(instance) { |
|
this.instance = instance |
|
this.insertionPoint = null |
|
|
|
this.afterMount = this.afterMount.bind(this) |
|
this.rerender = this.rerender.bind(this) |
|
|
|
this._noOverlay = !!instance.hotOptions.noOverlay |
|
} |
|
|
|
|
|
|
|
|
|
static getErrorOverlay(noCreate = false) { |
|
if (!noCreate && !this.errorOverlay) { |
|
this.errorOverlay = ErrorOverlay() |
|
} |
|
return this.errorOverlay |
|
} |
|
|
|
|
|
static renderCompileError(message) { |
|
const noCreate = !message |
|
const overlay = this.getErrorOverlay(noCreate) |
|
if (!overlay) return |
|
overlay.setCompileError(message) |
|
} |
|
|
|
dispose() { |
|
|
|
|
|
if (this.insertionPoint) { |
|
removeElement(this.insertionPoint) |
|
this.insertionPoint = null |
|
} |
|
this.clearError() |
|
} |
|
|
|
|
|
afterMount(target, anchor) { |
|
const { |
|
instance: { debugName }, |
|
} = this |
|
if (!this.insertionPoint) { |
|
this.insertionPoint = document.createComment(debugName) |
|
} |
|
svelteInsert(target, this.insertionPoint, anchor) |
|
} |
|
|
|
rerender() { |
|
this.clearError() |
|
const { |
|
instance: { refreshComponent }, |
|
insertionPoint, |
|
} = this |
|
if (!insertionPoint) { |
|
throw new Error('Cannot rerender: missing insertion point') |
|
} |
|
refreshComponent(insertionPoint.parentNode, insertionPoint) |
|
} |
|
|
|
renderError(err) { |
|
if (this._noOverlay) return |
|
const { |
|
instance: { debugName }, |
|
} = this |
|
const title = debugName || err.moduleName || 'Error' |
|
this.constructor.getErrorOverlay().addError(err, title) |
|
} |
|
|
|
clearError() { |
|
if (this._noOverlay) return |
|
const overlay = this.constructor.getErrorOverlay(true) |
|
if (!overlay) return |
|
overlay.clearErrors() |
|
} |
|
} |
|
|
|
|
|
if (typeof window !== 'undefined') { |
|
window.__SVELTE_HMR_ADAPTER = adapter |
|
} |
|
|
|
|
|
|
|
|
|
export default adapter |
|
|