Skip to content

Commit

Permalink
Add example Webpack plugin for watching route files to Readme. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerkretzmar committed May 6, 2022
1 parent a9e0e07 commit c03b1b1
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Ziggy supports all versions of Laravel from `5.4` onward, and all modern browser
- [Vue](#vue)
- [React](#react)
- [SPAs or separate repos](#spas-or-separate-repos)
- [Recompiling on route changes](#recompiling-on-route-changes)
- [**Filtering Routes**](#filtering-routes)
- [Basic Filtering](#basic-filtering)
- [Filtering using Groups](#filtering-using-groups)
Expand Down Expand Up @@ -414,6 +415,32 @@ const Ziggy = await response.toJson();
route('home', undefined, undefined, Ziggy);
```

#### Recompiling on route changes

If you're generating a `ziggy.js` file and using Laravel Mix, you can watch your route files and regenerate your Ziggy configuration when they change by adding a custom Webpack plugin like the one below to your build:

```js
// webpack.mix.js
const { exec } = require('child_process');
const { resolve } = require('path');
const glob = require('glob');

mix.webpackConfig({
plugins: [
{
apply: (compiler) => {
compiler.hooks.compilation.tap('WatchRoutesPlugin', (compilation) => {
glob.sync('./routes/*.php').map(file => compilation.fileDependencies.add(resolve(file)));
});
compiler.hooks.afterDone.tap('ZiggyGeneratePlugin', () => {
exec('php artisan ziggy:generate');
});
}
},
],
});
```

## Filtering Routes

Ziggy supports filtering the routes it makes available to your JavaScript, which is great if you have certain routes that you don't want to be included and visible in the source of the response sent back to clients. Filtering routes is optional—by default, Ziggy includes all your application's named routes.
Expand Down

0 comments on commit c03b1b1

Please sign in to comment.