From 2c37811032cc75dedf69e3701134213f743a7398 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 12 May 2017 12:27:24 +0430 Subject: [PATCH] [modules] Add extendRoutes --- lib/build.js | 2 +- lib/module.js | 19 +++++++------------ lib/utils.js | 12 ++++++++++++ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/build.js b/lib/build.js index 5aaf8f0448..756f5605c8 100644 --- a/lib/build.js +++ b/lib/build.js @@ -223,7 +223,7 @@ function * generateRoutesAndFiles () { templateVars.router.routes = createRoutes(files, this.srcDir) if (typeof this.options.router.extendRoutes === 'function') { // let the user extend the routes - this.options.router.extendRoutes(templateVars.router.routes, r) + this.options.router.extendRoutes.call(this, templateVars.router.routes, r) } // Routes for Generate command this.routes = flatRoutes(templateVars.router.routes) diff --git a/lib/module.js b/lib/module.js index d2e059ffea..51009a2499 100755 --- a/lib/module.js +++ b/lib/module.js @@ -4,6 +4,7 @@ import path from 'path' import fs from 'fs' import {uniq} from 'lodash' import hash from 'hash-sum' +import {chainFn} from './utils' class Module { constructor (nuxt) { @@ -53,18 +54,12 @@ class Module { }) } - extendBuild (extendFn) { - if (!(extendFn instanceof Function)) { - return - } - // Add extendFn to chain - const _extend = this.options.build.extend - this.options.build.extend = function () { - extendFn.apply(this, arguments) - if (_extend) { - _extend.apply(this, arguments) - } - } + extendBuild (fn) { + this.options.build.extend = chainFn(this.options.build.extend, fn) + } + + extendRoutes (fn) { + this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn) } installModule (moduleOpts) { diff --git a/lib/utils.js b/lib/utils.js index 58ea779d46..8fa93f03ba 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -61,3 +61,15 @@ export function promisifyRoute (fn) { export function sequence (tasks, fn) { return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve()) } + +export function chainFn (base, fn) { + if (!(fn instanceof Function)) { + return + } + return function () { + if (base instanceof Function) { + base.apply(this, arguments) + } + fn.apply(this, arguments) + } +}