Nuxt/examples/style-resources
2023-03-16 06:24:27 -07:00
..
assets
pages refactor: use PascalCase for components names (#4396) 2018-11-24 22:19:19 +03:30
nuxt.config.js
package.json chore(examples): use 2.x version of nuxt instead of latest (#19737) 2023-03-16 06:24:27 -07:00
README.md refactor: use nuxt everywhere (#8210) 2020-11-30 23:44:04 +01:00

Using build.styleResources with Nuxt

This is useful when you need to inject some variables and mixins in your pages without having to import them every time.

Nuxt uses https://github.com/yenshih/style-resources-loader to achieve this behaviour.

You need to specify the patterns/path you want to include for the given pre-processors: css, less, sass, scss or stylus

⚠️ You cannot use path aliases here (~ and @), you need to use relative or absolute paths.

nuxt.config.js:

{
  build: {
    styleResources: {
      scss: './assets/variables.scss',
      less: './assets/*.less',
      // sass: ...,
      // scss: ...
      options: {
        // See https://github.com/yenshih/style-resources-loader#options
        // Except `patterns` property
      }
    }
  }
}

⚠️ If you want to use @import in these styleResources files, you should use path alias (~@) like this

@import '~@/assets/other.scss';

Then in your pages, you can use directly:

pages/index.vue

<template>
  <div class="main">
    <p>Page with SCSS</p>
    <p><NuxtLink to="/less">LESS</NuxtLink></p>
  </div>
</template>

<style lang="scss">
/* ~/assets/variables.scss is injected automatically here */
.main {
  background: $main;
}
</style>

Thanks to 0pt1m1z3r for adding this feature and example.