2021-04-02 11:47:01 +00:00
|
|
|
import { nuxtCtx } from '../nuxt'
|
|
|
|
import { installModule } from './install'
|
|
|
|
import {
|
|
|
|
addTemplate,
|
|
|
|
addErrorLayout,
|
|
|
|
addLayout,
|
|
|
|
addPlugin,
|
|
|
|
addServerMiddleware,
|
|
|
|
extendBuild,
|
|
|
|
extendRoutes
|
|
|
|
} from './utils'
|
2021-05-20 10:58:30 +00:00
|
|
|
import type { Nuxt } from '../types/nuxt'
|
|
|
|
import type { NuxtOptions } from '../types/config'
|
|
|
|
import type { TemplateOpts, PluginTemplateOpts } from '../types/module'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Legacy ModuleContainer for backwards compatibility with existing Nuxt 2 modules. */
|
2021-04-02 11:47:01 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/**
|
|
|
|
* Returns a resolved promise immediately.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2021-04-02 11:47:01 +00:00
|
|
|
ready () {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** @deprecated */
|
2021-04-02 11:47:01 +00:00
|
|
|
addVendor () {
|
2021-04-15 18:49:29 +00:00
|
|
|
console.warn('addVendor has been deprecated and has no effect.')
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/**
|
|
|
|
* 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].
|
|
|
|
*/
|
2021-04-02 11:47:01 +00:00
|
|
|
addTemplate (tmpl: TemplateOpts | string) {
|
|
|
|
return this._call(addTemplate, tmpl)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* })
|
|
|
|
* ```
|
|
|
|
*/
|
2021-04-02 11:47:01 +00:00
|
|
|
addPlugin (tmpl: PluginTemplateOpts) {
|
|
|
|
return this._call(addPlugin, tmpl)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Register a custom layout. If its name is 'error' it will override the default error layout. */
|
2021-04-02 11:47:01 +00:00
|
|
|
addLayout (tmpl: TemplateOpts, name: string) {
|
|
|
|
return this._call(addLayout, tmpl, name)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/**
|
|
|
|
* 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`)
|
|
|
|
*/
|
2021-04-02 11:47:01 +00:00
|
|
|
addErrorLayout (dst: string) {
|
|
|
|
return this._call(addErrorLayout, dst)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Adds a new server middleware to the end of the server middleware array. */
|
2021-04-02 11:47:01 +00:00
|
|
|
addServerMiddleware (middleware) {
|
|
|
|
return this._call(addServerMiddleware, middleware)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Allows extending webpack build config by chaining `options.build.extend` function. */
|
2021-04-02 11:47:01 +00:00
|
|
|
extendBuild (fn) {
|
|
|
|
return this._call(extendBuild, fn)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Allows extending routes by chaining `options.build.extendRoutes` function. */
|
2021-04-02 11:47:01 +00:00
|
|
|
extendRoutes (fn) {
|
|
|
|
return this._call(extendRoutes, fn)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** `requireModule` is a shortcut for `addModule` */
|
|
|
|
requireModule (moduleOpts: string | [src: string, options: any]) {
|
2021-04-02 11:47:01 +00:00
|
|
|
return installModule(this.nuxt, moduleOpts)
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Registers a module. moduleOpts can be a string or an array ([src, options]). */
|
|
|
|
addModule (moduleOpts: string | [src: string, options: any]) {
|
2021-04-02 11:47:01 +00:00
|
|
|
return installModule(this.nuxt, moduleOpts)
|
|
|
|
}
|
|
|
|
}
|