Vue Scss样式加载因Mocamapck失败

时间:2020-04-20 18:42:11

标签: vue.js webpack node-sass sass-loader mochapack

在以某种scss样式对vue单个文件组件进行单元测试时,我遇到了问题,因为它无法处理@import语句。当我使用yarn run dev运行启动应用程序或为生产yarn run build构建应用程序时,它起作用,问题仅在于单元测试模式,并且我的设置在执行测试之前使用mochapack来运行webpack。

 WEBPACK  Compiling...

  [=========================] 98% (after emitting)

 ERROR  Failed to compile with 1 errors

 error  in ./src/components/WorkTrigger.vue?vue&type=style&index=0&lang=scss&

Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Can't find stylesheet to import.
  ╷
1 │ @import 'themes/default/variables';
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  ╵
  home/xxxxx/src/styles/main.scss 1:9  @import
  /home/xxxxx/src/components/WorkTrigger.vue 39:9                                             root stylesheet

 @ ./node_modules/vue-style-loader??ref--9-oneOf-1-0!./node_modules/css-loader/dist/cjs.js??ref--9-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--9-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--9-oneOf-1-3!./node_modules/cache-loader/dist/cjs.js??ref--1-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/WorkTrigger.vue?vue&type=style&index=0&lang=scss& 4:14-442
 @ ./src/components/WorkTrigger.vue?vue&type=style&index=0&lang=scss&
 @ ./src/components/WorkTrigger.vue
 @ ./tests/unit/WorkTrigger.spec.ts
 @ ./node_modules/mochapack/lib/entry.js

Type checking in progress...
  [=========================] 100% (completed)

 WEBPACK  Failed to compile with 1 error(s)

Error in ./src/components/WorkTrigger.vue?vue&type=style&index=0&lang=scss&

  Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
  SassError: Can't find stylesheet to import.
    ╷
  1 │ @import 'themes/default/variables';

有关如何解决此问题的任何想法。我不需要加载scss进行测试,尽管我无法禁用样式处理(空加载器),因为我在项目中没有webpack.config文件,除非绝对必要,否则我不会创建一个文件。项目是使用Vue CLI生成的。

1 个答案:

答案 0 :(得分:1)

我能够通过在路径前面加上'〜@ / ...'来解决此问题,尝试一下。例如:“ @import:'〜@ / styles / variables';”