@@ -35,16 +35,29 @@ if (require.main === module) {
35
35
}
36
36
}
37
37
38
- const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , {
39
- ...ts . getDefaultCompilerOptions ( ) ,
38
+ // Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
39
+ const tsConfig = JSON . parse (
40
+ fs . readFileSync ( require . resolve ( '../tsconfig.json' ) , 'utf-8' ) ,
41
+ ) ;
42
+ assert (
43
+ tsConfig . compilerOptions ,
44
+ '"tsconfig.json" should have `compilerOptions`' ,
45
+ ) ;
46
+ const tsOptions = {
47
+ ...tsConfig . compilerOptions ,
48
+ noEmit : false ,
40
49
declaration : true ,
41
50
declarationDir : './npmDist' ,
42
51
emitDeclarationOnly : true ,
43
- } ) ;
52
+ } ;
44
53
45
- const tsResult = tsProgram . emit ( undefined , ( filepath , body ) => {
54
+ const tsHost = ts . createCompilerHost ( tsOptions ) ;
55
+ tsHost . writeFile = ( filepath , body ) => {
46
56
writeGeneratedFile ( filepath , body ) ;
47
- } ) ;
57
+ } ;
58
+
59
+ const tsProgram = ts . createProgram ( [ 'src/index.ts' ] , tsOptions , tsHost ) ;
60
+ const tsResult = tsProgram . emit ( ) ;
48
61
assert (
49
62
! tsResult . emitSkipped ,
50
63
'Fail to generate `*.d.ts` files, please run `npm run check`' ,
0 commit comments