mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
feat(module): support src as a function in addModule (#4956)
This commit is contained in:
parent
93089543be
commit
1e9eb4b70c
@ -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)
|
||||||
|
@ -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 },
|
||||||
|
Loading…
Reference in New Issue
Block a user