mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 09:33:54 +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)
|
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)
|
||||||
|
@ -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
|
extendRoutes (fn) {
|
||||||
const _extend = this.options.build.extend
|
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
|
||||||
this.options.build.extend = function () {
|
|
||||||
extendFn.apply(this, arguments)
|
|
||||||
if (_extend) {
|
|
||||||
_extend.apply(this, arguments)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
installModule (moduleOpts) {
|
installModule (moduleOpts) {
|
||||||
|
12
lib/utils.js
12
lib/utils.js
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user