Skip to content

Commit

Permalink
feat: clear dist dir before build
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukinoko-kun committed Jul 27, 2024
1 parent 0fee9ea commit 7d7cb6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
13 changes: 12 additions & 1 deletion bin/ecs.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, relative } from "node:path"
import { existsSync } from "node:fs"
import { readdir, readFile, writeFile } from "node:fs/promises"
import { readdir, readFile, rm, unlink, writeFile } from "node:fs/promises"
import { Script } from "node:vm"
import { Parcel } from "@parcel/core"
import { fileURLToPath } from "node:url"
Expand Down Expand Up @@ -33,6 +33,17 @@ async function main(args) {
async function build() {
const config = await getConfig()

// clean dist
if (existsSync(config.dist)) {
for (const file of await readdir(config.dist, { withFileTypes: true })) {
if (file.isDirectory()) {
await rm(join(config.dist, file.name), { recursive: true })
} else {
await unlink(join(config.dist, file.name))
}
}
}

let bundler = new Parcel({
entries: config.htmlFiles.map((file) => join(config.src, file)),
config: "@parcel/config-default",
Expand Down
23 changes: 2 additions & 21 deletions lib/builtin/systems/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,10 @@ export function renderHtmlRoot(): void {
render(e, el)
}

diffRender(root.element, el, false)
diffRender(root.element, el)
}

function diffRender(old: Element, next: HTMLElement, deleteAttributes = true): void {
// check attributes
const oldAttrs = old.attributes
const nextAttrs = next.attributes
for (let i = 0; i < nextAttrs.length; i++) {
const nextAttr = nextAttrs[i]!
const oldAttr = oldAttrs.getNamedItem(nextAttr.name)
if (!oldAttr || oldAttr.value !== nextAttr.value) {
old.setAttribute(nextAttr.name, nextAttr.value)
}
}
if (deleteAttributes) {
for (let i = 0; i < oldAttrs.length; i++) {
const oldAttr = oldAttrs[i]!
if (nextAttrs.getNamedItem(oldAttr.name) === null) {
old.removeAttribute(oldAttr.name)
}
}
}

function diffRender(old: Element, next: HTMLElement) {
if (old.childNodes.length !== next.childNodes.length) {
old.innerHTML = next.innerHTML
return
Expand Down

0 comments on commit 7d7cb6b

Please sign in to comment.