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[],