-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
alias '@' to path.resolve(__dirname, './src') is not working #279
Comments
Vite alias don't work like webpack does. |
@remyz17 const path = require('path');
const srcPath = path.resolve(__dirname, './src');
module.exports = {
resolvers: [{
alias (id) {
if (id.startsWith('@/')) {
return path.join(srcPath, id.slice(2));
}
}
}],
}; |
I guess you need to implement this 2 functions. Take a look at this code |
i tried to do it and it's not fully working.
I noticed that we can not use @/ because it’s recognised as a node module so I used /@/ |
@remyz17 |
Send your vite config so i can take a look |
@remyz17 // vite.config.ts
import * as path from 'path';
import * as reactPlugin from 'vite-plugin-react';
import type { UserConfig } from 'vite';
const srcPath = path.resolve(__dirname, './src');
const config: UserConfig = {
jsx: 'react',
alias: {
'exenv': 'exenv-esm',
'prop-types': 'es-react/prop-types.js',
},
resolvers: [{
fileToRequest (filePath) {
console.log('@@@', filePath);
if (filePath.startsWith(srcPath)) {
return `/@/${path.relative(srcPath, filePath)}`;
}
},
requestToFile (publicPath) {
if (publicPath.startsWith('/@/')) {
return path.join(srcPath, publicPath.replace(/^\/@\//, ''));
}
},
}],
root: './src',
plugins: [reactPlugin],
};
export default config; |
I think we probably will eventually provide an easier way to alias src directories. If your resolver is not working, it's better to provide a full reproduction instead of a single config file. |
btw you don't need an alias like that... you can just do |
@yyx990803 |
It's a bug. It's fixed in 801951e but it also adds an easier way to alias a directory so you can just do // vite.config.js
module.exports = {
alias: {
'/@/': path.resolve(__dirname, './src')
}
} |
@yyx990803 |
You have to import as Note Vite is not a bundler - it's a dev server. Any request not starting with This also means you can NOT directly import from fs paths like in webpack: import foo from '/some/fs/path/foo.js' This doesn't make sense in Vite. So anything starting with |
Heads up I was porting an app and |
Hi, has this been solved? I'm using Vue v3. and I've tried the alias in the vite.config.js, added the resolvers, nothing works.
The only path that works is './src/assets/logo.png', but when I build for production I don't have the /src folder. |
The Asset Handling section of the vite documentation is probably what you're looking for. Your logic is flawed (I think) since vite doesn't get the chance to transform import logo from '/@/assets/logo.png'
import logoSmall from '/@/assets/logoSmall.png'
// ...
computed: { urlLogo() { return this.collapsed ? smallLogo : logo; } }
// ... This solution assumes that you have the alias correctly configured. Make sure to read through Alias Behavior Change. |
Thanks @jsmith, Your solution worked well and I learnt something new. Thanks |
import vue from '@vitejs/plugin-vue'
export default {
alias: {
'/@': path.resolve(__dirname, './src')
},
plugins: [vue()]
} does not work |
I think you are just missing the Edit: I also used Edit2: You could also use the find and replace option to alias the const path = require('path');
import vue from '@vitejs/plugin-vue'
export default {
alias: [
{find: "@", replacement: path.resolve(__dirname, 'src')}
],
optimizeDeps: {
include: [
"javascript-time-ago/locale/de"
],
},
plugins: [vue()]
}; then again you don't need the leading slash when importing. Edit 3: Since vite 2.0.1 introduces the const path = require('path');
import vue from '@vitejs/plugin-vue'
export default {
resolve: {
alias: [
{find: "@", replacement: path.resolve(__dirname, 'src')}
],
optimizeDeps: {
include: [
"javascript-time-ago/locale/de"
],
},
},
plugins: [vue()]
}; |
Thanks @theovier |
With the |
Having trouble with aliases here after upgrading to
import path from 'path'
import { defineConfig } from 'vite'
export default defineConfig({
resolve: {
alias: [
{
find: '@/',
replacement: path.resolve(__dirname, './src')
}
]
},
optimizeDeps: {
include: ['lodash']
}
}) And then it errors in places such as:
The error message says I've tried the various |
@ndom91 Does it work when you delete the |
@Enixam yes that did the trick! |
@ndom91 Standard settings without the |
2.0.5 is not work |
@aa4790139 @ginzatron, please do not comment on closed issues. |
@patak-js I think that there should be comments because of developers that have problem with migration of vite landing on this page. I wold be great if there will be published solution of this problem or direct link to other place with solution. |
It makes sense to post a comment with a clear solution for others to find, that is not a problem 👍🏼 But asking questions against a closed bug report is not helpful because the context refers to an older version of Vite, and it doesn't allow proper tracking of concrete bugs in newer versions. Issues are not intended to be used for discussions, we have better platforms for that with GitHub Discussion and Vite Land. If a good solution to something that is not clear in this issue is identified there, it is a good idea to link it from here for later reference. |
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
// 路径代理
resolve: {
alias: [
{ find: '@', replacement: '/src' },
{ find: 'views', replacement: '/src/views' },
{ find: 'components', replacement: '/src/components' },
]
}
}) |
It's working but TS flag this as an error. |
|
Use // @ts-ignore |
TS is still flags the import. I don't know if this is the right way to do it, I already solve the issue by declaring this to tsconfig.json
|
Can't we just define a resolver that resolves src folder so we don't have to use aliases ? MyViteProject > src > components > Button how can resolve src folder in vite.config.js ? |
I solve it by declaring this to tsconfig.json
The error is gone and did not break any component in build. |
For what it's worth, adding the following to tsconfig.json makes typescript happy:
BUT you still need to also add corresponding:
to vite.config.js to make vite happy. Maybe that's obvious to most, but it wasn't to me. ;-) |
Still no working for me with It works when This is my vite.config.js: // https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()],
resolve: {
alias: [
{ find: /^@(?=\/)/, replacement: path.resolve(__dirname, './src') }
],
},
}) |
Mine is working fine with this config.
|
Thanks @csulit for your reply! Reviewing my code I've figured out that my scenario is a bit different... I mean: I've tested that using the alias works like a charm when index.html is on the root of the project, but does not work when moving index.html into src (using Also noticed that when moving the index.html into Using this jsconfig.json: {
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
}
}
} Gets folloging sugestions: But when using root slash: Any ideas how to use this alias moving index.html into src folder?
export default defineConfig({
plugins: [reactRefresh()],
resolve: {
alias: [
{ find: /^@(?=\/)/, replacement: path.resolve(__dirname, './src') },
],
},
}) |
This worked for me! Using vite with Svelte and Typescript and this setup works perfectly with VSCode importing my files automatically. Thanks a bunch slumtrimpet |
Working for me |
Is there a way of specifying
You can write:
This looks much cleaner. How to achieve the same in Vite? |
idea.config.js System.config({
paths: {
'@/*': './src/*',
}
}) |
The following is working for me:
|
vite 1 verson vite.config.js
|
This issue has been locked since it has been closed for more than 14 days. If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion. |
Describe the bug
Alias
{ '@': path.resolve(__dirname, './src') }
is not working.Reproduction
project structure:
vite.config.js:
src/index.js
Got error in vite:
System Info
vite
version: 0.17.0The text was updated successfully, but these errors were encountered: