import * as t from '@babel/types' ; import { NodePath } from '@babel/traverse' ; import Store from '@remax/build-store' ; import { slash } from '@remax/shared' ; const lifecycleEvents = [ 'onPageScroll' , 'onShareAppMessage' , 'onShareTimeline' ] ; interface Options { test : ( file : string ) => boolean ; } export default ( options ) => { let skip = false ; return { pre ( state ) { const importer = slash ( state . opts . filename ) ; skip = ! options . test ( importer ) ; if ( skip ) { return ; } Store . pageEvents . delete ( importer ) ; } , visitor : { StringLiteral : ( path , state ) => { if ( skip ) { return ; } const { node } = path ; const importer = slash ( state . file . opts . filename ) ; if ( ! lifecycleEvents . includes ( node . value ) ) { return ; } Store . pageEvents . set ( importer , Store . pageEvents . get ( importer ) ?. add ( node . value ) ?? new Set ( [ node . value ] ) ) ; } , Identifier : ( path , state ) => { if ( skip ) { return ; } const { node } = path ; const importer = slash ( state . file . opts . filename ) ; if ( ! lifecycleEvents . includes ( node . name ) ) { return ; } Store . pageEvents . set ( importer , Store . pageEvents . get ( importer ) ?. add ( node . name ) ?? new Set ( [ node . name ] ) ) ; } , } , } ; } ;