thebigk's avatar
Level 13

Difference between .js() Vs. scripts()

I'm unable to figure out the difference between mix.js() and mix.scripts(). I've found out that for me, my own scripts with app.js (has jQuery) do not work with mix.scripts(), but work with mix.js

For other scripts, I can use mix.scripts() to combine them.

What's the deal?

0 likes
3 replies
crnkovic's avatar

https://laravel.com/docs/5.6/mix#working-with-scripts

Mix provides several features to help you work with your JavaScript files, such as compiling ECMAScript 2015, module bundling, minification, and concatenating plain JavaScript files. Even better, this all works seamlessly, without requiring an ounce of custom configuration:

mix.js('resources/assets/js/app.js', 'public/js'); With this single line of code, you may now take advantage of:

  • ES2015 syntax.
  • Modules
  • Compilation of .vue files.
  • Minification for production environments.

Similar to combining stylesheets with mix.styles(), you may also combine and minify any number of JavaScript files with the scripts() method:

mix.scripts([
    'public/js/admin.js',
    'public/js/dashboard.js'
], 'public/js/all.js');

This option is particularly useful for legacy projects where you don't require Webpack compilation for your JavaScript.

In essence, .scripts takes scripts and just puts all of them into a single file, whereas .js compiles babel and modules and shit like that into JS that all browsers can run.

thebigk's avatar
Level 13

Thanks @crnkovic - I read that already. Makes me wonder if I should first run .scripts and then .js on my scripts for my production server?

crnkovic's avatar
crnkovic
Best Answer
Level 43

scripts is used to combine multiple script files into single one so your code doesn't take many trips to a server. Compile Vue stuff or whatever frontend you use with .js and them combine all your scripts with .scripts. If you have only one script, no need to use .scripts.

1 like

Please or to participate in this conversation.