import path from 'path' ; import { ensureAbsolutePath , fs , findExists , MAIN_ENTRY_NAME , } from '@modern-js/utils' ; import { IAppContext , NormalizedConfig } from '@modern-js/core' ; import type { Entrypoint } from '@modern-js/types' ; import { getFileSystemEntry } from './getFileSystemEntry' ; import { JS_EXTENSIONS } from './constants' ; const ensureExtensions = ( file ) => { if ( ! path . extname ( file ) ) { return findExists ( JS_EXTENSIONS . map ( ext => `${ file }${ ext }` ) ) || file ; } return file ; } ; const ifAlreadyExists = ( entrypoints , checked ) : boolean => entrypoints . some ( entrypoint => { if ( ensureExtensions ( entrypoint . entry ) === ensureExtensions ( checked . entry ) ) { checked . entryName = entrypoint . entryName ; return true ; } if ( entrypoint . entry . startsWith ( checked . entry ) || checked . entry . startsWith ( entrypoint . entry ) ) { throw new Error ( `Entry configuration conflicts\n Your configuration: ${ checked . entry }.\n Default entrypoint: ${ entrypoint . entry }\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules.` ) ; } return false ; } ) ; export const getBundleEntry = ( appContext , config ) => { const { appDirectory , packageName } = appContext ; const { source : { disableDefaultEntries , entries , entriesDir } , } = config ; const defaults = disableDefaultEntries ? [ ] : getFileSystemEntry ( appContext , config ) ; if ( entries ) { Object . keys ( entries ) . forEach ( name => { const value = entries [ name ] ; const entrypoint = typeof value === 'string' ? { entryName : name , entry : ensureAbsolutePath ( appDirectory , value ) , isAutoMount : true , fileSystemRoutes : fs . statSync ( ensureAbsolutePath ( appDirectory , value ) ) . isDirectory ( ) ? { } : undefined , } : { entryName : name , entry : ensureAbsolutePath ( appDirectory , value . entry ) , isAutoMount : ! value . disableMount , fileSystemRoutes : value . enableFileSystemRoutes ? { } : undefined , } ; if ( ! ifAlreadyExists ( defaults , entrypoint ) ) { defaults . push ( entrypoint ) ; } } ) ; } if ( defaults . length === 1 && path . dirname ( defaults [ 0 ] . entry ) === ensureAbsolutePath ( appDirectory , entriesDir ! ) ) { defaults [ 0 ] . entryName = MAIN_ENTRY_NAME ; } else { const found = defaults . find ( ( { entryName } ) => entryName === packageName ) ; found && ( found . entryName = MAIN_ENTRY_NAME ) ; } return defaults ; } ;