[modules] Add extendRoutes

This commit is contained in:
Pooya Parsa 2017-05-12 12:27:24 +04:30
parent b0b9101a8d
commit 2c37811032
3 changed files with 20 additions and 13 deletions

View File

@ -223,7 +223,7 @@ function * generateRoutesAndFiles () {
templateVars.router.routes = createRoutes(files, this.srcDir) templateVars.router.routes = createRoutes(files, this.srcDir)
if (typeof this.options.router.extendRoutes === 'function') { if (typeof this.options.router.extendRoutes === 'function') {
// let the user extend the routes // 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 // Routes for Generate command
this.routes = flatRoutes(templateVars.router.routes) this.routes = flatRoutes(templateVars.router.routes)

View File

@ -4,6 +4,7 @@ import path from 'path'
import fs from 'fs' import fs from 'fs'
import {uniq} from 'lodash' import {uniq} from 'lodash'
import hash from 'hash-sum' import hash from 'hash-sum'
import {chainFn} from './utils'
class Module { class Module {
constructor (nuxt) { constructor (nuxt) {
@ -53,18 +54,12 @@ class Module {
}) })
} }
extendBuild (extendFn) { extendBuild (fn) {
if (!(extendFn instanceof Function)) { this.options.build.extend = chainFn(this.options.build.extend, fn)
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)
}
} }
extendRoutes (fn) {
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
} }
installModule (moduleOpts) { installModule (moduleOpts) {

View File

@ -61,3 +61,15 @@ export function promisifyRoute (fn) {
export function sequence (tasks, fn) { export function sequence (tasks, fn) {
return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve()) 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)
}
}