dividab / tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.
MIT License
1.8k stars 100 forks source link

Exact patterns should match before asterisk patterns #155

Open eirikurn opened 3 years ago

eirikurn commented 3 years ago

With a config like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "app/utils": ["vendor/utils"],
      "app/*": ["src/*"]
    }
  }
}

When someone imports app/utils, it should first do an exact match and resolve to "vendor/utils" before trying asterisk patterns sorted by the longest prefix. See microsoft/typescript. Note that the order of the patterns should not matter.

Currently, tsconfig-paths only finds the first match in the order of longest prefix. Patterns that don't have an asterisk, have a prefix length of 0, so they get matched after all patterns with asterisks in them.

phil-lgr commented 3 years ago

What about supporting what webpack does?

https://webpack.js.org/configuration/resolve/#resolvealias

A trailing $ can also be added to the given object's keys to signify an exact match:

const path = require('path');

module.exports = {
  //...
  resolve: {
    alias: {
      xyz$: path.resolve(__dirname, 'path/to/file.js'),
    },
  },
};