将 Vuetify 从 1.5 升级到 2 时,Sass 加载程序错误“选项具有未知属性“缩进语法”

kri*_*ahl 9 sass vue.js vuetify.js

我正在将我的 Vue CLI 3 项目中的 Vuetify 从版本 1.5 升级到 2。我已按照这些说明进行了完整安装。升级后,运行 'npm run serve' 给了我一大堆类似这样的错误:

error  in ./node_modules/vuetify/src/components/VGrid/_grid.sass

Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
    ValidationError: Invalid options object. Sass Loader has been initialised using an options object that does not match the API schema.
     - options has an unknown property 'indentedSyntax'. These properties are valid:
       object { implementation?, sassOptions?, prependData?, sourceMap?, webpackImporter? }
        at validate (C:\Users\kristoffer.dahl\Documents\Prosjekter\fridgein\fridgein_fe\node_modules\sass-loader\node_modules\schema-utils\dist\validate.js:50:11)
        at Object.loader (C:\Users\kristoffer.dahl\Documents\Prosjekter\fridgein\fridgein_fe\node_modules\sass-loader\dist\index.js:36:28)
Run Code Online (Sandbox Code Playgroud)

除了提到的 Vuetify 组件之外,所有错误看起来都一样。

这是我的package.json

{
  "name": "fridgein_fe",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.22",
    "@fortawesome/free-brands-svg-icons": "^5.10.2",
    "@fortawesome/free-regular-svg-icons": "^5.10.2",
    "@fortawesome/free-solid-svg-icons": "^5.10.2",
    "@fortawesome/vue-fontawesome": "^0.1.7",
    "@vue/cli": "^3.11.0",
    "@vue/cli-shared-utils": "^3.11.0",
    "auth0-js": "^9.11.3",
    "axios": "^0.18.1",
    "dotenv": "^8.1.0",
    "es6-promise": "^4.2.8",
    "jquery": "^3.4.1",
    "js-cookie": "^2.2.1",
    "popper.js": "^1.14.7",
    "register-service-worker": "^1.6.2",
    "sockjs-client": "^1.3.0",
    "vue": "^2.6.10",
    "vue-float-action-button": "^0.4.4",
    "vue-google-charts": "^0.3.2",
    "vue-plugin-load-script": "^1.2.0",
    "vue-router": "^3.0.2",
    "vuetify": "^2.1.0",
    "vuex": "^3.1.1",
    "vuex-persistedstate": "^2.5.4",
    "webstomp-client": "^1.2.6"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.4.0",
    "@vue/cli-plugin-eslint": "^3.4.0",
    "@vue/cli-plugin-pwa": "^3.11.0",
    "@vue/cli-service": "^3.5.1",
    "babel-eslint": "^10.0.1",
    "babel-plugin-module-resolver": "^3.2.0",
    "css-loader": "^2.1.1",
    "deepmerge": "^4.0.0",
    "eslint": "^5.0.0",
    "eslint-plugin-vue": "^5.2.3",
    "eslint-plugin-vuetify": "^1.0.0-beta.3",
    "fibers": "^4.0.1",
    "sass": "^1.23.0",
    "sass-loader": "^8.0.0",
    "stylus": "^0.54.5",
    "stylus-loader": "^3.0.1",
    "vue-cli-plugin-vuetify": "^0.5.0",
    "vue-template-compiler": "^2.6.10",
    "vuetify-loader": "^1.3.0",
    "webpack": "^4.41.0"
  },
Run Code Online (Sandbox Code Playgroud)

这是我的vue.config.js

module.exports = {
    css: {
      loaderOptions: {
        sass: {
          data: `@import "~@/sass/main.scss"`
        },
      },
    },
}
Run Code Online (Sandbox Code Playgroud)

我已经检查并尝试了开发人员 Github 页面上相关问题中提出的所有解决方案,但都没有任何效果。

有人遇到过这种情况么?

小智 20

我遇到了同样的问题,并sass-loader通过设置降级来修复它

"sass-loader": "7.3.1",
Run Code Online (Sandbox Code Playgroud)

package.json

这是在 Vuetify Discord 上提出的

  • 你好!您不需要降级`sass-loader`。只需使用新属性“prependData”而不是“data”。检查这个[Github问题](https://github.com/webpack-contrib/sass-loader/issues/760#issuecomment-534043880) (2认同)

Pis*_*lli 18

实际上,您使用的是 sass-loader 8+,它的选项略有不同。
尝试使用prependData代替data.
检查这个github问题

module.exports = {
    css: {
      loaderOptions: {
        sass: {
          prependData: `@import "~@/sass/main.scss"`
        }
      }
    }
}

Run Code Online (Sandbox Code Playgroud)

  • 我需要添加;最后它的工作方式如下: prependData: `@import "~@/sass/main.scss";` 否则很好的解决方案。 (2认同)

Ale*_*igh 8

为使用版本 10+ 的用户更新。prependData不再有效。但是,您可以将其additionalData用作替代品。


Dan*_*ana 5

使用 webpack 5.1.3 和 sass-loader 版本 10.0.3,我不得不使用不同的语法(additionalData而不是prependData),如下所示:

webpack.config.js

        {
          test: /\.(css|scss|sass|less)$/,
          use: [
            'style-loader',
            'css-loader',
            'sass-loader',
            {
              loader: 'sass-loader',
              options: {
                sourceMap: true,
                additionalData: '@import "assets/scss/_variables.scss";'
                },
              },
            },
          ],
        },

Run Code Online (Sandbox Code Playgroud)

请记住,语法需要路径中的双引号和单引号结尾之前的分号,就像您正在编写普通的 scss 文件一样。

例子:

'@import "path/_file.scss";' 
Run Code Online (Sandbox Code Playgroud)

如果您需要导入多个文件,您可以使用模板字符串符号 (``) 如下:

        {
          test: /\.(css|scss|sass|less)$/,
          use: [
            ...
            {
              ...
              options: {
                sourceMap: true,
                additionalData: `@import "assets/scss/_variables.scss";
                                 @import "assets/scss/_mixins.scss";
                                 @import "assets/scss/_misc.scss";`
                },
              },
            },
          ],
        },

Run Code Online (Sandbox Code Playgroud)

您还可以利用模板字符串,如下所示:

`@import "${path.resolve(APP_DIR, '/assets/scss/')}/_variables.scss";`
Run Code Online (Sandbox Code Playgroud)

请注意:我尝试了这种方法来减少包的大小,结果很糟糕,因为当你开始看到 webpack 实际在做什么时,你返回到基本导入你的主要组件中所有需要的样式,因为 sass-loader 实际上会预先添加多个组件您在附加数据中通知的样式。就我而言,捆绑包的大小是原始大小的 3 倍。

干杯