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

Don't write to the output files if they have not changed at all #722

Closed
wants to merge 2 commits into from

Conversation

gms8994
Copy link
Contributor

@gms8994 gms8994 commented Mar 4, 2024

When trying to include the ziggy:generate run as part of webpack (via webpack-watch-files-plugin and hook-shell-script-webpack-plugin), ziggy:generate will always write the output files even if there are no changes to the source routes file(s), which will then cause webpack to see a change to some of the source files that are being watched, which will trigger ziggy:generate to run again, causing an infinite loop.

When trying to include the `ziggy:generate` run as part of `webpack` (via `webpack-watch-files-plugin` and `hook-shell-script-webpack-plugin`), `ziggy:generate` will always write the output files even if there are no changes to the source routes file(s), which will then cause `webpack` to see a change to some of the source files that are being watched, which will trigger `ziggy:generate` to run again, causing an infinite loop.
@bakerkretzmar bakerkretzmar self-assigned this Mar 4, 2024
@bakerkretzmar
Copy link
Collaborator

Thanks for the PR! Can you configure your watcher to only run php artisan ziggy:generate if one of your route files is changed/saved, not on every Webpack run? Ideally instead of doing a check like this within Ziggy, the command would just only run when it actually needs to.

@bakerkretzmar
Copy link
Collaborator

Closing for now, looks like this should be possible within your Webpack config, which I think is the right place for this kind of thing to be handled. Here's a simplified plugin I have working locally that you can adapt to your project:

class Plugin {
    apply(compiler) {
        compiler.hooks.watchRun.tap('ziggy-generate', function (compiler) {
            if (compiler.modifiedFiles && Array.from(compiler.modifiedFiles).find((file) => file.includes('/routes/'))) {
                console.log('Route file changed, regenerating Ziggy config:')
                execSync('php artisan ziggy:generate');
            }
        });
    }
}

This should cause changes to your route files to trigger 2 builds, one because of the change and one from the generate command updating the ziggy.js file, but then it should stop.

Let me know if there's anything else I can help with here!

@gms8994
Copy link
Contributor Author

gms8994 commented Mar 29, 2024

@bakerkretzmar given this, do you think it would be possible to include your plugin class with the ziggy code so that it could easily be included in the webpack config? You and I can't be the only ones that want this kind of functionality :)

Perhaps it could take params for the paths to watch and the command to run?

@gms8994 gms8994 deleted the gms8994-patch-1 branch March 29, 2024 15:58
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