@@ -6,61 +6,75 @@ import * as ts from 'typescript';
66
77import { addExtensionToImportPaths } from './add-extension-to-import-paths' ;
88import { inlineInvariant } from './inline-invariant' ;
9- import { readPackageJSON , showDirStats , writeGeneratedFile } from './utils' ;
10-
11- if ( require . main === module ) {
12- fs . rmSync ( './npmDist' , { recursive : true , force : true } ) ;
13- fs . mkdirSync ( './npmDist' ) ;
14-
15- const packageJSON = buildPackageJSON ( ) ;
16-
17- // Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
18- const tsConfig = JSON . parse (
19- fs . readFileSync ( require . resolve ( '../tsconfig.json' ) , 'utf-8' ) ,
20- ) ;
21- assert (
22- tsConfig . compilerOptions ,
23- '"tsconfig.json" should have `compilerOptions`' ,
9+ import {
10+ readdirRecursive ,
11+ readPackageJSON ,
12+ showDirStats ,
13+ writeGeneratedFile ,
14+ } from './utils' ;
15+
16+ fs . rmSync ( './npmDist' , { recursive : true , force : true } ) ;
17+ fs . mkdirSync ( './npmDist' ) ;
18+
19+ // Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
20+ const tsConfig = JSON . parse (
21+ fs . readFileSync ( require . resolve ( '../tsconfig.json' ) , 'utf-8' ) ,
22+ ) ;
23+ assert (
24+ tsConfig . compilerOptions ,
25+ '"tsconfig.json" should have `compilerOptions`' ,
26+ ) ;
27+
28+ const { options : tsOptions , errors : tsOptionsErrors } =
29+ ts . convertCompilerOptionsFromJson (
30+ {
31+ ...tsConfig . compilerOptions ,
32+ module : 'es2020' ,
33+ noEmit : false ,
34+ declaration : true ,
35+ declarationDir : './npmDist' ,
36+ outDir : './npmDist' ,
37+ } ,
38+ process . cwd ( ) ,
2439 ) ;
2540
26- const { options : tsOptions , errors : tsOptionsErrors } =
27- ts . convertCompilerOptionsFromJson (
28- {
29- ...tsConfig . compilerOptions ,
30- module : 'es2020' ,
31- noEmit : false ,
32- declaration : true ,
33- declarationDir : './npmDist' ,
34- outDir : './npmDist' ,
35- } ,
36- process . cwd ( ) ,
37- ) ;
38-
39- assert (
40- tsOptionsErrors . length === 0 ,
41- 'Fail to parse options: ' + tsOptionsErrors ,
42- ) ;
41+ assert (
42+ tsOptionsErrors . length === 0 ,
43+ 'Fail to parse options: ' + tsOptionsErrors ,
44+ ) ;
45+
46+ const tsHost = ts . createCompilerHost ( tsOptions ) ;
47+ tsHost . writeFile = ( filepath , body ) => {
48+ fs . mkdirSync ( path . dirname ( filepath ) , { recursive : true } ) ;
49+ writeGeneratedFile ( filepath , body ) ;
50+ } ;
51+
52+ const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
53+ const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
54+ after : [ addExtensionToImportPaths ( { extension : '.js' } ) , inlineInvariant ] ,
55+ } ) ;
56+ assert (
57+ ! tsResult . emitSkipped ,
58+ 'Fail to generate `*.d.ts` files, please run `npm run check`' ,
59+ ) ;
60+
61+ fs . copyFileSync ( './LICENSE' , './npmDist/LICENSE' ) ;
62+ fs . copyFileSync ( './README.md' , './npmDist/README.md' ) ;
63+
64+ // Should be done as the last step so only valid packages can be published
65+ writeGeneratedFile (
66+ './npmDist/package.json' ,
67+ JSON . stringify ( buildPackageJSON ( ) ) ,
68+ ) ;
69+
70+ showDirStats ( './npmDist' ) ;
4371
44- const tsHost = ts . createCompilerHost ( tsOptions ) ;
45- tsHost . writeFile = ( filepath , body ) => {
46- if ( path . basename ( filepath ) === 'index.js' ) {
47- const relative = './' + path . relative ( './npmDist' , filepath ) ;
48- const key = relative . replace ( / \/ ? i n d e x .j s $ / , '' ) ;
49- packageJSON . exports [ key ] = relative ;
50- }
51-
52- fs . mkdirSync ( path . dirname ( filepath ) , { recursive : true } ) ;
53- writeGeneratedFile ( filepath , body ) ;
54- } ;
72+ function buildPackageJSON ( ) {
73+ const packageJSON = readPackageJSON ( ) ;
5574
56- const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
57- const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
58- after : [ addExtensionToImportPaths ( { extension : '.js' } ) , inlineInvariant ] ,
59- } ) ;
60- assert (
61- ! tsResult . emitSkipped ,
62- 'Fail to generate `*.d.ts` files, please run `npm run check`' ,
63- ) ;
75+ delete packageJSON . private ;
76+ delete packageJSON . scripts ;
77+ delete packageJSON . devDependencies ;
6478
6579 assert ( packageJSON . types === undefined , 'Unexpected "types" in package.json' ) ;
6680 const supportedTSVersions = Object . keys ( packageJSON . typesVersions ) ;
@@ -75,33 +89,25 @@ if (require.main === module) {
7589 // Provoke syntax error to show this message
7690 `"Package 'graphql' support only TS versions that are ${ supportedTSVersions [ 0 ] } ".` ,
7791 ) ;
92+
7893 packageJSON . typesVersions = {
7994 ...packageJSON . typesVersions ,
8095 '*' : { '*' : [ notSupportedTSVersionFile ] } ,
8196 } ;
8297
83- fs . copyFileSync ( './LICENSE' , './npmDist/LICENSE' ) ;
84- fs . copyFileSync ( './README.md' , './npmDist/README.md' ) ;
85-
86- // Should be done as the last step so only valid packages can be published
87- writeGeneratedFile ( './npmDist/package.json' , JSON . stringify ( packageJSON ) ) ;
88-
89- showDirStats ( './npmDist' ) ;
90- }
91-
92- function buildPackageJSON ( ) {
93- const packageJSON = readPackageJSON ( ) ;
98+ packageJSON . type = 'module' ;
99+ packageJSON . exports = { } ;
94100
95- delete packageJSON . private ;
96- delete packageJSON . scripts ;
97- delete packageJSON . devDependencies ;
101+ for ( const filepath of readdirRecursive ( './src' , { ignoreDir : / ^ _ _ .* _ _ $ / } ) ) {
102+ if ( path . basename ( filepath ) === 'index.ts' ) {
103+ const key = path . dirname ( filepath ) ;
104+ packageJSON . exports [ key ] = filepath . replace ( / \. t s $ / , '.js' ) ;
105+ }
106+ }
98107
99- packageJSON . type = 'module' ;
100108 // Temporary workaround to allow "internal" imports, no grantees provided
101- packageJSON . exports = {
102- './*.js' : './*.js' ,
103- './*' : './*.js' ,
104- } ;
109+ packageJSON . exports [ './*.js' ] = './*.js' ;
110+ packageJSON . exports [ './*' ] = './*.js' ;
105111
106112 // TODO: move to integration tests
107113 const publishTag = packageJSON . publishConfig ?. tag ;
0 commit comments