import * as pkg from '../package.json' ; import micro from 'micro' ; import NuxtentConfig from './config' ; import createRouter from './content/api' ; import { addAssets , createStaticRoutes } from './content/build' ; import { logger , generatePluginMap } from './utils' ; import { Nuxt } from '../types/nuxt.js' ; import { Configuration as WebpackConfiguration } from 'webpack' ; import createServer from 'connect' ; description export async function nuxtentModule ( this , moduleOptions ) { const self = this ; self . options . watch . push ( '~/nuxtent.config.js' ) ; const nuxtentConfig = new NuxtentConfig ( moduleOptions , self . options ) ; nuxtentConfig . setApi ( self . options ) ; await nuxtentConfig . init ( self . options . rootDir ) ; nuxtentConfig . createContentDatabase ( ) ; const nuxtentRouter = createRouter ( nuxtentConfig ) ; this . addServerMiddleware ( { handler : nuxtentRouter , path : nuxtentConfig . api . apiServerPrefix , } ) ; this . options . build . templates . push ( { dst : 'nuxtent-config.js' , options : nuxtentConfig . config , src : require . resolve ( './plugins/nuxtent-config.template' ) , } ) ; this . extendBuild ( ( config , loaders ) => { if ( config . module ) { config . module . rules . push ( { test : /\.comp\.md$/ , use : [ 'vue-loader' , { loader : require . resolve ( './loader' ) , options : { componentsDir : nuxtentConfig . build . componentsDir , content : nuxtentConfig . content , database : nuxtentConfig . database , extensions : nuxtentConfig . build . loaderComponentExtensions , } , } , ] , } ) ; } } ) ; this . nuxt . hook ( 'listen' , async ( ) => { nuxtentConfig . setApi ( self . options ) ; } ) ; self . nuxt . hook ( 'build:before' , async ( builder , buildOptions ) => { const isStatic = ( ( builder . bundleBuilder || { } ) . buildContext || { } ) . isStatic || process . static ; if ( typeof isStatic === 'undefined' ) { logger . error ( "Can't define if this is a static build or not" ) ; } nuxtentConfig . isStatic = ! ! isStatic ; logger . info ( `Nuxtent Initiated in ${ nuxtentConfig . isStatic ? 'static' : 'dynamic' } mode` ) ; nuxtentConfig . interceptRoutes ( self ) ; this . addPlugin ( { src : require . resolve ( './plugins/nuxtent-request' ) , } ) ; this . addPlugin ( { options : { components : generatePluginMap ( nuxtentConfig . database ) , } , src : require . resolve ( './plugins/nuxtent-components.template' ) , } ) ; } ) ; this . nuxt . hook ( 'generate:before' , async ( nuxt , generateOptions ) => { createStaticRoutes ( nuxtentConfig ) ; addAssets ( this . options , nuxtentConfig . assetMap ) ; generateOptions . routes = generateOptions . routes ? generateOptions . routes . concat ( nuxtentConfig . staticRoutes ) : nuxtentConfig . staticRoutes ; } ) ; this . nuxt . hook ( 'build:done' , async ( ) => { logger . info ( `Generating: ${ String ( nuxtentConfig . isStatic ) }` ) ; if ( nuxtentConfig . isStatic ) { logger . info ( 'opening server connection' ) ; const app = await micro ( nuxtentRouter . namespaced ( ) ) ; logger . info ( `prefix: ${ nuxtentConfig . api . apiServerPrefix } baseurl: ${ nuxtentConfig . api . baseURL }` ) ; const server = await app . listen ( nuxtentConfig . api . port ) ; this . nuxt . hook ( 'generate:done' , ( ) => { logger . info ( 'closing server connection' ) ; server . close ( ) ; } ) ; } } ) ; } export { pkg as meta } ; export default nuxtentModule ;