How to Keep Using Laravel Elixir and Gulp instead of Moving to Laravel Mix

During an upgrade to Laravel 8, we encountered the following error:

 Call to undefined function elixir() (View: /var/www/resources/views/layouts/head.blade.php)

That's because the elixir() helper function was removed in Laravel 8, after having been deprecated with this commit.

The upgrade guide does mention this change, and recommends moving to Laravel Mix.

In case it's preferable to continue using Laravel Elixir and the underlying gulp implementation, just retain the following function somewhere else in your app:

if (! function_exists('elixir')) {
    /**
     * Get the path to a versioned Elixir file.
     *
     * @param  string  $file
     * @param  string  $buildDirectory
     * @return string
     *
     * @throws \InvalidArgumentException
     *
     * @deprecated Use Laravel Mix instead.
     */
    function elixir($file, $buildDirectory = 'build')
    {
        static $manifest = [];
        static $manifestPath;

        if (empty($manifest) || $manifestPath !== $buildDirectory) {
            $path = public_path($buildDirectory.'/rev-manifest.json');

            if (file_exists($path)) {
                $manifest = json_decode(file_get_contents($path), true);
                $manifestPath = $buildDirectory;
            }
        }

        $file = ltrim($file, '/');

        if (isset($manifest[$file])) {
            return '/'.trim($buildDirectory.'/'.$manifest[$file], '/');
        }

        $unversioned = public_path($file);

        if (file_exists($unversioned)) {
            return '/'.trim($file, '/');
        }

        throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
    }
}

Et voila - you should be back up and running until the next thing breaks!

Tags

 Laravel Elixir  Gulp  Laravel Mix  PHP