Skip to content
New issue

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

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

Already on GitHub? # to your account

buildStart is called 3 times in a row #262

Closed
BierDav opened this issue Mar 20, 2024 · 4 comments
Closed

buildStart is called 3 times in a row #262

BierDav opened this issue Mar 20, 2024 · 4 comments

Comments

@BierDav
Copy link

BierDav commented Mar 20, 2024

I have made a custom plugin that compiles my openapi yamls to javascript files and it worked fine with vite until i switched to vinxi and vite 5.0. Know my plugin is executed 3 times in a row and parralell which doesn't make any sense:

export default function openApiPlugin(): Plugin {
    return {
        name: 'open-api-generate',
        async buildStart() {
            console.log("Generating")
            await main();
            console.log("Generating finished")
        },
        configureServer(server) {
            server.watcher.add(sourceDir)
            server.watcher.on('change', async (file) => {
                if (file.startsWith(sourceDir))
                    await main();
            })
        }
    }
}

This is roughly my code and when i start pnpm dev i get following in the console:

Generating
Generating
Generating
Generating finished
Generating finished
Generating finished

Am I doing something wrong or is this a bug?

Note:
This only happens with pnpm dev. When using pnpm build everything works fine and buildStart is called only once.

@nksaraf
Copy link
Owner

nksaraf commented Mar 21, 2024

No this is as expected. A SolidStart app is made of three vite subapps (a client app, a server app and server functions app). If you apply a vite plugin normally, it will be applied to all three subapps, thus will run thrice. You can use solid-start's vite option to only apply the plugin to one router, say the server.

eg.

import { defineConfig } from "@solidjs/start/config";
export default defineConfig({
  vite({ router }) {
    if (router === "server") {
    } else if (router === "client") {
    } else if (router === "server-function") {
    }
    return { plugins: [] };
  }
});

@nksaraf
Copy link
Owner

nksaraf commented Mar 21, 2024

Wait why is buildStart even called during pnpm dev. That seems wrong.

@BierDav
Copy link
Author

BierDav commented Mar 21, 2024

No this is as expected. A SolidStart app is made of three vite subapps (a client app, a server app and server functions app). If you apply a vite plugin normally, it will be applied to all three subapps, thus will run thrice.

That makes sense, I didn't know that, thanks

Wait why is buildStart even called during pnpm dev. That seems wrong.

What should I use instead? If buildStart is not the intended way to do so.

@nksaraf
Copy link
Owner

nksaraf commented Mar 24, 2024

So you would do two things, one to configure the dev server (watcher), and for the build. You want to hook into the vinxi app hooks 'build:start' event. Right now the way to do this is like this.

const app = defineConfig({ ... });

// add a hook to the app before exporting it
app.hooks.hook('app:build:start', async () => {
	await main()
})

/**
 *
 * @returns {import('vinxi').PluginOption}
 */
function openAPIPlugin() {
  return {
    name: "fs-watcher",
    async configureServer(server) {
		// run it once at the beginning
		await main();
		server.watcher.add(sourceDir)
        server.watcher.on('change', async (file) => {
            if (file.startsWith(sourceDir))
                // run it again on change
				await main();
        })
	}
  };
}

export default app;

Please note this hooks API is experimental and likely to change soon:

probably to being able to add a hook via config directly (and simpler names like "build:start" instead of "app:build:start"

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants