mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
fix(kit): don't rely on ModuleThis (#272)
This commit is contained in:
parent
5b8c4caecc
commit
dfbb047be6
@ -1,7 +1,4 @@
|
||||
import { nuxtCtx } from '../nuxt'
|
||||
import type { Nuxt } from '../types/nuxt'
|
||||
import type { NuxtOptions } from '../types/config'
|
||||
import type { TemplateOpts, PluginTemplateOpts } from '../types/module'
|
||||
import {
|
||||
addTemplate,
|
||||
addErrorLayout,
|
||||
@ -14,19 +11,10 @@ import {
|
||||
import { installModule } from './install'
|
||||
|
||||
/** Legacy ModuleContainer for backwards compatibility with existing Nuxt 2 modules. */
|
||||
export class ModuleContainer {
|
||||
nuxt: Nuxt
|
||||
options: NuxtOptions
|
||||
|
||||
constructor (nuxt: Nuxt) {
|
||||
this.nuxt = nuxt
|
||||
this.options = nuxt.options
|
||||
}
|
||||
|
||||
private _call<F extends (...args: any) => any>(fn: F, ...args: Parameters<F>): ReturnType<F> {
|
||||
// @ts-ignore
|
||||
return nuxtCtx.call(this.nuxt, () => fn(...args))
|
||||
}
|
||||
export function createModuleContainer (nuxt: Nuxt) {
|
||||
return {
|
||||
nuxt,
|
||||
options: nuxt.options,
|
||||
|
||||
/**
|
||||
* Returns a resolved promise immediately.
|
||||
@ -35,12 +23,12 @@ export class ModuleContainer {
|
||||
*/
|
||||
ready () {
|
||||
return Promise.resolve()
|
||||
}
|
||||
},
|
||||
|
||||
/** @deprecated */
|
||||
addVendor () {
|
||||
console.warn('addVendor has been deprecated and has no effect.')
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Renders given template using lodash template during build into the project buildDir (`.nuxt`).
|
||||
@ -48,9 +36,7 @@ export class ModuleContainer {
|
||||
* If a fileName is not provided or the template is string, target file name defaults to
|
||||
* [dirName].[fileName].[pathHash].[ext].
|
||||
*/
|
||||
addTemplate (tmpl: TemplateOpts | string) {
|
||||
return this._call(addTemplate, tmpl)
|
||||
}
|
||||
addTemplate,
|
||||
|
||||
/**
|
||||
* Registers a plugin using `addTemplate` and prepends it to the plugins[] array.
|
||||
@ -70,46 +56,33 @@ export class ModuleContainer {
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
addPlugin (tmpl: PluginTemplateOpts) {
|
||||
return this._call(addPlugin, tmpl)
|
||||
}
|
||||
addPlugin,
|
||||
|
||||
/** Register a custom layout. If its name is 'error' it will override the default error layout. */
|
||||
addLayout (tmpl: TemplateOpts, name: string) {
|
||||
return this._call(addLayout, tmpl, name)
|
||||
}
|
||||
addLayout,
|
||||
|
||||
/**
|
||||
* Set the layout that will render Nuxt errors. It should already have been added via addLayout or addTemplate.
|
||||
*
|
||||
* @param dst - Path to layout file within the buildDir (`.nuxt/<dst>.vue`)
|
||||
*/
|
||||
addErrorLayout (dst: string) {
|
||||
return this._call(addErrorLayout, dst)
|
||||
}
|
||||
addErrorLayout,
|
||||
|
||||
/** Adds a new server middleware to the end of the server middleware array. */
|
||||
addServerMiddleware (middleware) {
|
||||
return this._call(addServerMiddleware, middleware)
|
||||
}
|
||||
addServerMiddleware,
|
||||
|
||||
/** Allows extending webpack build config by chaining `options.build.extend` function. */
|
||||
extendBuild (fn) {
|
||||
return this._call(extendBuild, fn)
|
||||
}
|
||||
extendBuild,
|
||||
|
||||
/** Allows extending routes by chaining `options.build.extendRoutes` function. */
|
||||
extendRoutes (fn) {
|
||||
return this._call(extendRoutes, fn)
|
||||
}
|
||||
extendRoutes,
|
||||
|
||||
/** `requireModule` is a shortcut for `addModule` */
|
||||
requireModule (moduleOpts: string | [src: string, options: any]) {
|
||||
return installModule(this.nuxt, moduleOpts)
|
||||
}
|
||||
requireModule: installModule,
|
||||
|
||||
/** Registers a module. moduleOpts can be a string or an array ([src, options]). */
|
||||
addModule (moduleOpts: string | [src: string, options: any]) {
|
||||
return installModule(this.nuxt, moduleOpts)
|
||||
addModule: installModule
|
||||
}
|
||||
}
|
||||
|
||||
export type ModuleContainer = ReturnType<typeof createModuleContainer>
|
||||
|
@ -3,7 +3,7 @@ import { resolveAlias } from '../utils/resolve'
|
||||
import type { LegacyNuxtModule, NuxtModule, ModuleMeta, ModuleInstallOptions, ModuleOptions, ModuleSrc } from '../types/module'
|
||||
import type { Nuxt } from '../types/nuxt'
|
||||
import { defineNuxtModule } from './define'
|
||||
import { ModuleContainer } from './container'
|
||||
import { createModuleContainer } from './container'
|
||||
|
||||
/** Installs a module on a Nuxt instance. */
|
||||
export async function installModule (nuxt: Nuxt, installOpts: ModuleInstallOptions) {
|
||||
@ -57,6 +57,6 @@ export async function installModule (nuxt: Nuxt, installOpts: ModuleInstallOptio
|
||||
}
|
||||
|
||||
// Execute in legacy container
|
||||
const container = new ModuleContainer(nuxt)
|
||||
const container = createModuleContainer(nuxt)
|
||||
await handler.call(container, options)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user