Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(vite): respect ctx.nuxt.options.modulesDir for resolving externals with vite-node #7612

Merged
merged 1 commit into from Sep 16, 2022

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Sep 16, 2022

πŸ”— Linked issue

resolves nuxt/nuxt#14952, nuxt/nuxt#14907, nuxt/nuxt#14802, nuxt/nuxt#14928

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 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)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

We are overriding shouldExternalize (PR: #6153) of vite-node, and use mlly resolver to directly resolve paths. Issue with nuxt/nuxt#14952 happens because directly setting URL to the directory of rootDir, causes resolving from parent node_modules and finally different resolved vue version. (Correct way is setting rootDir + '/_node_modules' or rootDir + /index.mjs)

This PR resolves issue by respecting modulesDir for resolving vue and other dependencies. In the future we might come of with better implementation to actually reuse same vite resolution. I didn't use return id on external branch (while it works!) until getting confirmation from @antfu. Vite-node's default external handler however does this. src

Also worth to mention that isExternal('vue') resolves to null as it is unable to determine external status.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@pi0 pi0 requested a review from antfu September 16, 2022 22:11
@netlify
Copy link

netlify bot commented Sep 16, 2022

βœ… Deploy Preview for nuxt3-docs canceled.

Name Link
πŸ”¨ Latest commit de1aa4e
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/6324f491ff8ba60008ef15e6

@@ -102,7 +102,7 @@ function createViteNodeMiddleware (ctx: ViteBuildContext, invalidates: Set<strin
node.shouldExternalize = async (id: string) => {
const result = await isExternal(id)
if (result?.external) {
return resolveModule(result.id, { url: ctx.nuxt.options.rootDir })
return resolveModule(result.id, { url: ctx.nuxt.options.modulesDir })
Copy link
Member Author

@pi0 pi0 Sep 16, 2022

Choose a reason for hiding this comment

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

@antfu Shall we do this to be same as vite-node? Would it diretly require vue or let vite resolve to fullpath?

Suggested change
return resolveModule(result.id, { url: ctx.nuxt.options.modulesDir })
return id

Copy link
Member

Choose a reason for hiding this comment

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

The id passed here should already be resolved by Vite. So yes I think we could directly do this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for checking. FYI, it is not always resolved. You can reproduce it only with an external repository by putting a debugger. vue for instance is directly passed to shouldExternalize.

@pi0
Copy link
Member Author

pi0 commented Sep 16, 2022

Merging as hotfix.

@pi0 pi0 changed the title fix(vite): respect ctx.nuxt.options.modulesDir for resolving externals fix(vite): respect ctx.nuxt.options.modulesDir for resolving externals with vite-node Sep 16, 2022
@pi0 pi0 merged commit abd0feb into main Sep 16, 2022
@pi0 pi0 deleted the fix/vite-external branch September 16, 2022 22:29
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RC 9] [RC 10] Cannot read properties of undefined (reading 'modules') when vue is installed in parent dir
3 participants