Vite: Support `@/` -> src folder alias to keep consistant with vue-cli

Created on 3 Nov 2020  路  3Comments  路  Source: vitejs/vite

Is your feature request related to a problem? Please describe.

As the comment describes, when using directory mappings, the key must start and end with a slash

https://github.com/vitejs/vite/blob/e0acfdf395391df61db219fc928ba047b3aabe91/src/node/config.ts#L93-L113

Describe the solution you'd like

Since @/ -> src folder is a convention in vue-cli, it would be great to also add it to vite. It should be technically feasible because @/xx is not a valid npm package name.

Describe alternatives you've considered

Therer is another option: /@/, which matches the current rule.

Most helpful comment

My workaround is as follows:

vite.config.ts

const pathAliasMap = {
  '@/': '/src/',
}

export default {
  resolvers: [
    {
      alias(path: string) {
        for (const [slug, res] of Object.entries(pathAliasMap)) {
          if (path.startsWith(slug)) {
            return path.replace(slug, res)
          }
        }
      },
    },
  ],
}

All 3 comments

+1

+1

My workaround is as follows:

vite.config.ts

const pathAliasMap = {
  '@/': '/src/',
}

export default {
  resolvers: [
    {
      alias(path: string) {
        for (const [slug, res] of Object.entries(pathAliasMap)) {
          if (path.startsWith(slug)) {
            return path.replace(slug, res)
          }
        }
      },
    },
  ],
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashubham picture ashubham  路  3Comments

hyingreborn picture hyingreborn  路  3Comments

shen-zhao picture shen-zhao  路  3Comments

haikyuu picture haikyuu  路  3Comments

robrich picture robrich  路  4Comments