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 f40f3f92c1
commit e2c811a6d0
2 changed files with 32 additions and 2 deletions

View File

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

View File

@ -309,6 +309,31 @@ describe('core: module', () => {
expect(result).toEqual({ src: 'moduleTest', options: {} }) 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 () => { test('should add array module', async () => {
const module = new ModuleContainer({ const module = new ModuleContainer({
resolver: { requireModule }, resolver: { requireModule },