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

Add plugin option runAfterServe #321

Closed
wants to merge 2 commits into from

Conversation

hirasso
Copy link

@hirasso hirasso commented Feb 15, 2025

Add a new plugin option runAfterServe. The option allows to run an arbitrary callback each time vite or vite serve is cancelled.

Use Case

When running vite serve (or vite), the asset state can drift from the last build. Automatically running vite build on exit ensures the assets folder stays up-to-date.

Usage

import { defineConfig } from "vite";
import laravel from "laravel-vite-plugin";
import { execSync } from "child_process";

export default defineConfig(async ({ mode }) => {
  return {
    plugins: [
      laravel({
        runAfterServe: () => {
          /** build assets each time serve is stopped */
          execSync("pnpm run build");
        },
      }),
    ]
  };
});

Why include this in laravel-vite-plugin

  • Laravel (and most PHP sites) are usually served via an independent dev server (e.g., Herd, Valet, ddev). This means it's easy to access the site when Vite isn't running, potentially leading to outdated assets. Automatically running build on exit ensures a consistent asset state.
  • laravel-vite-plugin already has a robust exit handler for removing the hot file (kudos for that!). Adding a custom callback here introduces minimal overhead while providing significant benefits for developers who rely on this behavior.

Alternatives Considered

Maintain an independent vite plugin. An example can be found here: https://gist.github.com/hirasso/604e3776ecfb402c8513154a8ae46cba

Naming

  • runAfterServe?
  • onExit?
  • cleanup?

The option allows to run an arbitrary callback each time `vite` or `vite serve` is cancelled.
@hirasso hirasso marked this pull request as draft February 15, 2025 11:30
@hirasso
Copy link
Author

hirasso commented Feb 15, 2025

Closing this for now, as I'll need more time to implement tests. eslint tests are now passing. If the idea is considered interesting at all, I could look into a possibility to add vitest tests, if required (and possible).

@hirasso hirasso closed this Feb 15, 2025
@hirasso hirasso reopened this Feb 15, 2025
@hirasso hirasso marked this pull request as ready for review February 15, 2025 20:59
@hirasso hirasso changed the title Add new plugin option runAfterServe Add plugin option runAfterServe Feb 15, 2025
@timacdonald
Copy link
Member

I would prefer to see this one released as dedicated plugin that anyone in the Vite community can use. This functionality would then be accessible to Laravel applications and other communities.

The great thing about using Vite directly is that we can lean on all the existing community plugins.

@hirasso
Copy link
Author

hirasso commented Feb 16, 2025

Point taken. I'll think about releasing it as a separate plugin.

@hirasso hirasso closed this Feb 16, 2025
@hirasso hirasso deleted the feat/run-after-serve branch February 16, 2025 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants