import path from 'path' ; import type Config from 'webpack-chain' ; import readDir from 'fs-readdir-recursive' ; import type { ConfigManager } from '../../../config/configManager' ; import type { ApplicationConfigEntry } from '../../../typings/configEntry/application' ; const filesToPages = ( { configManager , pagesRootDirectory , config , } ) => { const pagesDir = path . resolve ( configManager . rootDir , configManager . root , pagesRootDirectory ) ; const pagesFiles = readDir ( pagesDir , ( name ) => name [ 0 ] !== '.' && name [ 0 ] !== '_' ) ; const fsPages = [ ] ; for ( const file of pagesFiles ) { const extname = path . extname ( file ) ; const name = file . replace ( new RegExp ( `\\${ extname }$` ) , '' ) . replace ( /\\/g , '/' ) ; if ( config . resolve . extensions . has ( extname ) ) { const pageComponentName = `@/${ pagesRootDirectory }/${ name }` ; const pageComponentPath = path . resolve ( pagesDir , name ) . replace ( /\\/g , '\\\\' ) ; const pageComponentChunkName = pageComponentName . replace ( /\//g , '_' ) ; fsPages . push ( `'${ pageComponentName }': lazy(() => import(/* webpackChunkName: "${ pageComponentChunkName }" */ '${ pageComponentPath }'))` ) ; } } return fsPages ; } ; export const pagesResolve = ( configManager ) => ( config ) => { const { fileSystemPages } = configManager . build . configurations . experiments ; const fsRoutes = fileSystemPages . routesDir ? filesToPages ( { config , configManager , pagesRootDirectory : fileSystemPages . routesDir , } ) : [ ] ; const fsPages = fileSystemPages . pagesDir ? filesToPages ( { config , configManager , pagesRootDirectory : fileSystemPages . pagesDir , } ) : [ ] ; config . module . rule ( 'file-system-pages' ) . test ( /[\\/]cli[\\/]lib[\\/]external[\\/]pages.js$/ ) . use ( 'replace' ) . loader ( path . resolve ( __dirname , '../loaders/replaceContent' ) ) . options ( { code : `import { lazy } from '@tramvai/react';

export default {
  routes: {
    ${ fsRoutes . join ( ',\n' ) }
  },
  pages: {
    ${ fsPages . join ( ',\n' ) }
  },
}` , } ) . end ( ) . enforce ( 'pre' ) ; } ;