@@ -6,70 +6,24 @@ import ts from 'typescript';
66
77import { inlineInvariant } from './inline-invariant.js' ;
88import {
9- localRepoPath ,
10- readdirRecursive ,
119 readPackageJSON ,
10+ readTSConfig ,
1211 showDirStats ,
1312 writeGeneratedFile ,
1413} from './utils.js' ;
1514
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 tsConfigPath = localRepoPath ( 'tsconfig.json' ) ;
21- const { config : tsConfig , error : tsConfigError } = ts . parseConfigFileTextToJson (
22- tsConfigPath ,
23- fs . readFileSync ( tsConfigPath , 'utf-8' ) ,
24- ) ;
25- assert ( tsConfigError === undefined , 'Fail to parse config: ' + tsConfigError ) ;
26- assert (
27- tsConfig . compilerOptions ,
28- '"tsconfig.json" should have `compilerOptions`' ,
29- ) ;
30-
31- const { options : tsOptions , errors : tsOptionsErrors } =
32- ts . convertCompilerOptionsFromJson (
33- {
34- ...tsConfig . compilerOptions ,
35- module : 'es2020' ,
36- noEmit : false ,
37- declaration : true ,
38- declarationDir : './npmDist' ,
39- outDir : './npmDist' ,
40- } ,
41- process . cwd ( ) ,
42- ) ;
43-
44- assert (
45- tsOptionsErrors . length === 0 ,
46- 'Fail to parse options: ' + tsOptionsErrors ,
47- ) ;
48-
49- const tsHost = ts . createCompilerHost ( tsOptions ) ;
50- tsHost . writeFile = writeGeneratedFile ;
51-
52- const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
53- const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
54- after : [ inlineInvariant ] ,
55- } ) ;
56- assert (
57- ! tsResult . emitSkipped ,
58- 'Fail to generate `*.d.ts` files, please run `npm run check`' ,
59- ) ;
15+ buildPackage ( ) ;
16+ showDirStats ( './npmDist' ) ;
6017
61- fs . copyFileSync ( './LICENSE' , './npmDist/LICENSE' ) ;
62- fs . copyFileSync ( './README.md' , './npmDist/README.md' ) ;
18+ function buildPackage ( ) {
19+ fs . rmSync ( './npmDist' , { recursive : true , force : true } ) ;
20+ fs . mkdirSync ( './npmDist' ) ;
6321
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- ) ;
22+ fs . copyFileSync ( './LICENSE' , './npmDist/LICENSE' ) ;
23+ fs . copyFileSync ( './README.md' , './npmDist/README.md' ) ;
6924
70- showDirStats ( './npmDist' ) ;
25+ const { emittedTSFiles } = emitTSFiles ( './npmDist' ) ;
7126
72- function buildPackageJSON ( ) {
7327 const packageJSON = readPackageJSON ( ) ;
7428
7529 delete packageJSON . private ;
@@ -97,10 +51,10 @@ function buildPackageJSON() {
9751
9852 packageJSON . exports = { } ;
9953
100- for ( const filepath of readdirRecursive ( './src' , { ignoreDir : / ^ _ _ . * _ _ $ / } ) ) {
101- if ( path . basename ( filepath ) === 'index.ts ' ) {
102- const key = path . dirname ( filepath ) ;
103- packageJSON . exports [ key ] = filepath . replace ( / \. t s $ / , '.js' ) ;
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 ;
10458 }
10559 }
10660
@@ -136,5 +90,37 @@ function buildPackageJSON() {
13690 ) ;
13791 }
13892
139- return packageJSON ;
93+ // Should be done as the last step so only valid packages can be published
94+ writeGeneratedFile ( './npmDist/package.json' , JSON . stringify ( packageJSON ) ) ;
95+ }
96+
97+ // Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
98+ function emitTSFiles ( outDir : string ) : {
99+ emittedTSFiles : ReadonlyArray < string > ;
100+ } {
101+ const tsOptions = readTSConfig ( {
102+ module : 'es2020' ,
103+ noEmit : false ,
104+ declaration : true ,
105+ declarationDir : outDir ,
106+ outDir,
107+ listEmittedFiles : true ,
108+ } ) ;
109+
110+ const tsHost = ts . createCompilerHost ( tsOptions ) ;
111+ tsHost . writeFile = writeGeneratedFile ;
112+
113+ const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
114+ const tsResult = tsProgram . emit ( undefined , undefined , undefined , undefined , {
115+ after : [ inlineInvariant ] ,
116+ } ) ;
117+ assert (
118+ ! tsResult . emitSkipped ,
119+ 'Fail to generate `*.d.ts` files, please run `npm run check`' ,
120+ ) ;
121+
122+ assert ( tsResult . emittedFiles != null ) ;
123+ return {
124+ emittedTSFiles : tsResult . emittedFiles . sort ( ( a , b ) => a . localeCompare ( b ) ) ,
125+ } ;
140126}
0 commit comments