From 2336b3e6eacd9742bb2293f5cb5415118de3a718 Mon Sep 17 00:00:00 2001 From: Ivan Nikulin Date: Wed, 23 Aug 2017 00:10:43 +0200 Subject: [PATCH] Update extendBuild modules API --- lib/common/utils.js | 18 +++++++++++++----- lib/core/module.js | 20 +------------------- test/fixtures/module/modules/basic/index.js | 1 + 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/lib/common/utils.js b/lib/common/utils.js index 54a13cbaa1..24a08f454a 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -72,12 +72,20 @@ export function chainFn (base, fn) { if (!(fn instanceof Function)) { return } - return function () { - if (base instanceof Function) { - base.apply(this, arguments) - } - fn.apply(this, arguments) + if (typeof base !== 'function') { + return 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, ...arguments.slice(1)) + // Return mutated argument if no result was returned + if (fnResult === undefined) { + return baseResult + } + return fnResult } export function isPureObject (o) { diff --git a/lib/core/module.js b/lib/core/module.js index 346d4ba102..726227b8c7 100755 --- a/lib/core/module.js +++ b/lib/core/module.js @@ -74,25 +74,7 @@ export default class ModuleContainer extends Tapable { } extendRoutes (fn) { - const extendRoutesFromConfig = this.options.router.extendRoutes - this.options.router.extendRoutes = function (routes, resolve) { - /* istanbul ignore if */ - if (!(fn instanceof Function)) { - return - } - if (typeof extendRoutesFromConfig !== 'function') { - return fn.call(this, routes, resolve) - } - let extendedRoutes = extendRoutesFromConfig.call(this, routes, resolve) - if (extendedRoutes === undefined) { - extendedRoutes = routes - } - let moduleExtendedRoutes = fn.call(this, extendedRoutes, resolve) - if (moduleExtendedRoutes === undefined) { - return extendedRoutes - } - return moduleExtendedRoutes - } + this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn) } requireModule (moduleOpts) { diff --git a/test/fixtures/module/modules/basic/index.js b/test/fixtures/module/modules/basic/index.js index fc2707b041..62a3ced7fa 100755 --- a/test/fixtures/module/modules/basic/index.js +++ b/test/fixtures/module/modules/basic/index.js @@ -15,6 +15,7 @@ module.exports = function basicModule (options, resolve) { // Extend build again this.extendBuild((config, { isClient, isServer }) => { // Do nothing! + return config }) // Extend routes