import { resolve } from 'path' ; import Debug from 'debug' ; import { slash } from '@antfu/utils' ; import { MODULE_ID_VIRTUAL } from './constants' ; import type { ViteDevServer } from 'vite' ; import type { ResolvedOptions } from './types' ; export const debug = { hmr : Debug ( 'vite-plugin-pages:hmr' ) , routeBlock : Debug ( 'vite-plugin-pages:routeBlock' ) , options : Debug ( 'vite-plugin-pages:options' ) , pages : Debug ( 'vite-plugin-pages:pages' ) , search : Debug ( 'vite-plugin-pages:search' ) , env : Debug ( 'vite-plugin-pages:env' ) , cache : Debug ( 'vite-plugin-pages:cache' ) , resolver : Debug ( 'vite-plugin-pages:resolver' ) , } ; export function extsToGlob ( extensions ) { return extensions . length > 1 ? `{${ extensions . join ( ',' ) }}` : extensions [ 0 ] || '' ; } export function countSlash ( value ) { return ( value . match ( /\//g ) || [ ] ) . length ; } function isPagesDir ( path , options ) { for ( const page of options . dirs ) { const dirPath = slash ( resolve ( options . root , page . dir ) ) ; if ( path . startsWith ( dirPath ) ) return true ; } return false ; } export function isTarget ( path , options ) { return isPagesDir ( path , options ) && options . extensionsRE . test ( path ) ; } const dynamicRouteRE = /^\[.+\]$/ ; export const nuxtDynamicRouteRE = /^_[\s\S]*$/ ; export function isDynamicRoute ( routePath , nuxtStyle = false ) { return nuxtStyle ? nuxtDynamicRouteRE . test ( routePath ) : dynamicRouteRE . test ( routePath ) ; } export function isCatchAllRoute ( routePath , nuxtStyle = false ) { return nuxtStyle ? /^_$/ . test ( routePath ) : /^\[\.{3}/ . test ( routePath ) ; } export function resolveImportMode ( filepath , options ) { const mode = options . importMode ; if ( typeof mode === 'function' ) return mode ( filepath ) ; for ( const page of options . dirs ) { if ( options . syncIndex && page . baseRoute === '' && filepath === `/${ page . dir }/index.vue` ) return 'sync' ; } return mode ; } export function pathToName ( filepath ) { return filepath . replace ( /[_.\-\\/]/g , '_' ) . replace ( /[[:\]()]/g , '$' ) ; } export function invalidatePagesModule ( server ) { const { moduleGraph } = server ; const module = moduleGraph . getModuleById ( MODULE_ID_VIRTUAL ) ; if ( module ) { moduleGraph . invalidateModule ( module ) ; return module ; } }