ModuleContainer 类
- 源码: core/module.js
所有模块 都在 ModuleContainer 实例的上下文中被调用。
Tapable 插件
可以在特定生命周期事件上注册钩子。
nuxt.moduleContainer.plugin('ready', async moduleContainer => {
// 所有模块准备就绪后执行此操作
})
在模块 上下文中,可以改用以下方式:
this.plugin('ready', async moduleContainer => {
// 所有模块准备就绪后执行此操作
})
| 插件 | 参数 | 时机 |
|---|---|---|
ready |
moduleContainer | nuxt.config.js 中所有模块初始化完成时 |
方法
addVendor (vendor)
vendor 已废弃,不再使用
向 options.build.vendor 添加并应用唯一过滤器。
addTemplate (template)
-
template:
String或Object-
src -
options -
fileName
-
在构建期间,使用 lodash template 将给定模板渲染到项目的 buildDir(.nuxt)中。
如果未提供 fileName 或 template 为字符串,目标文件名默认为 [dirName].[fileName].[pathHash].[ext]。
此方法返回最终的 { dst, src, options } 对象。
addPlugin (template)
-
template: 对象属性(
src、options、fileName、mode)。
使用 addTemplate 注册插件并将其添加到 plugins[] 数组的开头。
this.addPlugin({
src: path.resolve(__dirname, 'templates/foo.js'),
fileName: 'foo.server.js' // [可选] 仅包含在服务端 bundle 中
options: moduleOptions
})
注意: 如果只在客户端或服务端使用插件,可以使用 mode 或在 fileName 选项中使用 .client 和 .server 修饰符(可用选项请参阅 plugins )。
如果指定了 fileName,可以为 fileName 设置自定义路径,可以选择 .nuxt 文件夹内的文件夹结构以防止名称冲突:
{
fileName: path.join('folder', 'foo.client.js'), // 结果为 `.nuxt/folder/foo.client.js`
}
addServerMiddleware (middleware)
将中间件推送到 options.serverMiddleware 。
extendBuild (fn)
通过链接 options.build.extend 函数,轻松扩展 webpack 构建配置。
extendRoutes (fn)
通过链接 options.build.extendRoutes 函数,轻松扩展路由。
addModule (moduleOpts, requireOnce)
异步函数
注册模块。moduleOpts 可以是字符串或数组([src, options])。如果 requireOnce 为 true 且解析的模块导出了 meta,则可避免同一模块被注册两次。
requireModule (moduleOpts)
异步函数
addModule(moduleOpts, true) 的简写。
钩子
可以在特定生命周期事件上注册钩子。
| 钩子 | 参数 | 时机 |
|---|---|---|
modules:before |
(moduleContainer, options) | 创建 ModuleContainer 类之前调用,可用于重载方法和选项。 |
modules:done |
(moduleContainer) | 所有模块加载完成时调用。 |