I'd like to remove temp build files during or after my laravel-mix build.
Here's some code that I have currently, but del
isn't working:
const mix = require('laravel-mix');
const del = require('del');
// compile sass into temp css file
mix.sass('resources/stylesheets/style.scss', 'public/css/temp.css');
// compile css
mix.styles([
// other css stylesheets here...
'public/css/temp.css' // include temp css file
], 'public/css/admin.min.css');
// delete temp css file
del('public/css/temp.css'); // not deleting the file
Luckily Laravel 8 already provided a function to delete the file directly after sending it to the user end. With the help of this function deleteFileAfterSend(true) a chaining method for Response class in Laravel 8 our task is done!
One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.
You could use PHP's unlink() method just as @Khan suggested. But if you want to do it the Laravel way, use the File::delete() method instead. $files = array($file1, $file2); File::delete($files);
I was able to solve this by running del
within a then()
returned by mix.styles()
:
const mix = require('laravel-mix');
const del = require('del');
// compile sass into temp css file
mix.sass('resources/stylesheets/style.scss', 'public/css/temp.css');
// compile css
mix.styles([
// other css stylesheets here...
'public/css/temp.css' // include temp css file
], 'public/css/admin.min.css').then(() => {
del('public/css/temp.css'); // deletes the temp file
});
The same thing also works with mix.scripts()
:
mix.scripts([
'public/js/app.js',
'public/js/global.js',
], 'public/js/app.combined.js').then(() => {
del('public/js/app.js');
del('public/js/global.js');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With