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

Possibility to define additional node paths #8439

Closed
4 tasks done
KingSora opened this issue Jun 2, 2022 · 10 comments
Closed
4 tasks done

Possibility to define additional node paths #8439

KingSora opened this issue Jun 2, 2022 · 10 comments

Comments

@KingSora
Copy link

KingSora commented Jun 2, 2022

Clear and concise description of the problem

I've tried to replace my current bundler (rollup) with vite and got stuck with path resolution.
My setup is quite basic. I'm using absolute import paths in my project:

import { something } from 'someDir/someFile'; 

The relative file system path of this import would be ./src/someDir/someFile.

As you can see, I can omit the ./src/ part here. I did this by using the moduleDirectories prop from the @rollup/plugin-node-resolve. Unfortunately vite isn't providing this functionality out of the box which is in my opinion a key feature for every bundler.

Bundlers which are providing this feature:

This is also supported by Eslint with the eslint-import-resolver-node plugin (moduleDirectory option)

Suggested solution

Provide this feature "natively" in the vite config. Something like resolve.moduleDirectories or resolve.nodePaths.

Alternative

It seems like you can use the @rollup/plugin-node-resolve plugin here, but it still feels weird to have this key feature not supported. Additionally since vite is doing quite some resolve logic itself I'm in fear of something not working properly by using the plugin.

You could also use aliases, but is this the "right" way? - With this solution I would be forced to rewrite all of my imports.

Additional context

No response

Validations

@sapphi-red
Copy link
Member

If you set root to fileURLToPath(new URL('./src/', import.meta.url)), it enables you to write ./src/someDir/someFile as /someDir/someFile.
Would this be enough for you?

@KingSora
Copy link
Author

KingSora commented Jun 2, 2022

@sapphi-red
The documentation says:

Project root directory (where index.html is located). Can be an absolute path, or a path relative to the location of the config file itself.

Which is not the case in my setup.. the root directory is not ./src in my case

@sapphi-red
Copy link
Member

May I ask you why you don't want to move root directory to ./src? IIUC moving index.html inside ./src will solve it.

@KingSora
Copy link
Author

KingSora commented Jun 2, 2022

The question is just whether this property was made for this purpose, or whether its just a side effect of it.

The default vue template for example has this config:

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
});

To achieve the effect I want to have, its using an alias.

@bluwy
Copy link
Member

bluwy commented Jun 13, 2022

The equivalent of the rollup/esbuild/webpack feature noted in Vite is resolve.alias. If you want to use someDir/someFile, you'd have to alias someDir (and any directories under src manually). otherwise the more convenient way is to alias src as @ like in the example above. I don't think Vite should provide another way of resolving though.

@KingSora
Copy link
Author

@bluwy Thanks for your reply!
As you can imagine, aliasing every directory under ./src manually is not really the way to go for me.
Can you elaborate on why you think Vite shouldn't provide any other way of resolving? (Do you think this should be handled by plugins or do you think the current feature set is enough?)..

I would have even more usecases for the moduleDirectories feature I described.

@bluwy
Copy link
Member

bluwy commented Jun 13, 2022

Can you elaborate on why you think Vite shouldn't provide any other way of resolving?

Mostly because resolve.alias already covers a lot of base, and Vite is trying to minimize the config surface area.

(Do you think this should be handled by plugins or do you think the current feature set is enough?)..

I think both. And with plugins like https://github.com/Subwaytime/vite-aliases should cover the heavylifting too.

Usually I'd alias src as @ and call it a day, as there's no need to add more complexity for code preference. But the option is always available with plugins 🙂

@bluwy
Copy link
Member

bluwy commented Apr 1, 2023

Closing as we don't want to introduce new options that do similar things.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Apr 1, 2023
@jamesdh
Copy link

jamesdh commented Apr 5, 2023

For those of us trying to make the migration from create-react-app where having absolute imports without any kind of alias was easy and widely used, this is painful. Yes I can create an @ alias but then I'll have to modify our entire codebase to include that where necessary.

I feel like I've gotta be missing something here.

@jamesdh
Copy link

jamesdh commented Apr 5, 2023

For anyone finding this issue while migrating away from CRA, it appears vite-tsconfig-paths solves the issue of alias-free absolute imports.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants