-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable placing islands anywhere (#176)
* feat: enable placing islands anywhere * add e2e test * fixed path resolving * improve resolving paths * add some tests * import only `.tsx` at the client
- Loading branch information
Showing
12 changed files
with
216 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { PropsWithChildren } from 'hono/jsx' | ||
import { useState } from 'hono/jsx' | ||
|
||
export default function Counter({ | ||
children, | ||
initial = 0, | ||
}: PropsWithChildren<{ | ||
initial?: number | ||
}>) { | ||
const [count, setCount] = useState(initial) | ||
const increment = () => setCount(count + 1) | ||
return ( | ||
<div> | ||
<p>Count: {count}</p> | ||
<button onClick={increment}>Increment</button> | ||
{children} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Counter from '../../components/$counter' | ||
|
||
export default function Interaction() { | ||
return ( | ||
<> | ||
<Counter initial={5} /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { matchIslandComponentId } from './path' | ||
|
||
describe('matchIslandComponentId', () => { | ||
describe('match', () => { | ||
const paths = [ | ||
'/islands/counter.tsx', | ||
'/islands/directory/counter.tsx', | ||
'/routes/$counter.tsx', | ||
'/routes/directory/$counter.tsx', | ||
'/routes/_counter.island.tsx', | ||
'/routes/directory/_counter.island.tsx', | ||
'/$counter.tsx', | ||
'/directory/$counter.tsx', | ||
'/_counter.island.tsx', | ||
'/directory/_counter.island.tsx', | ||
] | ||
|
||
paths.forEach((path) => { | ||
it(`Should match ${path}`, () => { | ||
const match = matchIslandComponentId(path) | ||
expect(match).not.toBeNull() | ||
expect(match![0]).toBe(path) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('not match', () => { | ||
const paths = [ | ||
'/routes/directory/component.tsx', | ||
'/routes/directory/foo$component.tsx', | ||
'/routes/directory/foo_component.island.tsx', | ||
'/routes/directory/component.island.tsx', | ||
'/directory/islands/component.tsx', | ||
] | ||
|
||
paths.forEach((path) => { | ||
it(`Should not match ${path}`, () => { | ||
const match = matchIslandComponentId(path) | ||
expect(match).toBeNull() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('not match - with `islandDir`', () => { | ||
const paths = ['/islands/component.tsx'] | ||
|
||
paths.forEach((path) => { | ||
it(`Should not match ${path}`, () => { | ||
const match = matchIslandComponentId(path, '/directory/islands') | ||
expect(match).toBeNull() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Check if the name is a valid component name | ||
* | ||
* @param name - The name to check | ||
* @returns true if the name is a valid component name | ||
* @example | ||
* isComponentName('Badge') // true | ||
* isComponentName('BadgeComponent') // true | ||
* isComponentName('badge') // false | ||
* isComponentName('MIN') // false | ||
* isComponentName('Badge_Component') // false | ||
*/ | ||
export function isComponentName(name: string) { | ||
return /^[A-Z][A-Z0-9]*[a-z][A-Za-z0-9]*$/.test(name) | ||
} | ||
|
||
/** | ||
* Matches when id is the filename of Island component | ||
* | ||
* @param id - The id to match | ||
* @returns The result object if id is matched or null | ||
*/ | ||
export function matchIslandComponentId(id: string, islandDir: string = '/islands') { | ||
const regExp = new RegExp( | ||
`^${islandDir}\/.+?\.tsx$|.*\/(?:\_[a-zA-Z0-9-]+\.island\.tsx$|\\\$[a-zA-Z0-9-]+\.tsx$)` | ||
) | ||
return id.match(regExp) | ||
} |
Oops, something went wrong.