Skip to content

Commit

Permalink
fix: use baseUrl when resolving paths arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Nov 30, 2022
1 parent c9b2655 commit 9fcd4b8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Expand Up @@ -125,7 +125,10 @@ export default (opts: PluginOptions = {}): Plugin => {

let resolveId: Resolver
if (paths) {
const pathMappings = resolvePathMappings(paths, dirname(configPath))
const pathMappings = resolvePathMappings(
paths,
options.baseUrl ?? dirname(configPath)
)
const resolveWithPaths: Resolver = async (viteResolve, id, importer) => {
for (const mapping of pathMappings) {
const match = id.match(mapping.pattern)
Expand Down
4 changes: 2 additions & 2 deletions src/mappings.ts
Expand Up @@ -7,7 +7,7 @@ export type PathMapping = {

export function resolvePathMappings(
paths: Record<string, string[]>,
root: string
base: string
) {
// If a module name can be matched with multiple patterns then pattern
// with the longest prefix will be picked.
Expand All @@ -20,7 +20,7 @@ export function resolvePathMappings(
pattern = escapeStringRegexp(pattern).replace(/\*/g, '(.+)')
resolved.push({
pattern: new RegExp('^' + pattern + '$'),
paths: relativePaths.map((relativePath) => resolve(root, relativePath)),
paths: relativePaths.map((relativePath) => resolve(base, relativePath)),
})
}
return resolved
Expand Down

0 comments on commit 9fcd4b8

Please sign in to comment.