You need to separate your functions, variables & mixins from actual CSS rules.
These files will be imported and available to every component you write, which is great for things like variables, functions, or mixins, but you should avoid any actual CSS rules. Adding CSS rules to your shared Sass files will import those rules into every component and bloat your project. For global CSS rules, create a separate file and import it into your main App.vue file instead.
Source: https://austingil.com/global-sass-vue-project/
Answer from mstroiu on Stack Overflow
» npm install sass-loader
Serg already gave the answer in comment. It's worked for me. Adding the answer for others. Hope it will help others.
Just run this command
npm i sass-loader@latest
You can update your packages via using yarn upgrade-interactive --latest, in case of sass-loader, if you have upgrade it from 7 to 9, you may have some invalid options erros which you can fixed in below steps as I've explained before: https://stackoverflow.com/a/62844942/12666332
Problem
Based on https://github.com/vuejs/vue-cli/issues/4513, And If you have upgrade your sass-loader from v7 to 8 or 9, You may have faced with th validation-error for invalid options
vue.config.js(valid syntax for sass-loaderv7 in webpack)
css: {
loaderOptions: {
sass: {
data: `@import "@/assets/styles/variables/index.scss";`
}
}
}
errors
object:
ValidationError: Invalid options object. Sass Loader has been
initialized using an options object that does not match the API schema.
- options has an unknown property 'data'. These properties are valid:
sass-loader v8.0 Breaking Changes
You should know that v8. has below breaking changes :https://github.com/webpack-contrib/sass-loader/releases/tag/v8.0.0
- minimum required webpack version is 4.36.0
- minimum required node.js version is 8.9.0
- move all sass (includePaths, importer, functions) options to the sassOptions option. The functions option can't be used as Function, you should use sassOption as Function to achieve this.
- the data option was renamed to the prependData option default value of the sourceMap option depends on the devtool value (eval/false values don't enable source map generation)
Solution v8
As the docs says, move all sass (includePaths, importer, functions) options to the sassOptions option. The functions option can't be used as Function, you should use sassOption as Function to achieve this.
the data option was renamed to the prependData option
sass-loader v9.0 Breaking Changes
You should know that v9. has below breaking changes :https://github.com/webpack-contrib/sass-loader/releases/tag/v9.0.0
- BREAKING CHANGES minimum supported Nodejs version is 10.13
- prefer sass (dart-sass) by default, it is strongly recommended to migrate on sass (dart-sass)
- the prependData option was removed in favor the additionalData option, see docs
- when the sourceMap is true, sassOptions.sourceMap, sassOptions.sourceMapContents, sassOptions.sourceMapEmbed, sassOptions.sourceMapRoot and sassOptions.omitSourceMapUrl will be ignored.
Solution v9
In ver9 as you can see in the docs https://github.com/webpack-contrib/sass-loader#options (https://github.com/webpack-contrib/sass-loader#sassoptions , https://github.com/webpack-contrib/sass-loader#additionaldata), if we change data to additionalData if will fix the invalid options errors.
css: {
loaderOptions: {
sass: {
additionalData: `@import "@/assets/styles/variables/index.scss";`
}
}
}
Seems same issue like here: https://github.com/JeffreyWay/laravel-mix/issues/2206
Solution is
npm uninstall --save-dev sass-loader
npm install --save-dev [email protected]
Problem
Based on vue-cli - github issue, If you did upgrade your sass-loader from v7 to 8 or 9, You may have faced with the validation-error for invalid options :
vue.config.js(valid syntax for sass-loaderv7 in webpack)
css: {
loaderOptions: {
sass: {
data: `@import "@/assets/styles/variables/index.scss";`
}
}
}
errors
object:
ValidationError: Invalid options object. Sass Loader has been
initialized using an options object that does not match the API schema.
- options has an unknown property 'data'. These properties are valid:
sass-loader v8.0 Breaking Changes
You should know that v8. has below breaking changes
- minimum required webpack version is 4.36.0
- minimum required node.js version is 8.9.0
- move all sass (includePaths, importer, functions) options to the sassOptions option. The functions option can't be used as Function, you should use sassOption as Function to achieve this.
- the data option was renamed to the prependData option default value of the sourceMap option depends on the devtool value (eval/false values don't enable source map generation)
Solution v8
As the docs says:
- move all sass (
includePaths,importer,functions) options to thesassOptionsoption. Thefunctionsoption can't be used as Function, you should usesassOptionas Function to achieve this. - the
dataoption was renamed to theprependDataoption
sass-loader v9.0 Breaking Changes
You should know that v9. has below breaking changes
- BREAKING CHANGES minimum supported Nodejs version is 10.13
- prefer sass (dart-sass) by default, it is strongly recommended to migrate on sass (dart-sass)
- the prependData option was removed in favor the additionalData option, see docs
- when the sourceMap is true, sassOptions.sourceMap, sassOptions.sourceMapContents, sassOptions.sourceMapEmbed, sassOptions.sourceMapRoot and sassOptions.omitSourceMapUrl will be ignored.
Solution v9
In ver9 as you can see in the options, sassOption , additionalData, if we change data to additionalData it will fix the invalid options errors.
css: {
loaderOptions: {
sass: {
additionalData: `@import "@/assets/styles/variables/index.scss";`
}
}
}
Without using sass-resources-loader:
Thanks to @Arseniy-II for helping me get to this answer, in conjunction with this thread: https://github.com/webpack-contrib/sass-loader/issues/218
Using loader options in your webpack module rules, you can assign a data property to sass-loader, you should then be able to use all sass functions as expected:
module: {
rules: [
// Apply loader
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
data: '@import "path/to/global.scss";',
includePaths:[__dirname, 'src']
},
},
],
},
],
}
You have to import the vars file into every Sass partial that uses those variables, because every partial is compiled on its own; none of the files will 'know about' the others unless you specifically import them.
If you don't want to have to type the imports in every Sass file, you can look at baggage-loader, which will automatically add them for you.