import Shell from 'shelljs' ; import axios from 'axios' ; import fs from 'fs' ; import { resolveDir , logGreen } from './utils' ; import { ServerJs } from './interface/config' ; const getServerBundle = async ( cdn , path ) : Promise < ServerJs > => { logGreen ( `get serverBundle from CDN file ${ cdn }` ) ; const res = await axios . get ( cdn ) ; const str = res . data ; Shell . mkdir ( resolveDir ( './.serverBundle' ) ) ; fs . writeFileSync ( path , str ) ; const serverJs = require ( path ) . default ; return serverJs ; } ; const useCdn = async ( serverJs , isLocal , filename ) : Promise < ServerJs > => { const serverJsPath = resolveDir ( `./.serverBundle/${ filename }.js` ) ; let SEVER_JS ; if ( isLocal ) { delete require . cache [ serverJsPath ] ; } if ( fs . existsSync ( serverJsPath ) ) { if ( isLocal ) { SEVER_JS = await getServerBundle ( serverJs , serverJsPath ) ; } } else { SEVER_JS = await getServerBundle ( serverJs , serverJsPath ) ; } if ( ! isLocal ) { logGreen ( `get serverBundle from local file ${ serverJsPath }` ) ; SEVER_JS = require ( serverJsPath ) . default ; } return SEVER_JS ; } ; export { useCdn } ;