mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
[modules] Add extendRoutes
This commit is contained in:
parent
b0b9101a8d
commit
2c37811032
@ -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)
|
||||
|
@ -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) {
|
||||
|
12
lib/utils.js
12
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user