Skip to content

Commit

Permalink
feat(utils): resolvePaths
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 7, 2023
1 parent 08ace44 commit 7038572
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utils/__snapshots__/resolve-paths.snap
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'"
`;
30 changes: 30 additions & 0 deletions src/utils/__tests__/resolve-paths.spec.ts
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()
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { default as normalizeModuleDetection } from './normalize-module-detectio
export { default as normalizeModuleResolution } from './normalize-module-resolution'
export { default as normalizeNewLine } from './normalize-new-line'
export { default as normalizeTarget } from './normalize-target'
export { default as resolvePaths } from './resolve-paths'
61 changes: 61 additions & 0 deletions src/utils/resolve-paths.ts
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

0 comments on commit 7038572

Please sign in to comment.