Delete Temp Files in Laravel Mix
Asked Answered
M

1

9

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
Morton answered 14/12, 2018 at 11:41 Comment(3)
Why are you doing this? Why not directly creating the admin.min.css?Spiv
@KuebelElch15 you can't mix css and sass files inside mix.sass() so this is my workaround.Morton
Oh, I've overread the //other css stylesheets commentSpiv
W
15

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');
});
Windowsill answered 26/7, 2019 at 22:25 Comment(1)
I had no idea that those methods returned promises. I've since started importing css files in my sass files so I haven't been needing a temp file. Thanks a lot!Morton

© 2022 - 2024 — McMap. All rights reserved.