import path from 'path' ; import { logger , isMonorepo , normalizeRuleMatch } from '@reskript/core' ; import { SettingsPlugin } from '@reskript/settings' ; import { Options , LocalPackageInfo } from './interface' ; import { resolveParticipant , findSiblingPackages , buildPackageInfo , buildPeerAlias , checkDependencyGraph } from './utils' ; export default ( options = { } ) : SettingsPlugin => async ( settings , { cwd } ) => { const isWorkspace = await isMonorepo ( cwd ) ; if ( ! isWorkspace ) { logger . error ( 'Current project is not a monorepo workspace' ) ; process . exit ( 24 ) ; } const self = await buildPackageInfo ( cwd ) ; const siblings = await findSiblingPackages ( cwd , self ) ; const isDependencyOfSelf = ( { name } ) => ! ! ( self . dependencies [ name ] ?? self . devDependencies [ name ] ) ; const includedSiblings = resolveParticipant ( siblings . filter ( isDependencyOfSelf ) , options ) ; const dependencyGraphChecked = checkDependencyGraph ( includedSiblings , self ) ; if ( ! dependencyGraphChecked ) { process . exit ( 24 ) ; } const incomingBabelFilter = normalizeRuleMatch ( cwd , settings . build . script . babel ) ; const incomingModulesFilter = normalizeRuleMatch ( cwd , settings . build . style . modules ) ; const includedSiblingDirectories = includedSiblings . map ( v => v . directory ) ; return { ... settings , build : { ... settings . build , style : options . styles ? { ... settings . build . style , modules : ( resource ) => { const shouldProcess = incomingModulesFilter ( resource ) ; return shouldProcess || includedSiblingDirectories . some ( v => resource . startsWith ( v ) ) ; } , } : settings . build . style , script : { ... settings . build . script , babel : ( resource ) => { const shouldProcess = incomingBabelFilter ( resource ) ; return shouldProcess || includedSiblingDirectories . some ( v => resource . startsWith ( v ) ) ; } , } , finalize : ( config , env , internals ) => { const previous = settings . build . finalize ( config , env , internals ) ; Object . assign ( previous . resolve ?. alias , buildPeerAlias ( cwd , includedSiblings ) ) ; for ( const { name , directory } of includedSiblings ) { Object . assign ( previous . resolve ?. alias , { [ `${ name }` ] : path . join ( directory , 'src' ) } ) ; } return previous ; } , } , } ; } ;