Mr.Singh
Mr.Singh

Reputation: 1789

Laravel-MIX: Remove unused files after combine/merge

I am new at Laravel-mix and webpack. After some practice I was able to combining css and js files into 1 single file respectively. However, after the combine the other files are still there, which are of no use to the application.

For example:

resources
|-- scss
  |-- font-awesome.scss
  |-- template.scss

public
|-- css
  |-- font-awesome.css
  |-- template.css

After combining:

public
|-- css
  |-- font-awesome.css
  |-- template.css
  |-- combined.min.css (font-awesome.css + template.css = combined.min.css)

Webpack

mix
    .sass('resources/sass/font-awesome.scss', 'public/css/font-awesome.css')
    .sass('resources/sass/template.scss', 'public/css/template.css');

// combining css files
mix.combine([
    'public/css/font-awesome.css',
    'public/css/template.css'
], 'public/css/combined.min.css');

As the font-awesome.css & template.css files are combined into combined.min.css, there is no need for them anymore, same goes for the javascript files.

How to remove these useless files, is this possible..?

Upvotes: 5

Views: 2569

Answers (1)

Mr.Singh
Mr.Singh

Reputation: 1789

Here is the link where I found the solution https://stackoverflow.com/questions/53779090/delete-temp-files-in-laravel-mix

webpack.mix.js:

...
const del = require('del');
const env = process.env.NODE_ENV || 'dev';

mix
    .sass('resources/sass/font-awesome.scss', 'public/css/font-awesome.css')
    .sass('resources/sass/template.scss', 'public/css/template.css');

// combining css files
mix.combine([
    'public/css/font-awesome.css',
    'public/css/template.css'
], 'public/css/combined.min.css').then(() => {
    if (env === 'production') {
        del('public/css/font-awesome.css');
        del('public/css/template.css');
    }
});

Upvotes: 4

Related Questions