npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

laravel-mix-assets-splitter

v1.0.9

Published

Laravel Mix Extension Allowing You To Easily Code Split without needing to specify every file (fork of laravel-mix-code-splitter)

Downloads

36

Readme

Version Maintenance


Install


NPM

npm install --save-dev laravel-mix-code-splitter

Yarn

yarn add laravel-mix-code-splitter --save

Purpose


  • [x] Split Code
  • [x] Improve Apps
  • [x] Improve User Lives
  • [x] Improve Developer Lives
  • [x] Hook Into Laravel Mix without Losing any original, core logic

Laravel Mix Code Splitter


Definition from google


"Modern sites often combine all of their JavaScript into a single, large bundle. ... An alternative to large bundles is code-splitting, which is where JavaScript is split into smaller chunks. This enables sending the minimal code required to provide value upfront, improving page-load times. The rest can be loaded on demand."



Setup


1. webpack.mix.js


let mix = require('laravel-mix');
require('laravel-mix-code-splitter');

/** Normally, we create a single global file -- available on all pages **/
mix.js('resources/js/app.js', 'public/js/app.js')

  /*----------------------------------------------------------
   | Code splitting allows us to "split" our code up.
   *----------------------------------------------------------
   |  
   | -> resources   
   |    . /js/split/home.js       
   |    . /js/split/badges.js
   |    . /js/split/invoices.js
   |         
   */  
   .split(mix.js, 'resources/js/split', 'public/js/split') 

   /*----------------------------------------------------------------------------
 	| Providing splits of our code for each page or feature specific page
 	*----------------------------------------------------------------------------
 	|  
 	| -> public  
 	|    . /js/split/home.js
 	|    . /js/split/badges.js
 	|    . /js/split/invoices.js
 	|
 	*/

2. Cool, cool -- sound great, how do we actually "code split" within Laravel?


After mix does its thing, there's multitudes of ways to dynamically load the proper split for the current route ~ here's a simpler implementation I personally like


routes/web.php


Add names to your routes, we'll use resourceful routes to speed the example up


Route::resource('home', 'HomeController')->home('home');
Route::resource('badges', 'BadgesController')->name('badges');
Route::resource('invoices', 'InvoiceController')->name('invoices');

Add snippet to your primary layout blade file


resources/views/layouts/app.blade.php


  • First we load our js (app.js)
  • Then we check if there is a code split
  • If there is a code split for the current route resource group, we load it

<script src="{{ asset('js/app.js') }}"></script>

@if (asset( "js/split". Str::of(Route::currentRouteName())->before('.')->append('.js') ))
	<script src="{{ asset( Str::of(Route::currentRouteName())->before('.')->start('js/split')->finish('.js') ) }}"></script>
@endif

home.js loads on visits to


  • /home
  • /home/{id}
  • /home/create
  • /home/{id}/edit

badges.js loads on visits to


  • /badges
  • /badges/{id}
  • /badges/create
  • /badges/{id}/edit

invoices.js loads on visits to


  • /invoices
  • /invoices/{id}
  • /invoices/create
  • /invoices/{id}/edit

app.js loads on every page



Code splitting non-js files


let mix = require('laravel-mix');
require('laravel-mix-code-splitter');

mix

.js('resources/js/app.js', 'public/js/app.js')
.split(mix.js, 'resources/js/split', 'public/js/split') 


.css('resources/js/app.css', 'public/css/app.css')
.split(mix.css, 'resources/css/split', 'public/css/split')

.css('resources/js/app.css', 'public/css/app.css')
.split(mix.css, 'resources/css/split', 'public/css/split')

.postCss('resources/css/main.css', 'public/css', [require('precss')()])
.split(mix.postCss, 'resources/css', 'public/css/split', [require('precss')()])

// etc...