This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
container.ts
100 lines (84 loc) · 2.88 KB
/
container.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { relative } from 'pathe'
import type { Nuxt, ModuleContainer } from '@nuxt/schema'
import { chainFn } from '../internal/task'
import { addTemplate } from '../template'
import { addLayout } from '../layout'
import { isNuxt2 } from '../compatibility'
import { addPluginTemplate } from '../plugin'
import { useNuxt } from '../context'
import { installModule } from './install'
const MODULE_CONTAINER_KEY = '__module_container__'
export function useModuleContainer (nuxt: Nuxt = useNuxt()): ModuleContainer {
// @ts-ignore
if (nuxt[MODULE_CONTAINER_KEY]) { return nuxt[MODULE_CONTAINER_KEY] }
async function requireModule (moduleOpts: any) {
let src, inlineOptions
if (typeof moduleOpts === 'string') {
src = moduleOpts
} else if (Array.isArray(moduleOpts)) {
[src, inlineOptions] = moduleOpts
} else if (typeof moduleOpts === 'object') {
if (moduleOpts.src || moduleOpts.handler) {
src = moduleOpts.src || moduleOpts.handler
inlineOptions = moduleOpts.options
} else {
src = moduleOpts
}
} else {
src = moduleOpts
}
await installModule(src, inlineOptions)
}
const container: ModuleContainer = {
nuxt,
options: nuxt.options,
ready () { return Promise.resolve() },
addVendor () {},
requireModule,
addModule: requireModule,
// TODO
addServerMiddleware: () => { },
addTemplate (template) {
if (typeof template === 'string') {
template = { src: template }
}
if (template.write === undefined) {
template.write = true
}
return addTemplate(template)
},
addPlugin (pluginTemplate) {
return addPluginTemplate(pluginTemplate)
},
addLayout (tmpl, name) {
return addLayout(tmpl, name)
},
addErrorLayout (dst) {
const relativeBuildDir = relative(nuxt.options.rootDir, nuxt.options.buildDir)
;(nuxt as any).options.ErrorPage = `~/${relativeBuildDir}/${dst}`
},
extendBuild (fn) {
// @ts-ignore
nuxt.options.build.extend = chainFn(nuxt.options.build.extend, fn)
if (!isNuxt2(nuxt)) {
console.warn('[kit] [compat] Using `extendBuild` in Nuxt 3 has no effect. Instead call extendWebpackConfig and extendViteConfig.')
}
},
extendRoutes (fn) {
if (isNuxt2(nuxt)) {
(nuxt.options.router as any).extendRoutes = chainFn((nuxt.options.router as any).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.')
}
})
}
}
}
// @ts-ignore
nuxt[MODULE_CONTAINER_KEY] = container
// @ts-ignore
return nuxt[MODULE_CONTAINER_KEY]
}