example: Add Less file to show multi pre-processors

This commit is contained in:
Sébastien Chopin 2018-01-08 10:37:40 +01:00
parent eceb92502b
commit f0cda98ad7
7 changed files with 81 additions and 12 deletions

View File

@ -1,3 +1,50 @@
# Using build.styleResources with Nuxt.js
https://nuxtjs.org/examples
This is usefull when you need to inject some variables and mixins in your pages without having to import them everytime.
Nuxt.js 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: `less`, `sass`, `scss` or `stylus`
:warning: You cannot use path aliases here (`~` and `@`), you need to use relative or absolute paths.
`nuxt.config.js`:
```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
}
}
}
}
```
Then in your pages, you can use directly:
`pages/index.vue`
```vue
<template>
<div class="main">
<p>Page with SCSS</p>
<p><nuxt-link to="/less">LESS</nuxt-link></p>
</div>
</template>
<style lang="scss">
/* ~/assets/variables.scss is injected automatically here */
.main {
background: $main;
}
</style>
```
Thanks to [0pt1m1z3r](https://github.com/0pt1m1z3r) for adding this feature and example.

View File

@ -0,0 +1 @@
@main: orange;

View File

@ -1,9 +1,9 @@
module.exports = {
build: {
// You cannot use ~/ or @/ here since it's a Webpack plugin
styleResources: {
patterns: [
'./assets/resources.scss'
]
scss: './assets/variables.scss',
less: './assets/*.less'
}
}
}

View File

@ -1,6 +1,8 @@
{
"name": "style-resources",
"dependencies": {
"less": "^2.7.3",
"less-loader": "^4.0.5",
"nuxt": "latest"
},
"scripts": {

View File

@ -1,12 +1,14 @@
<style lang="scss">
.main {
background: $main;
}
</style>
<template>
<div class="main">
<h1>Welcome!</h1>
<p>Background is grey</p>
<p>Page with SCSS</p>
<p><nuxt-link to="/less">LESS</nuxt-link></p>
</div>
</template>
<style lang="scss" scoped>
/* ~/assets/variables.scss is injected automatically here */
.main {
background: $main;
padding: 10px;
}
</style>

View File

@ -0,0 +1,17 @@
<template>
<div class="main">
<p>Page with LESS</p>
<p><nuxt-link to="/">SCSS</nuxt-link></p>
</div>
</template>
<style lang="less" scoped>
/* ~/assets/*.less are injected automatically here */
.main {
background: @main;
padding: 10px;
}
a {
color: white;
}
</style>