Skip to content

Commit 91050b0

Browse files
authored
refactor(generator): minor refactor afterInvoke/afterAnyInvoke (#5993)
make afterInvoke/afterAnyInvoke code more readable and add test
1 parent 0372160 commit 91050b0

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

packages/@vue/cli/__tests__/Generator.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,54 @@ test('api: afterInvoke', async () => {
658658
expect(cbs).toContain(fn)
659659
})
660660

661+
test('api: afterAnyInvoke and afterInvoke in hooks', async () => {
662+
const fooAnyInvokeHandler = () => {}
663+
const fooInvokeHandler = () => {}
664+
const barAnyInvokeHandler = () => {}
665+
const barInvokeHandler = () => {}
666+
667+
const getGeneratorFn = (invokeHandler, anyInvokeHandler) => {
668+
const generatorFn = () => {}
669+
generatorFn.hooks = api => {
670+
api.afterInvoke(invokeHandler)
671+
api.afterAnyInvoke(anyInvokeHandler)
672+
}
673+
return generatorFn
674+
}
675+
676+
jest.doMock('vue-cli-plugin-foo-hooks/generator', () => {
677+
return getGeneratorFn(fooInvokeHandler, fooAnyInvokeHandler)
678+
}, { virtual: true })
679+
680+
jest.doMock('vue-cli-plugin-bar-hooks/generator', () => {
681+
return getGeneratorFn(barInvokeHandler, barAnyInvokeHandler)
682+
}, { virtual: true })
683+
684+
const afterAnyInvokeCbs = []
685+
const afterInvokeCbs = []
686+
const generator = new Generator('/', {
687+
pkg: {
688+
devDependencies: {
689+
'vue-cli-plugin-foo-hooks': '1.0.0',
690+
'vue-cli-plugin-bar-hooks': '1.0.0'
691+
}
692+
},
693+
plugins: [
694+
{
695+
id: 'vue-cli-plugin-foo-hooks',
696+
apply: getGeneratorFn(fooInvokeHandler, fooAnyInvokeHandler)
697+
}
698+
],
699+
afterInvokeCbs,
700+
afterAnyInvokeCbs
701+
})
702+
703+
await generator.generate()
704+
705+
expect(afterAnyInvokeCbs).toEqual([fooAnyInvokeHandler, barAnyInvokeHandler])
706+
expect(afterInvokeCbs).toEqual([fooInvokeHandler])
707+
})
708+
661709
test('api: resolve', () => {
662710
new Generator('/foo/bar', { plugins: [
663711
{

packages/@vue/cli/lib/Generator.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ module.exports = class Generator {
9090
this.pm = new PackageManager({ context })
9191
this.imports = {}
9292
this.rootOptions = {}
93-
// we don't load the passed afterInvokes yet because we want to ignore them from other plugins
94-
this.passedAfterInvokeCbs = afterInvokeCbs
95-
this.afterInvokeCbs = []
93+
this.afterInvokeCbs = afterInvokeCbs
9694
this.afterAnyInvokeCbs = afterAnyInvokeCbs
9795
this.configTransforms = {}
9896
this.defaultConfigTransforms = defaultConfigTransforms
@@ -124,7 +122,10 @@ module.exports = class Generator {
124122
const { rootOptions, invoking } = this
125123
const pluginIds = this.plugins.map(p => p.id)
126124

127-
// apply hooks from all plugins
125+
// avoid modifying the passed afterInvokes, because we want to ignore them from other plugins
126+
const passedAfterInvokeCbs = this.afterInvokeCbs
127+
this.afterInvokeCbs = []
128+
// apply hooks from all plugins to collect 'afterAnyHooks'
128129
for (const id of this.allPluginIds) {
129130
const api = new GeneratorAPI(id, this, {}, rootOptions)
130131
const pluginGenerator = loadModule(`${id}/generator`, this.context)
@@ -139,7 +140,7 @@ module.exports = class Generator {
139140
const afterAnyInvokeCbsFromPlugins = this.afterAnyInvokeCbs
140141

141142
// reset hooks
142-
this.afterInvokeCbs = this.passedAfterInvokeCbs
143+
this.afterInvokeCbs = passedAfterInvokeCbs
143144
this.afterAnyInvokeCbs = []
144145
this.postProcessFilesCbs = []
145146

@@ -155,10 +156,9 @@ module.exports = class Generator {
155156
// because `afterAnyHooks` is already determined by the `allPluginIds` loop above
156157
await apply.hooks(api, options, rootOptions, pluginIds)
157158
}
158-
159-
// restore "any" hooks
160-
this.afterAnyInvokeCbs = afterAnyInvokeCbsFromPlugins
161159
}
160+
// restore "any" hooks
161+
this.afterAnyInvokeCbs = afterAnyInvokeCbsFromPlugins
162162
}
163163

164164
async generate ({

0 commit comments

Comments
 (0)