mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
feat(core): better module not found handling (#7079)
Co-authored-by: pooya parsa <pooya@pi0.ir>
This commit is contained in:
parent
f20d932c4d
commit
52caef2053
@ -143,7 +143,32 @@ export default class ModuleContainer {
|
|||||||
|
|
||||||
// Resolve handler
|
// Resolve handler
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
|
try {
|
||||||
handler = this.nuxt.resolver.requireModule(src, { useESM: true })
|
handler = this.nuxt.resolver.requireModule(src, { useESM: true })
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'MODULE_NOT_FOUND') {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
let message = 'Module `{name}` not found.'
|
||||||
|
|
||||||
|
if (this.options.buildModules.includes(src)) {
|
||||||
|
message += ' Please ensure `{name}` is in `devDependencies` and installed. HINT: During build step, for npm/yarn, `NODE_ENV=production` or `--production` should NOT be used.'.replace('{name}', src)
|
||||||
|
} else if (this.options.modules.includes(src)) {
|
||||||
|
message += ' Please ensure `{name}` is in `dependencies` and installed.'
|
||||||
|
}
|
||||||
|
|
||||||
|
message = message.replace(/{name}/g, src)
|
||||||
|
|
||||||
|
if (this.options._cli) {
|
||||||
|
throw new Error(message)
|
||||||
|
} else {
|
||||||
|
// TODO: Remove in next major version
|
||||||
|
message += ' Silently ignoring module as programatic usage detected.'
|
||||||
|
consola.warn(message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate handler
|
// Validate handler
|
||||||
|
@ -454,4 +454,25 @@ describe('core: module', () => {
|
|||||||
expect(handler).toBeCalledTimes(1)
|
expect(handler).toBeCalledTimes(1)
|
||||||
expect(module.requiredModules.moduleTest).toBeDefined()
|
expect(module.requiredModules.moduleTest).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('should prevent adding not installed buildModules', async () => {
|
||||||
|
const module = new ModuleContainer({
|
||||||
|
resolver: { requireModule },
|
||||||
|
options: {
|
||||||
|
...defaultOptions,
|
||||||
|
buildModules: ['test-build-module']
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
requireModule.mockImplementationOnce(() => {
|
||||||
|
const moduleNotFound = new Error()
|
||||||
|
moduleNotFound.code = 'MODULE_NOT_FOUND'
|
||||||
|
throw moduleNotFound
|
||||||
|
})
|
||||||
|
|
||||||
|
const result = await module.addModule('test-build-module', true)
|
||||||
|
|
||||||
|
expect(result).toBeUndefined()
|
||||||
|
expect(consola.warn).toBeCalledWith('Module `test-build-module` not found. Please ensure `test-build-module` is in `devDependencies` and installed. HINT: During build step, for npm/yarn, `NODE_ENV=production` or `--production` should NOT be used. Silently ignoring module as programatic usage detected.')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user