@@ -73,8 +73,15 @@ async function buildTypeScript(
7373 }
7474
7575 const tsconfig = project ? parseTsconfig ( project ) : getTsconfig ( options . cwd ) ?. config ;
76- const moduleResolution = tsconfig ?. compilerOptions ?. moduleResolution || '' ;
76+
77+ const moduleResolution = ( tsconfig ?. compilerOptions ?. moduleResolution || '' ) . toLowerCase ( ) ;
7778 const isModernNodeModuleResolution = [ 'node16' , 'nodenext' ] . includes ( moduleResolution ) ;
79+ const isOldNodeModuleResolution = [ 'classic' , 'node' , 'node10' ] . includes ( moduleResolution ) ;
80+ if ( moduleResolution && ! isOldNodeModuleResolution && ! isModernNodeModuleResolution ) {
81+ throw new Error (
82+ `'moduleResolution' option '${ moduleResolution } ' cannot be used to build CommonJS"` ,
83+ ) ;
84+ }
7885
7986 async function build ( out : PackageJsonType ) {
8087 const revertPackageJsonsType = await setPackageJsonsType ( options . cwd , out ) ;
@@ -88,7 +95,9 @@ async function buildTypeScript(
8895 ? moduleResolution // match module with moduleResolution for modern node (nodenext and node16)
8996 : out === 'module'
9097 ? 'es2022'
91- : 'node16' , // modern commonjs
98+ : isOldNodeModuleResolution
99+ ? 'commonjs' // old commonjs
100+ : 'node16' , // modern commonjs
92101 sourceMap : false ,
93102 inlineSourceMap : false ,
94103 incremental : options . incremental ,
@@ -525,7 +534,7 @@ async function setPackageJsonsType(
525534 throw new Error ( `Invalid "type" property value "${ pkg . type } " in ${ pkgJsonPath } ` ) ;
526535 }
527536
528- const originalType : PackageJsonType | undefined = pkg . type ;
537+ const originalPkg = { ... pkg } ;
529538 const differentType =
530539 ( pkg . type ||
531540 // default when the type is not defined
@@ -541,10 +550,9 @@ async function setPackageJsonsType(
541550
542551 // revert change, of course only if we changed something
543552 reverts . push ( async ( ) => {
544- pkg . type = originalType ;
545553 await fse . writeFile (
546554 pkgJsonPath ,
547- JSON . stringify ( pkg , null , ' ' ) + ( endsWithNewline ? '\n' : '' ) ,
555+ JSON . stringify ( originalPkg , null , ' ' ) + ( endsWithNewline ? '\n' : '' ) ,
548556 ) ;
549557 } ) ;
550558 }
0 commit comments