Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const watch = require('./lib/watch')
const parseArgs = require('./args')
const {
exit,
requirePath,
requireModule,
requireFastifyForModule,
requireServerPluginFromPath,
showHelpForCommand
Expand Down Expand Up @@ -72,7 +72,7 @@ async function runFastify (args) {
/* This check ensures we ignore `-r ""`, trailing `-r`, or
* other silly things the user might (inadvertently) be doing.
*/
requirePath(module)
requireModule(module)
}
})
} catch (e) {
Expand All @@ -94,7 +94,7 @@ async function runFastify (args) {
let logger
if (opts.loggingModule) {
try {
logger = requirePath(opts.loggingModule)
logger = requireModule(opts.loggingModule)
} catch (e) {
module.exports.stop(e)
}
Expand Down
18 changes: 18 additions & 0 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,24 @@ test('should support custom logger configuration', async t => {
t.pass('server closed')
})

test('preloading a built-in module works', async t => {
t.plan(1)

const argv = ['-r', 'path', './examples/plugin.js']
const fastify = await start.start(argv)
await fastify.close()
t.pass('server closed')
})

test('preloading a module in node_modules works', async t => {
t.plan(1)

const argv = ['-r', 'tap', './examples/plugin.js']
const fastify = await start.start(argv)
await fastify.close()
t.pass('server closed')
})

test('should throw on logger configuration module not found', async t => {
t.plan(2)

Expand Down
17 changes: 10 additions & 7 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ function exit (message) {
process.exit()
}

function requirePath (filePath) {
const moduleFilePath = path.resolve(filePath)
const moduleFileExtension = path.extname(filePath)
const modulePath = moduleFilePath.split(moduleFileExtension)[0]
const module = require(modulePath)
return module
function requireModule (moduleName) {
if (fs.existsSync(moduleName)) {
const moduleFilePath = path.resolve(moduleName)
const moduleFileExtension = path.extname(moduleName)
const modulePath = moduleFilePath.split(moduleFileExtension)[0]
return require(modulePath)
} else {
return require(moduleName)
}
}

function requireFastifyForModule (modulePath) {
Expand Down Expand Up @@ -95,4 +98,4 @@ function showHelpForCommand (commandName) {
}
}

module.exports = { exit, requirePath, requireFastifyForModule, showHelpForCommand, requireServerPluginFromPath }
module.exports = { exit, requireModule, requireFastifyForModule, showHelpForCommand, requireServerPluginFromPath }