Skip to content

Commit 8841cd8

Browse files
authored
fix(unplugin-dts): correct module-resolution precedence in setModuleResolution (#486)
In setModuleResolution, `options.target ?? ts.ScriptTarget.ES5 >= 2` parsed as `options.target ?? (ES5 >= 2)` = `options.target ?? false`, because `>=` binds tighter than `??`. The resolved target was never compared: called in isolation with an explicit low target (e.g. ES5), the helper picked ES2015 modules instead of CommonJS. Parenthesize as `(options.target ?? ts.ScriptTarget.ES5) >= ts.ScriptTarget.ES2015` so the default is applied before the comparison, and use the enum rather than the literal `2` for intent. Scope: this is an internal-correctness fix to the helper. Its only caller in Runtime spreads `fixedCompilerOptions` (target: ESNext) before calling it, so the plugin itself never reaches the ES5 branch and behaviour is unchanged today; the fix makes the helper correct if reused, or if that call path ever changes. Adds three setModuleResolution cases, appended to the existing utils suite.
1 parent 03782b7 commit 8841cd8

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

packages/unplugin-dts/src/core/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export function setModuleResolution(options: CompilerOptions) {
416416
const module =
417417
typeof options.module === 'number'
418418
? options.module
419-
: (options.target ?? ts.ScriptTarget.ES5 >= 2)
419+
: (options.target ?? ts.ScriptTarget.ES5) >= ts.ScriptTarget.ES2015
420420
? ts.ModuleKind.ES2015
421421
: ts.ModuleKind.CommonJS
422422

packages/unplugin-dts/tests/utils.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { normalize, resolve } from 'node:path'
22
import { existsSync } from 'node:fs'
33
import { describe, expect, it } from 'vitest'
44

5+
import ts from '../src/core/ts-loader.cjs'
56
import {
67
base64VLQEncode,
78
ensureAbsolute,
@@ -18,6 +19,7 @@ import {
1819
parseTsAliases,
1920
queryPublicPath,
2021
resolveConfigDir,
22+
setModuleResolution,
2123
toCapitalCase,
2224
transformDtsPath,
2325
transformSourceMappingURL,
@@ -492,3 +494,28 @@ export { testFn } from './comment';
492494
)
493495
})
494496
})
497+
498+
describe('setModuleResolution', () => {
499+
it('keeps an explicit moduleResolution', () => {
500+
const options: ts.CompilerOptions = {
501+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
502+
}
503+
setModuleResolution(options)
504+
expect(options.moduleResolution).toBe(ts.ModuleResolutionKind.NodeNext)
505+
})
506+
507+
it('maps an ES5 target to CommonJS module resolution (Node10)', () => {
508+
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES5 }
509+
setModuleResolution(options)
510+
expect(options.moduleResolution).toBe(ts.ModuleResolutionKind.Node10)
511+
})
512+
513+
it('maps an ES2015+ target to ES2015 module resolution', () => {
514+
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES2020 }
515+
setModuleResolution(options)
516+
const expected = ts.version.startsWith('5')
517+
? ts.ModuleResolutionKind.Bundler
518+
: ts.ModuleResolutionKind.Classic
519+
expect(options.moduleResolution).toBe(expected)
520+
})
521+
})

0 commit comments

Comments
 (0)