@@ -4,6 +4,7 @@ import path from 'node:path';
4
4
5
5
import ts from 'typescript' ;
6
6
7
+ import { changeExtensionInImportPaths } from './change-extension-in-import-paths.js' ;
7
8
import { inlineInvariant } from './inline-invariant.js' ;
8
9
import {
9
10
readPackageJSON ,
@@ -12,17 +13,20 @@ import {
12
13
writeGeneratedFile ,
13
14
} from './utils.js' ;
14
15
15
- buildPackage ( ) ;
16
+ console . log ( '\n./npmDist' ) ;
17
+ buildPackage ( './npmDist' , false ) ;
16
18
showDirStats ( './npmDist' ) ;
17
19
18
- function buildPackage ( ) {
19
- fs . rmSync ( './npmDist ' , { recursive : true , force : true } ) ;
20
- fs . mkdirSync ( './npmDist ' ) ;
20
+ console . log ( '\n./npmEsmDist' ) ;
21
+ buildPackage ( './npmEsmDist ' , true ) ;
22
+ showDirStats ( './npmEsmDist ' ) ;
21
23
22
- fs . copyFileSync ( './LICENSE' , './npmDist/LICENSE' ) ;
23
- fs . copyFileSync ( './README.md' , './npmDist/README.md' ) ;
24
+ function buildPackage ( outDir : string , isESMOnly : boolean ) : void {
25
+ fs . rmSync ( outDir , { recursive : true , force : true } ) ;
26
+ fs . mkdirSync ( outDir ) ;
24
27
25
- const { emittedTSFiles } = emitTSFiles ( './npmDist' ) ;
28
+ fs . copyFileSync ( './LICENSE' , `./${ outDir } /LICENSE` ) ;
29
+ fs . copyFileSync ( './README.md' , `./${ outDir } /README.md` ) ;
26
30
27
31
const packageJSON = readPackageJSON ( ) ;
28
32
@@ -39,7 +43,7 @@ function buildPackage() {
39
43
// TODO: revisit once TS implements https://github.com/microsoft/TypeScript/issues/32166
40
44
const notSupportedTSVersionFile = 'NotSupportedTSVersion.d.ts' ;
41
45
fs . writeFileSync (
42
- path . join ( './npmDist' , notSupportedTSVersionFile ) ,
46
+ path . join ( outDir , notSupportedTSVersionFile ) ,
43
47
// Provoke syntax error to show this message
44
48
`"Package 'graphql' support only TS versions that are ${ supportedTSVersions [ 0 ] } ".` ,
45
49
) ;
@@ -49,19 +53,6 @@ function buildPackage() {
49
53
'*' : { '*' : [ notSupportedTSVersionFile ] } ,
50
54
} ;
51
55
52
- packageJSON . exports = { } ;
53
-
54
- for ( const filepath of emittedTSFiles ) {
55
- if ( path . basename ( filepath ) === 'index.js' ) {
56
- const relativePath = './' + path . relative ( './npmDist' , filepath ) ;
57
- packageJSON . exports [ path . dirname ( relativePath ) ] = relativePath ;
58
- }
59
- }
60
-
61
- // Temporary workaround to allow "internal" imports, no grantees provided
62
- packageJSON . exports [ './*.js' ] = './*.js' ;
63
- packageJSON . exports [ './*' ] = './*.js' ;
64
-
65
56
// TODO: move to integration tests
66
57
const publishTag = packageJSON . publishConfig ?. tag ;
67
58
assert ( publishTag != null , 'Should have packageJSON.publishConfig defined!' ) ;
@@ -90,16 +81,51 @@ function buildPackage() {
90
81
) ;
91
82
}
92
83
84
+ if ( isESMOnly ) {
85
+ packageJSON . exports = { } ;
86
+
87
+ const { emittedTSFiles } = emitTSFiles ( {
88
+ outDir,
89
+ module : 'es2020' ,
90
+ extension : '.js' ,
91
+ } ) ;
92
+
93
+ for ( const filepath of emittedTSFiles ) {
94
+ if ( path . basename ( filepath ) === 'index.js' ) {
95
+ const relativePath = './' + path . relative ( './npmEsmDist' , filepath ) ;
96
+ packageJSON . exports [ path . dirname ( relativePath ) ] = relativePath ;
97
+ }
98
+ }
99
+
100
+ // Temporary workaround to allow "internal" imports, no grantees provided
101
+ packageJSON . exports [ './*.js' ] = './*.js' ;
102
+ packageJSON . exports [ './*' ] = './*.js' ;
103
+
104
+ packageJSON . publishConfig . tag += '-esm' ;
105
+ packageJSON . version += '+esm' ;
106
+ } else {
107
+ delete packageJSON . type ;
108
+ packageJSON . main = 'index' ;
109
+ packageJSON . module = 'index.mjs' ;
110
+ emitTSFiles ( { outDir, module : 'commonjs' , extension : '.js' } ) ;
111
+ emitTSFiles ( { outDir, module : 'es2020' , extension : '.mjs' } ) ;
112
+ }
113
+
93
114
// Should be done as the last step so only valid packages can be published
94
- writeGeneratedFile ( './npmDist /package.json' , JSON . stringify ( packageJSON ) ) ;
115
+ writeGeneratedFile ( `./ ${ outDir } /package.json` , JSON . stringify ( packageJSON ) ) ;
95
116
}
96
117
97
118
// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
98
- function emitTSFiles ( outDir : string ) : {
119
+ function emitTSFiles ( options : {
120
+ outDir : string ;
121
+ module : string ;
122
+ extension : string ;
123
+ } ) : {
99
124
emittedTSFiles : ReadonlyArray < string > ;
100
125
} {
126
+ const { outDir, module, extension } = options ;
101
127
const tsOptions = readTSConfig ( {
102
- module : 'es2020' ,
128
+ module,
103
129
noEmit : false ,
104
130
declaration : true ,
105
131
declarationDir : outDir ,
@@ -108,11 +134,12 @@ function emitTSFiles(outDir: string): {
108
134
} ) ;
109
135
110
136
const tsHost = ts . createCompilerHost ( tsOptions ) ;
111
- tsHost . writeFile = writeGeneratedFile ;
137
+ tsHost . writeFile = ( filepath , body ) =>
138
+ writeGeneratedFile ( filepath . replace ( / .j s $ / , extension ) , body ) ;
112
139
113
140
const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
114
141
const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
115
- after : [ inlineInvariant ] ,
142
+ after : [ changeExtensionInImportPaths ( { extension } ) , inlineInvariant ] ,
116
143
} ) ;
117
144
assert (
118
145
! tsResult . emitSkipped ,
0 commit comments