build-types.js 947 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import dts from 'rollup-plugin-dts';
  2. import { rollup } from 'rollup';
  3. import fs from 'fs/promises';
  4. import path from 'path';
  5. import * as util from 'util';
  6. import { exec as _exec } from 'child_process';
  7. const exec = util.promisify( _exec );
  8. const dir = './dist/types';
  9. async function clean() {
  10. const files = await fs.readdir( dir );
  11. await Promise.all( files.map( file => {
  12. if ( file !== 'index.d.ts' ) {
  13. return fs.rm( path.join( dir, file ), { recursive: true, force: true } );
  14. }
  15. } ) );
  16. }
  17. async function emit() {
  18. await exec( 'tsc --emitDeclarationOnly' );
  19. }
  20. async function bundle() {
  21. const file = path.join( dir, 'index.d.ts' );
  22. const bundle = await rollup( {
  23. input : file,
  24. plugins: [ dts( { respectExternal: true } ) ],
  25. } );
  26. await bundle.write( { file } );
  27. }
  28. async function build() {
  29. await clean();
  30. await emit();
  31. await bundle();
  32. await clean();
  33. }
  34. build().catch( e => console.error( e ) );