@@ -5,11 +5,11 @@ import { cpus } from 'node:os'
55
66import ts from 'typescript'
77import { createFilter } from '@rollup/pluginutils'
8- import { green , yellow } from 'kolorist'
8+ import { green , red , yellow } from 'kolorist'
99import { loadProgramProcesses } from './program'
1010import { JsonResolver , SvelteResolver , VueResolver , parseResolvers } from './resolvers'
1111import { hasExportDefault , hasNormalExport , normalizeGlob , transformCode } from './transform'
12- import { bundleDtsFiles } from './bundle'
12+ import { bundleDtsFiles , getHasExtractor } from './bundle'
1313import {
1414 defaultIndex ,
1515 dtsRE ,
@@ -657,58 +657,67 @@ export class Runtime {
657657 handleDebug ( 'insert index' )
658658
659659 if ( bundleTypes ) {
660- logger . info ( green ( `${ logPrefix } Start rollup declaration files...` ) )
661-
662- const rollupFiles = new Set < string > ( )
663- const compilerOptions = configPath
664- ? getTsConfig ( configPath , this . host . readFile ) . compilerOptions
665- : rawCompilerOptions
666-
667- const rollup = async ( path : string ) => {
668- const result = bundleDtsFiles ( {
669- root,
670- configPath,
671- compilerOptions,
672- outDir,
673- entryPath : path ,
674- fileName : basename ( path ) ,
675- libFolder : getTsLibFolder ( ) ,
676- extractorConfig,
677- bundledPackages,
678- invokeOptions,
679- } )
680-
681- emittedFiles . delete ( path )
682- rollupFiles . add ( path )
683-
684- if ( typeof afterRollup === 'function' ) {
685- await unwrapPromise ( afterRollup ( result ) )
660+ logger . info ( green ( `${ logPrefix } Start bundling declaration files...` ) )
661+
662+ if ( ! getHasExtractor ( ) ) {
663+ logger . error (
664+ `\n${ logPrefix } ${ red ( "Failed to load '@microsoft/api-extractor', have you installed it?" ) } \n` ,
665+ )
666+ logger . warn (
667+ `\n${ logPrefix } ${ yellow ( 'Error occurred, skip bundle declaration files.' ) } \n` ,
668+ )
669+ } else {
670+ const rollupFiles = new Set < string > ( )
671+ const compilerOptions = configPath
672+ ? getTsConfig ( configPath , this . host . readFile ) . compilerOptions
673+ : rawCompilerOptions
674+
675+ const rollup = async ( path : string ) => {
676+ const result = await bundleDtsFiles ( {
677+ root,
678+ configPath,
679+ compilerOptions,
680+ outDir,
681+ entryPath : path ,
682+ fileName : basename ( path ) ,
683+ libFolder : getTsLibFolder ( ) ,
684+ extractorConfig,
685+ bundledPackages,
686+ invokeOptions,
687+ } )
688+
689+ emittedFiles . delete ( path )
690+ rollupFiles . add ( path )
691+
692+ if ( typeof afterRollup === 'function' ) {
693+ await unwrapPromise ( afterRollup ( result ) )
694+ }
686695 }
687- }
688-
689- if ( multiple ) {
690- await runParallel ( cpus ( ) . length , entryNames , async name => {
691- await rollup ( cleanPath ( resolve ( outDir , tsToDts ( name ) ) , emittedFiles ) )
696+
697+ if ( multiple ) {
698+ await runParallel ( cpus ( ) . length , entryNames , async name => {
699+ await rollup ( cleanPath ( resolve ( outDir , tsToDts ( name ) ) , emittedFiles ) )
700+ } )
701+ } else {
702+ await rollup ( typesPath )
703+ }
704+
705+ await runParallel ( cpus ( ) . length , Array . from ( emittedFiles . keys ( ) ) , f => unlink ( f ) )
706+ removeDirIfEmpty ( outDir )
707+ emittedFiles . clear ( )
708+
709+ const declared = declareModules . join ( '\n' )
710+
711+ await runParallel ( cpus ( ) . length , [ ...rollupFiles ] , async filePath => {
712+ await writeOutput (
713+ filePath ,
714+ ( await readFile ( filePath , 'utf-8' ) ) + ( declared ? `\n${ declared } ` : '' ) ,
715+ dirname ( filePath ) ,
716+ )
692717 } )
693- } else {
694- await rollup ( typesPath )
718+
719+ handleDebug ( 'rollup output' )
695720 }
696-
697- await runParallel ( cpus ( ) . length , Array . from ( emittedFiles . keys ( ) ) , f => unlink ( f ) )
698- removeDirIfEmpty ( outDir )
699- emittedFiles . clear ( )
700-
701- const declared = declareModules . join ( '\n' )
702-
703- await runParallel ( cpus ( ) . length , [ ...rollupFiles ] , async filePath => {
704- await writeOutput (
705- filePath ,
706- ( await readFile ( filePath , 'utf-8' ) ) + ( declared ? `\n${ declared } ` : '' ) ,
707- dirname ( filePath ) ,
708- )
709- } )
710-
711- handleDebug ( 'rollup output' )
712721 }
713722 }
714723
0 commit comments