Laravel Mix is a powerful tool that is included as a part of the Radix Drupal theme, it simplifies the process of compiling and optimizing assets in your Laravel applications. While it is primarily used for compiling CSS and JavaScript, you can extend its functionality to watch file changes and execute custom shell commands using ZSH or Bash.

- Install additional watcher library via
npm install
1
| npm install chokidar-cli --save-dev
|
- Modify
webpack.mix.js
configuration file (for this instance I will run ddev drush cr
when any of the theme or SDC’s php
/yml
file is changed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| const mix = require('laravel-mix');
const { exec } = require('node:child_process');
// Declare the files that need to be watched
const watching_SdcAllFiles = 'components/**/*';
const watching_SdcComponentFiles = 'components/**/*.component.yml';
const watching_ThemeConfigFile = 'theme_name.info.yml';
const watching_ThemeLibraryFile = 'theme_name.libraries.yml';
const watching_ThemePhpFile = 'theme_name.theme';
// Declare the watcher for the files that need to be watched
const watcher = require('chokidar').watch([
watching_SdcComponentFiles,
watching_ThemeConfigFile,
watching_ThemeLibraryFile,
watching_ThemePhpFile
]);
// Watch the files and run 'ddev drush cr' when detected
watcher.on(
'change',
(path) => {
console.log(`File ${path} has been changed, running 'ddev drush cr'...`);
exec('ddev drush cr', (error, stdout, stderr) => {
if (error) { console.error(`Error executing 'ddev drush cr': ${error}`); return; }
console.log(`OUTCOME:\t'ddev drush cr' executed successfully: ${stdout}, "\n OUTPUT:\t${stderr}`);
});
}
);
|
Reference#