import path from 'path' ; import { ROUTE_SPEC_FILE , fs , isSingleEntry } from '@modern-js/utils' ; import { ServerRoute as ModernRoute } from '@modern-js/types' ; import { EntryPoint , MultiEntryOptions , SSG , SsgRoute } from '../types' ; export function formatOutput ( filename ) { const outputPath = path . extname ( filename ) ? filename : `${ filename }/index.html` ; return outputPath ; } export function formatPath ( str ) { let addr = str ; if ( ! addr || typeof addr !== 'string' ) { return addr ; } if ( addr . startsWith ( '.' ) ) { addr = addr . slice ( 1 ) ; } if ( ! addr . startsWith ( '/' ) ) { addr = `/${ addr }` ; } if ( addr . endsWith ( '/' ) && addr !== '/' ) { addr = addr . slice ( 0 , addr . length - 1 ) ; } return addr ; } export function isDynamicUrl ( url ) { return url . includes ( ':' ) ; } export function getUrlPrefix ( route , baseUrl ) { let base = '' ; if ( Array . isArray ( baseUrl ) ) { const filters = baseUrl . filter ( url => route . urlPath . includes ( url ) ) ; if ( filters . length > 1 ) { const matched = filters . sort ( ( a , b ) => a . length - b . length ) [ 0 ] ; if ( ! matched ) { throw new Error ( '' ) ; } base = matched ; } } else { base = baseUrl ; } base = base === '/' ? '' : base ; const entryName = route . entryName === 'main' ? '' : route . entryName ; const prefix = `${ base }/${ entryName as string }` ; return prefix . endsWith ( '/' ) ? prefix . slice ( 0 , - 1 ) : prefix ; } export function getOutput ( route , base , agreed ? ) { const { output } = route ; if ( output ) { return output ; } if ( agreed ) { const urlWithoutBase = route . urlPath . replace ( base , '' ) ; return urlWithoutBase . startsWith ( '/' ) ? urlWithoutBase . slice ( 1 ) : urlWithoutBase ; } throw new Error ( `routing must provide output when calling createPage(), check ${ route . urlPath }` ) ; } export const readJSONSpec = ( dir ) => { const routeJSONPath = path . join ( dir , ROUTE_SPEC_FILE ) ; const routeJSON = require ( routeJSONPath ) ; const { routes } = routeJSON ; return routes ; } ; export const writeJSONSpec = ( dir , routes ) => { const routeJSONPath = path . join ( dir , ROUTE_SPEC_FILE ) ; fs . writeJSONSync ( routeJSONPath , { routes } , { spaces : 2 } ) ; } ; export const replaceWithAlias = ( base , filePath , alias ) => path . posix . join ( alias , path . posix . relative ( base , filePath ) ) ; export const standardOptions = ( ssgOptions , entrypoints ) => { if ( ssgOptions === false ) { return false ; } if ( ssgOptions === true ) { return entrypoints . reduce ( ( opt , entry ) => { opt [ entry . entryName ] = ssgOptions ; return opt ; } , { } as MultiEntryOptions ) ; } else if ( typeof ssgOptions === 'object' ) { const isSingle = isSingleEntry ( entrypoints ) ; if ( isSingle && typeof ( ssgOptions as any ) . main === 'undefined' ) { return { main : ssgOptions } as MultiEntryOptions ; } else { return ssgOptions as MultiEntryOptions ; } } else if ( typeof ssgOptions === 'function' ) { const intermediateOptions = { } ; for ( const entrypoint of entrypoints ) { const { entryName } = entrypoint ; intermediateOptions [ entryName ] = ssgOptions ( entryName ) ; } return intermediateOptions ; } return false ; } ;