Laravel 5: Generating Public URLs to Elixir Compiled Assets With elixir

April 14, 2018 —John Koster

The elixir helper function can be used to get the path to a versioned Elixir file. The function accepts two arguments, the name of the $file to find the path for and the $buildDirectory to look in. By default, the $buildDirectory is set to build. The build directory is relative to the applications public directory (app/public).

As of Laravel 5.4, Laravel Elixir was renamed to Laravel Mix. This was largely due to the fact that the underlying system that powered Laravel Elixir was changed. For the function to use with the updated system, see the mix helper function.

#Signature

The signature of the elixir function is:

1function elixir(
2 $file,
3 $buildDirectory = 'build'
4);

#Example Use

Assuming the following simple gulpfile:

1var elixir = require('laravel-elixir');
2 
3elixir(function(mix) {
4 mix.sass('app.scss');
5 
6 // Version using the default configured build path.
7 mix.version('css/app.css');
8 
9 // Version using a custom build path.
10 mix.version('css/app.css', 'public/build2');
11});

The path to the file versioned using the default configured build directory can be determined like this:

1elixir('css/app.css');

and would produce output similar to the following:

1/build/css/app-aa3c9d985b.css

Likewise, the path to the versioned file with the custom build directory can be determined by supplying an argument for $buildDirectory:

1elixir('css/app.css', 'build2');

which would produce output similar to the following:

1/build2/css/app-64f4edce94.css

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.