mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
refactor: add hooks (#3772)
This commit is contained in:
parent
7b910428bc
commit
b920f22483
@ -59,7 +59,7 @@ export default class Nuxt {
|
|||||||
|
|
||||||
// Add hooks
|
// Add hooks
|
||||||
if (_.isPlainObject(this.options.hooks)) {
|
if (_.isPlainObject(this.options.hooks)) {
|
||||||
this.addObjectHooks(this.options.hooks)
|
this.addHooks(this.options.hooks)
|
||||||
} else if (typeof this.options.hooks === 'function') {
|
} else if (typeof this.options.hooks === 'function') {
|
||||||
this.options.hooks(this.hook)
|
this.options.hooks(this.hook)
|
||||||
}
|
}
|
||||||
@ -105,30 +105,23 @@ export default class Nuxt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getObjectHooks(hooksObj, keyList = [], stack = '') {
|
flatHooks(configHooks, hooks = {}, parentName) {
|
||||||
Object.keys(hooksObj).forEach((key) => {
|
Object.keys(configHooks).forEach((key) => {
|
||||||
if (typeof hooksObj[key] === 'object' && hooksObj[key] !== null) {
|
const subHook = configHooks[key]
|
||||||
this.getObjectHooks(hooksObj[key], keyList, `${stack}:${key}`)
|
const name = parentName ? `${parentName}:${key}` : key
|
||||||
|
if (typeof subHook === 'object' && subHook !== null) {
|
||||||
|
this.flatHooks(subHook, hooks, name)
|
||||||
} else {
|
} else {
|
||||||
const value = hooksObj[key]
|
hooks[name] = subHook
|
||||||
key = `${stack}:${key}`
|
|
||||||
keyList.push({
|
|
||||||
name: key.slice(1),
|
|
||||||
hook: value
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return keyList.reduce((hash, pair) => {
|
return hooks
|
||||||
return { ...hash, [pair.name]: pair.hook }
|
|
||||||
}, {})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addObjectHooks(obj) {
|
addHooks(configHooks) {
|
||||||
const hooksObj = this.getObjectHooks(obj)
|
const hooks = this.flatHooks(configHooks)
|
||||||
Object.keys(hooksObj).filter(Boolean).forEach((key) => {
|
Object.keys(hooks).filter(Boolean).forEach((key) => {
|
||||||
const hook = hooksObj[key]
|
[].concat(hooks[key]).forEach(h => this.hook(key, h))
|
||||||
const hooks = Array.isArray(hook) ? hook : [hook]
|
|
||||||
hooks.forEach(h => this.hook(key, h))
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
test/fixtures/basic/nuxt.config.js
vendored
4
test/fixtures/basic/nuxt.config.js
vendored
@ -42,11 +42,11 @@ export default {
|
|||||||
modulesDir: path.join(__dirname, '..', '..', '..', 'node_modules'),
|
modulesDir: path.join(__dirname, '..', '..', '..', 'node_modules'),
|
||||||
hooks: {
|
hooks: {
|
||||||
ready(nuxt) {
|
ready(nuxt) {
|
||||||
nuxt.__hook_called__ = true
|
nuxt.__hook_ready_called__ = true
|
||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
done(builder) {
|
done(builder) {
|
||||||
builder.__hook_called__ = true
|
builder.__hook_built_called__ = true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
bad: null,
|
bad: null,
|
||||||
|
@ -34,7 +34,7 @@ describe('basic dev', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('Check build:done hook called', () => {
|
test('Check build:done hook called', () => {
|
||||||
expect(builder.__hook_called__).toBe(true)
|
expect(builder.__hook_built_called__).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Config: build.transpile', () => {
|
test('Config: build.transpile', () => {
|
||||||
|
@ -40,7 +40,7 @@ describe('basic generate', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('Check ready hook called', () => {
|
test('Check ready hook called', () => {
|
||||||
expect(generator.nuxt.__hook_called__).toBe(true)
|
expect(generator.nuxt.__hook_ready_called__).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Format errors', () => {
|
test('Format errors', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user