Laravel-mix: [Proposal] Best code splitting when we use mix.extract();

Created on 28 May 2019  路  4Comments  路  Source: JeffreyWay/laravel-mix

Actual Code Splitting

When we use mix.extract (); it generates three main files:

  • Application Code: app.js
  • Vendor Libraries: vendor.js
  • Manifest (webpack Runtime): manifest.js

The files of vendor.js andmanifest.js are fine that are always loaded, but in the app.js there is a large amount of code that we do not use in some pages.

Best Code Splitting

  • Application Code In Separate Files: hello.js, counter.js, todos.js, header.js, link.js, card.js
  • Vendor Libraries: vendor.js
  • Manifest (webpack Runtime): manifest.js

The javascript code that we will always need throughout our page

``` blade





Laravel

    <link rel="stylesheet" href="{{ mix('css/app.css') }}">
</head>
<body>
    <div id="app">
       <div id="header"></div> <!-- JS component -->
       @yield('content')
    </div>

      <script src="/js/manifest.js"></script>
      <script src="/js/vendor.js"></script>
      <script src="/js/header.js"></script>
      @stack('scripts')
</body>


````

Import the code that needs to be loaded into a specific page

``` blade

@extends('layouts.app')

@section('content')

Dashboard
You are logged in!

@endsection

@push('scripts')

@endpush
````

All 4 comments

Seems like dynamic imports would be more appropriate here.

Seems like dynamic imports would be more appropriate here.

Of course, but it would be better to divide our code when we use mix.extract();

Right. Dynamic imports will divide your code for you. You're asking for a feature that basically already exists, is what I'm saying.

Agree with @QWp6t. You can use dynamic imports once Mix 5 is out (after webpack 5 is released).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpriceonline picture jpriceonline  路  3Comments

wendt88 picture wendt88  路  3Comments

RomainGoncalves picture RomainGoncalves  路  3Comments

Micaso picture Micaso  路  3Comments

dtheb picture dtheb  路  3Comments