最近,我一直在切换到我的下一个项目中使用模块,但是我在新创建的.module.scss files中一直收到此错误班级或ID)”。我知道这是因为我没有像我在网上其他地方看到的那样使用纯CSS选择器,而唯一的问题是我正在使用的导入,但是我需要这些导入的变量,例如 $ cl-cl-light--灰色
如下所示:
@import "src/common/styles/global-styles.scss";
@import "node_modules/bootstrap/scss/bootstrap";
@import "src/common/styles/palette.scss";
@import "src/common/styles/typography.scss";
.dashboard-dropdown-hover {
@extend .px-1;
@extend .py-2;
@extend .mt-3;
border: 1px solid transparent;
border-radius: 8px;
transition: 200ms;
background-color: transparent;
}
.dashboard-dropdown-hover:hover {
background-color: $cl-light-gray;
}
是否有人可以解决我应该如何解决此导入问题的解决方案?我知道,如果我切换到.scss,它将起作用,但是我试图避免在_app.tsx中导入所有.scss文件,因为这至少是30个导入,而且这些样式也不打算是全局。最后,为什么Next.js希望我在使用SASS时使用纯CSS选择器,而Sass由于其非纯元素而被使用?
I've recently been switching to using modules in my next.js project, but I keep receiving this error in my newly created .module.scss files: "Selector ":root" is not pure (pure selectors must contain at least one local class or id)". I know this is because I'm not using pure css selectors as I've seen elsewhere online, and the only problem is the imports that I'm using, but I need those imports for variables like $cl-light-gray
as seen below in this example file:
@import "src/common/styles/global-styles.scss";
@import "node_modules/bootstrap/scss/bootstrap";
@import "src/common/styles/palette.scss";
@import "src/common/styles/typography.scss";
.dashboard-dropdown-hover {
@extend .px-1;
@extend .py-2;
@extend .mt-3;
border: 1px solid transparent;
border-radius: 8px;
transition: 200ms;
background-color: transparent;
}
.dashboard-dropdown-hover:hover {
background-color: $cl-light-gray;
}
Does anyone have a solution to how I should fix this import problem? I know that if I switch back to .scss it will work, but I'm trying to avoid importing all the .scss files in _app.tsx because that would be at least 30 imports and also these styles aren't intended to be global. Lastly, why does Next.js expect me to use pure css selectors when I'm using Sass, which is used because of its non-pure elements?
发布评论
评论(4)
搜寻互联网几个小时后,我从这里找到了一个很棒的解决方案: https ://dhanrajsp.me/snippets/customize-css-loader-options-in-nextjs
编辑:如果您使用next.js 12,请检查上面的文章底部,因为该解决方案有点小不同的。
您将需要更改您的next.config.js文件以包括以下内容:
我没有对WebPack进行调味或确切工作方式,但是此解决方案对我有用。如果需要,您也可以将正则态度更改为CSS(SCSS | SASS | CSS)。
After scouring the internet for a few hours I found a great solution from here: https://dhanrajsp.me/snippets/customize-css-loader-options-in-nextjs
EDIT: If you're using Next.js 12, check the bottom of the article above, because the solution is a little different.
You'll want to change your next.config.js file to include the following:
I'm not seasoned with webpack or how it exactly works, but this solution worked for me. You can also change the regex to include css by doing (scss|sass|css) if you want.
正如指出的在这里,还有另一个选择:您可以在global.css文件中导入这些样式。如果您这样做,NextJ会很高兴。
As pointed out here, there is another option: you can import those styles in the global.css file. If you do that, Nextjs will be happy.
任何全局样式(例如,
:root
或您想要在应用程序中绝对拥有相同样式的任何HTML元素/CSS类)都应将您导入到_app.js (如果尚不存在,则可以将其添加到项目的根文件夹中)。
该全局CSS文件也是您要导入将使用应用程序范围的任何字体的地方。
逐步说明在这里: https://nextjs.org/文档/基本功能/内置CSS-Support
Any global styles (e.g.,
:root
or any HTML elements/CSS classes that you want to have the same style absolutely everywhere in your app) should be placed into a global CSS file that you import into_app.js
(which you just can add to the root folder of your project, if it doesn't already exist).This global CSS file is also where you want to import any fonts that you will use app-wide.
Step-by-step instructions here: https://nextjs.org/docs/basic-features/built-in-css-support
在我的特殊情况下,我在这个问题上遇到了同样的头痛,这是因为我试图用路径导入文件:
该文件正在导入另一个称为_root.scss的文件,该文件以这种样式定义为选择器。
对于解决方案,我简单地导入用于我的要求的特定文件,
另一种资源可以帮助您:
In my particular case i was having the same headache with that issue, and was because i was trying to import the file with the path:
and that file was importing another file called _root.scss which was defined a selector in this style.
for solution that error i simply import the specific files used for my requirements
Another resources could help you: