Next.js Discord

Discord Forum

Hook in to "next dev" command

Answered
Common Pochard posted this in #help-forum
Open in Discord
Common PochardOP
I would like to make a third party library that users can install along with Next. It will need to watch for file changes in the users code and perform actions in response.

Obviously I can instruct the user to update their dev command to something like next dev && npx my-library but would greatly prefer a solution that starts up automatically (and without configuration) along with next dev

Note: In prod this will not be necessary, as the user's code will be known ahead of time, so it can just be done once before / during build
Answered by joulev
you can write a webpack plugin wrapping your logic, and hook that webpack plugin into the user's next.config.js.

example here https://github.com/joulev/nextjs-route-types/blob/main/packages/nextjs-route-types/src/index.ts#L55
View full answer

10 Replies

Answer
Common PochardOP
oh wow, this is perfect, thank you! so if i'm understanding this correctly, there probably is not a way to do this silently, with zero user configuration?

eg: user installs dependency, dependency configures itself to start with next

would probably be a security issue anyway i guess
yeah need user intervention
you notice many things require users to withSomething(config), this is why
Common PochardOP
yep, for sure, makes sense. i'm sure it could also potentially cause really insidious bugs, some dep is causing performance issues or something, but user has no idea why
(if they allowed the dep to configure itself, i mean)
yeah
technically you can use postinstall scripts to auto-configure for users
i wouldn't recommend that though, unnecessarily complex
not to mention some package managers block postinstall by default