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包含文件呢?有什么方法可以改善上述情况吗?