From b920f22483fb96958cf96826b9858d906685332e Mon Sep 17 00:00:00 2001 From: Clark Du Date: Mon, 20 Aug 2018 15:20:45 +0100 Subject: [PATCH] refactor: add hooks (#3772) --- lib/core/nuxt.js | 33 ++++++++++++------------------ test/fixtures/basic/nuxt.config.js | 4 ++-- test/unit/basic.dev.test.js | 2 +- test/unit/basic.generate.test.js | 2 +- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/lib/core/nuxt.js b/lib/core/nuxt.js index b45d33cbe6..6061a3f002 100644 --- a/lib/core/nuxt.js +++ b/lib/core/nuxt.js @@ -59,7 +59,7 @@ export default class Nuxt { // Add hooks if (_.isPlainObject(this.options.hooks)) { - this.addObjectHooks(this.options.hooks) + this.addHooks(this.options.hooks) } else if (typeof this.options.hooks === 'function') { this.options.hooks(this.hook) } @@ -105,30 +105,23 @@ export default class Nuxt { } } - getObjectHooks(hooksObj, keyList = [], stack = '') { - Object.keys(hooksObj).forEach((key) => { - if (typeof hooksObj[key] === 'object' && hooksObj[key] !== null) { - this.getObjectHooks(hooksObj[key], keyList, `${stack}:${key}`) + flatHooks(configHooks, hooks = {}, parentName) { + Object.keys(configHooks).forEach((key) => { + const subHook = configHooks[key] + const name = parentName ? `${parentName}:${key}` : key + if (typeof subHook === 'object' && subHook !== null) { + this.flatHooks(subHook, hooks, name) } else { - const value = hooksObj[key] - key = `${stack}:${key}` - keyList.push({ - name: key.slice(1), - hook: value - }) + hooks[name] = subHook } }) - return keyList.reduce((hash, pair) => { - return { ...hash, [pair.name]: pair.hook } - }, {}) + return hooks } - addObjectHooks(obj) { - const hooksObj = this.getObjectHooks(obj) - Object.keys(hooksObj).filter(Boolean).forEach((key) => { - const hook = hooksObj[key] - const hooks = Array.isArray(hook) ? hook : [hook] - hooks.forEach(h => this.hook(key, h)) + addHooks(configHooks) { + const hooks = this.flatHooks(configHooks) + Object.keys(hooks).filter(Boolean).forEach((key) => { + [].concat(hooks[key]).forEach(h => this.hook(key, h)) }) } diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index 4ca623626f..b1006e5a00 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -42,11 +42,11 @@ export default { modulesDir: path.join(__dirname, '..', '..', '..', 'node_modules'), hooks: { ready(nuxt) { - nuxt.__hook_called__ = true + nuxt.__hook_ready_called__ = true }, build: { done(builder) { - builder.__hook_called__ = true + builder.__hook_built_called__ = true } }, bad: null, diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index c7b6b63f37..1cd0654ac8 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -34,7 +34,7 @@ describe('basic dev', () => { }) test('Check build:done hook called', () => { - expect(builder.__hook_called__).toBe(true) + expect(builder.__hook_built_called__).toBe(true) }) test('Config: build.transpile', () => { diff --git a/test/unit/basic.generate.test.js b/test/unit/basic.generate.test.js index 5c149f0b75..c5c420ee43 100644 --- a/test/unit/basic.generate.test.js +++ b/test/unit/basic.generate.test.js @@ -40,7 +40,7 @@ describe('basic generate', () => { }) test('Check ready hook called', () => { - expect(generator.nuxt.__hook_called__).toBe(true) + expect(generator.nuxt.__hook_ready_called__).toBe(true) }) test('Format errors', () => {