简体   繁体   English

Laravel Mix js结合了所有js文件

[英]Laravel Mix js combining all js files

How do I combine all of my js files to a single file with laravel mix? 如何将所有js文件与laravel mix合并为一个文件? and is it a good practice to directly keep the downloaded external plugins js files and CSS files in public directory? 将下载的外部插件js文件和CSS文件直接保留在公共目录中是一种好习惯吗? because we have a js folder already in resource folder and it's getting compiled and saved in public directory when we do npm run production , not sure if this is correct or not. 因为我们在资源文件夹中已经有一个js文件夹,并且在执行npm run production时正在对其进行编译并保存在公共目录中,所以不确定这是否正确。

This is how my resource folder looks like : 这是我的资源文件夹的样子:

在此处输入图片说明

And my app.js file looks like this : 我的app.js文件看起来像这样:

在此处输入图片说明

You can include those libraries by installing them to NPM with npm install jquery for instance. 您可以通过使用npm install jquery将它们安装到NPM来包括这些库。 Then in your bootstrap.js file you would have something like: 然后,在bootstrap.js文件中,您将得到以下内容:

window.jQuery = window.$ = require('jquery');

That would make it available in the window, and then you still have just one js file to include on the client (your compiled app.js file). 这将使其在窗口中可用,然后您仍然只有一个js文件要包含在客户端上(您的已编译app.js文件)。

If you do have a file you want separate from your main bundle, and it isn't one you have pulled in through NPM, it's not a bad thing to commit it straight to the public folder. 如果确实有要与主捆绑包分开的文件,并且不是通过NPM引入的文件,则直接将其提交到公用文件夹不是一件坏事。 I would keep them in /resources/assets/js/vendor and then use mix.copy to move them into /public/js/vendor . 我会将它们保留在/resources/assets/js/vendor ,然后使用mix.copy将其移至/public/js/vendor Thats just because I prefer all of my work to be in resources and to have public be all compiled or copied files. 那只是因为我希望我的所有工作都在resources并且public所有编译或复制的文件。

You can use npm to install jquery and do it like this same with jeff 您可以使用npm安装jquery并使用jeff进行相同的操作

window.$ = require('jquery');

OR but if you have your own script want to include then 或者,但如果您有自己的脚本要包含,则

just put it top on vue instance 只需将其放在vue实例上

require('./jquery.js') //based on your picture it jquery and app.js are in same level 


window.vue = require('Vue')

this will automatically read and being compiled by the npm. 这将自动读取并由npm进行编译。

you can add them from npm , or if these are external plugins like my case you can do this 您可以从npm添加它们,或者如果这些是像我的案例那样的外部插件,则可以执行此操作

mix.js('resources/assets/js/app.js', 'public/js')
 .scripts([
          "path/plugin1.js",
          "path/plugin2.js",
           .......
         ], 'public/js/all.js').styles([
          "path/plugin1.css",
          "path/plugin2.css",
          .....
         ],'public/css/all.css');

for more useful information you can visit : https://laravel.com/docs/5.6/mix 有关更多有用信息,您可以访问: https : //laravel.com/docs/5.6/mix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM