@@ -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 { ConditionalExports } from './utils.js' ;
9
10
import {
10
11
prettify ,
11
12
readPackageJSON ,
@@ -98,18 +99,35 @@ async function buildPackage(outDir: string, isESMOnly: boolean): Promise<void> {
98
99
}
99
100
}
100
101
101
- // Temporary workaround to allow "internal" imports, no grantees provided
102
102
packageJSON . exports [ './*.js' ] = './*.js' ;
103
103
packageJSON . exports [ './*' ] = './*.js' ;
104
104
105
105
packageJSON . publishConfig . tag += '-esm' ;
106
106
packageJSON . version += '+esm' ;
107
107
} else {
108
108
delete packageJSON . type ;
109
- packageJSON . main = 'index' ;
109
+ packageJSON . main = 'index.js ' ;
110
110
packageJSON . module = 'index.mjs' ;
111
- emitTSFiles ( { outDir, module : 'commonjs' , extension : '.js' } ) ;
111
+ packageJSON . types = 'index.d.ts' ;
112
+
113
+ const { emittedTSFiles } = emitTSFiles ( {
114
+ outDir,
115
+ module : 'commonjs' ,
116
+ extension : '.js' ,
117
+ } ) ;
112
118
emitTSFiles ( { outDir, module : 'es2020' , extension : '.mjs' } ) ;
119
+
120
+ packageJSON . exports = { } ;
121
+ for ( const filepath of emittedTSFiles ) {
122
+ if ( path . basename ( filepath ) === 'index.js' ) {
123
+ const relativePath = './' + path . relative ( './npmDist' , filepath ) ;
124
+ packageJSON . exports [ path . dirname ( relativePath ) ] =
125
+ buildExports ( relativePath ) ;
126
+ }
127
+ }
128
+
129
+ packageJSON . exports [ './*.js' ] = buildExports ( './*.js' ) ;
130
+ packageJSON . exports [ './*' ] = buildExports ( './*.js' ) ;
113
131
}
114
132
115
133
const packageJsonPath = `./${ outDir } /package.json` ;
@@ -141,21 +159,31 @@ function emitTSFiles(options: {
141
159
142
160
const tsHost = ts . createCompilerHost ( tsOptions ) ;
143
161
tsHost . writeFile = ( filepath , body ) => {
144
- if ( filepath . match ( / .j s $ / ) && extension === '.mjs' ) {
145
- let bodyToWrite = body ;
146
- bodyToWrite = bodyToWrite . replace (
147
- '//# sourceMappingURL=graphql.js.map' ,
148
- '//# sourceMappingURL=graphql.mjs.map' ,
149
- ) ;
150
- writeGeneratedFile ( filepath . replace ( / .j s $ / , extension ) , bodyToWrite ) ;
151
- } else if ( filepath . match ( / .j s .m a p $ / ) && extension === '.mjs' ) {
152
- writeGeneratedFile (
153
- filepath . replace ( / .j s .m a p $ / , extension + '.map' ) ,
154
- body ,
155
- ) ;
156
- } else {
157
- writeGeneratedFile ( filepath , body ) ;
162
+ if ( extension === '.mjs' ) {
163
+ if ( filepath . match ( / .j s $ / ) ) {
164
+ let bodyToWrite = body ;
165
+ bodyToWrite = bodyToWrite . replace (
166
+ '//# sourceMappingURL=graphql.js.map' ,
167
+ '//# sourceMappingURL=graphql.mjs.map' ,
168
+ ) ;
169
+ writeGeneratedFile ( filepath . replace ( / .j s $ / , extension ) , bodyToWrite ) ;
170
+ return ;
171
+ }
172
+
173
+ if ( filepath . match ( / .j s .m a p $ / ) ) {
174
+ writeGeneratedFile (
175
+ filepath . replace ( / .j s .m a p $ / , extension + '.map' ) ,
176
+ body ,
177
+ ) ;
178
+ return ;
179
+ }
180
+
181
+ if ( filepath . match ( / .d .t s $ / ) ) {
182
+ writeGeneratedFile ( filepath . replace ( / .d .t s $ / , '.d.mts' ) , body ) ;
183
+ return ;
184
+ }
158
185
}
186
+ writeGeneratedFile ( filepath , body ) ;
159
187
} ;
160
188
161
189
const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
@@ -172,3 +200,20 @@ function emitTSFiles(options: {
172
200
emittedTSFiles : tsResult . emittedFiles . sort ( ( a , b ) => a . localeCompare ( b ) ) ,
173
201
} ;
174
202
}
203
+
204
+ function buildExports ( filepath : string ) : ConditionalExports {
205
+ const { dir, name } = path . parse ( filepath ) ;
206
+ const base = `./${ path . join ( dir , name ) } ` ;
207
+ return {
208
+ types : {
209
+ module : `${ base } .d.mts` ,
210
+ 'module-sync' : `${ base } .d.mts` ,
211
+ require : `${ base } .d.ts` ,
212
+ default : `${ base } .d.mts` ,
213
+ } ,
214
+ module : `${ base } .mjs` ,
215
+ 'module-sync' : `${ base } .mjs` ,
216
+ require : `${ base } .js` ,
217
+ default : `${ base } .mjs` ,
218
+ } ;
219
+ }
0 commit comments