0

我在 Laravel 5.7 中使用 vuejs 来构建一个 SPA。我正在使用 mocha-webpack 和 vue-test-utils 编写一些 vue 组件测试。

测试似乎无法将样式表导入组件。例如:我有一个 ExampleComponent.vue,其中包括以下内容:

</script>

<style lang="scss" scoped>

@import '@/_variables.scss';

.subpanel-control > ul > li {
    background-color: lighten($body-bg, 50);
    margin-left: 10px;
    height: 25px;
    width: 25px;
    line-height: 25px;
    color: $body-bg;
}

运行时npm run test出现以下错误:

Error in ./resources/assets/js/components/Panel.vue?vue&type=style&index=0&id=21f01f46&lang=scss&scoped=true&

  Module build failed (from ./node_modules/sass-loader/dist/cjs.js):

  @import '@/variables';
         ^
        Can't find stylesheet to import.
     ╷
  98 │ @import '@/variables';
     │         ^^^^^^^^^^^^^
     ╵
    stdin 98:9  root stylesheet
        in C:\Users\jjackson\Documents\projects\wave\resources\assets\js\components\Panel.vue (line 98, column 9)

我不知道为什么它不理解别名@。这是我的 webpack.mix.js:

let mix = require('laravel-mix');
const path = require('path');

mix.js('resources/assets/js/app.js', 'public/js')
    .sass('resources/assets/sass/app.scss', 'public/css')
    .sass('resources/assets/sass/nifty.scss', 'public/css', {
        implementation: require('node-sass')
    })
    .sass('resources/assets/sass/themes/type-b/theme-ocean.scss', 'public/css')
    .less('resources/assets/less/bootstrap-4-utilities.less', 'public/css')
    .webpackConfig({
        resolve: {
            alias: {
                '@': path.resolve('resources/assets/sass'),
                '~': path.resolve('resources/assets/js')
            }
        }
    })
    .sourceMaps();

if (mix.inProduction()) {
    mix.version();
}

任何帮助,将不胜感激

4

2 回答 2

0

原来这是一个码头问题。试图npm run dev在 docker 环境之外运行,并导致此错误。当我在容器内运行所有 npm 命令时,一切正常

于 2020-03-02T09:19:03.287 回答
0

在为同样的问题苦苦挣扎时,我登陆了这个页面 - https://github.com/vuejs/vue-cli/issues/4053和来自 robbishop 的评论引导我找到了我的解决方案。使用上面的方法,我设法在 webpack.mix.js 中找到了一个似乎有效的配置:

  1. 首先安装 null-loader (npm i -D null-loader)
  2. 在规则中添加以下行:
{ test: /\.scss$/, use: [{ loader: 'null-loader' }] }

我的 webpack.mix.js 看起来像这样:

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
    .webpackConfig({
        module: {
            rules: [
                { test: /\.scss$/, use: [{ loader: 'null-loader' }] }
            ]
        },
        resolve: {
            alias: {
                '@': path.resolve('somepath/sass')
            }
        }
    })
    .sass('some/path/app.scss', 'some/path/css');

我的单元测试现在在我的组件中有 scss 导入时执行。

于 2019-12-31T08:47:30.133 回答