import { pathToName , resolveImportMode , } from './utils' ; import type { ResolvedOptions } from './types' ; const componentRE = /"(?:component|element)":("(.*?)")/g ; const hasFunctionRE = /"(?:props|beforeEnter)":("(.*?)")/g ; const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm ; const singlelineCommentsRE = /\/\/.*/g ; function replaceFunction ( _ , value ) { if ( value instanceof Function || typeof value === 'function' ) { const fnBody = value . toString ( ) . replace ( multilineCommentsRE , '' ) . replace ( singlelineCommentsRE , '' ) . replace ( /(\t|\n|\r|\s)/g , '' ) ; if ( fnBody . length < 8 || fnBody . substring ( 0 , 8 ) !== 'function' ) return `_NuFrRa_${ fnBody }` ; return fnBody ; } return value ; } /**
 * Creates a stringified Vue Router route definition.
 */ export function stringifyRoutes ( preparedRoutes , options ) { const imports = [ ] ; function componentReplacer ( str , replaceStr , path ) { const mode = resolveImportMode ( path , options ) ; if ( mode === 'sync' ) { const importName = pathToName ( path ) ; const importStr = `import ${ importName } from "${ path }"` ; if ( ! imports . includes ( importStr ) ) imports . push ( importStr ) ; if ( options . resolver === 'react' ) return str . replace ( replaceStr , `React.createElement(${ importName })` ) ; else return str . replace ( replaceStr , importName ) ; } else { if ( options . resolver === 'react' ) return str . replace ( replaceStr , `React.lazy(() => import('${ path }'))` ) ; else return str . replace ( replaceStr , `() => import('${ path }')` ) ; } } function functionReplacer ( str , replaceStr , content ) { if ( content . startsWith ( 'function' ) ) return str . replace ( replaceStr , content ) ; if ( content . startsWith ( '_NuFrRa_' ) ) return str . replace ( replaceStr , content . slice ( 8 ) ) ; return str ; } const stringRoutes = JSON . stringify ( preparedRoutes , replaceFunction ) . replace ( componentRE , componentReplacer ) . replace ( hasFunctionRE , functionReplacer ) ; return { imports , stringRoutes , } ; } export function generateClientCode ( routes , options ) { const { imports , stringRoutes } = stringifyRoutes ( routes , options ) ; if ( options . resolver === 'react' ) imports . push ( 'import React from \"react\"' ) ; return `${ imports . join ( ';\n' ) };\n\nconst routes = ${ stringRoutes };\n\nexport default routes;` ; }