Custom chunk import splitting with Vue CLI and Webpack: A guide

Uncertain if the title is effective, but here's my attempt to clarify:

I have a total of 4 pages: a1.vue, a2.vue, b1.vue, and

b2.vue.<br>
In my <code>router/index.js
:

import a1 from '@/components/a1';  
import a2 from '@/components/a2';  
import b1 from '@/components/b1';  
import b2 from '@/components/b2';  

var router = new Router({  
    routes: [{  
        path: '/a1',  
        name: 'name-a1',  
        component: a1  
    }, {  
        path: '/b2',  
        name: 'name-b2-page',  
        component: b2  
    }]  
});  

Upon running npm run build, a app.js file is generated containing resources for pages a1~b2.
Furthermore, I understand that by writing:

const a1 = ()=> import('@/components/a1');  
const a2 = ()=> import('@/components/a2');  
const b1 = ()=> import('@/components/b1');  
const b2 = ()=> import('@/components/b2');  

webpack will create files a1.js, a2.js, b1.js, b2.js and only load them when their respective page is visited.
Now, onto the question at hand:

How can I chunk a.js and b.js,
where a.js contains sources from a1.js and a2.js, while b.js includes sources from b1.js and b2.js?
Also, ensure that upon visiting /a2 after /a1, a.js does not reload.

Thank you.

Answer №1

Utilize webpack's unique commenting feature to leverage special parameters.

For instance, consider the following in your scenario:

const a1 = ()=> import(/* webpackChunkName: "a" */'@/components/a1');
const a2 = ()=> import(/* webpackChunkName: "a" */'@/components/a2');
const b1 = ()=> import(/* webpackChunkName: "b" */'@/components/b1');
const b2 = ()=> import(/* webpackChunkName: "b" */'@/components/b2');

You can also make use of webpackMode: lazy.

To explore further information and see examples, please visit: https://webpack.js.org/api/module-methods/

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

What can I do to prevent masonry images from piling on top of one another?

I have been working on creating a photo gallery using the Bootstrap 5 example on masonry, and everything is working well so far. I have also successfully implemented modal boxes. However, the only issue I'm facing is with the JS Image Loaded not preve ...

Converting units to rem dynamically in CSS: a comprehensive guide

Hey there, I'm currently trying to dynamically convert units into rem in CSS and facing some issues. I have set the root font-size as 23px The current font-size is 16px The expected result should be 16 / 23 => 0.695rem Question: I am looking for ...

Loop through with <li> inside a <td> tag

I need help figuring out how to place a list of items inside a table row using ng-repeat in AngularJS. Here is my attempted code: <td> <div ng-repeat="x in total.count"> ...

Massive HTML Table Containing Rows upon Rows

Currently, I have a server that can provide me with a list of objects in json format, and my goal is to showcase them in a table on the client side. Initially, I thought about dynamically modifying the DOM after receiving data from the server. Building th ...

Calculating the sha1 hash of large files using JavaScript in the browser without causing excessive memory usage

Is there a method to calculate the hash of large files in JavaScript without needing to load the entire file in a FileReader? Specifically, I'm interested in finding out if it's possible to stream a file and calculate its sha1 hash in JavaScript. ...

Differences between Global and Local Variables in Middleware Development

While exploring ways to manage globally used data in my research, I stumbled upon this question: See 2. Answer After integrating the suggested approach into my codebase, I encountered a problem that I would like to discuss and seek help for. I created a ...

Loading jQuery via JavaScript in the head section of the HTML document

I'm currently developing an application using jQuery and jQuery Mobile. I have a script in the head section that dynamically loads both libraries. However, my body contains a script that relies on jQuery ($) and is unable to access it because it loads ...

The Google reCaptcha reply was "Uncaught (in promise) null"

When using reCaptcha v2, I encountered an issue in the developer console showing Uncaught (in promise) null message regardless of moving the .reset() function. Here is the console output: https://i.stack.imgur.com/l24dC.png This is my code for reCaptcha ...

When the time comes, ReactDOM will render your element into the designated container,

What does the [,callback] parameter represent in the ReactDOM.render(element, container) method? ...

Using a customized layout, merge AngularJS into Vaadin

I experimented with integrating an angular JS application into Vaadin by utilizing a custom layout as shown below: VerticalLayout mainLayout = new VerticalLayout(); mainLayout.setMargin(true); mainLayout.setWidth("1380px"); setCompositionRoot( ...

Include a new feature within an onClick event

I'm looking to implement a single page application using React.js and I want to incorporate a list within a material-ui drawer. The goal is to dynamically add elements to an array every time a button is clicked, but I'm stuck on how to write this ...

Customizing Axios actions in Vue JS using a variable

I have a form in my Vue component that sends a reservation object to my API for storage. I am exploring the possibility of setting the axios action dynamically based on a variable's value, without duplicating the entire axios function (as both the pos ...

Issue with wp_ajax function not being triggered in Wordpress

Attempting to implement my first AJAX Function in Wordpress 4.3. I created a plugin called "calipso". Within the plugin, there are two files: calipso.php : <?php /** * @package calipso * @version 1.0 */ /* Plugin Name: calipso Plugin URI: http://www. ...

Retrieve dynamically generated v-select elements' selected options in Vue Vuetify

In my Vue project, I am utilizing Vuetify to populate a list of objects retrieved from an API. The JSON data structure looks like this: users = [ { "id": "1234", "name": "John Doe", "description": "Male, 25 years old", " ...

What is the best way to store objects in files using Node.js?

As I manage my Node server, a question arises - what is the best way to convert objects into a serialized format and save them to a file? ...

Tips for cropping an image using CSS with dimensions specified for width, height, and x and y coordinates

As I work on implementing a cropping feature using react-easy-crop, my goal is to store the user's image along with pixel coordinates that dictate their preferred cropping area. My objective is to utilize the parameters provided by react-easy-crop in ...

Utilizing jQuery to Retrieve Column and Row Headers of a Selected Cell

I am currently working on a JavaFX project that includes a WebView. The HTML code is being generated using a StringBuilder. My goal is to retrieve the column and row headers of the selected cell. This information is necessary for my Java code. You can ...

Generating static HTML files from Node/Express views during the construction process

Seeking clarification on my terminology. My Node/Express server currently provides Jade template files for my AngularJS app. Transitioning to a live deployment, I am required to run the app with Apache using static files instead of dynamic Jade views. Is ...

Error message: Unexpected end of input found in HTML, CSS, or JavaScript files

I am currently working on a card game project but I keep encountering an error that says "unexpected end of input". This error is completely new to me, so any assistance or guidance would be greatly appreciated. Additionally, the codepen isn't display ...

Results not showing up

After attempting to incorporate the scores into a table, I am facing an issue where they are not displaying. Can anyone offer assistance with this problem? I am currently hosting on GitHub Pages. File: https://raw.githubusercontent.com/Eternal-Network/Ete ...