mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
feat(module): improve require
This commit is contained in:
parent
349f6e6219
commit
0468c7997e
@ -9,14 +9,14 @@ import {chainFn, sequence} from './utils'
|
||||
const debug = require('debug')('nuxt:module')
|
||||
|
||||
class Module {
|
||||
constructor (nuxt) {
|
||||
constructor(nuxt) {
|
||||
this.nuxt = nuxt
|
||||
this.options = nuxt.options
|
||||
this.modules = []
|
||||
this.requiredModules = []
|
||||
this.initing = this.ready()
|
||||
}
|
||||
|
||||
async ready () {
|
||||
async ready() {
|
||||
if (this.initing) {
|
||||
await this.initing
|
||||
return this
|
||||
@ -26,7 +26,7 @@ class Module {
|
||||
return this
|
||||
}
|
||||
|
||||
addVendor (vendor) {
|
||||
addVendor(vendor) {
|
||||
/* istanbul ignore if */
|
||||
if (!vendor) {
|
||||
return
|
||||
@ -34,7 +34,7 @@ class Module {
|
||||
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
|
||||
}
|
||||
|
||||
addTemplate (template) {
|
||||
addTemplate(template) {
|
||||
/* istanbul ignore if */
|
||||
if (!template) {
|
||||
return
|
||||
@ -60,7 +60,7 @@ class Module {
|
||||
return templateObj
|
||||
}
|
||||
|
||||
addPlugin (template) {
|
||||
addPlugin(template) {
|
||||
const {dst} = this.addTemplate(template)
|
||||
// Add to nuxt plugins
|
||||
this.options.plugins.push({
|
||||
@ -69,27 +69,24 @@ class Module {
|
||||
})
|
||||
}
|
||||
|
||||
addServerMiddleware (middleware) {
|
||||
addServerMiddleware(middleware) {
|
||||
this.options.serverMiddleware.push(middleware)
|
||||
}
|
||||
|
||||
extendBuild (fn) {
|
||||
extendBuild(fn) {
|
||||
this.options.build.extend = chainFn(this.options.build.extend, fn)
|
||||
}
|
||||
|
||||
extendRoutes (fn) {
|
||||
extendRoutes(fn) {
|
||||
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
|
||||
}
|
||||
|
||||
requireModule (moduleOpts) {
|
||||
if (this.modules.indexOf(moduleOpts) !== -1 || this.modules.indexOf(moduleOpts.src) !== -1) {
|
||||
return false
|
||||
}
|
||||
this.modules.push(moduleOpts.src || moduleOpts)
|
||||
return this.addModule(moduleOpts)
|
||||
requireModule(moduleOpts) {
|
||||
// Require once
|
||||
return this.addModule(moduleOpts, true)
|
||||
}
|
||||
|
||||
addModule (moduleOpts) {
|
||||
addModule(moduleOpts, requireOnce) {
|
||||
/* istanbul ignore if */
|
||||
if (!moduleOpts) {
|
||||
return
|
||||
@ -129,6 +126,19 @@ class Module {
|
||||
console.error(`[nuxt] Module [${originalSrc}] should export a function`)
|
||||
process.exit(1)
|
||||
}
|
||||
// Module meta
|
||||
if (!module.meta) {
|
||||
module.meta = {}
|
||||
}
|
||||
if (module.meta.name) {
|
||||
const alreadyRequired = this.requiredModules.indexOf(module.meta.name) !== -1
|
||||
if (requireOnce && alreadyRequired) {
|
||||
return
|
||||
}
|
||||
if (!alreadyRequired) {
|
||||
this.requiredModules.push(module.meta.name)
|
||||
}
|
||||
}
|
||||
// Call module with `this` context and pass options
|
||||
return new Promise((resolve, reject) => {
|
||||
const result = module.call(this, options, err => {
|
||||
|
Loading…
Reference in New Issue
Block a user