How can I change the public path to something containing an underscore in Laravel Mix?
Asked Answered
C

4

20

In Laravel 5.4 Mix was introduced to compile assets and maintain the asset pipeline. Mix defaults to your public directory being named public. In many cases, including mine, my public directory is called something else. In my case, it's public_html.

How can I change the public directory where assets are compiled to?

I have tried changing the paths within webpack.min.js to:

mix.js('resources/assets/js/app.js', 'public_html/assets/js')
   .sass('resources/assets/sass/app.scss', 'public_html/assets/css');

Unfortunately this compiles to:

- public
|- _html
|-- assets
|--- css
|--- js
|- fonts

In Laravel 5.3 and Elixir this was as simple as:

elixir.config.publicPath = 'public_html/assets';

I have checked Mix's config file, but can't see anything obvious here.

Please note: this is Laravel Mix, the npm package, so it's nothing to do with amends in the index.php file.

Cartilage answered 5/2, 2017 at 11:35 Comment(0)
C
39

There is an undocumented (I believe) method called setPublicPath. You can then omit the public path from the output. setPublicPath plays nicely with underscores.

mix.setPublicPath('public_html/');
mix.js('resources/assets/js/app.js', 'assets/js')
   .sass('resources/assets/sass/app.scss', 'assets/css');
Cartilage answered 5/2, 2017 at 11:50 Comment(0)
R
3

In Laravel 5.5 I've solved like this,

mix.setPublicPath('public_html/')
    .js('resources/assets/js/app.js', 'front/js')
    .js('resources/assets/js/custom.js', 'front/js')
   .sass('resources/assets/sass/app.scss', 'front/css')
   .styles('resources/assets/css/custom.css', 'public_html/front/css/custom.css');
Rosen answered 24/1, 2018 at 21:33 Comment(0)
B
1

In Laravel 5.8

mix.config.publicPath='public_html';
mix.js('resources/assets/js/app.js', 'public_html/js')
   .sass('resources/assets/sass/app.scss', 'public_html'/css');
Barthold answered 5/5, 2019 at 6:39 Comment(0)
M
0

In Laravel 5.4 you can us this code:

in AppServiceProvider :

public function register()
{

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

}
Marven answered 16/8, 2017 at 5:9 Comment(1)
This is nothing to do with Laravel Mix. Mix is an NPM package (javascript), as stated in my question.Cartilage

© 2022 - 2024 — McMap. All rights reserved.