@@ -6,70 +6,24 @@ import ts from 'typescript';
6
6
7
7
import { inlineInvariant } from './inline-invariant.js' ;
8
8
import {
9
- localRepoPath ,
10
- readdirRecursive ,
11
9
readPackageJSON ,
10
+ readTSConfig ,
12
11
showDirStats ,
13
12
writeGeneratedFile ,
14
13
} from './utils.js' ;
15
14
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' ) ;
60
17
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' ) ;
63
21
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' ) ;
69
24
70
- showDirStats ( './npmDist' ) ;
25
+ const { emittedTSFiles } = emitTSFiles ( './npmDist' ) ;
71
26
72
- function buildPackageJSON ( ) {
73
27
const packageJSON = readPackageJSON ( ) ;
74
28
75
29
delete packageJSON . private ;
@@ -97,10 +51,10 @@ function buildPackageJSON() {
97
51
98
52
packageJSON . exports = { } ;
99
53
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 ;
104
58
}
105
59
}
106
60
@@ -136,5 +90,37 @@ function buildPackageJSON() {
136
90
) ;
137
91
}
138
92
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
+ } ;
140
126
}
0 commit comments