Search code examples
javascriptwebpacklaravel-mixdel

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

Solution

  • 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');
    });