Laravel-mix: Understanding mix.extract() and mix.autoload()?

Created on 16 May 2018  路  5Comments  路  Source: JeffreyWay/laravel-mix

  • Laravel Mix Version: 1.7.2
  • Node Version: 9.7.1

1. Why Lodash work but not jQuery?

In webpack.mix.js, i use extract method like that:

mix.extract(['jquery', 'lodash'])

In main file app.js, i try to check them

console.log(_)  <-- OK
console.log($)  <-- Undefined
console.log(jQuery)  <-- Undefined

2. Why window not show when use mix.autoload()

In order to work for jQuery, i must use autoload method

mix.autoload({
    jquery: ['$', 'window.jQuery', 'jQuery']
});

In main file app.js, i try to check them again

console.log($)  <-- OK now
console.log(window)  <-- ??? I can not find out `$` in browser console

Any help me explain those!? Thanks
@JeffreyWay

stale

Most helpful comment

I've been looking for it for a long time, but no one knows, including the person who made it.

All 5 comments

autoload() is using webpack ProvidePlugin internally.
extract() is just a way to keep your dependencies in a separate file, it does not autoload them.

ProvidePlugin will replace any occurrence with the specified module, in your case;
'$', 'window.jQuery', 'jQuery' will be replace by node_modules/jquery/main-file-specified-in-package.json module during webpack compilation.

Thank @ankurk91.
So why i add extract() for lodash, i dont use autoload, _ still be init in window?

Updated
I find out in lodash's source code window._ = lodash, maybe that help it auto init?!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I've been looking for it for a long time, but no one knows, including the person who made it.

If you are using laravel framework, there is one line in resources/js/bootstrap.js:

window._ = require('lodash');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mstralka picture mstralka  路  3Comments

amin101 picture amin101  路  3Comments

stefensuhat picture stefensuhat  路  3Comments

rderimay picture rderimay  路  3Comments

dtheb picture dtheb  路  3Comments