feat(module): support src as a function in addModule (#4956)

This commit is contained in:
Ricardo Gobbo de Souza 2019-02-05 09:35:42 -02:00 committed by Pooya Parsa
parent 93089543be
commit 1e9eb4b70c
2 changed files with 32 additions and 2 deletions

View File

@ -115,8 +115,8 @@ export default class ModuleContainer {
let options
let handler
// Type 1: String
if (typeof moduleOpts === 'string') {
// Type 1: String or Function
if (typeof moduleOpts === 'string' || typeof moduleOpts === 'function') {
src = moduleOpts
} else if (Array.isArray(moduleOpts)) {
// Type 2: Babel style array
@ -126,6 +126,11 @@ export default class ModuleContainer {
({ src, options, handler } = moduleOpts)
}
// Define handler if src is a function
if (typeof src === 'function') {
handler = src
}
// Resolve handler
if (!handler) {
handler = this.nuxt.resolver.requireModule(src)

View File

@ -309,6 +309,31 @@ describe('core: module', () => {
expect(result).toEqual({ src: 'moduleTest', options: {} })
})
test('should add function module', async () => {
const module = new ModuleContainer({
resolver: { requireModule },
options: {}
})
const functionModule = function (options) {
return Promise.resolve(options)
}
functionModule.meta = { name: 'moduleTest' }
const result = await module.addModule(functionModule)
expect(requireModule).not.toBeCalled()
expect(module.requiredModules).toEqual({
moduleTest: {
handler: expect.any(Function),
options: undefined,
src: functionModule
}
})
expect(result).toEqual({ })
})
test('should add array module', async () => {
const module = new ModuleContainer({
resolver: { requireModule },