Repository

jeroennoten/Laravel-AdminLTE

Easy AdminLTE integration with Laravel
3480 990 129 763

[BUG] Laravel mix issues

Describe the bug

Following the rules given to us here Other Configuration, I discovered that this doesn't load any of the plugins like datatables, date picker etc...

Is there something that was omitted there or should we just create pull request adding plugins after linking to the app.js file which per my testing works., or somehow find a way to import the enabled plugins to the js files

Expected behavior

No console errors

Actual behaviour

Inability to initialize plugins

Screenshots

If applicable, add screenshots to help explain your problem or to shown the issue.

12 Comments

  1. Please, provide more details on how you are trying to load the plugins (datatable, date picker). However, take into consideration that when you are using Laravel Mix, all plugins should be included using it, you can't use the plugins configuration section of the adminlte.php file anymore.

    For further details, check how the enabled_laravel_mix option impacts on the master.blade.php file.

    Also, you may check this issue #1030

  2. Yes issue #1030 sums it up

    When you use mix, no plugins are loaded which is not the intended behaviour

    I believe adding the plugins in the master.php file under the if block would solve this problem and prevent others from having this issue and opening PRs

    Would you love me to do that for you

    And keep up the good work maintaining this lovely package

  3. I'm not sure, I always believed the idea of using Laravel Mix is to include all plugins into compiled files, so your blade views will only include one JS and one CSS file... I have never used Laravel Mix, but I think that code was created that way for some reason...

  4. Yes, it's for building up all assets into one file

    But in this case, there is no way we can include some without including all plugin files which would lead to a very huge file

    It would be possible if we include all plugin files and find a way to purge unused styles and scripts

    This would be similar to tailwind just-in-time compilation but the build step would always be necessary which is not a problem

    Unfortunately, I've searched and I haven't seen a good method of purging unused js and styles without crippling functionality

    So therefore to be able to use plugins, you'd have to include the plugins files in the master blade file.

  5. Ok, you may create a PR for this, but I will have to learn how Laravel Mix works before merging it (just to sure...). The other alternative is to use the workaround mentioned on the issue #1030 ...

  6. The workaround requires you to add the CSS and js on all pages

    Maybe explaining Mix briefly would do for you, because learning webpack and mix takes a decent amount of time

    Mix uses webpack to compile and bundle defences to form static assets

    Eg if I require bootstrap in my app.js file, Webpack creates the corresponding file in public/js/app.js that is using the default laravel mix configuration but no need for that now

    Now what is going on here is that there is no non-hacky way to include to add the plugin js files without including each and every one

    That is you'd have to require each plugin

    Consider this example in app.js

    //although not technically true imagine this is us linking all these scripts using a script tag with the src attribute
    
    require('overlayscrollbars');
    require('bootstrap');
    require('../../vendor/almasaeed2010/adminlte/

    We have loaded what adminlte itself requires.

    Now for datatables, sweet alerts etc to work, you'd have to add some of the following

    //you'll do this for all plugins
    require('path-to-datatables-js')
    require('path-to-select2-js')

    Now since we are adding the contents of these scripts into the app.js file, it would become huge

    I'm talking around 6 to 15 mib. In this case, it's not worth using Mix since in a real sense we cannot selectively load what plugins we would use

    That's why we just use a regular script tag to load assets like this

    Now the proposed solution

    In master.blade.php

    {{-- Base Scripts --}}
        @if(!config('adminlte.enabled_laravel_mix'))
            <script src="{{ asset('vendor/jquery/jquery.min.js') }}"></script>
            <script src="{{ asset('vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
            <script src="{{ asset('vendor/overlayScrollbars/js/jquery.overlayScrollbars.min.js') }}"></script>
    
            {{-- Configured Scripts --}}
            @include('adminlte::plugins', ['type' => 'js'])
    
            <script src="{{ asset('vendor/adminlte/dist/js/adminlte.min.js') }}"></script>
        @else
            <script src="{{ mix(config('adminlte.laravel_mix_js_path', 'js/app.js')) }}"></script>
        @endif

    This says if the laravel mix option is enabled link only the app.js file that mix created, but remember that we cannot add the plugin scripts into app.js therefore, we have to link it the regular way using
    @include('adminlte::plugins', ['type' => 'js'])

    Now the solution is simple

    Remove the plugin include from the if block so it loads plugins in all cases

    It would then look something like

    {{-- Base Scripts --}}
        @if(!config('adminlte.enabled_laravel_mix'))
            <script src="{{ asset('vendor/jquery/jquery.min.js') }}"></script>
            <script src="{{ asset('vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
            <script src="{{ asset('vendor/overlayScrollbars/js/jquery.overlayScrollbars.min.js') }}"></script>
    
            <script src="{{ asset('vendor/adminlte/dist/js/adminlte.min.js') }}"></script>
        @else
            <script src="{{ mix(config('adminlte.laravel_mix_js_path', 'js/app.js')) }}"></script>
        @endif
    
    
      {{-- Configured Scripts --}}
            @include('adminlte::plugins', ['type' => 'js'])

    Do this also for CSS and that's all

  7. Ok @yungifez I understood the problem you are facing when using mix. You may create a PR for this, but I will maintain order of plugins when not using Laravel Mix, just to be sure nothing is broken, like this:

    {{-- Base Scripts --}}
    @if(!config('adminlte.enabled_laravel_mix'))
        <script src="{{ asset('vendor/jquery/jquery.min.js') }}"></script>
        <script src="{{ asset('vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
        <script src="{{ asset('vendor/overlayScrollbars/js/jquery.overlayScrollbars.min.js') }}"></script>
    
        {{-- Configured Scripts --}}
        @include('adminlte::plugins', ['type' => 'js'])
    
        <script src="{{ asset('vendor/adminlte/dist/js/adminlte.min.js') }}"></script>
    @else
        <script src="{{ mix(config('adminlte.laravel_mix_js_path', 'js/app.js')) }}"></script>
    
        {{-- Configured Scripts --}}
        @include('adminlte::plugins', ['type' => 'js'])
    @endif

    And applying the same idea for the css...

  8. It's better if the scripts go down so that features like the menu works before all enabled plugins.

    Putting the same condition twice in if and else is considered bad practice though ( but who cares in the real world ).
    You can review the pr and edit to your taste

  9. Yeah, but I'm not sure if order matters and plugins should be before adminlte.min.js. I will try the changes locally just to be sure...