Improve hooks in nuxt.config.js (#3766)

* Improve hooks in nuxt.config.js

* No mult-line for
This commit is contained in:
Jonas Galvez 2018-08-20 09:19:09 -03:00 committed by Sébastien Chopin
parent feaa07e208
commit 97910de3ec
4 changed files with 39 additions and 7 deletions

View File

@ -66,7 +66,9 @@ if (typeof storeData !== 'function') {
const appendedMods = {} const appendedMods = {}
if (module[name].appends) { if (module[name].appends) {
appendedMods.appends = module[name].appends appendedMods.appends = module[name].appends
for (const append of module[name].appends) { appendedMods[append] = module[name][append] } for (const append of module[name].appends) {
appendedMods[append] = module[name][append]
}
} }
module[name] = Object.assign({}, module[name], fileModule, appendedMods) module[name] = Object.assign({}, module[name], fileModule, appendedMods)

View File

@ -105,11 +105,30 @@ export default class Nuxt {
} }
} }
addObjectHooks(hooksObj) { getObjectHooks(hooksObj, keyList = [], stack = '') {
Object.keys(hooksObj).forEach((name) => { Object.keys(hooksObj).forEach((key) => {
let hooks = hooksObj[name] if (typeof hooksObj[key] === 'object' && hooksObj[key] !== null) {
hooks = Array.isArray(hooks) ? hooks : [hooks] this.getObjectHooks(hooksObj[key], keyList, `${stack}:${key}`)
hooks.forEach(hook => this.hook(name, hook)) } else {
const value = hooksObj[key]
key = `${stack}:${key}`
keyList.push({
name: key.slice(1),
hook: value
})
}
})
return keyList.reduce((hash, pair) => {
return { ...hash, [pair.name]: pair.hook }
}, {})
}
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))
}) })
} }

View File

@ -44,6 +44,11 @@ export default {
ready(nuxt) { ready(nuxt) {
nuxt.__hook_called__ = true nuxt.__hook_called__ = true
}, },
build: {
done(builder) {
builder.__hook_called__ = true
}
},
bad: null, bad: null,
'': true '': true
}, },

View File

@ -4,6 +4,7 @@ let port
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let builder = null
let transpile = null let transpile = null
describe('basic dev', () => { describe('basic dev', () => {
@ -26,11 +27,16 @@ describe('basic dev', () => {
} }
}) })
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
await new Builder(nuxt).build() builder = new Builder(nuxt)
await builder.build()
port = await getPort() port = await getPort()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}) })
test('Check build:done hook called', () => {
expect(builder.__hook_called__).toBe(true)
})
test('Config: build.transpile', () => { test('Config: build.transpile', () => {
expect(transpile('vue-test')).toBe(true) expect(transpile('vue-test')).toBe(true)
expect(transpile('node_modules/test.js')).toBe(false) expect(transpile('node_modules/test.js')).toBe(false)