requireModule and addModule

requireModule can be used for Meta-Modules to prevent duplicate requires
This commit is contained in:
Pooya Parsa 2017-05-20 13:43:55 +04:30
parent 148c2fc471
commit f0c66b490a
2 changed files with 12 additions and 2 deletions

View File

@ -10,6 +10,7 @@ class Module {
constructor (nuxt) { constructor (nuxt) {
this.nuxt = nuxt this.nuxt = nuxt
this.options = nuxt.options this.options = nuxt.options
this.modules = []
} }
addVendor (vendor) { addVendor (vendor) {
@ -75,7 +76,14 @@ class Module {
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn) this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
} }
installModule (moduleOpts) { requireModule (moduleOpts) {
if (this.modules.indexOf(moduleOpts) !== -1 || this.modules.indexOf(moduleOpts.src) !== -1) {
return
}
this.addModule(moduleOpts)
}
addModule (moduleOpts) {
/* istanbul ignore if */ /* istanbul ignore if */
if (!moduleOpts) { if (!moduleOpts) {
return return
@ -107,6 +115,8 @@ class Module {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error('[Nuxt] Module should be a function', module) console.error('[Nuxt] Module should be a function', module)
} }
// Add module to this.modules
this.modules.push(module)
// Call module with `this` context and pass options // Call module with `this` context and pass options
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const result = module.call(this, options, err => { const result = module.call(this, options, err => {

View File

@ -118,7 +118,7 @@ class Nuxt {
// Add module integration // Add module integration
this.module = new Module(this) this.module = new Module(this)
// Install all modules in sequence and then return `this` instance // Install all modules in sequence and then return `this` instance
return utils.sequence(options.modules, this.module.installModule.bind(this.module)) return utils.sequence(options.modules, this.module.addModule.bind(this.module))
.then(() => this) .then(() => this)
.catch(/* istanbul ignore next */ (err) => { .catch(/* istanbul ignore next */ (err) => {
console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console