Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Temp Files in Laravel Mix

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
like image 544
brad Avatar asked Dec 14 '18 11:12

brad


People also ask

How do I delete files after downloading in Laravel?

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!

How do I delete files from storage folder in Laravel?

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.

How do you delete a file in Laravel?

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);


1 Answers

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');
});
like image 165
Matt Rabe Avatar answered Sep 30 '22 16:09

Matt Rabe