Skip to content

Commit

Permalink
feat: replace jsdom with cheerio
Browse files Browse the repository at this point in the history
  • Loading branch information
infodusha committed Jul 22, 2024
1 parent f77817c commit adfe095
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ npx --yes define-html
or

```sh
bunx define-html
bunx --bun define-html
```

This will create `dist` folder with the output files.
Expand Down
Binary file modified bun.lockb
Binary file not shown.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"watch": "bun build ./src/index.ts --outdir ./dist --watch",
"build": "bun build ./src/index.ts --outdir ./dist",
"build:bin": "bun build ./src/bin.ts --target node --outdir ./dist",
"compile": "cd example && bun ../src/bin.ts --outdir ./dist",
"compile": "cd example && bun --bun ../src/bin.ts --outdir ./dist",
"lint": "bunx biome lint",
"test": "playwright test",
"test:local": "playwright test --ui"
Expand Down Expand Up @@ -44,12 +44,11 @@
"devDependencies": {
"@biomejs/biome": "1.8.3",
"@playwright/test": "^1.37.1",
"@types/jsdom": "^21.1.7",
"http-server": "^14.1.1",
"typescript": "^5.2.2"
},
"dependencies": {
"fast-glob": "^3.3.2",
"jsdom": "^24.1.1"
"cheerio": "^1.0.0-rc.12",
"fast-glob": "^3.3.2"
}
}
20 changes: 9 additions & 11 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

import fg from "fast-glob";
import { JSDOM } from "jsdom";
import * as cheerio from "cheerio";

import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
import { dirname, relative, resolve } from "node:path";

import { commentMarker, getComponentLinks } from "./helpers";
import { commentMarker, componentSelector } from "./helpers";

// TODO get from args
const DIST_DIR = "./dist";
Expand All @@ -20,9 +20,9 @@ const pages = new Set<string>();

for (const path of files) {
const text = await readFile(path, "utf8");
const dom = new JSDOM(text);
const $ = cheerio.load(text);

const links = getComponentLinks(dom.window.document);
const links = $(componentSelector);
if (links.length === 0) {
if (!components.has(path)) {
pages.add(path);
Expand All @@ -33,19 +33,17 @@ for (const path of files) {
console.log(`${path} has ${links.length} components`);

for (const link of links) {
const component = relative(dirname(path), link.href);
const component = relative(dirname(path), link.attribs.href);
components.add(component);
pages.delete(component);
const text = await readFile(component, "utf8");
const comment = dom.window.document.createComment(
`${commentMarker}${component}\n${text}`
);
link.before(comment);
link.remove();
$(`<!--${commentMarker}${component}\n${text}-->`).insertBefore(link);
}

$(componentSelector).remove();

const out = await prepareSave(path);
await writeFile(out, dom.serialize());
await writeFile(out, $.html());
}

for (const page of pages) {
Expand Down
9 changes: 3 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const IGNORE_DATA_ATTRIBUTE = "data-define-html-ignore";

export const commentMarker = "__DEFINE_HTML__";

export function cloneNode<T extends Node>(element: T): T {
Expand All @@ -21,9 +23,4 @@ export function returnIfDefined<T>(
return value;
}

const ignoreDataAttribute = "data-define-html-ignore";
const selector = `link[rel='preload'][as='fetch'][href$='.html']:not([${ignoreDataAttribute}])`;

export function getComponentLinks(document: Document): HTMLLinkElement[] {
return Array.from(document.querySelectorAll(selector));
}
export const componentSelector = `link[rel='preload'][as='fetch'][href$='.html']:not([${IGNORE_DATA_ATTRIBUTE}])`;
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createComponent } from "./create-component.js";
import {
commentMarker,
getComponentLinks,
componentSelector,
returnIfDefined,
} from "./helpers.js";

Expand All @@ -13,7 +13,10 @@ document.addEventListener("DOMContentLoaded", () => {
});

async function fetchFromLinks(): Promise<void> {
await Promise.all(getComponentLinks(document).map(defineHtml));
const links = Array.from<HTMLLinkElement>(
document.querySelectorAll(componentSelector)
);
await Promise.all(links.map(defineHtml));
}

async function defineHtml(link: HTMLLinkElement): Promise<void> {
Expand Down

0 comments on commit adfe095

Please sign in to comment.