Elixir versioning public path
Asked Answered
H

3

9

I'm trying to use Elixir's version() method with my 'public' folder being public_html (instead of the default 'public' method).

I version my css file, which produces a build folder with the manifest inside public_html

elixir.config.cssOutput = 'public_html/css';
elixir.config.jsOutput = 'public_html/js';

elixir(function(mix) {

    mix.styles([
        'vendor/normalize.css',
        'app.css'
    ], null, 'public_html/css');

    mix.version('public_html/css/all.css');

});

In my blade template I use

<link href="{{ elixir("css/all.css") }}" rel="stylesheet">

Problem is the elixir function searches in 'public/build' folder. How can I change this so it searches public_html folder?

Thank you in advance

Huntress answered 7/4, 2015 at 7:27 Comment(3)
laracasts.com/discuss/channels/general-discussion/…Isom
Any way of not changing the public path in elixir config file? I had to change it in /node_modules/laravel-elixir/ingredients/version.js before it would work!Huntress
elixir() uses public_path() so you need to override path.public as shown in the post I linkedIsom
U
11

You forgot to overwrite your publicDir folder in the gulpfile.js file, this points the build folder to the correct location which is used by elixir to determine the version hash.

elixir.config.publicDir = 'public_html';

After you did that you must overwrite your public_path in Laravel, the recommended way for Laravel 5 is to go to the index.php in your public folder:

Underneath

$app = require_once __DIR__.'/../bootstrap/app.php';

Add

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});
Upas answered 18/4, 2015 at 11:40 Comment(5)
Thanks, I found the solutions already but forgot to update this thread. Thanks anyway!Huntress
I added those lines but it does not seem to fix the problem, it still creates the public directory (instead of public_html)Aerophone
You also need to add elixir.config.publicPath = 'public_html'; as told by LordcashSik
It seems the way to do it now is using the 2nd parameter to the php elixir function e.g. elixir("app.js", "") the default value is build. Setting it to the empty-string "moves it back" to public.Mackenzie
.. or public_html in this case.Mackenzie
M
5

all you need do is this

var elixir = require('laravel-elixir');
elixir.config.publicDir = 'public_html';
elixir.config.publicPath = 'public_html';
Marnie answered 1/9, 2015 at 14:58 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Wholly
A
1

There is no option in elixir() for changing the public path:

function elixir($file)
    {
        static $manifest = null;

        if (is_null($manifest))
        {
            $manifest = json_decode(file_get_contents(public_path().'/build/rev-manifest.json'), true);
        }

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

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

There are only few options. You can change public path by adding this to App\Providers\AppServiceProvider

public function register()
    {
        $this->app->bind('path.public', function() {
          return base_path().'/public_html';
        });
    }

or you can create your own elixir function or a helper class

Example:

    function my_own_elixir($file,$path=public_path())
            {
                static $manifest = null;

                if (is_null($manifest))
                {
                    $manifest = json_decode(file_get_contents($path.'/build/rev-manifest.json'), true);
                }

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

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

Then you can use this later in view:

{{my_own_elixir('/css/all.css',$path='path to public_html')}}

Hopefully in future version of elixir this future will be included. :)

Abolish answered 7/4, 2015 at 8:24 Comment(1)
The path is not hardcoded it uses Laravels globally configured public_path which makes sense. And it can be easily overridden. No need to write your own function. (You also don't have to create a new service provider but can just use the AppServiceProvider for that)Isom

© 2022 - 2024 — McMap. All rights reserved.