refactor(kit)!: remove support for module container (#9010)

This commit is contained in:
Daniel Roe 2022-11-15 13:22:14 +00:00 committed by GitHub
parent 4a779460a5
commit 74b81bbeaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 3 additions and 151 deletions

View File

@ -22,7 +22,7 @@ By using [vue-demi](https://github.com/vueuse/vue-demi) they should be compatibl
## Module Migration ## Module Migration
When Nuxt 3 users add your module, a compatible module container layer from `@nuxt/kit` is **automatically injected**, so as long as your code is following the guidelines below, it should continue working as-is. When Nuxt 3 users add your module, you will not have access to the module container (`this.*`) so you will need to use utilities from `@nuxt/kit` to access the container functionality.
### Test with `@nuxt/bridge` ### Test with `@nuxt/bridge`

View File

@ -1,5 +1,4 @@
// Module // Module
export * from './module/container'
export * from './module/define' export * from './module/define'
export * from './module/install' export * from './module/install'

View File

@ -1,100 +0,0 @@
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]
}

View File

@ -2,7 +2,6 @@ import type { Nuxt, NuxtModule } from '@nuxt/schema'
import { useNuxt } from '../context' import { useNuxt } from '../context'
import { resolveModule, requireModule, importModule } from '../internal/cjs' import { resolveModule, requireModule, importModule } from '../internal/cjs'
import { resolveAlias } from '../resolve' import { resolveAlias } from '../resolve'
import { useModuleContainer } from './container'
/** Installs a module on a Nuxt instance. */ /** Installs a module on a Nuxt instance. */
export async function installModule (moduleToInstall: string | NuxtModule, _inlineOptions?: any, _nuxt?: Nuxt) { export async function installModule (moduleToInstall: string | NuxtModule, _inlineOptions?: any, _nuxt?: Nuxt) {
@ -10,12 +9,7 @@ export async function installModule (moduleToInstall: string | NuxtModule, _inli
const { nuxtModule, inlineOptions } = await normalizeModule(moduleToInstall, _inlineOptions) const { nuxtModule, inlineOptions } = await normalizeModule(moduleToInstall, _inlineOptions)
// Call module // Call module
await nuxtModule.call( await nuxtModule(inlineOptions, nuxt)
// Provide this context for backwards compatibility with Nuxt 2
useModuleContainer() as any,
inlineOptions,
nuxt
)
nuxt.options._installedModules = nuxt.options._installedModules || [] nuxt.options._installedModules = nuxt.options._installedModules || []
nuxt.options._installedModules.push({ nuxt.options._installedModules.push({

View File

@ -1,5 +1,5 @@
import { NuxtHooks } from './hooks' import { NuxtHooks } from './hooks'
import type { Nuxt, NuxtPluginTemplate, NuxtTemplate } from "./nuxt" import type { Nuxt } from "./nuxt"
import type { NuxtCompatibility } from './compatibility' import type { NuxtCompatibility } from './compatibility'
export interface ModuleMeta { export interface ModuleMeta {
@ -41,44 +41,3 @@ export interface NuxtModule<T extends ModuleOptions = ModuleOptions> {
getOptions?: (inlineOptions?: T, nuxt?: Nuxt) => Promise<T> getOptions?: (inlineOptions?: T, nuxt?: Nuxt) => Promise<T>
getMeta?: () => Promise<ModuleMeta> getMeta?: () => Promise<ModuleMeta>
} }
/**
* Legacy ModuleContainer for backwards compatibility with Nuxt 2 module format.
*/
export interface ModuleContainer {
nuxt: Nuxt
options: Nuxt['options']
/** @deprecated */
ready(): Promise<any>
/** @deprecated */
addVendor(): void
/** Renders given template using lodash template during build into the project buildDir (`.nuxt`).*/
addTemplate(template: string | NuxtTemplate): NuxtTemplate
/** Registers a custom plugin. */
addPlugin(template: NuxtPluginTemplate): NuxtPluginTemplate
/** Registers a custom layout. If its name is 'error' it will override the default error layout. */
addLayout(tmpl: NuxtTemplate, name: string): any
/** Sets the layout that will render Nuxt errors. It should already have been added via addLayout or addTemplate. */
addErrorLayout(dst: string): void
/** Adds a new server middleware to the end of the server middleware array. */
addServerMiddleware(arg1: any): void
/** Allows extending webpack build config by chaining `options.build.extend` function. */
extendBuild(fn: Function): void
/** Allows extending routes by chaining `options.router.extendRoutes` function. */
extendRoutes(fn: Function): void
/** Registers a module. */
requireModule(installOptions: any, opts: any): Promise<void>
/** Registers a module. */
addModule(installOptions: any, opts: any): Promise<void>
}