@@ -6,6 +6,7 @@ import ts from 'typescript';
66
77import { changeExtensionInImportPaths } from './change-extension-in-import-paths.js' ;
88import { inlineInvariant } from './inline-invariant.js' ;
9+ import type { ConditionalExports } from './utils.js' ;
910import {
1011 prettify ,
1112 readPackageJSON ,
@@ -98,18 +99,35 @@ async function buildPackage(outDir: string, isESMOnly: boolean): Promise<void> {
9899 }
99100 }
100101
101- // Temporary workaround to allow "internal" imports, no grantees provided
102102 packageJSON . exports [ './*.js' ] = './*.js' ;
103103 packageJSON . exports [ './*' ] = './*.js' ;
104104
105105 packageJSON . publishConfig . tag += '-esm' ;
106106 packageJSON . version += '+esm' ;
107107 } else {
108108 delete packageJSON . type ;
109- packageJSON . main = 'index' ;
109+ packageJSON . main = 'index.js ' ;
110110 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+ } ) ;
112118 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' ) ;
113131 }
114132
115133 const packageJsonPath = `./${ outDir } /package.json` ;
@@ -141,21 +159,31 @@ function emitTSFiles(options: {
141159
142160 const tsHost = ts . createCompilerHost ( tsOptions ) ;
143161 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+ }
158185 }
186+ writeGeneratedFile ( filepath , body ) ;
159187 } ;
160188
161189 const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
@@ -172,3 +200,22 @@ function emitTSFiles(options: {
172200 emittedTSFiles : tsResult . emittedFiles . sort ( ( a , b ) => a . localeCompare ( b ) ) ,
173201 } ;
174202}
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+ node : `${ base } .d.ts` ,
212+ require : `${ base } .d.ts` ,
213+ default : `${ base } .d.mts` ,
214+ } ,
215+ module : `${ base } .mjs` ,
216+ 'module-sync' : `${ base } .mjs` ,
217+ node : `${ base } .js` ,
218+ require : `${ base } .js` ,
219+ default : `${ base } .mjs` ,
220+ } ;
221+ }
0 commit comments