简体   繁体   English

如何在 next-js 中正确导入 scss 文件?

[英]How to import scss files properly in next-js?

I have a nextJS project in which I am using sass with typescript.我有一个 nextJS 项目,我在其中使用 sass 和 typescript。 It all works in development environment but when I try to make a build version of my project it gives me this error.这一切都在开发环境中工作,但是当我尝试制作我的项目的构建版本时,它给了我这个错误。

在此处输入图像描述

This is my next.config.js这是我的 next.config.js

const config = {
  sassOptions: {
  cssModules: false,
},
  images: {
  domains: ["image.tmdb.org"],
 },
};

After some trial and error, I found the way to work around this error.经过反复试验,我找到了解决此错误的方法。

Step 1步骤1

Create a file named declaration.d.ts on the root of project.在项目的根目录上创建一个名为declaration.d.ts的文件。

Step 2第2步

declare module "*.scss" {
  const content: Record<string, string>;
  export default content;
}

By default typescript does not knows any files other than.ts or.tsx so any other files which is imported in typescript file need to be first declared like this.默认情况下 typescript 不知道除 .ts 或 .tsx 以外的任何文件,因此在 typescript 文件中导入的任何其他文件都需要首先像这样声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM