[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)
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)

View File

@ -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) {

View File

@ -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)
}
}