Skip to content
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: import functions from h3 with relative path #2207

Closed
wants to merge 1 commit into from

Conversation

Louis9902
Copy link

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

When I use nuxt with pnpm without shamefully-host, I noticed that I can't import functions like defineEventHandler from #imports. When I inspected the generated types in the nitro-imports.d.ts I found why this is. The import in this file is not relative into the node modules store. It is a named re-export like export { defineEventHandler, ... } from 'h3'. This re-export will not work because of the not hoisted h3 package. Every other export/import in this file already used a relative import, so i figured this might be a problem.

With this change, the already existing import resolution map is used to resolve the named import to a relative path. If this named import was by design, I'm interested why, but feel free to just close this PR.

πŸ“ Checklist

  • I have linked an issue or discussion. (not applicable)
  • I have updated the documentation accordingly. (not applicable)

I also provide a patch for applying the same to nitropack with pnpm patch

PNPM patch to test it with nuxt example project
  "pnpm": {
    "patchedDependencies": {
      "[email protected]": "patches/[email protected]"
    }
  }
diff --git a/dist/nitro.mjs b/dist/nitro.mjs
index 1dcaaacd17bb17e68d4f740839ce9a2f1221fd73..c2214be1bd73231be52b7ba15cdde4f729a3b0ce 100644
--- a/dist/nitro.mjs
+++ b/dist/nitro.mjs
@@ -2336,9 +2336,6 @@ async function writeTypes(nitro) {
   let autoImportExports;
   if (nitro.unimport) {
     await nitro.unimport.init();
-    autoImportExports = await nitro.unimport.toExports(typesDir).then(
-      (r) => r.replace(/#internal\/nitro/g, relative(typesDir, runtimeDir))
-    );
     const resolvedImportPathMap = /* @__PURE__ */ new Map();
     const imports = await nitro.unimport.getImports().then((r) => r.filter((i) => !i.type));
     for (const i of imports) {
@@ -2368,6 +2365,9 @@ async function writeTypes(nitro) {
       }
       resolvedImportPathMap.set(i.from, path);
     }
+    autoImportExports = await nitro.unimport.toExports(typesDir).then(
+      (r) => r.replace(/'(.*)'/g, (_, i) => `'${resolvedImportPathMap.get(i) ?? i}'`)
+    );
     autoImportedTypes = [
       (await nitro.unimport.generateTypeDeclarations({
         exportHelper: false,

@pi0
Copy link
Member

pi0 commented Mar 3, 2024

Thanks for the PR. And i'm certain it is valid. But please reopen when you could provide a minimal reproduction.

@pi0 pi0 closed this Mar 3, 2024
autoImportExports = await nitro.unimport
.toExports(typesDir)
.then((r) =>
r.replace(/'(.*)'/g, (_, i) => `'${resolvedImportPathMap.get(i) ?? i}'`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be also something potentially unimport could directly solve. /cc @antfu

autoImportExports = await nitro.unimport
.toExports(typesDir)
.then((r) =>
r.replace(/#internal\/nitro/g, relative(typesDir, runtimeDir))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to retain this?

@danielroe
Copy link
Member

danielroe commented Mar 3, 2024

i believe this reproduces on a new Nuxt install without shamefully hoist.

i think it is a regression as this worked previously, and indeed we need to fully resolve paths in ts declarations in this case.

@Louis9902
Copy link
Author

i believe this reproduces on a new Nuxt install without shamefully hoist.

That's correct, I didn't state that very clear in my initial comment

@pi0
Copy link
Member

pi0 commented Mar 3, 2024

I get the issue but having a fix in nitro that can easily cause regression in Nuxt, does seems needs better fix, possibly in unimport.

@danielroe Feel free to open a new hotfix PR if it is a regression of latest Nitro only, i trust you if you believe it needs hotfix in nitro.

@danielroe
Copy link
Member

We are already doing this (within nitro) for all the other imports. I think consistency means we should hotfix this now, even if we later move elsewhere.

I haven't yet checked the implementation above.

@pi0
Copy link
Member

pi0 commented Mar 3, 2024

Regardless i very much appreciate a clear tracker issue for this. Even if we are going with a hotfix first. πŸ™

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants