Can you explain the process of NuxtJS css extraction for creating static websites?

I am currently working on creating a static website using my minimal code with Nuxt. This code includes integration of the tailwindcss toolkit and vue2-leaflet. When I run

nuxt generate

I end up with two CSS files - one for the tailwindcss styles and another for leaflet styles. However, the latter file is lacking most of the necessary CSS:

.leaflet-tile-pane{z-index:200}@-webkit-keyframes leaflet-gestures-fadein{to{opacity:1}}@keyframes leaflet-gestures-fadein{0%{opacity:0}to{opacity:1}}

This incomplete styling results in the map rendering incorrectly. Here's my current nuxt.config.js:

module.exports = {
  mode: 'universal',

  head: {
    title: pkg.name,
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: pkg.description }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  css: [
  ],

  plugins: [
    { src: '~plugins/leaflet.js', mode: 'client' }
  ],

  buildModules: [
    '@nuxtjs/tailwindcss'
  ],

  modules: ['@nuxtjs/apollo', 'nuxt-purgecss', ['nuxt-i18n', i18n]],

  [...]

  build: {
    extractCSS: true,
  }
}

Removing the extractCSS option pulls all relevant CSS into the index.html, but it triggers an error:

ERROR  Webpack mode only works with build.extractCSS set to *true*. Either extract your CSS or use 'postcss' mode

I'm confused about how exactly CSS extraction functions. Can someone clarify why extractCSS: true isn't functioning as expected? How can I resolve this issue? Why does it work in SPA mode but not in static mode?

Answer №1

Utilizing nuxt-purgecss which employs purgecss to eliminate unused CSS.

purgecss examines HTML (or vue) files for utilized CSS classes and eliminates any unused classes from the final CSS bundle.

You can reference the default configuration of purgecss used by nuxt-purgecss here. The paths specify the locations that purgecss will analyze for CSS usage.

Since you are not directly using most of the leaflet css in your components, it is essential to configure purgecss to exclude leaflet's css.

This can be accomplished by whitelisting (although unsure if the "comment" method works in Vue\Nuxt)

Further information can be found here and here

Not tested!!

// nuxt.config.js
{
  purgeCSS: {
    whitelistPatterns: [/leaflet/, /marker/]
  }
}

Addressing the error message

The error message originates from the nuxt-purgecss module - detailed information available here

I lack extensive knowledge of the Nuxt build process. Based on the documentation, I presume that extractCSS: true will utilize extract-css-chunks-webpack-plugin to extract all CSS into a separate file, while the default extractCSS: false will use PostCSS to extract all CSS and embed it directly into the rendered page's <style> tag.

In my opinion, none of this matters because the underlying issue lies with the use of purgecss and the solution resides in configuring it correctly to whitelist the leaflet CSS classes....

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 to create a CSS checkbox without using an ID or "for" attribute

Is it possible to create a CSS checkbox with label without using id and for attributes? I have tried the following method, but it doesn't seem to work. I know that I can create a CSS checkbox with id and for attributes like this: <div class="chec ...

The method for viewing stack traces in Express JS server logs

In my tech stack, I'm utilizing Nuxt (Vue), Express.js, and Axios. Encountering a scenario where a REST call fails, exemplified by the code snippet below: axios.get('api/data-test'); The client displays an error message like this: xhr.js ...

Type definition for Vuex store functionality

Working on creating a versatile type to provide typing hints for mutations in Vuex. After reading an inspiring article on Vuex + TypeScript, I decided to develop something more generic. Here is what I came up with: export type MutationType<S, P, K exten ...

I am having trouble aligning the element perfectly in the center

I'm working on a radio input design featuring a circle in the middle, achieved via the pseudo element ::before However, I've noticed that when the input size is an odd number, the centering isn't quite perfect This issue seems more promine ...

Utilizing a background image in the slick slider

<div id="largeCarousel" style="display:inline-block; float:right;"> <div class="homepage-item" style="padding-bottom:52%; position:relative; box-sizing:border-box;"> <div id="largeCarouselContent" style="position:absolute ...

Adjusting the overflow of a parent div based on the position of the div within it by scrolling

I'm trying to create a page with 3 different sections: <div class="container" id="main-container"> <div class="section" id="profile"> Hello World </div> <div class="section" id="projects"> Hello World 2 ...

Ways to customize the border of the ListItemButton when it's in a selected state using Material UI?

I'm currently working on a feature that involves highlighting a ListItemButton with a specific background color when selected. However, I now want to take it a step further and add an outline or border color around the ListItemButton when it is select ...

The Facebook share button on my website is malfunctioning

Could someone please help me figure out why my custom Facebook share button is not functioning properly? When I click the button, the share window fails to appear. I have tested it as an html file on a live web server and have thoroughly reviewed the Faceb ...

Issue detected: Props that are of type Object/Array must utilize a factory function in order to provide the default value

I recently started using Vue-Cli3.0 and came across this interesting module for Vue.js called https://github.com/holiber/sl-vue-tree It's a customizable draggable tree component for Vue.js, but I encountered an issue where it couldn't copy funct ...

Experience Markdown's Single Input Update Feature

I have recently developed a Markdown editor using Vue, similar to the examples found on Vue's website. However, my query is more related to implementation rather than Vue itself. Therefore, I am open to suggestions that do not necessarily involve Vue. ...

Can a sophisticated matrix-like design be created using a CSS grid system?

I'm currently working on implementing a unique grid structure in Material-UI using a CSS grid: Browse the desired complex layout here Here's the current CSS grid layout I've created with Material-UI: View the existing layout here This is ...

Display a div using Jquery when hovering over it with several classes

I want to create a hover effect where div2 fades in slowly when hovering over div1 within the same container. However, my jQuery code doesn't seem to be working as expected... $(".div").hover(function() { $(this).find(".div2").animate({ opac ...

Is the whitespace-pre-line feature of TailwindCSS experiencing issues?

whitespace-pre-line doesn't seem to be working for me. I attempted to replicate the same code from my text editor on https://play.tailwindcss.com/, and it worked perfectly. Below is a screenshot that I have attached. This is the sample code in my tex ...

Is webpack.js missing in the Laravel Vue application?

When using Vue in Laravel and attempting to run npm run dev, an error is encountered: code: 'MODULE_NOT_FOUND', requireStack: [ 'G:\\laravel-vuetify-spa-starter\\node_modules\\webpack-cli\\bin&b ...

(no longer supported) We are unsure about the usage of /deep/, >>>, and ::ng-deep, ::v-deep

Since /deep/ has been deprecated, I have found that using global styles instead is the only viable solution. Does anyone know of an alternative solution where we can still maintain encapsulation or scoped styling? ...

reduce the size of the image as the browser width decreases

Struggling with a webpage layout that features an image on the left and content area on the right, both with fixed widths. When reducing browser width, the image should not shrink but be cropped from the left side instead. Any solutions for this issue? ...

What is the best way to create a column that adjusts its width based on the row width in a table?

Here is the code snippet that I am working with: return ( <Box sx={{ height: 400 }}> <DataGrid columns={columns} rows={rows} /> </Box> ) Currently, my table appears like this: https://i.sstatic.net/KWXwL.png I am aiming for the ...

Trouble with HTML2PDF.js - download not being initiated

Currently, I am utilizing html2pdf.js in my project by following this tutorial: https://github.com/eKoopmans/html2pdf.js However, I've encountered an issue where despite implementing everything as instructed, the download prompt for the specified div ...

When the screen is resized, the embedded iframe moves smoothly to the left side

I recently created a projects page, but I am facing an issue with the iframe embed. Whenever the screen is resized, it keeps shifting to the left. However, what I really want is for it to be centered instead of being on the left side: https://i.stack.imgu ...

Implementing Pagination in Vue: How to Make it Work with Response Data

I am looking to integrate pagination from the response data into my existing code, while also incorporating filters. JavaScript var entriesList = new Vue({ el: "#post-list-template", data: { posts: [], categories: [], cu ...