【问题标题】:Gatsby SCSS Module not compilingGatsby SCSS 模块未编译
【发布时间】:2021-06-04 04:00:59
【问题描述】:

使用 sass 1.32.8 运行 Gatsby 版本 3.0.1。我刚开始玩一些东西,但遇到了一个我无法解决的奇怪问题。

./gatsby-config.js

module.exports = {
  /* Your site config here */
  plugins: [
    {
      resolve: `gatsby-plugin-sass`,
      options: {
        implementation: require("sass"),
      },
    },
  ],
}

./src/pages/index.js

import React from "react"
import homeStyles from '../styles/scss/index.module.scss'

export default function Home() {
  return <div className={homeStyles.testElement}>Hello world!</div>
}

./styles/scss/index.module.scss

.testElement {
  font-size: 72px;
}

我得到的错误是Attempted import error: '../styles/scss/index.module.scss' does not contain a default export (imported as 'homeStyles').

如果我尝试使用import * as homeStyles from '../styles/scss/index/module.scss,错误是:Attempted import error: 'testElement' is not exported from '../styles/scss/index.module.scss' (imported as 'homeStyles').

在不确切知道插件的工作原理的情况下,我看不到任何可能导致此问题的问题。

【问题讨论】:

    标签: gatsby gatsby-plugin


    【解决方案1】:

    像这样使用它:

    module.exports = {
      /* Your site config here */
      plugins: [
        {
          resolve: `gatsby-plugin-sass`,
        },
      ],
    }
    

    您不需要添加额外的实现来使用 CSS 模块,因此您可以省略它们。

    另外,根据这个GitHub thread解决办法是把插件降级到v4

    【讨论】:

    • 谢谢,但我仍然遇到同样的错误。我尝试删除插件并重新安装,但没有效果。
    • 尝试降级到 Gatsby v2。 v3 的实现取决于插件的代码,因此在他们更新之前,您可能会遇到一些警告或问题
    • 事实证明这有点极端。他们发布的最新版本(4.0.1)恰好在过去 24 小时内发布,有一个错误。暂时降级到 4.0.0 是可行的。 github.com/gatsbyjs/gatsby/issues/30037
    • 感谢您的见解!我已经用解决方法更新了答案。您是否最终修复了降级依赖项?
    • 降级暂时修复!
    猜你喜欢
    • 2020-03-05
    • 2019-12-11
    • 2020-06-20
    • 2018-11-18
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多