Skip to content

Commit

Permalink
fix: make inject moduleResolution as patch for Vue
Browse files Browse the repository at this point in the history
fix #280
  • Loading branch information
qmhc committed Oct 30, 2023
1 parent bd96a7a commit a6f12dc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/plugin.ts
Expand Up @@ -29,6 +29,7 @@ import {
removeDirIfEmpty,
resolve,
runParallel,
setModuleResolution,
toCapitalCase,
tryGetPkgPath,
wrapPromise
Expand Down Expand Up @@ -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,
Expand All @@ -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))
Expand Down
36 changes: 34 additions & 2 deletions 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'

Expand Down Expand Up @@ -212,7 +212,7 @@ export function getTsConfig(
extends?: string | string[]
} = {
compilerOptions: {},
...(typescript.readConfigFile(tsConfigPath, readFileSync).config ?? {})
...(ts.readConfigFile(tsConfigPath, readFileSync).config ?? {})
}

if (tsConfig.extends) {
Expand Down Expand Up @@ -334,3 +334,35 @@ export function findTypesPath(...pkgs: Record<any, any>[]) {
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
}

0 comments on commit a6f12dc

Please sign in to comment.