@@ -2,10 +2,12 @@ import { readFileSync } from 'fs';
22import { dirname , resolve } from 'path' ;
33
44import { PluginContext } from 'rollup' ;
5- import type {
5+ import {
66 Diagnostic ,
77 ExtendedConfigCacheEntry ,
88 MapLike ,
9+ ModuleKind ,
10+ ModuleResolutionKind ,
911 ParsedCommandLine ,
1012 ProjectReference ,
1113 TypeAcquisition ,
@@ -99,6 +101,28 @@ function containsEnumOptions(
99101 return enums . some ( ( prop ) => prop in compilerOptions && typeof compilerOptions [ prop ] === 'number' ) ;
100102}
101103
104+ /**
105+ * The module resolution kind is a function of the resolved `compilerOptions.module`.
106+ * This needs to be set explicitly for `resolveModuleName` to select the correct resolution method
107+ */
108+ function setModuleResolutionKind ( parsedConfig : ParsedCommandLine ) : ParsedCommandLine {
109+ const moduleKind = parsedConfig . options . module ;
110+ const moduleResolution =
111+ moduleKind === ModuleKind . Node16
112+ ? ModuleResolutionKind . Node16
113+ : moduleKind === ModuleKind . NodeNext
114+ ? ModuleResolutionKind . NodeNext
115+ : ModuleResolutionKind . NodeJs ;
116+
117+ return {
118+ ...parsedConfig ,
119+ options : {
120+ ...parsedConfig . options ,
121+ moduleResolution
122+ }
123+ } ;
124+ }
125+
102126const configCache = new Map ( ) as import ( 'typescript' ) . Map < ExtendedConfigCacheEntry > ;
103127
104128/**
@@ -132,39 +156,43 @@ export function parseTypescriptConfig(
132156 // If compilerOptions has enums, it represents an CompilerOptions object instead of parsed JSON.
133157 // This determines where the data is passed to the parser.
134158 if ( containsEnumOptions ( compilerOptions ) ) {
135- parsedConfig = ts . parseJsonConfigFileContent (
136- {
137- ...tsConfigFile ,
138- compilerOptions : {
139- ...DEFAULT_COMPILER_OPTIONS ,
140- ...tsConfigFile . compilerOptions
141- }
142- } ,
143- ts . sys ,
144- basePath ,
145- { ...compilerOptions , ...makeForcedCompilerOptions ( noForceEmit ) } ,
146- tsConfigPath ,
147- undefined ,
148- undefined ,
149- configCache
159+ parsedConfig = setModuleResolutionKind (
160+ ts . parseJsonConfigFileContent (
161+ {
162+ ...tsConfigFile ,
163+ compilerOptions : {
164+ ...DEFAULT_COMPILER_OPTIONS ,
165+ ...tsConfigFile . compilerOptions
166+ }
167+ } ,
168+ ts . sys ,
169+ basePath ,
170+ { ...compilerOptions , ...makeForcedCompilerOptions ( noForceEmit ) } ,
171+ tsConfigPath ,
172+ undefined ,
173+ undefined ,
174+ configCache
175+ )
150176 ) ;
151177 } else {
152- parsedConfig = ts . parseJsonConfigFileContent (
153- {
154- ...tsConfigFile ,
155- compilerOptions : {
156- ...DEFAULT_COMPILER_OPTIONS ,
157- ...tsConfigFile . compilerOptions ,
158- ...compilerOptions
159- }
160- } ,
161- ts . sys ,
162- basePath ,
163- makeForcedCompilerOptions ( noForceEmit ) ,
164- tsConfigPath ,
165- undefined ,
166- undefined ,
167- configCache
178+ parsedConfig = setModuleResolutionKind (
179+ ts . parseJsonConfigFileContent (
180+ {
181+ ...tsConfigFile ,
182+ compilerOptions : {
183+ ...DEFAULT_COMPILER_OPTIONS ,
184+ ...tsConfigFile . compilerOptions ,
185+ ...compilerOptions
186+ }
187+ } ,
188+ ts . sys ,
189+ basePath ,
190+ makeForcedCompilerOptions ( noForceEmit ) ,
191+ tsConfigPath ,
192+ undefined ,
193+ undefined ,
194+ configCache
195+ )
168196 ) ;
169197 }
170198
0 commit comments