diff --git a/packages/kit/src/module/container.ts b/packages/kit/src/module/container.ts index aef1f9d683..9a8c0dddbd 100644 --- a/packages/kit/src/module/container.ts +++ b/packages/kit/src/module/container.ts @@ -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,102 +11,78 @@ import { import { installModule } from './install' /** Legacy ModuleContainer for backwards compatibility with existing Nuxt 2 modules. */ -export class ModuleContainer { - nuxt: Nuxt - options: NuxtOptions +export function createModuleContainer (nuxt: Nuxt) { + return { + nuxt, + options: nuxt.options, - constructor (nuxt: Nuxt) { - this.nuxt = nuxt - this.options = nuxt.options - } + /** + * Returns a resolved promise immediately. + * + * @deprecated + */ + ready () { + return Promise.resolve() + }, - private _call any>(fn: F, ...args: Parameters): ReturnType { - // @ts-ignore - return nuxtCtx.call(this.nuxt, () => fn(...args)) - } + /** @deprecated */ + addVendor () { + console.warn('addVendor has been deprecated and has no effect.') + }, - /** - * Returns a resolved promise immediately. - * - * @deprecated - */ - ready () { - return Promise.resolve() - } + /** + * Renders given template using lodash template during build into the project buildDir (`.nuxt`). + * + * If a fileName is not provided or the template is string, target file name defaults to + * [dirName].[fileName].[pathHash].[ext]. + */ + addTemplate, - /** @deprecated */ - addVendor () { - console.warn('addVendor has been deprecated and has no effect.') - } + /** + * Registers a plugin using `addTemplate` and prepends it to the plugins[] array. + * + * Note: You can use mode or .client and .server modifiers with fileName option + * to use plugin only in client or server side. + * + * If you choose to specify a fileName, you can configure a custom path for the + * fileName too, so you can choose the folder structure inside .nuxt folder in + * order to prevent name collisioning: + * + * @example + * ```js + * this.addPlugin({ + * src: path.resolve(__dirname, 'templates/foo.js'), + * fileName: 'foo.server.js' // [optional] only include in server bundle + * }) + * ``` + */ + addPlugin, - /** - * Renders given template using lodash template during build into the project buildDir (`.nuxt`). - * - * 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) - } + /** Register a custom layout. If its name is 'error' it will override the default error layout. */ + addLayout, - /** - * Registers a plugin using `addTemplate` and prepends it to the plugins[] array. - * - * Note: You can use mode or .client and .server modifiers with fileName option - * to use plugin only in client or server side. - * - * If you choose to specify a fileName, you can configure a custom path for the - * fileName too, so you can choose the folder structure inside .nuxt folder in - * order to prevent name collisioning: - * - * @example - * ```js - * this.addPlugin({ - * src: path.resolve(__dirname, 'templates/foo.js'), - * fileName: 'foo.server.js' // [optional] only include in server bundle - * }) - * ``` - */ - addPlugin (tmpl: PluginTemplateOpts) { - return this._call(addPlugin, tmpl) - } + /** + * 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/.vue`) + */ + addErrorLayout, - /** 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) - } + /** Adds a new server middleware to the end of the server middleware array. */ + addServerMiddleware, - /** - * 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/.vue`) - */ - addErrorLayout (dst: string) { - return this._call(addErrorLayout, dst) - } + /** Allows extending webpack build config by chaining `options.build.extend` function. */ + extendBuild, - /** Adds a new server middleware to the end of the server middleware array. */ - addServerMiddleware (middleware) { - return this._call(addServerMiddleware, middleware) - } + /** Allows extending routes by chaining `options.build.extendRoutes` function. */ + extendRoutes, - /** Allows extending webpack build config by chaining `options.build.extend` function. */ - extendBuild (fn) { - return this._call(extendBuild, fn) - } + /** `requireModule` is a shortcut for `addModule` */ + requireModule: installModule, - /** Allows extending routes by chaining `options.build.extendRoutes` function. */ - extendRoutes (fn) { - return this._call(extendRoutes, fn) - } - - /** `requireModule` is a shortcut for `addModule` */ - requireModule (moduleOpts: string | [src: string, options: any]) { - return installModule(this.nuxt, moduleOpts) - } - - /** 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) + /** Registers a module. moduleOpts can be a string or an array ([src, options]). */ + addModule: installModule } } + +export type ModuleContainer = ReturnType diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index 2e6fec5a31..5f27b12d65 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -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) }