SvelteKit Process Hooks
See original GitHub issueIs your feature request related to a problem? Please describe. As of right now, there’s no real way to hook into the SvelteKit process to run code before build, dev, or start.
The SvelteKit hooks.js
(not sure if this idea below works better for the former setup.js
file), has hooks for when a route is called serverside. However, it’d be nice to have hooks that would essentially correspond with svelte-kit build
, svelte-kit dev
, and svelte-kit start
. Elder.js has “Build Hooks” that handle hooking into the build process. JungleJS also has ways of configuring async code in between build/start process steps.
Describe the solution you’d like
My current idea is code like the following, probably in hooks.js
.
export async function devProcess({ dev, port, config }) {
//Fancy async code here
return await dev({ port, config });
}
export async function buildProcess({ build, adapt, verbose, config }) {
//Fancy async code here
await build(config);
return await adapt(config, { verbose });
}
export async function startProcess({ start, port, config }) {
//Fancy async code here
return await start(config);
}
And then the cli.js
file would be like it is now, but if one of these above (ofc optional) options is specified it runs it instead of the default code. For example, for the dev command
//Somehow import the devProcess command from above as customDevProcess
.action(async ({ port, open }) => {
process.env.NODE_ENV = 'development';
const config = await get_config();
const { dev } = await import('./core/dev/index.js');
const devPromise = customDevProcess ? customDevProcess({ dev, port, config }) : dev({ port, config });
try {
const watcher = await devPromise;
watcher.on('stdout', (data) => {
process.stdout.write(data);
});
watcher.on('stderr', (data) => {
process.stderr.write(data);
});
console.log(colors.bold().cyan(`> Listening on http://localhost:${watcher.port}`));
if (open) launch(watcher.port);
} catch (error) {
handle_error(error);
}
});
Describe alternatives you’ve considered
My original idea were options for preDev
, preBuild
, and preStart
just in the svelte.config.cjs
, for simplicity sake. This is of course kinda messy, and probably not the most dynamic way to do it. But also, these options for “process hooks” might not make sense in the current hooks.js
file.
How important is this feature to you?
In my efforts to recreate the ✨magic✨ of JungleJS within SvelteKit (take a look at progress here), I hit a road block where this is kinda critical. As of rn I have a literal temp-cli.js
file that I’m using to pull in dataSources in a separate command. I could make a legit CLI for JungleJS, and will probably have to do so if this isn’t pushed forward, but I feel this would be better as hooking into the whole SvelteKit process.
Additional context
I decided to go ahead and make this an issue after seeing others ask for stuff that could be solved with this, one example here of where some sort of preStart
hook would be useful.
Issue Analytics
- State:
- Created 2 years ago
- Comments:19 (14 by maintainers)
Top GitHub Comments
You can create db connections etc in
src/hooks.js
. Whether those connections are short-lived or long-lived depends solely on whether you deploy the app as a Node server or to a serverless platformhttps://kit.svelte.dev/docs#hooks
If I can give my opinion, the documentation for the hooks module, https://kit.svelte.dev/docs#hooks, does not let it clear that it will keep a side-effect running in a node environment, like a database connection
I think that’s something that should be addressed, if that’s the case