Subscribe on changes!

Using an alias with an absolute path and outside of the root directory

avatar
May 13th 2020

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

Currently, adding an alias in vite.config.js with an absolute path or a path that is outside of the root directory isn't working.

Configuration example :

const path = require('path')

module.exports = {
  root: path.join(__dirname, 'src'), // E:\vite\src
  alias: {
    __APP_COMPONENTS__: path.join(__dirname, 'components') // An absolute path and outside of the root directory : E:\vite\components
  }
}

Whereas with Webpack's aliases, it works.

Here a repo that illustrates the case with a vite project that doesn't work and an equivalent working webpack project : https://github.com/clemvnt/vite-alias-path-issue

Is that on purpose or can we consider this possible in the future ?

avatar
May 13th 2020

Vite alias is only for aliasing of package names - it doesn't work like webpack alias.

What you want to do is possible by using resolvers instead - example: https://github.com/vuejs/vitepress/blob/master/src/utils/pathResolver.ts#L18

The reason you need to also provide a "fileToRequest" function is so that HMR can work.

avatar
May 22nd 2021

@yyx990803 the link https://github.com/vuejs/vitepress/blob/master/src/utils/pathResolver.ts#L18 is broken. Could you provide another example?