Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to add plugins that run before internal plugins #383

Open
samualtnorman opened this issue Sep 27, 2024 · 4 comments
Open

No way to add plugins that run before internal plugins #383

samualtnorman opened this issue Sep 27, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@samualtnorman
Copy link

This means Vinxi projects cannot properly integrate other languages like Civet or plugins that need to run on the original source code like babel-plugin-here.

I did some digging into the source code and found 2 places where the order of the plugins is hardcoded.
For vinxi build it's here:

plugins: [
buildTargetPlugin[buildRouter.target]?.(buildRouter) ?? [],
routerModePlugin[buildRouter.internals.type.name]?.(buildRouter) ?? [],
...((await buildRouter.plugins?.(buildRouter)) ?? []),
{
name: "vinxi:build:router:config",
async configResolved(config) {
await app.hooks.callHook("app:build:router:vite:config:resolved", {
vite: config,
router: buildRouter,
app,
});
},
},
],

For vinxi dev it's here:

const plugins = [
// ...(serveConfig.devtools ? [inspect()] : []),
...(((await router.internals.type.dev.plugins?.(router, app)) ?? []).filter(
Boolean,
) || []),
...(((await router.plugins?.(router)) ?? []).filter(Boolean) || []),
].filter(Boolean);

I found I could hook into vinxi build thanks to:

await app.hooks.callHook("app:build:router:vite:start", {
vite: viteBuildConfig,
router: buildRouter,
app,
});

Like so:

// app.config.js
const app = createApp({ /* ... */ })

app.hooks.hook("app:build:router:vite:start", async ({ vite }) => {
	vite.plugins.unshift(/* plugins here */)
})

export default app

This requires inside knowledge however and I assume this is not stable.

I could not find anything similar for vinxi dev.

@samualtnorman
Copy link
Author

my current workaround is this patch

diff --git a/lib/build.js b/lib/build.js
index 812f346df2651bb4bab4ef54ac71b8142f15531a..e04fe63ea8219f2cdfc684a48a95a4818a34e1cd 100644
--- a/lib/build.js
+++ b/lib/build.js
@@ -503,6 +503,7 @@ async function createRouterBuild(app, router) {
 		app,
 	});
 
+	await app.hooks.callHook("3m6vP6KyC27N6foZgWisB", viteBuildConfig.plugins);
 	await createViteBuild(viteBuildConfig);
 
 	await app.hooks.callHook("app:build:router:vite:end", {
diff --git a/lib/dev-server.js b/lib/dev-server.js
index 381b6326958e55feb0e611d94c29c2b5e6fc8038..0bfb65120d48b5b76bb6881edfd82cee6f5cf17a 100644
--- a/lib/dev-server.js
+++ b/lib/dev-server.js
@@ -58,6 +58,8 @@ export async function createViteHandler(router, app, serveConfig) {
 		...(((await router.plugins?.(router)) ?? []).filter(Boolean) || []),
 	].filter(Boolean);
 
+	await app.hooks.callHook("3m6vP6KyC27N6foZgWisB", plugins);
+
 	let base = join(app.config.server.baseURL ?? "/", router.base);
 
 	const viteDevServer = await createViteDevServer({

and this in my app.config.ts

const app = createApp({ /* ... */ })

app.hooks.hook("3m6vP6KyC27N6foZgWisB", plugins => {
 	plugins.unshift(/* plugins here */)
})

export default app

@nksaraf
Copy link
Owner

nksaraf commented Oct 9, 2024

the hook api is meant to be stable in terms of all the hooks we call today .. removing them would be a breaking change now .. their arguments would also remain the same, and the api of the arguments like vite and nitro as as stable as those dependencies themselves .

So this is a perfect way to solve this problem. I am surprised the same hooks couldn't be found in dev. On my phone will check and see if I can find the appropriate hook there

@nksaraf nksaraf added the bug Something isn't working label Oct 9, 2024
@nksaraf
Copy link
Owner

nksaraf commented Oct 9, 2024

All the hooks haven't been documented but they will not be removed ... docs are coming soon

I Would classify it as a bug that there's no hook exposing the vite config before it's used in dev ..

Also btw couldn't you add a vite plugin that did these things in its configResolved.. I think you can rearrange plugins etc at that time

@samualtnorman
Copy link
Author

Also btw couldn't you add a vite plugin that did these things in its configResolved..

hmm, maybe. but in the types, the received resolvedConfig is set to readonly so I don't know if this is an intended or stable thing to do.

It's good to hear that the hooks are stable though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants