mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
feat(module): support src as a function in addModule (#4956)
This commit is contained in:
parent
f40f3f92c1
commit
e2c811a6d0
@ -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)
|
||||
|
@ -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 },
|
||||
|
Loading…
Reference in New Issue
Block a user