next.js: Global Sass imports don't work when you use variables, mixins, functions, etc!
I’ve been working in Gatsby for a while, and recently did a project in Nuxt.js, and I’d love to compare and hope for a similar feature in Next.js. I recently read the blog about Sass imports in version 9.3 and I thought now would be a great time to test out Next.js with a new project.
I would love to know if this is a bug that’s getting fixed, or there are similar “community” packages that solve this problem, and/or if Next.js will implement this in the future ever?
Bug report
Global imports for Sass work via pages/_app.js, but the Sass mixins, variables, functions, etc don’t apply globally as the styles do.
You can achieve this in Nuxt.js like so in the nuxt.config.js:
export default {
modules: [
'@nuxtjs/style-resources',
],
// for global imports
css: ['~/styles/normalize.scss', '~/styles/typography.scss', '~/styles/global.scss'],
// for utility imports
styleResources: {
scss: [
'~/styles/abstracts/_mixins.scss',
'~/styles/abstracts/_variables.scss',
'~/styles/abstracts/_functions.scss'
]
},
}
and in Gatsby via the gatsby.config.js (but unfortunately they don’t have global Sass imports)
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-sass`,
options: {
data: `
@import './src/styles/_variables.scss';
@import './src/styles/_mixins.scss';
@import './src/styles/_functions.scss';
`,
includePaths: ['./src/styles/']
}
}
]
};
I tried to achieve the same thing in Next.js by adding this to the pages/_app.js
import '../styles/functions.scss';
import '../styles/keyframes.scss';
import '../styles/mixins.scss';
import '../styles/variables.scss';
// these work properly IF there's no variables referencing the above, otherwise they'll error
import '../styles/global.scss';
import '../styles/typography.scss';
import '../styles/normalize.scss';
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
I tried this method but next-sass wasn’t playing well with built-in sass, so it doesn’t seem like it’s backwards compatible?
And when I try to just import the file in a page or component it will then just tell me to import via pages/_app.js which doesn’t work.
I feel like I’m missing something… (sorry if I am), but I’m not understanding why all these other issues are closed. I’m wondering why I can’t use Sass features but yet there’s documentation about use Sass modules?
There also claims to be a workaround here but provides no context on how to implement that or where to put the code? I would love if there was some more documentation around this. I would love to be able to use these Sass core features in Next!
Hopefully someone can help me out, thanks!
System information
- Latest macOS Catalina
- Next 9.3.4
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Add support for sass-loader prependData option (#12277) This PR adds support for prepending sass code before the actual entry file. It's common for developers to import their sass mixins and variabl... — committed to vercel/next.js by Tylerian 6 years ago
- Add support for sass-loader prependData option (#12277) This PR adds support for prepending sass code before the actual entry file. It's common for developers to import their sass mixins and variabl... — committed to rokinsky/next.js by Tylerian 6 years ago
My workaround:
scss-resources.jsIf i’m understanding your issue correctly, whenever you’d like to consume sass variables and mixins in a .module.scss file, you have to individually import those partials to that specific .module.scss file.
The following works for me:
_colors.scss
button.module.scss
button.js
For this to work all you need is to npm install sass.
That being said it’d be nice if we could import variable partials into _app.js file and be able to refer to them globally across different .module.scss file without having to repeatedly import them.
I’m also unable to make it work for the 9.4 version. I’m wondering if this is wanted, see the discussion in #12277
It says in Next 9.4 this support was added, but I can’t get it to work yet. Anyone else having better luck? These webpack configs are always so confusing 😕
You must wait until PR https://github.com/zeit/next.js/pull/12277 get merged, use your solution for now, it’s pretty neat