Skip to content

Commit

Permalink
fix: remove html-parsed-element dependency and fix ssr error
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang committed Aug 29, 2023
1 parent aa8dd6d commit e25472d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
3 changes: 1 addition & 2 deletions packages/runtime-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"dependencies": {
"@vue/shared": "3.3.4",
"@vue/runtime-core": "3.3.4",
"csstype": "^3.1.1",
"html-parsed-element": "^0.4.1"
"csstype": "^3.1.1"
}
}
2 changes: 1 addition & 1 deletion packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
SlotsType
} from '@vue/runtime-core'
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
import HTMLParsedElement from 'html-parsed-element'
import HTMLParsedElement from './html-parsed-element'
import { hydrate, render } from '.'

export type VueElementConstructor<P = {}> = {
Expand Down
91 changes: 91 additions & 0 deletions packages/runtime-dom/src/html-parsed-element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Based on https://github.com/WebReflection/html-parsed-element/blob/11081fc102e2eec745a1a1d32723eef342df2452/esm/index.js
/*! (c) Andrea Giammarchi - ISC */

const HTMLParsedElement = (() => {
if (typeof window === 'undefined') return class {}

const DCL = 'DOMContentLoaded'
const init = new WeakMap()
// @ts-ignore
const queue = []
const isParsed = (el: Element) => {
do {
if (el.nextSibling) return true
// @ts-ignore
} while ((el = el.parentNode))
return false
}
const upgrade = () => {
// @ts-ignore
queue.splice(0).forEach(info => {
if (init.get(info[0]) !== true) {
init.set(info[0], true)
info[0][info[1]]()
}
})
}
window.document.addEventListener(DCL, upgrade)
class HTMLParsedElement extends HTMLElement {
static withParsedCallback(
Class: CustomElementConstructor,
name = 'parsed'
) {
const { prototype } = Class
const { connectedCallback } = prototype
const method = name + 'Callback'
const cleanUp = (
el: Element,
observer: MutationObserver,
ownerDocument: Document,
onDCL: EventListener
) => {
observer.disconnect()
ownerDocument.removeEventListener(DCL, onDCL)
parsedCallback(el)
}
const parsedCallback = (el: Element) => {
if (!queue.length) requestAnimationFrame(upgrade)
queue.push([el, method])
}
Object.defineProperties(prototype, {
connectedCallback: {
configurable: true,
writable: true,
value() {
if (connectedCallback) connectedCallback.apply(this, arguments)
if (method in this && !init.has(this)) {
const self = this
const { ownerDocument } = self
init.set(self, false)
if (ownerDocument.readyState === 'complete' || isParsed(self))
parsedCallback(self)
else {
const onDCL = () =>
cleanUp(self, observer, ownerDocument, onDCL)
ownerDocument.addEventListener(DCL, onDCL)
const observer = new MutationObserver(() => {
/* istanbul ignore else */
if (isParsed(self))
cleanUp(self, observer, ownerDocument, onDCL)
})
observer.observe(self.parentNode, {
childList: true,
subtree: true
})
}
}
}
},
[name]: {
configurable: true,
get() {
return init.get(this) === true
}
}
})
return Class
}
}
return HTMLParsedElement.withParsedCallback(HTMLParsedElement)
})()
export default HTMLParsedElement
3 changes: 0 additions & 3 deletions packages/runtime-dom/src/shims.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ function createConfig(format, output, plugins = []) {
...(format === 'cjs' ? [] : [polyfillNode()]),
nodeResolve()
]
: [nodeResolve()]
: []

return nodePlugins
}
Expand Down

0 comments on commit e25472d

Please sign in to comment.