-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
- Loading branch information
1 parent
6e80985
commit 5f0df84
Showing
3 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { createPathsMatcher, type TsConfigResult } from 'get-tsconfig'; | ||
import type { Plugin } from 'rollup'; | ||
|
||
const name = 'resolve-tsconfig-paths'; | ||
|
||
const isRelative = (filePath: string) => filePath[0] === '.'; | ||
const isAbsolute = (filePath: string) => filePath[0] === '/' || /^[\s\S]:/.test(filePath); | ||
const isImports = (filePath: string) => filePath[0] === '#'; | ||
|
||
export const resolveTsconfigPaths = ( | ||
tsconfig: TsConfigResult, | ||
): Plugin => { | ||
const pathsMatcher = createPathsMatcher(tsconfig); | ||
if (!pathsMatcher) { | ||
return { | ||
name, | ||
}; | ||
} | ||
|
||
return { | ||
name, | ||
async resolveId(id, importer, options) { | ||
if ( | ||
!importer | ||
|| isRelative(id) | ||
|| isAbsolute(id) | ||
|| isImports(id) | ||
|| id.startsWith('\0') | ||
) { | ||
return null; | ||
} | ||
|
||
const possiblePaths = pathsMatcher(id); | ||
for (const tryPath of possiblePaths) { | ||
const resolved = await this.resolve( | ||
tryPath, | ||
importer, | ||
{ | ||
skipSelf: true, | ||
...options, | ||
}, | ||
); | ||
if (resolved) { | ||
return resolved; | ||
} | ||
} | ||
|
||
return null; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters