I am currently in the process of diving into Laravel Mix and so far, whilst I fully understand what Laravel Mix is and how it works, I am trying to understand a little more about the common practices and 'How-Tos'...

For instance, consider this file structure:

/resources/assets/js/app.js (all global functions)
/resources/assets/js/index/index.js (functions specific to index.js)
/resources/assets/js/about/about.js (functions specific to about.js)
/resources/assets/js/contact/contact.js (functions specific to contact.js)

Now, ideally, I would like the following combined and minified in the following way:

/public/js/index/index-some_hash.js (including app.js)
/public/js/about/about-some_hash.js (including app.js)
/public/js/contact/contact-some_hash.js (including app.js)

据我了解,达致这目标的方法大致如下:

// Index
mix.js([
    'resources/assets/js/app.js',
    'resources/assets/js/index/index.js'
], 'public/js/index/index.js').version();

// About
mix.js([
    'resources/assets/js/app.js',
    'resources/assets/js/about/about.js'
], 'public/js/about/about.js').version();

// Contact
mix.js([
    'resources/assets/js/app.js',
    'resources/assets/js/contact/contact.js'
], 'public/js/contact/contact.js').version();

My Question

Quite simply, I would like to know if the above is the correct method for doing what I am trying to do? Are there better ways, or more common ways of achieving this?

如果上面的 struct 是错误的,并且我的文件还有其他组合方式,那么请分享你的知识.然而,除非有很好的理由,否则我想避免以下情况:

  • Serving two separate files for each page i.e. app.min.js and index.min.js. This requires two lookups per page, ideally it should be as few as possible
  • Serving the same file to ALL pages on my site. Serving code to a page that is not going to use it is a waste of resource, regardless of caching...

One Idea...

我注意到其中一个JS文件中有一行代码;require('./bootstrap');.可以说我是老式的,但我从来没有在JavaScript中看到过这一点(我假设它来自node.js).也就是说,显然它只是将bootstrap.js文件作为依赖项加载到特定文件中.那么,考虑到这一点,下面的解决方案会更好吗:

about.js

require('./app'); // Include global functions

// Do some magic here specifically for the 'about' page...

webpack.mix.js:

mix.js(['resources/assets/js/*/*.js'); // For all pages

如果这是一个更好的解决方案,那么如何使用SASS包含文件呢?有什么方法可以改善上述情况吗?

推荐答案

I'd say there are 3 main things you have to consider here (they are not all equal):

  • Size - The amount of space your resources are going to take up and the file sizes that are being downloaded.
  • Number of requests-有多少文件被加载到页面中.
  • Browser Cache-文件将从缓存而不是服务器中提取.

在您的特定情况下,这将取决于您的app.js文件本身有多大,如果没有来自app.js的代码,您的页面特定的文件会有多大,您有多少页面特定的文件,以及您是否在不同的文件中使用一些相同的资源,例如,需要在不同的文件中使用相同的包.


One File

如果您的页面特定文件非常小,那么我会将它们包含在您的主app.js文件中:

require('./index/index')
require('./about/about')
//etc

网页包.混合js:

.js('resources/assets/js/app.js', 'public/js')

这意味着您只在服务器上存储了一个编译后的文件,对您的javascript只发出了一个请求,并且应该为整个站点的每个后续页面加载缓存该请求.


A main file and a page specific file

If you have a large number of page specific files or your page specific files aren't that small then I would suggest compiling your app.js independently of your page specific files. Don't forget to compare the results of this approach with the One file approach as the One File个 approach might still be more efficient.

网页包.混合js

.js('resources/assets/js/app.js', 'public/js')
.js('资源/assets资源 /js/index/index.js', 'public/js/index')
.js('resources/assets/js/about/about.js', 'public/js/about')

This will mean that the main bulk of your code (app.js) will still be getting cached and that only one request is being made for page specific code (which should then be cache for each subsequent load of that page).

请注意,如果您同时需要app.js个文件和特定于页面的文件中的包,它们将不会在这2个文件之间共享,因此会增加这些请求的总体大小.可以添加到window对象中的包(例如jQuery)应该只包含在您的app.js中.

解决这个问题的一种方法是使用Vendor Extraction(Laravel Mix docs):

.extract(['jquery'])

这将产生两个额外的文件,您需要将它们包括在html中:

<script src="/js/manifest.js"></script>
<script src="/js/vendor.js"></script> 

One main file and a few page specific file

如果你有一个或两个页面特定的文件相当大,但其余的不是,你可以把大部分页面特定的文件编译成app.js个,然后让较大的文件编译成自己的文件.


All page specific files

只有当您的所有页面特定文件都相当大,app.js个文件没有那么大,并且页面特定文件中的代码/逻辑不能重构以便可以在不同的文件之间共享时,我才会走这条路.

This way would similar to the example in your question, however, instead of having:

网页包.混合js

mix.js([
    'resources/assets/js/app.js',
    '资源/assets资源 /js/index/index.js'
], 'public/js/index/index.js').version();

you would have:

资源/assets资源 /js/index/index.js

require('../app.js')

//rest of file

网页包.混合js

mix.js('资源/assets资源 /js/index/index.js', 'public/js/index/index.js').version();

I would never reach for this approach straightaway though and there are very few situations where it would be the most efficient.


Summary

I would always reach for the One File个 approach and then look at optimising later (unless something really suck out during development) as premature optimisation can lead to code smell and harder maintainability.

这篇文章对你来说可能也是一篇好文章

Laravel相关问答推荐

使用Laravel判断其他数据库中是否存在记录

在 React 中将属性从一个组件传递到另一个组件

如何在 laravel 中为另一个用户 session()->forget('cart')?

RuntimeException 未安装 Zip PHP 扩展

Laravel 5.8 在从已清除的会话中单击注销后显示419 Page Expired

laravel url 验证变音符号

Laravel 合集日期比较

如果用户在 Laravel 中经过身份验证,如何签入 Vue 组件?

静态密码在默认的 Laravel 用户工厂中是如何工作的?

如何在使用 Laravel 在控制器中发送邮件之前更改邮件配置?

未找到 PHP 5.4 和 Laravel 类Memcached

将非框架 PHP 项目移植到 Laravel 4.x

在 Eloquent 中按自定义顺序对集合进行排序

Laravel 初学者的良好开端

Laravel / Eloquent内存泄漏重复检索相同的记录

laravel 队列 - 同步驱动程序如何工作?它是在单独的进程/线程还是主执行线程中执行?

Laravel - 使用 Eloquent 查询构建器在 Select 中添加自定义列

是否可以在 Laravel 的不同数据库中引用外键?

Eloquent的关系 - attach附加(但不保存)到 Has Many

找不到 HOME 环境 -- 扩展 `~'