使用next.js加载外部scss文件(反应)

时间:2019-10-31 05:41:48

标签: reactjs npm sass next.js server-side-rendering

从zeit加载SaSS文件失败,并出现以下错误

node_modules\@zeit\next-css\node_modules\mini-css-extract-plugin\dist\index.js:21} = _webpack2.default;
^TypeError: Cannot destructure property `createHash` of 'undefined' or 'null'.

next.config.js

const withCSS = require("@zeit/next-css");
const withSass = require('@zeit/next-sass')

module.exports = withCSS(withSass({
    webpack (config, options) {
      config.module.rules.push({
        test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 100000
          }
        }
      })

      return config
    }
  }))

样式表位于src>样式表文件夹下

引用https://dev.to/harveyjones282/the-simplest-way-to-configure-next-js-with-sass-3en

感谢任何反馈:)

2 个答案:

答案 0 :(得分:0)

前段时间我遇到了相同的错误,我不完全记得我是如何登陆这里的,但是这段代码有效-

const withSASS = require('@zeit/next-sass')

const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack')

function HACK_removeMinimizeOptionFromCssLoaders(config) {
  console.warn(
    'HACK: Removing `minimize` option from `css-loader` entries in Webpack config',
  )
  config.module.rules.forEach(rule => {
    if (Array.isArray(rule.use)) {
      rule.use.forEach(u => {
        if (u.loader === 'css-loader' && u.options) {
          delete u.options.minimize
        }
      })
    }
  })
}

module.exports = withSASS(
  {
      webpack(config) {
        HACK_removeMinimizeOptionFromCssLoaders(config)
        config.plugins.push(new webpack.EnvironmentPlugin(localEnv))

        return config
      }
  }
)

尝试使用this替换next.config.js文件中的代码,安装必需的依赖项,如dotenv,next-sass。并重新运行服务器以使更改生效。

答案 1 :(得分:0)

在安装最新的next.js软件包后,此问题已解决