vite-plugin-vanjs 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client/index.mjs CHANGED
@@ -36,8 +36,8 @@ export const styleToString = (style) => {
36
36
  : /* istanbul ignore next */ "";
37
37
  };
38
38
 
39
- /** @type {(el1: HTMLElement, el2: HTMLElement | HTMLElement[]) => boolean} */
40
- export function elementsMatch(el1, el2) {
39
+ /** @type {(el1: HTMLElement, el2: HTMLElement | HTMLElement[], deep?: boolean) => boolean} */
40
+ export function elementsMatch(el1, el2, deep) {
41
41
  // Quick initial checks before recursing
42
42
  // istanbul ignore else
43
43
  if (
@@ -63,15 +63,25 @@ export function elementsMatch(el1, el2) {
63
63
  child.querySelector("[data-hk]"))
64
64
  );
65
65
 
66
+ // istanbul ignore next
66
67
  if (!hasHydratedChildren) {
67
68
  return true; // If no hydrated children, elements match based on initial checks
68
69
  }
69
70
 
70
- return childNodes1.every((child, idx) =>
71
- elementsMatch(child, childNodes2[idx])
72
- );
71
+ // Only recurse if opted in
72
+ // istanbul ignore next
73
+ return deep
74
+ ? childNodes1.every((child, idx) => elementsMatch(child, childNodes2[idx]))
75
+ : true;
73
76
  }
74
77
 
78
+ /** @type {<E extends Element = Element, T extends keyof HTMLElementTagNameMap>(target: E, ...tagNames: T[]) => boolean} */
79
+ const isTag = (target, ...tagNames) => {
80
+ return tagNames.some((tag) =>
81
+ target.tagName.toLowerCase() === tag.toLowerCase()
82
+ );
83
+ };
84
+
75
85
  function createHydrationContext() {
76
86
  /** @type {WeakMap<Element, Element>} */
77
87
  const parentCache = new WeakMap();
@@ -180,12 +190,8 @@ export const hydrate = (target, content) => {
180
190
  const wrapper = unwrap(content);
181
191
  const currentChildren = Array.from(target.children);
182
192
  const newChildren = Array.from(wrapper.children);
183
- const isStyleLink = (tag) =>
184
- [HTMLStyleElement, HTMLLinkElement].some((e) => tag instanceof e);
185
- const isStyle = (tag) => tag instanceof HTMLStyleElement;
186
- const isLink = (tag) => tag instanceof HTMLLinkElement;
187
193
 
188
- if (target.tagName.toLowerCase() === "head") {
194
+ if (isTag(target, "head")) {
189
195
  // Keep current tags on first hydration
190
196
  if (!target.hasAttribute("data-h")) {
191
197
  target.setAttribute("data-h", "");
@@ -193,14 +199,18 @@ export const hydrate = (target, content) => {
193
199
  }
194
200
 
195
201
  // Handle non-style/link tags first
196
- const regularTags = newChildren.filter((child) => !isStyleLink(child));
202
+ const regularTags = newChildren.filter((child) =>
203
+ !isTag(child, "style", "link")
204
+ );
197
205
 
198
206
  // Handle style/link tags separately
199
- const styleTags = newChildren.filter((child) => isStyleLink(child));
207
+ const styleTags = newChildren.filter((child) =>
208
+ isTag(child, "style", "link")
209
+ );
200
210
 
201
211
  // Create maps for existing tags
202
212
  const existingStyles = new Map(
203
- currentChildren.filter(isStyleLink)
213
+ currentChildren.filter((child) => isTag(child, "style", "link"))
204
214
  .map((child) => [getTagKey(child), child]),
205
215
  );
206
216
 
@@ -224,29 +234,29 @@ export const hydrate = (target, content) => {
224
234
 
225
235
  // Skip if tag already exists with same content+id/href
226
236
  if (existing) {
227
- /* istanbul ignore next - try again later */
228
- if (isStyle(existing) && isStyle(newChild)) {
237
+ // istanbul ignore next - try again later
238
+ if (isTag(existing, "style") && isTag(newChild, "style")) {
229
239
  if (
230
240
  existing.textContent === newChild.textContent &&
231
241
  existing.id === newChild.id
232
242
  ) return;
233
243
  }
234
- /* istanbul ignore next - try again later */
235
- if (isLink(existing) && isLink(newChild)) {
244
+ // istanbul ignore next - try again later
245
+ if (isTag(existing, "link") && isTag(newChild, "link")) {
236
246
  if (existing.href === newChild.href) return;
237
247
  }
238
248
  }
239
249
 
240
250
  // For link tags, add with disabled state first
241
- /* istanbul ignore else - try again later */
242
- if (isLink(newChild)) {
251
+ // istanbul ignore else - try again later
252
+ if (isTag(newChild, "link")) {
243
253
  const temp = newChild.cloneNode();
244
254
  temp.disabled = true;
245
255
 
246
256
  const originalRel = temp.rel;
247
257
  temp.rel = "preload";
248
258
  temp.as = "style";
249
- /* istanbul ignore next */
259
+ // istanbul ignore next
250
260
  temp.onload = () => {
251
261
  temp.rel = originalRel;
252
262
  temp.removeAttribute("as");
@@ -258,9 +268,9 @@ export const hydrate = (target, content) => {
258
268
 
259
269
  target.appendChild(temp);
260
270
  } // For style tags, add new one first
261
- else if (isStyle(newChild)) {
271
+ else if (isTag(newChild, "style")) {
262
272
  target.appendChild(newChild);
263
- /* istanbul ignore next - try again later */
273
+ // istanbul ignore next - try again later
264
274
  if (existing && existing.parentNode === target) {
265
275
  // Remove old one in next frame
266
276
  existing.remove();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vanjs",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "author": "thednp",
5
5
  "license": "MIT",
6
6
  "description": "A mini meta-framework for VanJS powered by Vite",
package/plugin/types.d.ts CHANGED
@@ -6,18 +6,15 @@ export * from "../meta/types";
6
6
  export * from "../server/types";
7
7
  export * from "../client/types";
8
8
  export * from "../parser/types";
9
- import type { Plugin } from "vite";
9
+ import type { PluginOption } from "vite";
10
10
 
11
11
  export type VanJSPluginOptions = {
12
12
  routesDir: string;
13
13
  extensions: string[];
14
14
  };
15
15
 
16
- export type VanJSPlugin = (
17
- options?: Partial<VanJSPluginOptions>,
18
- ) => Plugin;
19
-
20
- // This is what your plugin actually returns, so declare it as a Plugin type
21
- declare const VitePluginVanJS: VanJSPlugin;
16
+ export declare const VitePluginVanJS: (
17
+ config?: VitePluginVanJS,
18
+ ) => PluginOption<VanJSPluginOptions>;
22
19
 
23
20
  export default VitePluginVanJS;