@@ -72,7 +72,12 @@ export function createFakeJsPlugin({
7272} : Pick < OptionsResolved , 'sourcemap' | 'cjsDefault' | 'sideEffects' > ) : Plugin {
7373 let declarationIdx = 0
7474 const declarationMap = new Map < number /* declaration id */ , DeclarationInfo > ( )
75+ const moduleDeclarationsMap = new Map <
76+ string /* filename */ ,
77+ number [ ] /* declaration ids */
78+ > ( )
7579 const commentsMap = new Map < string /* filename */ , t . Comment [ ] > ( )
80+ const fileCommentsMap = new Map < string /* filename */ , t . Comment [ ] > ( )
7681 const moduleExportsMap = new Map < string /* filename */ , ModuleExports > ( )
7782 const warnedCjsDtsInputs = new Set < string > ( )
7883
@@ -164,6 +169,11 @@ export function createFakeJsPlugin({
164169 )
165170 }
166171
172+ // A watch rebuild transforms changed modules again, so drop what the
173+ // previous transform stored. Unchanged modules may keep their cached
174+ // output with incremental builds, so their state must stay.
175+ clearModuleState ( id )
176+
167177 const { program } = file
168178 moduleExportsMap . set ( id , await collectModuleExports ( this , program . body , id ) )
169179 const identifierMap : Record < string , number > = Object . create ( null )
@@ -189,7 +199,16 @@ export function createFakeJsPlugin({
189199
190200 for ( const [ i , stmt ] of program . body . entries ( ) ) {
191201 const setStmt = ( stmt : t . ProgramStatement ) => ( program . body [ i ] = stmt )
192- if ( rewriteImportExport ( stmt , setStmt , appendStmts ) ) continue
202+ if ( rewriteImportExport ( stmt , setStmt , appendStmts ) ) {
203+ // The file's doc comment, e.g. `@module`, is attached to its first
204+ // statement. An import or export keeps no comments, so save it for
205+ // `renderChunk` to restore.
206+ if ( i === 0 ) {
207+ const docComments = stmt . comments ?. filter ( isDocComment )
208+ if ( docComments ?. length ) fileCommentsMap . set ( id , docComments )
209+ }
210+ continue
211+ }
193212
194213 const sideEffect =
195214 stmt . type === 'TSModuleDeclaration' && stmt . kind !== 'namespace'
@@ -280,7 +299,7 @@ export function createFakeJsPlugin({
280299 decl . comments = stmt . comments
281300 }
282301
283- const declarationId = registerDeclaration ( {
302+ const declarationId = registerDeclaration ( id , {
284303 decl,
285304 deps,
286305 bindings,
@@ -567,6 +586,14 @@ export function createFakeJsPlugin({
567586 program . body . push ( b . ExportNamedDeclaration ( { } ) )
568587 }
569588
589+ // recover the entry's doc comment, but not those of the modules it inlines
590+ const fileComments =
591+ chunk . facadeModuleId && fileCommentsMap . get ( chunk . facadeModuleId )
592+ if ( fileComments ) {
593+ program . body [ 0 ] . comments ||= [ ]
594+ program . body [ 0 ] . comments . unshift ( ...fileComments )
595+ }
596+
570597 // recover comments
571598 const comments = new Set < t . Comment > ( )
572599 const commentsValue = new Set < string > ( ) // deduplicate
@@ -581,7 +608,6 @@ export function createFakeJsPlugin({
581608 commentsValue . add ( id )
582609 comments . add ( c )
583610 } )
584- commentsMap . delete ( id )
585611 }
586612 }
587613 if ( comments . size ) {
@@ -606,12 +632,27 @@ export function createFakeJsPlugin({
606632 }
607633 }
608634
609- function registerDeclaration ( info : DeclarationInfo ) {
635+ function registerDeclaration ( id : string , info : DeclarationInfo ) {
610636 const declarationId = declarationIdx ++
611637 declarationMap . set ( declarationId , info )
638+
639+ let ids = moduleDeclarationsMap . get ( id )
640+ if ( ! ids ) moduleDeclarationsMap . set ( id , ( ids = [ ] ) )
641+ ids . push ( declarationId )
642+
612643 return declarationId
613644 }
614645
646+ function clearModuleState ( id : string ) {
647+ for ( const declarationId of moduleDeclarationsMap . get ( id ) || [ ] ) {
648+ declarationMap . delete ( declarationId )
649+ }
650+ moduleDeclarationsMap . delete ( id )
651+ moduleExportsMap . delete ( id )
652+ commentsMap . delete ( id )
653+ fileCommentsMap . delete ( id )
654+ }
655+
615656 function getDeclaration ( declarationId : number ) {
616657 return declarationMap . get ( declarationId ) !
617658 }
@@ -682,3 +723,11 @@ function removeDtsOnlyRuntimeChunks(bundle: OutputBundle): void {
682723 }
683724 }
684725}
726+
727+ function isDocComment ( comment : t . Comment ) : boolean {
728+ return (
729+ comment . position === 'before' &&
730+ comment . type === 'Block' &&
731+ comment . value . startsWith ( '*' )
732+ )
733+ }
0 commit comments