From 5994d8a0a5b5b3534a186a03243868afe3f36ea3 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Thu, 11 Apr 2024 16:45:32 -0500 Subject: [PATCH 1/5] refactor: Reduce subpath exports --- package.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1d66b3c..5eb8b07 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,8 @@ "types": "src/index.d.ts", "exports": { ".": "./src/index.js", - "./router": "./src/router.js", - "./lazy": "./src/lazy.js", "./prerender": "./src/prerender.js", - "./hydrate": "./src/hydrate.js" + "./package.json": "./package.json" }, "license": "MIT", "description": "Isomorphic utilities for Preact", @@ -35,6 +33,11 @@ "preact": ">=10", "preact-render-to-string": ">=6.4.0" }, + "peerDependenciesMeta": { + "preact-render-to-string": { + "optional": true + } + }, "devDependencies": { "@types/mocha": "^10.0.7", "@types/sinon-chai": "^3.2.12", From 7cd274f1e2cf602c82872b34843dd1db45b85055 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Thu, 11 Apr 2024 16:47:28 -0500 Subject: [PATCH 2/5] refactor: Use only named exports, rather than a mix --- src/hydrate.d.ts | 2 +- src/hydrate.js | 4 ++-- src/index.d.ts | 5 ++--- src/index.js | 9 +++------ src/lazy.d.ts | 2 +- src/lazy.js | 2 +- src/prerender.d.ts | 2 +- src/prerender.js | 2 +- test/node/prerender.test.js | 2 +- test/router.test.js | 2 +- 10 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/hydrate.d.ts b/src/hydrate.d.ts index ace9158..f56e39d 100644 --- a/src/hydrate.d.ts +++ b/src/hydrate.d.ts @@ -1,3 +1,3 @@ import { ComponentChild } from 'preact'; -export default function hydrate(jsx: ComponentChild, parent?: Element | Document | ShadowRoot | DocumentFragment): void; +export function hydrate(jsx: ComponentChild, parent?: Element | Document | ShadowRoot | DocumentFragment): void; diff --git a/src/hydrate.js b/src/hydrate.js index 68d9375..d7e2cd8 100644 --- a/src/hydrate.js +++ b/src/hydrate.js @@ -2,8 +2,8 @@ import { render, hydrate as hydrativeRender } from 'preact'; let initialized; -/** @type {typeof render} */ -export default function hydrate(jsx, parent) { +/** @type {typeof hydrativeRender} */ +export function hydrate(jsx, parent) { if (typeof window === 'undefined') return; let isodata = document.querySelector('script[type=isodata]'); // @ts-ignore-next diff --git a/src/index.d.ts b/src/index.d.ts index 70ba4a2..1d8e633 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,4 +1,3 @@ -export { default as prerender } from './prerender.js'; export * from './router.js'; -export { default as lazy, ErrorBoundary } from './lazy.js'; -export { default as hydrate } from './hydrate.js'; +export * from './lazy.js'; +export * from './hydrate.js'; diff --git a/src/index.js b/src/index.js index 3bfc751..44fe534 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,4 @@ +// lack of wildcard export is intentional to avoid exposing `exec` export { Router, LocationProvider, useLocation, Route, useRoute } from './router.js'; -export { default as lazy, ErrorBoundary } from './lazy.js'; -export { default as hydrate } from './hydrate.js'; - -export function prerender(vnode, options) { - return import('./prerender.js').then(m => m.default(vnode, options)); -} +export * from './lazy.js'; +export * from './hydrate.js'; diff --git a/src/lazy.d.ts b/src/lazy.d.ts index 829cdf3..8805198 100644 --- a/src/lazy.d.ts +++ b/src/lazy.d.ts @@ -1,5 +1,5 @@ import { ComponentChildren, VNode } from 'preact'; -export default function lazy(load: () => Promise<{ default: T } | T>): T; +export function lazy(load: () => Promise<{ default: T } | T>): T; export function ErrorBoundary(props: { children?: ComponentChildren; onError?: (error: Error) => void }): VNode; diff --git a/src/lazy.js b/src/lazy.js index 9670e8d..17ce2c8 100644 --- a/src/lazy.js +++ b/src/lazy.js @@ -1,7 +1,7 @@ import { h, options } from 'preact'; import { useState, useRef } from 'preact/hooks'; -export default function lazy(load) { +export function lazy(load) { let p, c; return props => { const [, update] = useState(0); diff --git a/src/prerender.d.ts b/src/prerender.d.ts index b9958bc..104ba3d 100644 --- a/src/prerender.d.ts +++ b/src/prerender.d.ts @@ -9,7 +9,7 @@ export interface PrerenderResult { links?: Set } -export default function prerender( +export function prerender( vnode: VNode, options?: PrerenderOptions ): Promise; diff --git a/src/prerender.js b/src/prerender.js index be435bf..4a5d223 100644 --- a/src/prerender.js +++ b/src/prerender.js @@ -14,7 +14,7 @@ options.vnode = vnode => { * @param {object} [options] * @param {object} [options.props] Additional props to merge into the root JSX element */ -export default async function prerender(vnode, options) { +export async function prerender(vnode, options) { options = options || {}; const props = options.props; diff --git a/test/node/prerender.test.js b/test/node/prerender.test.js index 434b151..1450d98 100644 --- a/test/node/prerender.test.js +++ b/test/node/prerender.test.js @@ -2,7 +2,7 @@ import { test } from 'uvu'; import * as assert from 'uvu/assert'; import { html } from 'htm/preact'; -import { default as prerender } from '../../src/prerender.js'; +import { prerender } from '../../src/prerender.js'; test('extracts links', async () => { const App = () => html` diff --git a/test/router.test.js b/test/router.test.js index d4e0d1f..16d37cc 100644 --- a/test/router.test.js +++ b/test/router.test.js @@ -5,7 +5,7 @@ import * as sinon from 'sinon'; import sinonChai from 'sinon-chai'; import { LocationProvider, Router, useLocation, Route, useRoute } from '../src/router.js'; -import lazy, { ErrorBoundary } from '../src/lazy.js'; +import { lazy, ErrorBoundary } from '../src/lazy.js'; const expect = chai.expect; chai.use(sinonChai); From 3e1d2c405505da60765c26b8acee41a90cf1108a Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 2 Sep 2024 01:45:17 -0500 Subject: [PATCH 3/5] refactor: Drop half implemented `url` prop on LocationProvider --- src/router.js | 2 +- test/router.test.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/router.js b/src/router.js index 354f451..d21f7da 100644 --- a/src/router.js +++ b/src/router.js @@ -71,7 +71,7 @@ export const exec = (url, route, matches) => { }; export function LocationProvider(props) { - const [url, route] = useReducer(UPDATE, props.url || location.pathname + location.search); + const [url, route] = useReducer(UPDATE, location.pathname + location.search); const wasPush = push === true; const value = useMemo(() => { diff --git a/test/router.test.js b/test/router.test.js index 16d37cc..4fdb2f5 100644 --- a/test/router.test.js +++ b/test/router.test.js @@ -37,12 +37,15 @@ describe('Router', () => { it('should strip trailing slashes from path', async () => { render( - + , scratch ); + loc.route('/a/'); + await sleep(1); + expect(loc).to.deep.include({ url: '/a/', path: '/a', From 70977694847a97882d6e4947acfb5a43ed0858c4 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 14 Oct 2024 16:58:16 -0500 Subject: [PATCH 4/5] fix: Ensure isodata script tag will pass spec validators --- src/hydrate.js | 2 +- src/prerender.js | 2 +- test/node/prerender.test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hydrate.js b/src/hydrate.js index d7e2cd8..796ed5c 100644 --- a/src/hydrate.js +++ b/src/hydrate.js @@ -5,7 +5,7 @@ let initialized; /** @type {typeof hydrativeRender} */ export function hydrate(jsx, parent) { if (typeof window === 'undefined') return; - let isodata = document.querySelector('script[type=isodata]'); + let isodata = document.getElementById('isodata'); // @ts-ignore-next parent = parent || (isodata && isodata.parentNode) || document.body; if (!initialized && isodata) { diff --git a/src/prerender.js b/src/prerender.js index 4a5d223..3c85cab 100644 --- a/src/prerender.js +++ b/src/prerender.js @@ -34,7 +34,7 @@ export async function prerender(vnode, options) { try { let html = await renderToStringAsync(vnode); - html += ``; + html += ``; return { html, links }; } finally { vnodeHook = null; diff --git a/test/node/prerender.test.js b/test/node/prerender.test.js index 1450d98..163e74c 100644 --- a/test/node/prerender.test.js +++ b/test/node/prerender.test.js @@ -23,7 +23,7 @@ test('extracts links', async () => { test('appends iso data script', async () => { const { html: h } = await prerender(html`
`); // Empty for now, but used for hydration vs render detection - assert.match(h, /