feat(core): better module not found handling (#7079)

Co-authored-by: pooya parsa <pooya@pi0.ir>
This commit is contained in:
Xin Du (Clark) 2020-03-13 20:25:53 +00:00 committed by GitHub
parent f20d932c4d
commit 52caef2053
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -143,7 +143,32 @@ export default class ModuleContainer {
// Resolve handler
if (!handler) {
try {
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

View File

@ -454,4 +454,25 @@ describe('core: module', () => {
expect(handler).toBeCalledTimes(1)
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.')
})
})