-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(jsx): fix nested islands output by using honox/vite/components #161
Conversation
6d5d5f7
to
013f1f0
Compare
013f1f0
to
6762c76
Compare
c678b90
to
dd3743b
Compare
It looks awesome at first glance. Making My additional thoughts is if we use https://github.com/honojs/honox/blob/main/src/server/components/has-islands.tsx |
@yusukebe Thank you!
Sure! It seems that runtime-independent HasIslands could also be implemented. |
Hi @usualoma Looks good! One thing. This uses export default defineConfig({
plugins: [
honox({
useContextModule: 'react',
}),
],
}) My rough implementation: diff --git a/src/vite/index.ts b/src/vite/index.ts
index 415ce59..bec7546 100644
--- a/src/vite/index.ts
+++ b/src/vite/index.ts
@@ -13,6 +13,7 @@ type Options = {
devServer?: DevServerOptions
islandComponents?: IslandComponentsOptions
external?: string[]
+ useContextModule?: string
}
export const defaultOptions: Options = {
diff --git a/src/vite/island-components.ts b/src/vite/island-components.ts
index 5496971..5203051 100644
--- a/src/vite/island-components.ts
+++ b/src/vite/island-components.ts
@@ -168,39 +168,41 @@ export const transformJsxTags = (contents: string, componentName: string) => {
type IsIsland = (id: string) => boolean
export type IslandComponentsOptions = {
isIsland: IsIsland
+ useContextModule?: string
}
export function islandComponents(options?: IslandComponentsOptions): Plugin {
let root = ''
- let jsxImportSource: string | undefined
+ let useContextModule = options?.useContextModule
return {
name: 'transform-island-components',
configResolved: async (config) => {
root = config.root
- const tsConfigPath = path.resolve(process.cwd(), 'tsconfig.json')
- try {
- const tsConfigRaw = await fs.readFile(tsConfigPath, 'utf8')
- const tsConfig = parseJsonc(tsConfigRaw)
+ if (useContextModule) {
+ const tsConfigPath = path.resolve(process.cwd(), 'tsconfig.json')
+ try {
+ const tsConfigRaw = await fs.readFile(tsConfigPath, 'utf8')
+ const tsConfig = parseJsonc(tsConfigRaw)
- jsxImportSource =
- tsConfig.compilerOptions?.honoxReactImportSource ??
- tsConfig.compilerOptions?.jsxImportSource
- if (jsxImportSource === 'hono/jsx/dom') {
- jsxImportSource = 'hono/jsx' // we should use hono/jsx instead of hono/jsx/dom
+ useContextModule = tsConfig.compilerOptions?.jsxImportSource
+ if (useContextModule === 'hono/jsx/dom') {
+ useContextModule = 'hono/jsx' // we should use hono/jsx instead of hono/jsx/dom
+ }
+ } catch (error) {
+ console.warn('Error reading tsconfig.json:', error)
}
- } catch (error) {
- console.warn('Error reading tsconfig.json:', error)
}
},
+
async load(id) {
if (/\/honox\/.*?\/vite\/components\./.test(id)) {
- if (!jsxImportSource) {
+ if (!useContextModule) {
return
}
const contents = await fs.readFile(id, 'utf-8')
return {
- code: contents.replaceAll('hono/jsx', jsxImportSource),
+ code: contents.replaceAll('hono/jsx', useContextModule),
map: null,
}
} |
You're right, we shouldn't add our own keys to "tsconfig.json", we should be able to specify them in useContextModule?I have previously named it So I'm thinking of making it PrioritySuppose I consider it as
What do you think? |
Hypothetically, I pushed 8fb2913, which was implemented with the above content. In most cases, automatic configuration from tsconfig.json should be fine, but if you want to specify explicitly, you can write the following. export default defineConfig({
plugins: [
honox({
islandComponents: {
reactApiImportSource: 'react',
},
}),
],
}) Feel free to comment if you have any requests for changes. |
9ba7c00
to
7fe3d1f
Compare
aa1827d
to
70c9f2f
Compare
I totally agree with naming One thing: how about making the |
cf7a8c6
to
c727336
Compare
Ahhhhh, sorry, sorry. I've typed it. |
7e78740
to
a1342e4
Compare
OK, I did think that |
fixed in a1342e4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Thaaank! I'll merge this now. |
What is this?
honox-island
and the inner components are not.honox-island
.Implementation
Context API
is provided by the runtime, so we need to replace it depending on the runtime the app is using. However, we can usejsxImportSource
from tsconfig.json in the following places (orhonoxReactImportSource
if the app wants a special override) and replace it.https://github.com/honojs/honox/compare/main...usualoma:honox:fix-nested-islands-output?expand=1#diff-dbf4bfe9bdc2ab09701d7f99d62aa69e45740919b350c8166aebe64daa3fdd54R181-R206
In addition, before this PR, the very complicated expression assembly used to be done in "src/vite/island-components.ts", but by making it independent as "src/vite/components.tsx", it can be cleaned up and future enhancements can be made easier.
tasks