mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
Merge pull request #1447 from Zip753/improve-nuxt-config-interface
Improve nuxt.config.js and Nuxt modules API
This commit is contained in:
commit
7738d0c0af
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
2
test/fixtures/module/modules/basic/index.js
vendored
2
test/fixtures/module/modules/basic/index.js
vendored
@ -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
|
||||
|
17
test/fixtures/with-config/nuxt.config.js
vendored
17
test/fixtures/with-config/nuxt.config.js
vendored
@ -4,11 +4,14 @@ module.exports = {
|
||||
base: '/test/',
|
||||
middleware: 'noop',
|
||||
extendRoutes (routes) {
|
||||
routes.push({
|
||||
name: 'about-bis',
|
||||
path: '/about-bis',
|
||||
component: '~/pages/about.vue'
|
||||
})
|
||||
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: [
|
||||
|
@ -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)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user