2021-10-02 16:01:17 +00:00
import { parse , relative } from 'pathe'
2021-07-28 11:35:24 +00:00
import consola from 'consola'
2021-11-21 16:14:46 +00:00
import type { Nuxt , NuxtPluginTemplate , NuxtTemplate , ModuleContainer } from '@nuxt/schema'
import { chainFn } from '../internal/task'
import { addTemplate } from '../template'
import { addServerMiddleware } from '../server'
import { isNuxt2 } from '../compatibility'
import { addPluginTemplate } from '../plugin'
2021-12-21 13:57:26 +00:00
import { useNuxt } from '../context'
2021-05-24 11:14:10 +00:00
import { installModule } from './install'
2021-04-02 11:47:01 +00:00
2021-12-21 13:57:26 +00:00
const MODULE_CONTAINER_KEY = '__module_container__'
export function useModuleContainer ( nuxt : Nuxt = useNuxt ( ) ) : ModuleContainer {
if ( nuxt [ MODULE_CONTAINER_KEY ] ) {
return nuxt [ MODULE_CONTAINER_KEY ]
}
async function requireModule ( module Opts ) {
let src , inlineOptions
if ( typeof module Opts === 'string' ) {
src = module Opts
} else if ( Array . isArray ( module Opts ) ) {
[ src , inlineOptions ] = module Opts
} else if ( typeof module Opts === 'object' ) {
if ( module Opts.src || module Opts.handler ) {
src = module Opts.src || module Opts.handler
inlineOptions = module Opts.options
} else {
src = module Opts
}
} else {
src = module Opts
}
await installModule ( src , inlineOptions , nuxt )
}
nuxt [ MODULE_CONTAINER_KEY ] = < ModuleContainer > {
2021-06-25 09:37:18 +00:00
nuxt ,
options : nuxt.options ,
2021-04-02 11:47:01 +00:00
2021-11-21 16:14:46 +00:00
ready ( ) { return Promise . resolve ( ) } ,
addVendor ( ) { } ,
2021-04-02 11:47:01 +00:00
2021-12-21 13:57:26 +00:00
requireModule ,
addModule : requireModule ,
2021-11-21 16:14:46 +00:00
addServerMiddleware ,
2021-04-02 11:47:01 +00:00
2021-08-09 20:54:12 +00:00
addTemplate ( template : string | NuxtTemplate ) {
if ( typeof template === 'string' ) {
template = { src : template }
}
if ( template . write === undefined ) {
template . write = true
}
return addTemplate ( template )
} ,
2021-04-02 11:47:01 +00:00
2021-07-28 11:35:24 +00:00
addPlugin ( pluginTemplate : NuxtPluginTemplate ) : NuxtPluginTemplate {
return addPluginTemplate ( pluginTemplate )
} ,
2021-04-02 11:47:01 +00:00
2021-07-28 11:35:24 +00:00
addLayout ( tmpl : NuxtTemplate , name : string ) {
const { filename , src } = addTemplate ( tmpl )
2021-10-02 16:01:17 +00:00
const layoutName = name || parse ( src ) . name
2021-07-28 11:35:24 +00:00
const layout = nuxt . options . layouts [ layoutName ]
if ( layout ) {
consola . warn ( ` Duplicate layout registration, " ${ layoutName } " has been registered as " ${ layout } " ` )
}
nuxt . options . layouts [ layoutName ] = ` ./ ${ filename } `
if ( name === 'error' ) {
this . addErrorLayout ( filename )
}
} ,
2021-04-02 11:47:01 +00:00
2021-07-28 11:35:24 +00:00
addErrorLayout ( dst : string ) {
2021-10-02 16:01:17 +00:00
const relativeBuildDir = relative ( nuxt . options . rootDir , nuxt . options . buildDir )
2021-07-28 11:35:24 +00:00
nuxt . options . ErrorPage = ` ~/ ${ relativeBuildDir } / ${ dst } `
} ,
2021-04-02 11:47:01 +00:00
2021-07-28 11:35:24 +00:00
extendBuild ( fn ) {
// @ts-ignore
nuxt . options . build . extend = chainFn ( nuxt . options . build . extend , fn )
} ,
2021-04-02 11:47:01 +00:00
2021-07-28 11:35:24 +00:00
extendRoutes ( fn ) {
2021-11-16 16:13:19 +00:00
if ( isNuxt2 ( nuxt ) ) {
nuxt . options . router . extendRoutes = chainFn ( nuxt . options . router . extendRoutes , fn )
} else {
nuxt . hook ( 'pages:extend' , async ( pages , . . . args ) = > {
const maybeRoutes = await fn ( pages , . . . args )
if ( maybeRoutes ) {
console . warn ( '[kit] [compat] Using `extendRoutes` in Nuxt 3 needs to directly modify first argument instead of returning updated routes. Skipping extended routes.' )
}
} )
}
2021-11-21 16:14:46 +00:00
}
2021-04-02 11:47:01 +00:00
}
2021-12-21 13:57:26 +00:00
return nuxt [ MODULE_CONTAINER_KEY ]
2021-04-02 11:47:01 +00:00
}