Skip to content

Commit

Permalink
refactor: refactor directory structure for honox-island component
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma committed May 2, 2024
1 parent 70c9f2f commit 9638879
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
"types": "./dist/vite/client.d.ts",
"import": "./dist/vite/client.js"
},
"./vite/components": {
"types": "./dist/vite/components.d.ts",
"import": "./dist/vite/components.js"
"./site/components": {
"types": "./dist/site/components/index.d.ts",
"import": "./dist/site/components/index.js"
}
},
"typesVersions": {
Expand Down Expand Up @@ -90,6 +90,9 @@
],
"vite/client": [
"./dist/vite/client"
],
"site/components": [
"./dist/site/components"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext, useContext } from 'hono/jsx'
import { COMPONENT_NAME, DATA_SERIALIZED_PROPS, DATA_HONO_TEMPLATE } from '../constants'
import { COMPONENT_NAME, DATA_SERIALIZED_PROPS, DATA_HONO_TEMPLATE } from '../../constants'

const inIsland = Symbol()
const inChildren = Symbol()
Expand Down
1 change: 1 addition & 0 deletions src/site/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HonoXIsland } from './honox-island.js'
21 changes: 12 additions & 9 deletions src/vite/island-components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('transformJsxTags', () => {
}`
const result = transformJsxTags(code, 'Badge.tsx')
expect(result).toBe(
`import { HonoXIsland } from "honox/vite/components";
`import { HonoXIsland } from "honox/site/components";
const BadgeOriginal = function () {
return <h1>Hello</h1>;
};
Expand All @@ -29,7 +29,7 @@ export default WrappedBadge;`
}`
const result = transformJsxTags(code, 'AsyncComponent.tsx')
expect(result).toBe(
`import { HonoXIsland } from "honox/vite/components";
`import { HonoXIsland } from "honox/site/components";
const AsyncComponentOriginal = async function () {
return <h1>Hello</h1>;
};
Expand All @@ -46,7 +46,7 @@ export default WrappedAsyncComponent;`
}`
const result = transformJsxTags(code, 'UnnamedComponent.tsx')
expect(result).toBe(
`import { HonoXIsland } from "honox/vite/components";
`import { HonoXIsland } from "honox/site/components";
const __HonoIsladComponent__Original = async function () {
return <h1>Hello</h1>;
};
Expand All @@ -63,7 +63,7 @@ export default Wrapped__HonoIsladComponent__;`
}`
const result = transformJsxTags(code, 'UnnamedComponent.tsx')
expect(result).toBe(
`import { HonoXIsland } from "honox/vite/components";
`import { HonoXIsland } from "honox/site/components";
const __HonoIsladComponent__Original = () => {
return <h1>Hello</h1>;
};
Expand All @@ -78,7 +78,7 @@ export default Wrapped__HonoIsladComponent__;`
const code = 'export default () => <h1>Hello</h1>'
const result = transformJsxTags(code, 'UnnamedComponent.tsx')
expect(result).toBe(
`import { HonoXIsland } from "honox/vite/components";
`import { HonoXIsland } from "honox/site/components";
const __HonoIsladComponent__Original = () => <h1>Hello</h1>;
const Wrapped__HonoIsladComponent__ = function (props) {
return import.meta.env.SSR ? <HonoXIsland componentName="UnnamedComponent.tsx" Component={__HonoIsladComponent__Original} props={props} /> : <__HonoIsladComponent__Original {...props}></__HonoIsladComponent__Original>;
Expand All @@ -91,7 +91,7 @@ export default Wrapped__HonoIsladComponent__;`
const code = 'export default ExportViaVariable'
const result = transformJsxTags(code, 'ExportViaVariable.tsx')
expect(result).toBe(
`import { HonoXIsland } from "honox/vite/components";
`import { HonoXIsland } from "honox/site/components";
const WrappedExportViaVariable = function (props) {
return import.meta.env.SSR ? <HonoXIsland componentName="ExportViaVariable.tsx" Component={ExportViaVariable} props={props} /> : <ExportViaVariable {...props}></ExportViaVariable>;
};
Expand All @@ -105,7 +105,7 @@ const ExportViaVariable = () => <h1>Hello</h1>
export { utilityFn, ExportViaVariable as default }`
const result = transformJsxTags(code, 'ExportViaVariable.tsx')
expect(result).toBe(
`import { HonoXIsland } from "honox/vite/components";
`import { HonoXIsland } from "honox/site/components";
const utilityFn = () => {};
const ExportViaVariable = () => <h1>Hello</h1>;
const WrappedExportViaVariable = function (props) {
Expand All @@ -118,8 +118,11 @@ export { utilityFn, WrappedExportViaVariable as default };`

describe('options', () => {
describe('reactApiImportSource', () => {
// get full path of ./components.tsx
const component = path.resolve(__dirname, 'components.tsx').replace(/\\/g, '/')
// get full path of honox-island.tsx
const component = path
.resolve(__dirname, '../site/components/honox-island.tsx')
// replace backslashes for Windows
.replace(/\\/g, '/')

// prettier-ignore
it('use \'hono/jsx\' by default', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/vite/island-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const transformJsxTags = (contents: string, componentName: string) => {
ast.program.body.unshift(
importDeclaration(
[importSpecifier(identifier('HonoXIsland'), identifier('HonoXIsland'))],
stringLiteral('honox/vite/components')
stringLiteral('honox/site/components')
)
)

Expand Down Expand Up @@ -196,7 +196,7 @@ export function islandComponents(options?: IslandComponentsOptions): Plugin {
},

async load(id) {
if (/\/honox\/.*?\/vite\/components\./.test(id)) {
if (/\/honox\/.*?\/site\/components\//.test(id)) {
if (!reactApiImportSource) {
return
}
Expand Down

0 comments on commit 9638879

Please sign in to comment.