From a6f12dcb52d10941c41a67ca18a3da01d57a4fda Mon Sep 17 00:00:00 2001 From: qmhc <40221744+qmhc@users.noreply.github.com> Date: Mon, 30 Oct 2023 15:39:30 +0800 Subject: [PATCH] fix: make inject moduleResolution as patch for Vue fix #280 --- src/plugin.ts | 11 ++++++++--- src/utils.ts | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 31b2316..9b0c362 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -29,6 +29,7 @@ import { removeDirIfEmpty, resolve, runParallel, + setModuleResolution, toCapitalCase, tryGetPkgPath, wrapPromise @@ -251,9 +252,6 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { : undefined compilerOptions = { - // (#277) If user don't specify `moduleResolution` in top config file, - // declaration of Vue files will be inferred to `any` type. - moduleResolution: ts.ModuleResolutionKind.Node10, ...(content?.options || {}), ...(options.compilerOptions || {}), ...fixedCompilerOptions, @@ -262,6 +260,13 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { } rawCompilerOptions = content?.raw.compilerOptions || {} + if (content?.fileNames.find(name => name.endsWith('.vue'))) { + // (#277) A patch for Vue + // If user don't specify `moduleResolution` in top config file, + // declaration of Vue files will be inferred to `any` type. + setModuleResolution(compilerOptions) + } + if (!outDirs) { outDirs = options.outDir ? ensureArray(options.outDir).map(d => ensureAbsolute(d, root)) diff --git a/src/utils.ts b/src/utils.ts index 241ddba..a6f57a9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import { resolve as _resolve, dirname, isAbsolute, normalize, posix, sep } from 'node:path' import { existsSync, lstatSync, readdirSync, rmdirSync } from 'node:fs' -import typescript from 'typescript' +import ts from 'typescript' import type { CompilerOptions } from 'typescript' @@ -212,7 +212,7 @@ export function getTsConfig( extends?: string | string[] } = { compilerOptions: {}, - ...(typescript.readConfigFile(tsConfigPath, readFileSync).config ?? {}) + ...(ts.readConfigFile(tsConfigPath, readFileSync).config ?? {}) } if (tsConfig.extends) { @@ -334,3 +334,35 @@ export function findTypesPath(...pkgs: Record[]) { if (path) return path } } + +export function setModuleResolution(options: CompilerOptions) { + if (options.moduleResolution) return + + const module = + typeof options.module === 'number' + ? options.module + : options.target ?? ts.ScriptTarget.ES5 >= 2 + ? ts.ModuleKind.ES2015 + : ts.ModuleKind.CommonJS + + let moduleResolution: ts.ModuleResolutionKind + + switch (module) { + case ts.ModuleKind.CommonJS: + moduleResolution = ts.ModuleResolutionKind.Node10 + break + case ts.ModuleKind.Node16: + moduleResolution = ts.ModuleResolutionKind.Node16 + break + case ts.ModuleKind.NodeNext: + moduleResolution = ts.ModuleResolutionKind.NodeNext + break + default: + moduleResolution = ts.version.startsWith('5') + ? ts.ModuleResolutionKind.Bundler + : ts.ModuleResolutionKind.Classic + break + } + + options.moduleResolution = moduleResolution +}