sebastiansulinski's avatar

Mix in Laravel 5.4 with jQuery plugins

I've spent several hours trying to resolve this issue without any luck so perhaps someone could help.

I've set up my webpack.mix.js in the following way:

let mix = require('laravel-mix');

mix
    .sass('./resources/assets/sass/admin/app.scss', './public/css/admin/app.css')
    .sass('./resources/assets/sass/admin/login.scss', './public/css/admin/login.css')
    .sass('./resources/assets/sass/front/app.scss', './public/css/app.css')
    .js(
        [
            './node_modules/cascading-dropdown/src/jquery.cascading-drop-down.js',
            './node_modules/ssd-form/src/jquery.ssd-form.js',
            './node_modules/ssd-confirm/src/jquery.ssd-confirm.js',
            './node_modules/simple-ajax-uploader/SimpleAjaxUploader.js',
            './node_modules/sortablejs/Sortable.js',
            './node_modules/swiper/dist/js/swiper.jquery.js',
            './node_modules/lightgallery/dist/js/lightgallery.js'
        ],
        './public/js/vendor.js'
    )
    .js(
        [
            './resources/assets/js/app.js'
        ],
        './public/js/app.js'
    )
    .version();

My app.js file looks like so:

require('./bootstrap');

Vue.component('basket', require('./components/Basket.vue'));
Vue.component('pagination', require('./components/Pagination.vue'));

import Form from './core/Form';
import Focus from './core/Focus';
import Editor from './core/Editor';
import Sorter from './core/Sorter';
import Trigger from './core/Trigger';
import Gallery from './core/Gallery';
import SlideShow from './core/SlideShow';
import Navigation from './core/Navigation';
import Confirmation from './core/Confirmation';
import UploadManager from './core/Upload/UploadManager';

const app = new Vue({
    el: '#app',
    mounted() {
        new Form();
        new Focus();
        new Editor();
        new Sorter();
        new Trigger();
        new Gallery();
        new SlideShow();
        new Navigation();
        new Confirmation();
        new UploadManager();
    }
});

When I run mix I get everything compiled without any errors, but when I preview the project in the browser it doesn't seem to be able to find the plugins which are included in the vendor.js file from within any of the classes i.e. Form.js:

class Form {

    constructor() {
        $('form[data-ajax-form]').ssdForm({
            extendBehaviours: {
                clickCallReload: function() {
                    window.location.reload(true);
                }
            }
        });
    }

}

export default Form;

I get

Uncaught TypeError: $(...).ssdForm is not a function

pointing to $('form[data-ajax-form]').ssdForm({}); call.

Also - my app.js contains jQuery and Sizzle multiple times - can't figure out where it comes from?

0 likes
16 replies
ejdelmonico's avatar

I am no expert on webpack but I believe the default config excludes node_modules in *.js. That may be why. I usually copy any files I need from node-modules using the copy command before running other functions. It does add time but I get better results.

sebastiansulinski's avatar

The plugins are actually compiled into vendor.js so it adds them just fine, but they don't seem to be accessible from within app.js

numberone's avatar

why dont you put all your js file in app.js file by require('node_module_name') ?

and for scss files, you can import them into app.scss.

sebastiansulinski's avatar

Not really as they go into different templates. I cannot require these plugins as they are not modules - just simple old school jQuery plugins.

ejdelmonico's avatar

Try removing the node_modules exclusion from *.js in the default config. I am guessing that webpack is not referencing the plugins correctly because of the exclusion. In other words, it compiles them but doesn't know where to find them for the class files. Just a guess.

It's line 93 in webpack.config.js

sebastiansulinski's avatar
Level 3

Thanks @ejdelmonico, but I'm afraid it wasn't that. I've updated the jQuery plugins by adding modular functionality and it seem to be working fine.

I've also removed all these plugins from mix.js([]) call and added them to mix.extract([]) together with vue and jquery.

1 like
livijn's avatar

Can you show your final webpack.mix.js?

sebastiansulinski's avatar

It's pretty much the same - it's just plugins now export module, which can be bundled - before, some of them didn't since the problem.

livijn's avatar

I have the same problem, but i can't get it to work. All libs are added in the package.json I am using this mix file:

mix
    .js([
        'resources/assets/js/app.js'
    ], 'public/js/app.js')
    .extract([
        'jquery',
        'bootstrap',
        'slidebars',
        'slick-carousel',
        'nouislider'
    ])
    .sass('resources/assets/sass/app.scss', 'public/css/app.css')
    .version();

If I type $('body').hide() in the console, I get $(...).hide is not a function.

livijn's avatar

This is my app.js: http://pastebin.com/suZCgTX8 It throws an error saying Uncaught ReferenceError: slidebars is not defined.

What do you mean with import jQuery? It is added in the compiled vendor.js since the mix.extract() along with the rest of the packs in that call.

livijn's avatar

The plugin does have a main pointing to its js-file. And the plugin is - as far as I understand - a module.

This is some of the code generated by Laravel Mix: http://pastebin.com/6VhAMjut

livijn's avatar

What is it that distinguishes a modular plugin from a non-modular one?

Please or to participate in this conversation.