Skip to content

Commit 97e19c0

Browse files
feat: add support for ${configDir} in path (#393)
* feat: add support for configDir * feat: add support for configDir * feat: add support for configDir - added example * feat: add support for configDir
1 parent acf627d commit 97e19c0

File tree

7 files changed

+46
-9
lines changed

7 files changed

+46
-9
lines changed

examples/react/src/components/HelloWorld.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useState } from 'react'
1+
import { useCount } from '@/hooks/useCount'
22

33
interface Props {
44
count?: number
55
}
66

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

1010
return (
1111
<div className="hello-world">

examples/react/src/hooks/useCount.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { useState } from 'react'
2+
3+
export function useCount(initilaValue = 0) {
4+
return useState(initilaValue)
5+
}

examples/react/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ReactDOM as MyReactDOM } from 'react'
44

55
export { HelloWorld }
66
export { default as App } from './App'
7+
export { useCount } from '@/hooks/useCount'
78
export * from './modules'
89

910
export function test(dom: MyReactDOM) {}

examples/react/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
],
2222
"baseUrl": ".",
2323
"paths": {
24-
"@/*": ["src/*"]
24+
"@/*": ["${configDir}/src/*"]
2525
},
2626
"lib": ["esnext", "dom", "dom.iterable"]
2727
},
28-
"include": ["src", "*.d.ts"]
28+
"include": ["${configDir}/src", "*.d.ts"]
2929
}

src/plugin.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
queryPublicPath,
2727
removeDirIfEmpty,
2828
resolve,
29+
resolveConfigDir,
2930
runParallel,
3031
setModuleResolution,
3132
toCapitalCase,
@@ -286,7 +287,14 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
286287
if (!outDirs) {
287288
outDirs = options.outDir
288289
? ensureArray(options.outDir).map(d => ensureAbsolute(d, root))
289-
: [ensureAbsolute(content?.raw.compilerOptions?.outDir || 'dist', root)]
290+
: [
291+
ensureAbsolute(
292+
content?.raw.compilerOptions?.outDir
293+
? resolveConfigDir(content.raw.compilerOptions.outDir, root)
294+
: 'dist',
295+
root
296+
)
297+
]
290298
}
291299

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

300308
if (pathsToAliases && baseUrl && paths) {
301309
aliases.push(
302-
...parseTsAliases(ensureAbsolute(baseUrl, configPath ? dirname(configPath) : root), paths)
310+
...parseTsAliases(
311+
ensureAbsolute(
312+
resolveConfigDir(baseUrl, root),
313+
configPath ? dirname(configPath) : root
314+
),
315+
paths
316+
)
303317
)
304318
}
305319

@@ -309,11 +323,15 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
309323
defaultGlob: string | string[]
310324
) => {
311325
if (rootGlobs?.length) {
312-
return ensureArray(rootGlobs).map(glob => normalizeGlob(ensureAbsolute(glob, root)))
326+
return ensureArray(rootGlobs).map(glob =>
327+
normalizeGlob(ensureAbsolute(resolveConfigDir(glob, root), root))
328+
)
313329
}
314330

315331
return ensureArray(tsGlobs?.length ? tsGlobs : defaultGlob).map(glob =>
316-
normalizeGlob(ensureAbsolute(glob, configPath ? dirname(configPath) : root))
332+
normalizeGlob(
333+
ensureAbsolute(resolveConfigDir(glob, root), configPath ? dirname(configPath) : root)
334+
)
317335
)
318336
}
319337

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

358376
publicRoot = compilerOptions.rootDir
359-
? ensureAbsolute(compilerOptions.rootDir, root)
377+
? ensureAbsolute(resolveConfigDir(compilerOptions.rootDir, root), root)
360378
: compilerOptions.composite && compilerOptions.configFilePath
361379
? dirname(compilerOptions.configFilePath as string)
362380
: queryPublicPath(

src/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export function slash(p: string): string {
2222
return p.replace(windowsSlashRE, '/')
2323
}
2424

25+
export function resolveConfigDir(path: string, configDir: string) {
26+
return path.replace('${configDir}', configDir)
27+
}
28+
2529
export function normalizePath(id: string): string {
2630
return posix.normalize(slash(id))
2731
}

tests/utils.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
normalizePath,
1515
parseTsAliases,
1616
queryPublicPath,
17+
resolveConfigDir,
1718
toCapitalCase,
1819
unwrapPromise
1920
} from '../src/utils'
@@ -85,6 +86,14 @@ describe('utils tests', () => {
8586
expect(ensureAbsolute('/vite-plugin-dts', root)).toBe('/vite-plugin-dts')
8687
})
8788

89+
it('test: resolveConfigDir', () => {
90+
const root = normalizePath(resolve(__dirname, '..'))
91+
92+
expect(resolveConfigDir('', root)).toBe('')
93+
expect(resolveConfigDir('./some/path', root)).toBe('./some/path')
94+
expect(resolveConfigDir('${configDir}/some/path', root)).toBe(`${root}/some/path`)
95+
})
96+
8897
it('test: ensureArray', () => {
8998
expect(ensureArray(1)).toEqual([1])
9099
expect(ensureArray({ a: 1 })).toEqual([{ a: 1 }])

0 commit comments

Comments
 (0)