JoshMountain's avatar

Using Mix to version combined files

I'm trying to convert my old gulpfile.js to be compatible with Mix and 5.4. Things went pretty smoothly until I got to versioning. My webpack.mix.js looks like this:

mix.js([
    'resources/assets/js/bootstrap.js',
    'node_modules/bootstrap/dist/js/bootstrap.js',
    'node_modules/rangetouch/dist/rangetouch.js',
    'node_modules/select2/dist/js/select2.full.js',
    'node_modules/spectrum-colorpicker/spectrum.js',
    'node_modules/flatpickr/dist/flatpickr.js',
    'resources/assets/js/app.js',
], 'public/js/app.js')

.less('resources/assets/less/app.less', '../resources/assets/build/app.css')

.combine([
    'resources/assets/build/app.css',
    'node_modules/font-awesome/css/font-awesome.css',
    'node_modules/select2/dist/css/select2.css',
    'node_modules/plyr/dist/plyr.css',
    'node_modules/spectrum-colorpicker/spectrum.css',
    'node_modules/flatpickr/dist/flatpickr.css',
    'node_modules/flatpickr/dist/themes/airbnb.css'
], 'public/css/app.css')

.version([
    'public/css/app.css',
    'public/js/app.js'
]);

What I expect to happen is:

  1. All JS files get combined into public/js/app.js
  2. One less file gets processed into resources/assets/build/app.css
  3. All CSS files (including the one from #2) are combined into public/css/app.css
  4. public/css/app.css and public/js/app.js get versioned.

In reality, the first 3 steps go fine, but when it comes to version the CSS file - it doesn't. Instead it versions resources/assets/build/app.css. Can you not pass in an array of things to get versioned like you could with gulpfile.js?

0 likes
18 replies
ejdelmonico's avatar

Good question. I haven't tried passing anything in. I just call version() and everything seems to work out fine. Mix versions everything in their current directory so no more build directory.

JoshMountain's avatar

@ejdelmonico Any idea how to accomplish what I am going for with Mix, combine 1 or more LESS files and 1 or more CSS files into one versioned CSS file?

ejdelmonico's avatar

@JoshMountain Let me have a look at the code and get back to you.

UPDATE: There is a mix.combine() that takes an array which might help.

mix.combine(files, destination);
JoshMountain's avatar

@z.bruce I'm not sure that is really related to the issue here. Versioning is working it just isn't versioning the right file. Also I'm not using hot reloading.

colourmill's avatar

@JoshMountain are you worried about syntax or about getting it up and running? Have you tried it like this?

mix.js([
    'resources/assets/js/bootstrap.js',
    'node_modules/bootstrap/dist/js/bootstrap.js',
    'node_modules/rangetouch/dist/rangetouch.js',
    'node_modules/select2/dist/js/select2.full.js',
    'node_modules/spectrum-colorpicker/spectrum.js',
    'node_modules/flatpickr/dist/flatpickr.js',
    'resources/assets/js/app.js',
], 'public/js/app.js').version()

.less('resources/assets/less/app.less', '../resources/assets/build/app.css')

.combine([
    'resources/assets/build/app.css',
    'node_modules/font-awesome/css/font-awesome.css',
    'node_modules/select2/dist/css/select2.css',
    'node_modules/plyr/dist/plyr.css',
    'node_modules/spectrum-colorpicker/spectrum.css',
    'node_modules/flatpickr/dist/flatpickr.css',
    'node_modules/flatpickr/dist/themes/airbnb.css'
], 'public/css/app.css').version()
JoshMountain's avatar

@colourmill Trying it like that it versions my JS properly but my CSS winds up in /resources/assets/build (versioned).

If I change line 11 to this:

.less('resources/assets/less/app.less', 'public/css/app.css')

it works properly, however now I have this useless app.css intermediary file in the public directory. I was hoping I could have it tucked away as a build file, or remove it but I don't think mix can remove files.

colourmill's avatar

@JoshMountain a quick solution could be to .less() the file to public/css/app.css, it should then get overwritten in the combine step.

JoshMountain's avatar

@colourmill Oddly enough, when I do that it versions the file before the combination. I get app.css which is a combined but not versioned file and then app.xxxxx.css in the same directory which is versioned but only includes the less file.

iraklisg's avatar

laravel-mix is a great tool that simplifies the process of bundling via webpack. However, it should not considered as a replacement of laravel-elixir (a gulp wrapper), since gulp and webpack are two different tools with different philosophies.

Having said that, ΙΜΗΟ removing completely ly `laravel-el from 5.3 and substituting it with th `laravel in 5.4 a hasty decision, since laravel users that used to simply concatenate and versioning their static assets cannot relay on laravel-mix for doing this

2 likes
gfabrizi's avatar

@iraklisg exactly, i feel the same. It seems to me that it was a rush decision to ditch elixir for mix in 5.4. Elixir still offers better flexibility for small-medium projects (obviously IMHO)

iraklisg's avatar

@gfabrizi, all, the good news is that already has been contributed a PR by @forehalo (many thanks dude :+1:) that deals with the versioning of combined files. Moreover, since [email protected] the combined files are automatically added to the mix-manifest.json so that the laravel's mix() helper function can be actually used to reference them see also here.

S11K's avatar

Maybe my solution can help, here is the original thread from github : https://github.com/JeffreyWay/laravel-mix/issues/211

And here is my contribution :

The problem is that css files generated from sass compilation are versioned, so they have a different name from original, just use wildcard for styles() or combine() use.

mix
.sass('resources/assets/sass/app.scss', 'public/css/app.css')
.styles([
        'public/css/app.*.css',
        ...
])
.version()
;

Hope it helps!

Please or to participate in this conversation.