Move extracted CSS from lazy-loaded VueJS component to a different location

I am currently implementing VueJS 2 and using vue-custom-element with lazy-loading in the following way:

Vue.customElement('nav-icon', () => new Promise((resolve) => {
    require(['./NavIcon.vue'], (lazyComponent) => resolve(lazyComponent.default));
}), {
    beforeCreateVueInstance
});

This setup allows me to upload the JS and CSS into a Shopify Theme and use <nav-icon> to display a custom component.

Everything is functioning correctly except for the fact that in my main entry-point JS file, the names of the chunks for the CSS files, and their paths, are incorrect.

In the configureWebpack section of my vue.config.js file, I have defined the output as follows:

output: {
    filename: '[name].js',
    chunkFilename: 'my-prefix-[name].js'
}

This configuration works fine for the JS files.

I also have a plugins entry for the MiniCssExtractPlugin that renames the resulting CSS files on-disk:

new MiniCssExtractPlugin({
    filename: '[name].css',
    chunkFilename: 'my-prefix-[name].css'
})

However, this does not update the names within the entry-point JavaScript (they are missing 'my-prefix-'), causing them all to return a 404 error when requested..!

In addition, within the entry-point JavaScript, they are all being requested under a css/ folder, which is not usable in my case.

My questions are:

A) How can I rename the CSS chunks correctly so that the filenames on disk match what is requested in the entry-point JavaScript?

B) How can I change the css/ path that the entry-point JavaScript assumes the files are located within? It is not feasible for me to use sub-folders as Shopify Theme Assets do not support them, all files must be top-level.

If I manually edit the generated entry-point file to remove the css/ path and set the correct names, everything works as expected. However, this process is tedious and not scalable.

Thank you for your help!

Answer №1

It turns out that I had missed a crucial detail in the Vue CLI documentation regarding passing an object to the extract option.

Rather than just using true, you can provide an object with options for the mini-css-extract-plugin to further customize its functionality.

By including the following configuration in the css section of my vue.config.js:

extract: {
    filename: '[name].css',
    chunkFilename: 'prefix-chunk-[contenthash].css'
}

And replicating this setup in the MiniCssExtractPlugin and Webpack output options, I successfully synchronized the on-disk filenames and filenames used within the loader function of the entry-point.

A special thanks to @benyuanzhang from the VueJS Discord community for bringing this to my attention.

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

Using the ng-app directive value with HTML and CSS can sometimes cause issues and the styles may not apply

angular.js:38 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=mainapp&p1=Error%3A…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.8%2Fangular.min.js%3A19%3A463) <body ng-app=""> It was working fine unt ...

What is the reason for the malfunctioning of this button upon clicking?

Attempting to customize my personal website by making the sidebar width change when the sidebar button is clicked. I'm puzzled as to why it's not working as intended. I prefer to figure it out independently but any helpful tips would be appreciat ...

Whenever text is present, the sides of my box model, constructed using HTML5 and CSS3, are always pushed downward

When I add extra text to the body of my homepage, it causes a distortion and pushes down the sidebar and advertising boxes on the side. I'm working on this project for class and even though I've asked my teacher for help, she says the code is fin ...

The implementation of `$('tr:even')` in jQuery is failing to generate alternative row colors

My jQuery code is not behaving as expected. When using the following code, the entire table is rendering with a grey background instead of alternating rows: $('#tenantsTbl tr:even').css('background-color','grey'); ...

Utilize Vue-chart.js to dynamically render labels and data in a loop

Can you guide me on how to display data using a loop? Utilize (for; v-for) within: { labels: [] } and datasets: [{ data:[] }] when rendering a chart with Vue-Chart.js I am seeking assistance for rendering data in the following structure: data: { ...

Text reveal animation in Framer Motion is not functioning as expected

I'm having trouble locating the issue in my code, and the text reveal feature isn't working. Interestingly, a simple animation works fine, indicating that there's no problem with the import or library: import { motion } from ...

Using the parameter of type 'never' is necessary as per the TypeScript error message, which states that the argument of type 'string' cannot be assigned to it. This error persists even when

https://i.sstatic.net/tkX07.png const index = selectedActivities.value.indexOf(activity_id); I encountered a TypeScript error saying 'Argument of type 'string' is not assignable to parameter of type 'never'. How can I fix this iss ...

Troubleshooting Vue component reactivity problems with async server requests

Providing some background information: Within my Vue application, I have a view called Articles. Upon mounting this component to the DOM, I use the axios library to fetch all posts from the database through Laravel controllers and API routes. In the articl ...

How can we prevent the modal from extending beyond the boundaries of the phone screen when zoomed in

I am currently developing a web application that features a large content page, specifically a map which should display detailed information upon zooming in similar to Google Maps. The interactive elements on my map are clickable, triggering a modal popup ...

Exploring the Terrain: Troubleshooting Meteor CSS with Twitter Bootstrap

Recently, I have encountered an issue that perplexes me - I am unable to debug less code in my meteor app when the twbs:boostrap package is present. Interestingly, everything works fine when I remove it, but as soon as I add it back, the CSS seems to be co ...

Utilize the v-for Directive in Vue-Tables-2 Templates

I am looking to showcase the data fetched using axios in my dynamically keyed datatables by creating a template. Data Set : "environment": "production", "version": "5.6", "apache_version": "3.2.1" ...

Arranging the Logo and hamburger menu for optimal visibility

I am having trouble aligning a CSS hamburger menu next to my logo. I have placed both the CSS hamburger and the logo in the header. Although I was able to float the logo to the right, it is not on the same line as the menu. After the header, I would like ...

Tips for creating TypeScript Google Cloud Functions using webpack

I'm currently facing a challenge while coding a Google Cloud Function using TypeScript. The concept involves having handler functions defined for various Cloud Functions in separate files within the source repository, along with some code that is shar ...

When hovering over .nav-item, the nav-link color remains unchanged

Is there a way to modify the code below to change the color of a navbar item to red when it is hovered over? .nav-item.active .nav-link{ color: blue !important; } .nav-item.hover .nav-link{ color: red !important; } ...

Elements displaying outside of block 'a' tag

I have a set of <a> elements that are styled with display:block. These contain various nested <divs>, for example: <a href="#" class="sliderframe"> <div class="container-caption"> <div class="slider ...

Creating time input forms that are responsive can be achieved by leveraging the features of Bootstrap

Currently, I am working on a Laravel project and I am facing an issue with making a form responsive. I have added the following viewport meta tag, but I have not seen any changes. Any suggestions on how to make this form responsive would be greatly appreci ...

What are the methods for defining fontSize and fontWeight in Quasar using :table-header-style?

Struggling to adjust the font size and weight in the header of a q-table component using Quasar. Surprisingly, changes to font color and background color are effective, but the size and weight remain unchanged. ...

How can we trigger a mutation in Vue.js using the created or mounted hook?

I've encountered an issue in my Vue.js application with the store.js file where I'm setting up a Vuex store. Here's how it looks: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export const store = new Vue ...

Vue: Load page content only after the route change is complete

Every time I visit a new route within the current session, I notice that the page loads and then takes about 2 seconds for the styles to be applied. This delay causes what is commonly known as CSS flashing or FOUC (Flash of Unstyled Content), which ultimat ...

Debugging Vue.js in Firefox through Visual Studio Code

I've been grappling with debugging my Vue.js app in Firefox, but unfortunately, I haven't had much luck. While Chrome works smoothly, Firefox seems to be giving me trouble with breakpoints. Interestingly, there are no visible errors; the app sim ...