如何在Nuxt中设置SASS/SCSS/sass-loader

问题描述 投票:0回答:3

enter image description here

我有一个 Nuxt 应用程序,我想使用 CSS 预处理器。
我安装了

sass-loader
纤维依赖项,但安装后,应用程序控制台中会出现一条消息,我在图像和代码中显示了该消息

这是代码错误:

 WARN  [email protected] is installed but ^4.46.0 is expected                                                                                                                                                                     17:22:44


 WARN  [email protected] is installed but ^10.1.1 is expected

     


Rule can only have one resource source (provided resource and test + include + exclude) in {                                                                                                                           17:22:46
  "use": [
    {
      "loader": "/home/sergey/all_project/pro_projects_all_language/empty/node_modules/babel-loader/lib/index.js",
      "options": {
        "configFile": false,
        "babelrc": false,
        "cacheDirectory": true,
        "envName": "server",
        "presets": [
          [
            "/home/sergey/all_project/pro_projects_all_language/empty/node_modules/@nuxt/babel-preset-app/src/index.js",
            {
              "corejs": {
                "version": 3
              }
            }
          ]
        ]
      },
      "ident": "clonedRuleSet-29[0].rules[0].use[0]"
    }
  ]
}

  "use": [
  {
  "loader": "node_modules/babel-loader/lib/index.js",
  "options": {
  "configFile": false,
  "babelrc": false,
  "cacheDirectory": true,
  "envName": "server",
  "presets": [
  [
  "node_modules/@nuxt/babel-preset-app/src/index.js",
  {
  "corejs": {
  "version": 3
  }
  }
  ]
  ]
  },
  "ident": "clonedRuleSet-29[0].rules[0].use[0]"
  }
  ]
  }
  at checkResourceSource (node_modules/@nuxt/webpack/node_modules/webpack/lib/RuleSet.js:167:11)
  at Function.normalizeRule (node_modules/@nuxt/webpack/node_modules/webpack/lib/RuleSet.js:198:4)
  at node_modules/@nuxt/webpack/node_modules/webpack/lib/RuleSet.js:110:20
  at Array.map (<anonymous>)
  at Function.normalizeRules (node_modules/@nuxt/webpack/node_modules/webpack/lib/RuleSet.js:109:17)
  at new RuleSet (node_modules/@nuxt/webpack/node_modules/webpack/lib/RuleSet.js:104:24)
  at new NormalModuleFactory (node_modules/@nuxt/webpack/node_modules/webpack/lib/NormalModuleFactory.js:115:18)
  at Compiler.createNormalModuleFactory (node_modules/@nuxt/webpack/node_modules/webpack/lib/Compiler.js:636:31)
  at Compiler.newCompilationParams (node_modules/@nuxt/webpack/node_modules/webpack/lib/Compiler.js:653:30)
  at Compiler.compile (node_modules/@nuxt/webpack/node_modules/webpack/lib/Compiler.js:661:23)
  at node_modules/@nuxt/webpack/node_modules/webpack/lib/Watching.js:77:18
  at AsyncSeriesHook.eval [as callAsync] (eval at create (node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
  at AsyncSeriesHook.lazyCompileHook (node_modules/tapable/lib/Hook.js:154:20)
  at Watching._go (node_modules/@nuxt/webpack/node_modules/webpack/lib/Watching.js:41:32)
  at node_modules/@nuxt/webpack/node_modules/webpack/lib/Watching.js:33:9
  at Compiler.readRecords (node_modules/@nuxt/webpack/node_modules/webpack/lib/Compiler.js:529:11)

我尝试重新安装依赖项,重新安装完全干净的 Nuxt 应用程序,但问题仍然存在。

vue.js sass nuxt.js
3个回答
34
投票

要在 Nuxt 中安装 SASS,您必须运行

yarn add -D sass [email protected]
(或
npm i -D [email protected] --save-exact && npm i -D sass
)。

sass-loader
的版本需要准确并设置为最新的
10.x.x
,因为下一个(
11.0.0
)正在使用Webpack5,因此这是一个重大更改,因为Nuxt2仅在Webpack4上运行,如下所示:https ://github.com/webpack-contrib/sass-loader/releases

IF 那么,您仍然无法在

<style lang="sass">
组件中使用
.vue
,然后继续。


将此添加到您的

nuxt.config.js
文件

export default {
  build: {
    loaders: {
      sass: {
        implementation: require('sass'),
      },
      scss: {
        implementation: require('sass'),
      },
    },
  }
}

这里是一个工作仓库,其中包含最新推荐的

sass
dart-sass)设置,可以与此类代码正常工作

<template>
  <div>
    <span class="test">
      Hello there
    </span>
  </div>
</template>

<style lang="sass" scoped>
div
  .test
    color: red
</style>

PS:如果 SASS 安装正确,那么 SCSS 也能正常工作,因为它基本上是一样的。


如果您对某些已弃用的内容有一些警告,例如

/ for divison
或此处列出的任何内容:https://sass-lang.com/documentation/writing-changes
您可以参考此答案进行修复:https://stackoverflow.com/a/68648204/8816585


0
投票

kissu的答案对我有用,但不是立即有效。最后我设法通过降级加载程序并删除并再次安装 nuxt 来解决问题。


0
投票

在 nuxt 中不需要 Sass 加载器,只需使用此命令在 nuxt 中安装 sass 即可:

npm install sass --save-dev
© www.soinside.com 2019 - 2024. All rights reserved.