Skip to content

Commit

Permalink
build-npm: fix type inference during TS declarations build
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Oct 28, 2021
1 parent a27ad0b commit 2b2f1b8
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions resources/build-npm.js
Expand Up @@ -35,16 +35,27 @@ if (require.main === module) {
}
}

const tsProgram = ts.createProgram(['src/index.ts'], {
...ts.getDefaultCompilerOptions(),
// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
const tsConfig = require('../tsconfig.json');
assert(
tsConfig.compilerOptions,
'"tsconfig.json" should have `compilerOptions`',
);
const tsOptions = {
...tsConfig.compilerOptions,
noEmit: false,
declaration: true,
declarationDir: './npmDist',
emitDeclarationOnly: true,
});
};

const tsResult = tsProgram.emit(undefined, (filepath, body) => {
const tsHost = ts.createCompilerHost(tsOptions);
tsHost.writeFile = (filepath, body) => {
writeGeneratedFile(filepath, body);
});
}

const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
const tsResult = tsProgram.emit();
assert(
!tsResult.emitSkipped,
'Fail to generate `*.d.ts` files, please run `npm run check`',
Expand Down

0 comments on commit 2b2f1b8

Please sign in to comment.