| Server IP : 159.203.156.69 / Your IP : 216.73.216.37 Web Server : nginx/1.24.0 System : Linux main-ubuntu 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/tanviranik.com/node_modules/vite-tsconfig-paths/src/ |
Upload File : |
import { resolve } from 'node:path'
export type PathMapping = {
pattern: RegExp
paths: string[]
}
export function resolvePathMappings(
paths: Record<string, string[]>,
base: string
) {
// If a module name can be matched with multiple patterns then pattern
// with the longest prefix will be picked.
const sortedPatterns = Object.keys(paths).sort(
(a: string, b: string) => getPrefixLength(b) - getPrefixLength(a)
)
const resolved: PathMapping[] = []
for (let pattern of sortedPatterns) {
const relativePaths = paths[pattern]
pattern = escapeStringRegexp(pattern).replace(/\*/g, '(.+)')
resolved.push({
pattern: new RegExp('^' + pattern + '$'),
paths: relativePaths.map((relativePath) => resolve(base, relativePath)),
})
}
return resolved
}
function getPrefixLength(pattern: string): number {
const prefixLength = pattern.indexOf('*')
return pattern.substr(0, prefixLength).length
}
// Adapted from:
// https://github.com/sindresorhus/escape-string-regexp/blob/ba9a4473850cb367936417e97f1f2191b7cc67dd/index.js
//
// MIT License
//
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://
// sindresorhus.com)
//
function escapeStringRegexp(string: string) {
// Escape characters with special meaning either inside or outside
// character sets. Use a simple backslash escape when it’s always
// valid, and a `\xnn` escape when the simpler form would be
// disallowed by Unicode patterns’ stricter grammar.
return string.replace(/[|\\{}()[\]^$+?.]/g, '\\$&').replace(/-/g, '\\x2d')
}