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

Question: Alias/resolver mappings with '*' supported? #300

Closed
m4rvr opened this issue May 29, 2020 · 7 comments
Closed

Question: Alias/resolver mappings with '*' supported? #300

m4rvr opened this issue May 29, 2020 · 7 comments
Labels
enhancement New feature or request

Comments

@m4rvr
Copy link
Contributor

m4rvr commented May 29, 2020

Is there a way to use alias configuration similar to the paths of tsconfig.json or do I need to add it for each alias?

This possible?

{
  alias: {
    '@scope/*': '/packages/*/src'
  }
}

Current approach:

{
  alias: {
    '@scope/pkg1': '/packages/pkg1/src',
    '@scope/pkg2': '/packages/pkg2/src'
  }
}

And how would I do that with the resolvers?

Thanks!

@yyx990803
Copy link
Member

yyx990803 commented May 29, 2020

See type definition of Resolver

You can do something like:

const scopeRegex = /^\/@scope\/(\w+)\//

const myResolver = {
  requestToFile(req) {
    const match = req.match(scopeRegex)
    if (match) {
      const pkg = match[1]
      return path.resolve(__dirname, 'packages', pkg, 'src', req.replace(scopeRegex, ''))
    }
  },
  fileToRequest(file) {
    // reverse logic
  }
}

module.exports = {
  resolvers: [myResolver]
}

We may add wildcard support eventually but as of now this is what you have to do. Might be easier to just duplicate a few aliases.

Also note: path aliases must start with / and end with /. Otherwise it's considered a package name alias.

@yyx990803 yyx990803 reopened this May 29, 2020
@yyx990803 yyx990803 added the enhancement New feature or request label May 29, 2020
@m4rvr
Copy link
Contributor Author

m4rvr commented May 29, 2020

@yyx990803 Thank you that really helped me!

@aleclarson
Copy link
Member

aleclarson commented Aug 5, 2020

I've published vite-tsconfig-paths to solve this problem:
https://github.com/aleclarson/vite-tsconfig-paths

@pepsighan
Copy link

Also note: path aliases must start with / and end with /.

This conflicts with tsconfig.json's paths. It does not allow any absolute paths. This limits the usage of alias within the limits of Javascript.

@RoB8080
Copy link

RoB8080 commented Oct 15, 2020

Also note: path aliases must start with / and end with /.

This conflicts with tsconfig.json's paths. It does not allow any absolute paths. This limits the usage of alias within the limits of Javascript.

Try this.

// vite.config.ts
alias: {
    '/@/': path.resolve(__dirname, 'src'),
},
resolvers: [
    {
        alias(id: string) {
            return id.replace(/^@\//, '/@/') // add slash to particular id, then vite won't resolve it as a module
        },
    },
],
// tsconfig.json
"paths": {
    "@/*": [
        "./src/*"
    ],
},

This trick can allow you to use relative path as alias.

@xieyezi
Copy link

xieyezi commented Nov 12, 2020

@RoB8080 thanks

@underfin
Copy link
Member

underfin commented Jan 6, 2021

Work on 2.0, see https://vitejs.dev/config/#alias

@underfin underfin closed this as completed Jan 6, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants