-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
08ace44
commit 7038572
Showing
4 changed files
with
101 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`unit:utils/resolvePaths > should return code with path aliases resolved 1`] = ` | ||
"import type { ResolvePathsOptions } from '../interfaces' | ||
import * as internal from '../internal' | ||
import * as mlly from '@flex-development/mlly' | ||
import type { CompilerOptions } from '@flex-development/tsconfig-types' | ||
import loadCompilerOptions from './load-compiler-options'" | ||
`; |
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,30 @@ | ||
/** | ||
* @file Unit Tests - resolvePaths | ||
* @module tsconfig-utils/utils/tests/unit/resolvePaths | ||
*/ | ||
|
||
import * as mlly from '@flex-development/mlly' | ||
import { dedent } from 'ts-dedent' | ||
import testSubject from '../resolve-paths' | ||
|
||
describe('unit:utils/resolvePaths', () => { | ||
it('should return code with path aliases resolved', async () => { | ||
// Arrange | ||
const code: string = dedent` | ||
import type { ResolvePathsOptions } from '#src/interfaces' | ||
import * as internal from '#src/internal' | ||
import * as mlly from '@flex-development/mlly' | ||
import type { CompilerOptions } from '@flex-development/tsconfig-types' | ||
import loadCompilerOptions from './load-compiler-options' | ||
` | ||
|
||
// Act | ||
const result = await testSubject(code, { | ||
ext: '', | ||
parent: mlly.toURL('src/utils/resolve-paths.ts') | ||
}) | ||
|
||
// Expect | ||
expect(result).toMatchSnapshot() | ||
}) | ||
}) |
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,61 @@ | ||
/** | ||
* @file Utilities - resolvePaths | ||
* @module tsconfig-utils/utils/resolvePaths | ||
*/ | ||
|
||
import type { ResolvePathsOptions } from '#src/interfaces' | ||
import * as internal from '#src/internal' | ||
import * as mlly from '@flex-development/mlly' | ||
import type { CompilerOptions } from '@flex-development/tsconfig-types' | ||
import loadCompilerOptions from './load-compiler-options' | ||
|
||
/** | ||
* Resolves path aliases in the given piece of source `code`. | ||
* | ||
* @async | ||
* | ||
* @param {string} code - Code to evaluate | ||
* @param {ResolvePathsOptions} options - Path alias resolution options | ||
* @return {Promise<string>} `code` unmodified or with path aliases resolved | ||
*/ | ||
const resolvePaths = async ( | ||
code: string, | ||
options: ResolvePathsOptions | ||
): Promise<string> => { | ||
const { | ||
absolute = false, | ||
condition, | ||
conditions, | ||
ext, | ||
extensions, | ||
file = internal.isFile, | ||
parent, | ||
preserveSymlinks, | ||
read = internal.readFile, | ||
tsconfig = mlly.toURL('tsconfig.json') | ||
} = options | ||
|
||
/** | ||
* User compiler options. | ||
* | ||
* @const {CompilerOptions} compilerOptions | ||
*/ | ||
const compilerOptions: CompilerOptions = loadCompilerOptions(tsconfig, { | ||
file, | ||
read | ||
}) | ||
|
||
return mlly.resolveAliases(code, { | ||
absolute, | ||
aliases: compilerOptions.paths, | ||
condition, | ||
conditions, | ||
cwd: compilerOptions.baseUrl, | ||
ext, | ||
extensions, | ||
parent, | ||
preserveSymlinks | ||
}) | ||
} | ||
|
||
export default resolvePaths |