Transforming Global CSS from a style tag to a CSS file in Nuxt.js

I'm having some difficulties with the Nuxt.js SCSS compilation process. In my project, I have an assets folder containing two SCSS files that are included in the nuxt.config.js:

// Global CSS (https://go.nuxtjs.dev/config-css)
css: [
  '~assets/scss/bootstrap.scss',
  '~assets/scss/main.scss'
],

After running npm run build and then npm run generate, I checked the dist folder and opened the index.html file to find all the CSS embedded within a large style tag on the page:

https://i.sstatic.net/uu1Zw.png

Instead of this approach, I would like Nuxt.js to compile the SCSS files from assets and put the CSS into separate files linked via <link> tags in the head section, like so:

<link rel="stylesheet" href="/css/bootstrap.css">
<link rel="stylesheet" href="/css/main.css">

While I am aware of the head settings in nuxt.config.js, I've learned that they only support remote and static file links, not local ones like in my case:

head: {
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1, user-scalable=0, shrink-to-fit=no' },
{ hid: 'description', name: 'description', content: '' }
],
link: [
{ rel: 'stylesheet', href: '~/assets/scss/bootstrap.scss' },
{ rel: 'stylesheet', href: '~/assets/scss/main.scss' },
{ rel: 'icon', type: 'image/svg+xml', href: '/images/logo.svg' },
],
script: []
},

Is there a way to instruct Nuxt.js to output Global CSS to individual files? Can this be achieved through Nuxt.js configuration or by altering the Webpack build configuration? Your guidance in clarifying this matter would be greatly appreciated :-)

Answer №1

If you want to separate all your CSS assets into distinct files (typically one per component) and cache each CSS file, you can utilize the extractCSS build option:

// nuxt.config.js

export default {
  build: {
    extractCSS: true
  }
}

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

Encountering the error "Module 'aws-sdk', 'child_process', 'net' cannot be resolved" within the /node_modules/watchpack directory when using Webpack

I'm encountering issues while trying to compile my prod webpack file, specifically receiving 5-10 errors related to "cannot resolve module" aws-sdk and child_process. All of these errors seem to point back to the same path: "ERROR in (webpack)/~/watc ...

Transform a key:value string with multiple lines into a JSON format

I need to convert a multiline key:value string into JSON so that I can use it to create HTTP headers for an ajax request. Here is a sample data: content-type:text host:myhost.com I want the output to be: {"content-type":"text", "host":"myhost.com" } I ...

Enhance your automation with the powerful combination of Java,

I am looking to retrieve data from a specific Russian gambling website using String information. Below is the code I have written for this purpose: import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.ope ...

Cypress .type is failing to properly update the v-model value in my Vue3-powered form

login.vue - this file represents the login page component <template> <v-container id="login-page"> <v-row> <v-col> <input id="username" v-model="username" data-cy="username&q ...

JavaScript: Append an ellipsis to strings longer than 50 characters

Can the ternary operator be utilized to append '...' if a string surpasses 50 characters? I attempted this approach, however it did not work as expected. {post.title.substring(0, 50) + post.title.length > 50 ? '...&ap ...

Sequential asynchronous code works, but parallel execution encounters issues

My asynchronous code works flawlessly when executed in series using await, however, as soon as I attempt to run it in parallel using .then() for better performance, I encounter null reference errors. The use of puppeteer in the launch method is not the i ...

utilizing BrowserRouter for dynamic routing in react-router-dom

I'm currently facing a challenge with creating a multi-tenant SaaS solution. Each tenant needs to be able to use a subdomain, so that I can extract the subdomain from the URL and make a call to a REST API to retrieve data specific to that tenant. For ...

Exploring the Contrast between 'this' Context in V8 Javascript: Chrome versus Node.js

Check out the code snippet below: var pocket = { cash: 1000, showCash: function() { return this.cash; } }; var thomas = { name: "Thomas", work: function() { console.log('I don&bs ...

What is the best way to rotate points around a mesh?

I am attempting to utilize Three.js to create a collection of points using Three.Points. My goal is to have these points rotate around a single point or mesh. I have already successfully generated the points randomly within a cylinder region, following the ...

Loop through the HTML and print it repeatedly

I've been using this code to retrieve Json data and display it based on the same id. However, I've noticed that it only prints once and doesn't display all the data as expected. var requiredData; $.getJSON('http://127.0.0.1/appcpanel/ ...

What is the process for reverting Node.js from version 16 LTS back to 14 LTS?

Looking for help to downgrade my Node.js version from 16 to 14 for a project. I want to avoid conflicts and errors during the process. Any tips or guidance would be appreciated! Currently working on Windows platform. Thanks in advance. ...

How to effectively create factories in AngularJS

I stumbled upon this angularjs styleguide that I want to follow: https://github.com/johnpapa/angular-styleguide#factories Now, I'm trying to write my code in a similar way. Here is my current factory implementation: .factory('Noty',functi ...

Is there a way to automatically update the selected item in a qselect when the page loads?

I have implemented a QSelect Dropdown in my page header with various options. Here is the code snippet: <q-select filled v-model="model" use-input hide-selected fill-input input-debounce="0" ...

Disabling function name renaming in Webpack for TypeScript and Javascript files

Is there a way to prevent Webpack from renaming function names? I have a class named MenuBlocksMenuPage in my code: import { MenuBlocksMenuPage } from "../pages/menu/blocks/menupage"; However, the compiled file turns this line into an unreadable string. ...

Utilizing Ajax for Multiplication

Is there a way to dynamically calculate and display the total amount by multiplying the unit price with the quantity entered in the text box as it changes? <div> <label id="" name="price" class="unitprice"><?php echo "Price: <label ...

Error: The property or method 'xxxx' has not been defined. Please check the component <component v-bind:is="xxxx">

I am developing a multi-page Vue app and steering clear of the SPA approach. Following the recommended "Option 2" from this Vuejs js for multiple pages, not for a single page application In my main layout.blade.php template, I have the following code: &l ...

Reset the form upon submission in AngularJS

Hey, I'm looking to reset the form values after a successful submission. How can I achieve this? <div ng-controller="employeelistController as listControl"> <div class="container form-group" ng-controller="addEmployee as addemp"> ...

Uh oh! We've encountered an issue: Unable to update headers once they have been sent

I am currently working with express and node.js My objective is to redirect users to /welcome when they visit /index and have a specific tracking code in the URL. The redirectBasedReferrer function handles the redirection based on the tracking code. inde ...

Is the node_modules folder compiled into the bundle.js file by Rollup?

I am currently experimenting with rollupjs in order to package a node application into a single bundle.js, but I am facing some confusion. Does rollup have the capability to bundle an entire node application (including node_modules), or does it only wor ...

Issue with Trix text editor not triggering the change event

Lately, I've been facing some difficulties in getting the tirx-change event to trigger when there are changes in the content of a trix-editor. Despite using React JS for the view, I haven't been able to identify the problem. Below is the code sni ...