Reduce the size of the vendor.js file using Laravel Mix

Upon reviewing the page load performance with Lighthouse, I discovered that the vendor.js file is a whopping 5MB and lacks dead code elimination. Despite using dynamic imports for components, the size of the vendor.js file remains unexpectedly large, indicating that there may be an issue in my implementation.

Here's an example of how I'm dynamically importing components:

const app = new Vue({
    vuetify,
    el: '#app',
    components: {
        'currency-converter': () => import(
          /* webpackChunkName: "components/js/Assets/Menu/Currency" */ 
          './components/Assets/Menu/Currency.vue'
        ),
...

When inspecting the network tab during page load, I noticed that each component and the vendor.js file are being loaded individually, resulting in the oversized 5MB file. It seems that dead code elimination is not functioning properly, leading to unnecessary bloat.

This is what my mix.js file currently looks like:

mix.js('resources/js/admin.js', 'public/js').js('public/assets/admin/main.js', 'public/js')
    .js('resources/js/app.js', 'public/js').vue().vuetify('vuetify-loader')
    .sass('resources/sass/admin.scss', 'public/css')
    .css('public/assets/css/main.css', 'public/css')
    .webpackConfig({
        output: {
            publicPath: '/',
            chunkFilename: '[name].js?id=[contenthash]',
         },
        plugins: [
            new webpack.ContextReplacementPlugin(/moment[/\]locale$/, /en-gb|el/),
            new NodePolyfillPlugin()
        ],
    });

    mix.extract();

    if (mix.inProduction()) {
        mix.version();
    }

Despite searching for solutions, I have yet to find a satisfactory way to address this issue efficiently.

Answer №1

Is it worth verifying whether the dynamically imported components are included in the vendor.js file? I have encountered situations where vendor bundles can become quite large, especially if the server is not configured to properly compress the sources for transportation (such as using gzip or brotli). It's possible that this issue could be attributed to a configuration problem on the server side.

Answer №2

If you're looking to trim down unnecessary code in your project, using a tree shaking plugin can effectively remove dead code. This method is particularly useful if you want to optimize your project without solely relying on production mode optimizations within Mix.

const DeadCodePlugin = require('webpack-deadcode-plugin');

module.exports = {
  // ...
  plugins: [
    new DeadCodePlugin({
      patterns: ['**/*.(js|jsx|css)'],
      exclude: ['**/*.(stories|spec).(js|jsx)'],
    }),
  ],
  // ...
};

Additionally, have you thought about incorporating the minify plugin for webpack?

const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = {
  // ...
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin(),
      new OptimizeCSSAssetsPlugin(),
    ],
  },
  plugins: [
    new webpack.optimize.MinimizePlugin(),
  ],
  // ...
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible in Javascript to convert a table cell at the end of a row into its own separate table row?

I need to adjust the layout of a table I am pulling from a SharePoint site. Unfortunately, I cannot make changes to the original table, but I am hoping to use JavaScript to modify it on the page. The issue is that the table has a fixed width with evenly d ...

Sourcesets organized in Gradle's hierarchy

In my current project, I am utilizing Gradle's SourceSets to define JavaScript sources. However, I am facing a challenge in arranging the sources in a specific order. Ideally, I would like to have something along the lines of: javascript.source { ...

Utilizing the background page to maintain a value required by an injected JavaScript code

{ "name": "coffee", "manifest_version":2, "version": "1.0", "description": "testing coffee app", "browser_action": { "default_icon": "icon.png" , "default_title": "My Task List", "default_popup": "popup.htm ...

Ways to modify the default text in a dropdown menu?

I'm currently attempting to modify the title of a dropdown using the Multi-select plugin found here. However, I've encountered an issue where I am unable to dynamically change the text (Dropdown title) of the dropdown using JavaScript. $(' ...

Choosing bookmarkable views in Angular 5 without using routes

I'm currently working on a unique Angular 5 application that deviates from the standard use of routes. Instead, we have our own custom menu structure for selecting views. However, we still want to be able to provide bookmarkable URLs that open specifi ...

Using `ng-model` within an Angular directive

Looking to achieve bi-directional binding in an Angular directive Here is my current approach: angular.module('myapp',[]),directive('mydirective', function() { var directive = {}; directive.restrict = 'E'; directi ...

Prevent iframe loading from occurring

I am currently working with an iframe where I upload files, but I am facing a challenge. How can I stop the loading process in the middle of it? I attempted to modify the src using a jQuery function attr(), however, it was not effective. Removing the entir ...

initialize input data in vue

How can I set a default value for an input in a date picker using Vue JS? I have tried setting the data but it's not showing up. When I inspect the element, I see this code: https://i.stack.imgur.com/fQDLL.png Here is my script: new Vue({ e ...

Check to see if the user has been redirected to the page using JavaScript

When a file input validation fails in a Laravel project, the user is redirected back to the current page. However, the issue arises when the user has to scroll down to see the error message and understand what went wrong. This doesn't provide the bes ...

Troubleshooting AJAX failures in Android Cordova app's Release mode

Encountering an issue with AJAX calls failing in release mode on Android but working fine on iOS. The error message seen in Chrome Dev Tools is: Provisional headers are shown Request URL:https://server.com/api/index.php?ticket=ST-111111-ABCDEFG-cas Requ ...

Guide on extracting data from a JSON array within a Flask template

I am a beginner with flask and python, and I am working on displaying a JSON list received from Python. Here is the Python code: myList = [] for data in dataList: myDict= { "a": data[0], "b": data[1], "c": data[2], "d": ...

Connecting two tables in an express API

Currently, I am in the process of developing an API using Express.js. At this stage, my initial tests are functioning correctly. My goal now is to retrieve values from two separate tables. For example, consider the following 2 tables: Table_A Id: 1, Name: ...

conversion of markdown into a JSON object

Upon importing a markdown file into a node module using a webpack loader, I encountered an issue. The imported file is a text book with chapters separated by h2 tags. I am seeking a solution to convert this markdown file into a JSON object where each h2 t ...

What is the best way to implement jQuery on my website?

Instead of copying all the example code here, you can check out the jQuery demo page: Demo Page I would like to integrate the Latest News example with scrolling text in red from the demo page into my website. The demo page also provides links to the sour ...

At times, the content in a JQuery dialog box may display outdated information

On my webpage, I have multiple buttons where each button contains specific data. When a button is clicked, it opens a dialog box with two input fields that are date types. If any changes are made, they should be saved for that particular button. The issue ...

"Encountering a problem with a missing object in jQuery when referencing

I'm encountering an issue with jQuery's $(this) object that's causing me to lose track of the this element in my code: $('.star').click(function (){ var id = $(this).parent().attr('id').split('rating')[ ...

Is there a way to access the name of a generic type T class in TypeScript?

I'm currently working on incorporating the Service Locator pattern into my TypeScript project. Below is the snippet of my code: //due to only partial knowledge of TypeScript private static serviceMap: Map<string, any>; public static get& ...

Transmitting boolean values from a Python API request

Is it possible that the error in my script is due to the boolean value I am trying to send in an API put call using Requests in Python? Here is how my script looks: import requests data = { 'name': 'John', 'lastname': ...

Encountering a Paypal SDK issue with the message: "Error [ERR_HTTP_HEADERS_SENT]: Unable to modify headers after they have already been sent to

I am encountering an issue where I receive a status code 201, but when attempting to redirect to the approval_url, I encounter an error stating: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Interestingly, if I re ...

I am having trouble with my jQuery login function not properly connecting to the PHP file

Hey there, I've been working on creating a login system from scratch by following an online tutorial. The initial Javascript is functioning properly as it detects errors when the inputs are empty. However, once I enter text into the input fields and c ...