Skip to content

Commit

Permalink
feat: add support for ${configDir} in path (#393)
Browse files Browse the repository at this point in the history
* feat: add support for configDir

* feat: add support for configDir

* feat: add support for configDir - added example

* feat: add support for configDir
  • Loading branch information
ThibautMarechal authored Dec 11, 2024
1 parent acf627d commit 97e19c0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/react/src/components/HelloWorld.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useState } from 'react'
import { useCount } from '@/hooks/useCount'

interface Props {
count?: number
}

function HelloWorld(props: Props) {
const [count, setCount] = useState(props.count || 0)
const [count, setCount] = useCount(props.count)

return (
<div className="hello-world">
Expand Down
5 changes: 5 additions & 0 deletions examples/react/src/hooks/useCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useState } from 'react'

export function useCount(initilaValue = 0) {
return useState(initilaValue)
}
1 change: 1 addition & 0 deletions examples/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ReactDOM as MyReactDOM } from 'react'

export { HelloWorld }
export { default as App } from './App'
export { useCount } from '@/hooks/useCount'
export * from './modules'

export function test(dom: MyReactDOM) {}
4 changes: 2 additions & 2 deletions examples/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
],
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
"@/*": ["${configDir}/src/*"]
},
"lib": ["esnext", "dom", "dom.iterable"]
},
"include": ["src", "*.d.ts"]
"include": ["${configDir}/src", "*.d.ts"]
}
28 changes: 23 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
queryPublicPath,
removeDirIfEmpty,
resolve,
resolveConfigDir,
runParallel,
setModuleResolution,
toCapitalCase,
Expand Down Expand Up @@ -286,7 +287,14 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
if (!outDirs) {
outDirs = options.outDir
? ensureArray(options.outDir).map(d => ensureAbsolute(d, root))
: [ensureAbsolute(content?.raw.compilerOptions?.outDir || 'dist', root)]
: [
ensureAbsolute(
content?.raw.compilerOptions?.outDir
? resolveConfigDir(content.raw.compilerOptions.outDir, root)
: 'dist',
root
)
]
}

const {
Expand All @@ -299,7 +307,13 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {

if (pathsToAliases && baseUrl && paths) {
aliases.push(
...parseTsAliases(ensureAbsolute(baseUrl, configPath ? dirname(configPath) : root), paths)
...parseTsAliases(
ensureAbsolute(
resolveConfigDir(baseUrl, root),
configPath ? dirname(configPath) : root
),
paths
)
)
}

Expand All @@ -309,11 +323,15 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
defaultGlob: string | string[]
) => {
if (rootGlobs?.length) {
return ensureArray(rootGlobs).map(glob => normalizeGlob(ensureAbsolute(glob, root)))
return ensureArray(rootGlobs).map(glob =>
normalizeGlob(ensureAbsolute(resolveConfigDir(glob, root), root))
)
}

return ensureArray(tsGlobs?.length ? tsGlobs : defaultGlob).map(glob =>
normalizeGlob(ensureAbsolute(glob, configPath ? dirname(configPath) : root))
normalizeGlob(
ensureAbsolute(resolveConfigDir(glob, root), configPath ? dirname(configPath) : root)
)
)
}

Expand Down Expand Up @@ -356,7 +374,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
}

publicRoot = compilerOptions.rootDir
? ensureAbsolute(compilerOptions.rootDir, root)
? ensureAbsolute(resolveConfigDir(compilerOptions.rootDir, root), root)
: compilerOptions.composite && compilerOptions.configFilePath
? dirname(compilerOptions.configFilePath as string)
: queryPublicPath(
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export function slash(p: string): string {
return p.replace(windowsSlashRE, '/')
}

export function resolveConfigDir(path: string, configDir: string) {
return path.replace('${configDir}', configDir)
}

export function normalizePath(id: string): string {
return posix.normalize(slash(id))
}
Expand Down
9 changes: 9 additions & 0 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
normalizePath,
parseTsAliases,
queryPublicPath,
resolveConfigDir,
toCapitalCase,
unwrapPromise
} from '../src/utils'
Expand Down Expand Up @@ -85,6 +86,14 @@ describe('utils tests', () => {
expect(ensureAbsolute('/vite-plugin-dts', root)).toBe('/vite-plugin-dts')
})

it('test: resolveConfigDir', () => {
const root = normalizePath(resolve(__dirname, '..'))

expect(resolveConfigDir('', root)).toBe('')
expect(resolveConfigDir('./some/path', root)).toBe('./some/path')
expect(resolveConfigDir('${configDir}/some/path', root)).toBe(`${root}/some/path`)
})

it('test: ensureArray', () => {
expect(ensureArray(1)).toEqual([1])
expect(ensureArray({ a: 1 })).toEqual([{ a: 1 }])
Expand Down

0 comments on commit 97e19c0

Please sign in to comment.