From a7bab4d1e23d791dd0d0d91f4cc935032d4c6986 Mon Sep 17 00:00:00 2001 From: qmhc <40221744+qmhc@users.noreply.github.com> Date: Thu, 21 Sep 2023 15:17:15 +0800 Subject: [PATCH] fix: ensure relative to config path if not specify include/exclude fix #272 --- README.md | 4 ++-- README.zh-CN.md | 4 ++-- src/plugin.ts | 22 +++++++++++++++++----- src/types.ts | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 361517b..062c096 100644 --- a/README.md +++ b/README.md @@ -222,9 +222,9 @@ export interface PluginOptions { staticImport?: boolean, /** - * Override `include` glob + * Override `include` glob (relative to root) * - * Defaults to `include` property of tsconfig.json + * Defaults to `include` property of tsconfig.json (relative to tsconfig.json located) */ include?: string | string[], diff --git a/README.zh-CN.md b/README.zh-CN.md index e0bcba6..c9a952c 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -222,9 +222,9 @@ export interface PluginOptions { staticImport?: boolean, /** - * 手动设置包含路径的 glob + * 手动设置包含路径的 glob(相对于 root) * - * 默认基于 tsconfig.json 的 `include` 选项 + * 默认基于 tsconfig.json 的 `include` 选项(相对于 tsconfig.json 所在目录) */ include?: string | string[], diff --git a/src/plugin.ts b/src/plugin.ts index 20d2511..d486718 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -278,12 +278,24 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { } } - include = ensureArray(options.include ?? content?.raw.include ?? '**/*').map(normalizeGlob) - exclude = ensureArray(options.exclude ?? content?.raw.exclude ?? 'node_modules/**').map( - normalizeGlob - ) + const computeGlobs = ( + rootGlobs: string | string[] | undefined, + tsGlobs: string | string[] | undefined, + defaultGlob: string | string[] + ) => { + if (rootGlobs?.length) { + return ensureArray(rootGlobs).map(glob => normalizeGlob(ensureAbsolute(glob, root))) + } + + return ensureArray(tsGlobs?.length ? tsGlobs : defaultGlob).map(glob => + normalizeGlob(ensureAbsolute(glob, configPath ? dirname(configPath) : root)) + ) + } + + include = computeGlobs(options.include, content?.raw.include, '**/*') + exclude = computeGlobs(options.exclude, content?.raw.exclude, 'node_modules/**') - filter = createFilter(include, exclude, { resolve: root }) + filter = createFilter(include, exclude) const rootNames = Object.values(entries) .map(entry => ensureAbsolute(entry, root)) diff --git a/src/types.ts b/src/types.ts index a55f980..8b30b82 100644 --- a/src/types.ts +++ b/src/types.ts @@ -128,9 +128,9 @@ export interface PluginOptions { staticImport?: boolean, /** - * Override `include` glob + * Override `include` glob (relative to root) * - * Defaults to `include` property of tsconfig.json + * Defaults to `include` property of tsconfig.json (relative to tsconfig.json located) */ include?: string | string[],