feat(module): improve require

This commit is contained in:
Pooya Parsa 2017-06-05 13:22:02 +04:30
parent 349f6e6219
commit 0468c7997e

View File

@ -12,7 +12,7 @@ class Module {
constructor(nuxt) {
this.nuxt = nuxt
this.options = nuxt.options
this.modules = []
this.requiredModules = []
this.initing = this.ready()
}
@ -82,14 +82,11 @@ class Module {
}
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)
// 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 => {