Laravel-mix: How to prevent laravel-mix to include the full jQuery Library

Created on 19 Feb 2017  路  7Comments  路  Source: JeffreyWay/laravel-mix

I basically used a .js file that I created in a Laravel 5.1 Project and that was now upgrade to Laravel 5.4.
In Laravel 5.1 we had elixer and the file was compress as expected.

Now with laravel-mix the fill is also compress and put into public/js/
but the file is much larger because the hole jQuery Library is included.

I want to load jQuery seperate e.g. from a CDN!

mix.js('resources/assets/js/foo.js', 'public/js')

With this lines you can test it:

// resources/assets/js/foo.js
$(document).ready(function(){
    $('#foo').removeStyle('bar');
});

this is also do the same (including jQuery):

// resources/assets/js/foo.js
+function ($) {
    $(document).ready(function(){
        $('#foo').removeStyle('bar');
    });
}(jQuery);

laravel-mix: v0.8.1
Node.js: v6.9.5 (latest LTS)
yarn: v0.20.3

Most helpful comment

All 7 comments

Perhaps I don't understand... but just comment out this line:
https://github.com/laravel/laravel/blob/master/resources/assets/js/bootstrap.js#L10

@JVMartin every other file in /resources/assets/js/ that comes with a fresh installation do not exist in my case. So if this line would exit it would make more sense.

Based on the idea that I could load jQuery via a CDN I tried to remove the jQuery package. Now if I run the mix it exits with the error:

ERROR  Failed to compile with 1 errors
This dependency was not found in node_modules:
* jquery
Did you forget to run npm install --save for it?

Commenting out this lines in the config file will do it but I'm not sure if this is really the way it is supposed to work, copy the hole config file just to remove view lines

// [email protected]/setup/webpack.config.js Line 295-299 
    new webpack.ProvidePlugin(Mix.autoload || {
        jQuery: 'jquery',
        $: 'jquery',
        jquery: 'jquery'
    }),

@stefanpolzer i have exactly ur case, however this happened when i tried to use mix in v5.3 installation. https://github.com/JeffreyWay/laravel-mix/issues/285#issuecomment-277307910

but now with fresh install of v5.4 and moved all my files to this new copy, i dont get the jquery issue anymore, so to disable jquery in webconfig
1- remove the jquery dependency from package.json
2- remove window.$ = window.jQuery = require('jquery'); from bootstrap.js

@QWp6t i believe this is an alt to including the window.$ = window.jQuery = require('jquery'); in bootstrap.js

This snippet specifies that Webpack should prepend var $ = require('jquery') to every location that it encounters either the global $ identifier, or window.jQuery. Nifty!

@QWp6t I do not have any bootstrap.js with the window.$ = window.jQuery = require('jquery');
But mix.autoload({}); in the webpack.mix.js file do the trick for me ....

Would be nice if this finds his way int the Laravel Docs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainGoncalves picture RomainGoncalves  路  3Comments

pixieaka picture pixieaka  路  3Comments

nezaboravi picture nezaboravi  路  3Comments

Cheddam picture Cheddam  路  3Comments

amin101 picture amin101  路  3Comments