最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nuxt.js: How move Global CSS from style tag to css file - Stack Overflow

programmeradmin3浏览0评论

I'm confused with the Nuxt.js SCSS pilation... I have the assets folder and it has two SCSS files included in nuxt.config.js:

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

Launch npm run build and then npm run generate, then I go into the dist folder and open the index.html file, what I see is all css (and it is too big) inside the style tag on the page:

Nuxt.js has piled the SCSS files from assets and put CSS in the style tag. How put into a file and connect with link tag in the head section like this?

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

You may say I can use head settings in nuxt.config.js, but I cannot because it is possible only with remote and static files, it does not work like this:

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: []
},

I did not find in the Nuxt.js documentation how put the Global CSS to a file. Is it possible with Nuxt.js config or change the Webpack build config only? Help me understand please :-)

I'm confused with the Nuxt.js SCSS pilation... I have the assets folder and it has two SCSS files included in nuxt.config.js:

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

Launch npm run build and then npm run generate, then I go into the dist folder and open the index.html file, what I see is all css (and it is too big) inside the style tag on the page:

Nuxt.js has piled the SCSS files from assets and put CSS in the style tag. How put into a file and connect with link tag in the head section like this?

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

You may say I can use head settings in nuxt.config.js, but I cannot because it is possible only with remote and static files, it does not work like this:

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: []
},

I did not find in the Nuxt.js documentation how put the Global CSS to a file. Is it possible with Nuxt.js config or change the Webpack build config only? Help me understand please :-)

Share Improve this question asked Feb 12, 2021 at 16:49 eleaveeleave 1272 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can use the extractCSS build option to extract all your CSS assets into separate files (usually one per ponent) and allows you to put in cache each CSS file:

// nuxt.config.js

export default {
  build: {
    extractCSS: true
  }
}
发布评论

评论列表(0)

  1. 暂无评论