@@ -6,6 +6,7 @@ import ts from 'typescript';
6
6
7
7
import { changeExtensionInImportPaths } from './change-extension-in-import-paths.js' ;
8
8
import { inlineInvariant } from './inline-invariant.js' ;
9
+ import type { ImportsMap } from './utils.js' ;
9
10
import {
10
11
prettify ,
11
12
readPackageJSON ,
@@ -102,12 +103,23 @@ async function buildPackage(outDir: string, isESMOnly: boolean): Promise<void> {
102
103
packageJSON . exports [ './*.js' ] = './*.js' ;
103
104
packageJSON . exports [ './*' ] = './*.js' ;
104
105
106
+ packageJSON . imports = mapImports ( packageJSON . imports , ( value : string ) =>
107
+ updateImportPath ( value , '.js' ) ,
108
+ ) ;
109
+
110
+ packageJSON . type = 'module' ;
105
111
packageJSON . publishConfig . tag += '-esm' ;
106
112
packageJSON . version += '+esm' ;
107
113
} else {
108
- delete packageJSON . type ;
114
+ packageJSON . type = 'commonjs' ;
109
115
packageJSON . main = 'index' ;
110
116
packageJSON . module = 'index.mjs' ;
117
+
118
+ packageJSON . imports = mapImports ( packageJSON . imports , ( value : string ) => ( {
119
+ import : updateImportPath ( value , '.mjs' ) ,
120
+ default : updateImportPath ( value , '.js' ) ,
121
+ } ) ) ;
122
+
111
123
emitTSFiles ( { outDir, module : 'commonjs' , extension : '.js' } ) ;
112
124
emitTSFiles ( { outDir, module : 'es2020' , extension : '.mjs' } ) ;
113
125
}
@@ -121,6 +133,25 @@ async function buildPackage(outDir: string, isESMOnly: boolean): Promise<void> {
121
133
writeGeneratedFile ( packageJsonPath , prettified ) ;
122
134
}
123
135
136
+ function updateImportPath ( value : string , extension : string ) {
137
+ return value . replace ( / \/ s r c \/ / g, '/' ) . replace ( / \. t s $ / , extension ) ;
138
+ }
139
+
140
+ function mapImports (
141
+ imports : ImportsMap ,
142
+ replacer : ( value : string ) => string | ImportsMap ,
143
+ ) : ImportsMap {
144
+ const newImports : ImportsMap = { } ;
145
+ for ( const [ key , value ] of Object . entries ( imports ) ) {
146
+ if ( typeof value === 'string' ) {
147
+ newImports [ key ] = replacer ( value ) ;
148
+ continue ;
149
+ }
150
+ newImports [ key ] = mapImports ( value , replacer ) ;
151
+ }
152
+ return newImports ;
153
+ }
154
+
124
155
// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
125
156
function emitTSFiles ( options : {
126
157
outDir : string ;
@@ -143,7 +174,15 @@ function emitTSFiles(options: {
143
174
tsHost . writeFile = ( filepath , body ) =>
144
175
writeGeneratedFile ( filepath . replace ( / .j s $ / , extension ) , body ) ;
145
176
146
- const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
177
+ const tsProgram = ts . createProgram (
178
+ [
179
+ 'src/index.ts' ,
180
+ 'src/jsutils/instanceOf.ts' ,
181
+ 'src/jsutils/instanceOfForDevelopment.ts' ,
182
+ ] ,
183
+ tsOptions ,
184
+ tsHost ,
185
+ ) ;
147
186
const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
148
187
after : [ changeExtensionInImportPaths ( { extension } ) , inlineInvariant ] ,
149
188
} ) ;
0 commit comments