Skip to content

Commit ea35b54

Browse files
committed
Pass an already imported module in helper
This is useful when running in environment that transpile on the fly, such as Jest/Vitest and so on, which don't handle the dynamic import all that well.
1 parent d1dcfd1 commit ea35b54

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

helper.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fastify from 'fastify'
22

33
declare module 'fastify-cli/helper.js' {
44
module helper {
5-
export function build (args: Array<string>, additionalOptions?: Object, serverOptions?: Object): ReturnType<typeof fastify>
5+
export function build (args: Array<string>, additionalOptions?: Object, serverOptions?: Object, module?: Object): ReturnType<typeof fastify>
66
}
77

88
export = helper

helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
const { runFastify } = require('./start')
44

55
module.exports = {
6-
build (args, additionalOptions = {}, serverOptions = {}) {
6+
build (args, additionalOptions = {}, serverOptions = {}, module = undefined) {
77
Object.defineProperty(additionalOptions, 'ready', {
88
value: true,
99
enumerable: false,
1010
writable: false
1111
})
1212

13-
return runFastify(args, additionalOptions, serverOptions)
13+
return runFastify(args, additionalOptions, serverOptions, module)
1414
},
1515
listen: runFastify
1616
}

start.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async function preloadESModules (opts) {
9696
})
9797
}
9898

99-
async function runFastify (args, additionalOptions, serverOptions) {
99+
async function runFastify (args, additionalOptions, serverOptions, module) {
100100
const opts = parseArgs(args)
101101

102102
if (opts.require) {
@@ -113,7 +113,11 @@ async function runFastify (args, additionalOptions, serverOptions) {
113113
let file = null
114114

115115
try {
116-
file = await requireServerPluginFromPath(opts._[0])
116+
if (module != null) {
117+
file = module
118+
} else {
119+
file = await requireServerPluginFromPath(opts._[0])
120+
}
117121
} catch (e) {
118122
return module.exports.stop(e)
119123
}

0 commit comments

Comments
 (0)