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

Set Vite's root cwd() instead of .nuxt #10981

Closed
antfu opened this issue Jun 18, 2021 · 7 comments · Fixed by nuxt/framework#239
Closed

Set Vite's root cwd() instead of .nuxt #10981

antfu opened this issue Jun 18, 2021 · 7 comments · Fixed by nuxt/framework#239

Comments

@antfu
Copy link
Member

antfu commented Jun 18, 2021

Currently the Vite mode forced the root set to .nuxt, while there is a lot of plugins relaying on the vite's root option, this would make them not compatible with nuxt without hacks (or indeed introduces more friction to users). If this is not something we can't overcome, I'd think it's better to keep Vite's root option resolve to current folder.

@pi0
Copy link
Member

pi0 commented Jun 18, 2021

Hi @antfu. Apart from vite-plugin-components mentioned in nuxt/framework#239, do you have any other plugin examples? Nuxt DX is via its own ecosystem modules. So naturally, we cannot support some webpack/vite DX plugins without proper nuxt integration.

@antfu
Copy link
Member Author

antfu commented Jun 18, 2021

I would say most of the vite plugins replying on the root options when they need to deal with file system. And I believe most of them does not expose a separate root options to override it, this could make the nuxt integration also harder (for those wrapping vite plugins). For example

  • vite-plugin-components
  • vite-plugin-windicss
  • vite-plugin-pages
  • so on.

So naturally, we cannot support some webpack/vite DX plugins without proper nuxt integration.

Totally agreed. But I would expect most of vite plugin just works without hacks right?

So, if there is no big reason / technical barrier, I would think it's worth to align with the default behaviour of vite.

@pi0
Copy link
Member

pi0 commented Jun 18, 2021

Totally agreed. But I would expect most of vite plugin just works without hacks right?

It depends. Nuxt features, conventions and integrations like components, pages, and windicss are added as nuxt modules and we don't suggest people adding a vite or webpack plugin directly to the project . But use nuxt modules (~nuxt plugins) that ensures it keeps working with nuxt regardless of chosen bundler :)

Shorter: There is no guarantee every webpack/vite plugin works with nuxt out of the box without module integration.

So, if there is no big reason / technical barrier

At least currently, there is:

  • Nuxt modules are not supposed to write anywhere other than buildDir (cli is exception)
  • For fixing we need to resolve buildDir relative to rootDir (.nuxt can also be in node_modules/.cache/nuxt) or configured being somewhere else unless we deprecate the option
  • fs sandbox in vite 2.3+ has limitations (see feat: vite 2.3 vite#138)

If at some point, we could entirely replace .nuxt with a virtual filesystem for generated templates (including vite's entry), we can safely move root to rootDir :) (still terms apply that vite modules should not directly being added nor write outside buildDir)

@antfu
Copy link
Member Author

antfu commented Jun 18, 2021

It's not really about write outside for buildDir, it's more about path resolving. For example, in vite-plugin-windicss, if we have the following config:

{
  extract: {
    include: ['src/**/*.vue']
  }
}

the scanning will be resolved incorrectly to /project/.nuxt/src/**/*.vue instead of /project/src/**/*.vue which I think, would confused users. While this might be an inappropriate example, as we do exposed internal APIs for nuxt-windicss to utilize, if someone want to make a nuxt module combining a vite plugin and a webpack plugin, they will have a hard time if the plugin replying on vite's root without exposing the override.

(in the vite-plugin-compoents case it's just and optional feature to enable TS support for auto imported components, just testing it when trying to make an app with Nuxt 3. I would expected to see @nuxt/components support this feature in the future, but that's just another story)

for fixing we need to resolve buildDir relative to rootDir

We could use /@fs/ directly without resolving the relative.

fs sandbox in vite 2.3+ has limitations

We are trying to solve this properly without user to explicit configure it for the most of the time, while being safe by default: vitejs/vite#3784

@pi0
Copy link
Member

pi0 commented Jun 18, 2021

As I said, we cannot consider what conventions vite or vite plugins expect (likewise for webpack). Nuxt is only using vite as bundler/transpiler not depending on its plugins for DX. (added dts to #11026).

Blocker for changing vite's rootDir, is when we can safely use the full path to entrypoint and sandbox issues are solved (thanks for ref to vitejs/vite#3784)

We could use /@fs/ directly without resolving the relative.

I see. Still .nuxt shall not be hardcoded (nuxt/framework#239 (comment), nuxt/framework#239 (comment))

@antfu
Copy link
Member Author

antfu commented Jun 19, 2021

Yeah, I could fix the hardcoded path next week. Or if you don't want it, I can also close it and leave to future iterations.

@pi0
Copy link
Member

pi0 commented Jun 21, 2021

PR is nice if fixing hardcode / relative resolution between rootDir/buildDir issue :)

pi0 referenced this issue in nuxt/framework Jul 15, 2021
@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe transferred this issue from nuxt/framework Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants