honox icon indicating copy to clipboard operation
honox copied to clipboard

It would be nice to place islands anywhere, not just in routes and islands directories.

Open liltechnomancer opened this issue 10 months ago • 4 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

I wanted to be able to place islands anywhere not just within route and islands

Here is the diff that solved my problem:

This should probably be done in a more elegant way, but I wanted to provide what worked for me. If you like the idea, I can make the commits to add config options or something to allow for this.

diff --git a/node_modules/honox/dist/vite/inject-importing-islands.js b/node_modules/honox/dist/vite/inject-importing-islands.js
index 78c7d0f..47a28aa 100644
--- a/node_modules/honox/dist/vite/inject-importing-islands.js
+++ b/node_modules/honox/dist/vite/inject-importing-islands.js
@@ -8,7 +8,7 @@ import { IMPORTING_ISLANDS_ID } from "../constants.js";
 const generate = _generate.default ?? _generate;
 async function injectImportingIslands() {
   const isIslandRegex = new RegExp(/(\/islands\/|\_[a-zA-Z0-9[-]+\.island\.[tj]sx$)/);
-  const fileRegex = new RegExp(/(routes|_renderer|_error|_404)\/.*\.[tj]sx$/);
+  const fileRegex = new RegExp(/(.|routes|_renderer|_error|_404)\/.*\.[tj]sx$/);
   const cache = {};
   const walkDependencyTree = async (baseFile, dependencyFile) => {
     const depPath = dependencyFile ? path.join(path.dirname(baseFile), dependencyFile) + ".tsx" : baseFile;
@@ -37,8 +37,10 @@ async function injectImportingIslands() {
       }
       const hasIslandsImport = (await walkDependencyTree(id)).flat().some((x) => isIslandRegex.test(normalizePath(x)));
       if (!hasIslandsImport) {
+        
         return;
       }
+
       const ast = parse(sourceCode, {
         sourceType: "module",
         plugins: ["jsx", "typescript"]
diff --git a/node_modules/honox/dist/vite/island-components.js b/node_modules/honox/dist/vite/island-components.js
index eb99b62..8998758 100644
--- a/node_modules/honox/dist/vite/island-components.js
+++ b/node_modules/honox/dist/vite/island-components.js
@@ -157,10 +157,11 @@ function getIslandComponentName(root, id, options) {
     return path.resolve(id2).startsWith(islandDirectoryPath);
   };
   const matchIslandPath = options?.isIsland ?? defaultIsIsland;
+  
   if (!matchIslandPath(id)) {    
     return null;
   }
-  const match = id.match(/(\/islands\/.+?\.tsx$)|(\/routes\/.*\_[a-zA-Z0-9[-]+\.island\.tsx$)/);
+  const match = id.match(/(\/islands\/.+?\.tsx$)|(\/app\/.*\_[a-zA-Z0-9[-]+\.island\.tsx$)/);
   if (!match) {    
     return null;
   }

This issue body was partially generated by patch-package.

liltechnomancer avatar Apr 25 '24 01:04 liltechnomancer

Hi @liltechnomancer

Sorry for the delayed response. Yes. This should be fixed. If Some PR will be merged, I can work on it! Thanks.

yusukebe avatar May 01 '24 03:05 yusukebe

@yusukebe No need to apologize, thanks for all the amazing work!

liltechnomancer avatar May 05 '24 17:05 liltechnomancer

Hi @liltechnomancer

I will create the PR by referring to your code. Can I make you a co-author for the PR?

yusukebe avatar May 08 '24 13:05 yusukebe

If you would like! That would be cool 😄

liltechnomancer avatar May 08 '24 21:05 liltechnomancer