Merge pull request #1447 from Zip753/improve-nuxt-config-interface

Improve nuxt.config.js and Nuxt modules API
This commit is contained in:
Sébastien Chopin 2017-08-24 15:49:10 +02:00 committed by GitHub
commit 7738d0c0af
7 changed files with 90 additions and 12 deletions

View File

@ -260,7 +260,11 @@ export default class Builder extends Tapable {
// router.extendRoutes method
if (typeof this.options.router.extendRoutes === 'function') {
// let the user extend the routes
this.options.router.extendRoutes(templateVars.router.routes, r)
const extendedRoutes = this.options.router.extendRoutes(templateVars.router.routes, r)
// Only overwrite routes when something is returned for backwards compatibility
if (extendedRoutes !== undefined) {
templateVars.router.routes = extendedRoutes
}
}
// Make routes accessible for other modules and webpack configs

View File

@ -221,10 +221,14 @@ export default function webpackClientConfig () {
// Extend config
if (typeof this.options.build.extend === 'function') {
this.options.build.extend.call(this, config, {
const extendedConfig = this.options.build.extend.call(this, config, {
dev: this.options.dev,
isClient: true
})
// Only overwrite config when something is returned for backwards compatibility
if (extendedConfig !== undefined) {
config = extendedConfig
}
}
return config

View File

@ -75,10 +75,14 @@ export default function webpackServerConfig () {
// Extend config
if (typeof this.options.build.extend === 'function') {
this.options.build.extend.call(this, config, {
const extendedConfig = this.options.build.extend.call(this, config, {
dev: this.options.dev,
isServer: true
})
// Only overwrite config when something is returned for backwards compatibility
if (extendedConfig !== undefined) {
config = extendedConfig
}
}
return config

View File

@ -73,10 +73,20 @@ export function chainFn (base, fn) {
return
}
return function () {
if (base instanceof Function) {
base.apply(this, arguments)
if (typeof base !== 'function') {
return fn.apply(this, arguments)
}
fn.apply(this, arguments)
let baseResult = base.apply(this, arguments)
// Allow function to mutate the first argument instead of returning the result
if (baseResult === undefined) {
baseResult = arguments[0]
}
let fnResult = fn.call(this, baseResult, ...Array.prototype.slice.call(arguments, 1))
// Return mutated argument if no result was returned
if (fnResult === undefined) {
return baseResult
}
return fnResult
}
}

View File

@ -15,11 +15,13 @@ module.exports = function basicModule (options, resolve) {
// Extend build again
this.extendBuild((config, { isClient, isServer }) => {
// Do nothing!
return config
})
// Extend routes
this.extendRoutes((routes, resolve) => {
// Do nothing!
return routes
})
// Require same module twice

View File

@ -4,11 +4,14 @@ module.exports = {
base: '/test/',
middleware: 'noop',
extendRoutes (routes) {
routes.push({
return [
...routes,
{
name: 'about-bis',
path: '/about-bis',
component: '~/pages/about.vue'
})
}
]
}
},
transition: 'test',
@ -31,7 +34,9 @@ module.exports = {
generateStatsFile: true
},
extend (config, options) {
config.devtool = 'nosources-source-map'
return Object.assign({}, config, {
devtool: 'nosources-source-map'
})
}
},
css: [

View File

@ -94,3 +94,52 @@ test('promisifyRoute (fn(cb) with result)', t => {
t.is(res, array)
})
})
test('chainFn (mutate, mutate)', t => {
// Pass more than one argument to test that they're actually taken into account
const firstFn = function (obj, count) {
obj.foo = count + 1
}
const secondFn = function (obj, count) {
obj.bar = count + 2
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (mutate, return)', t => {
const firstFn = function (obj, count) {
obj.foo = count + 1
}
const secondFn = function (obj, count) {
return Object.assign({}, obj, { bar: count + 2 })
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (return, mutate)', t => {
const firstFn = function (obj, count) {
return Object.assign({}, obj, { foo: count + 1 })
}
const secondFn = function (obj, count) {
obj.bar = count + 2
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (return, return)', t => {
const firstFn = function (obj, count) {
return Object.assign({}, obj, { foo: count + 1 })
}
const secondFn = function (obj, count) {
return Object.assign({}, obj, { bar: count + 2 })
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})