Laravel-mix自定义规则不起作用?

时间:2018-07-24 13:24:22

标签: laravel reactjs laravel-5 webpack

我试图在我的Laravel项目的React部分中使用this package将scss用作javascript对象。

现在,当我尝试将规则添加到我的webpack.mix.js文件夹中时,对于我的所有.scss文件,我总是收到以下错误消息

ERROR in ./node_modules/css-loader??ref--5-2!./node_modules/postcss-loader/lib??postcss!./node_modules/resolve-url-loader??ref--5-4!./node_modules/sass-loader/lib/loader.js??ref--5-5!./node_modules/css-loader??ref--9-0!./resources/assets/sass/app.scss
Module build failed: 

^
      Invalid CSS after "e": expected 1 selector or at-rule, was "exports = module.ex"
      in /Users/user/Desktop/project/resources/assets/sass/app.scss (line 1, column 1)
 @ ./resources/assets/sass/app.scss 2:14-318
 @ multi ./resources/assets/js/app.js ./resources/assets/sass/app.scss

我的webpack.mix.js文件:

let 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.react('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

mix.webpackConfig({
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: "css-loader",
                        options: {
                            modules: true
                        }
                    }
                ]
            }
        ]
    }
});

编辑

我已经更新了webpack.mix.js并添加了sass-loader,但存在相同的错误

mix.webpackConfig({
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: "css-loader",
                        options: {
                            modules: true
                        }
                    },
                    {
                        loader: "sass-loader",
                        options: {
                            modules: true
                        }
                    }
                ]
            }
        ]
    }
});

2 个答案:

答案 0 :(得分:0)

sass-loader没有模块选项。同时安装样式加载器

尝试:

module: {
        rules: [
            {
                test: /\.scss$/,
                use: [
                    'style-loader',
                    {
                        loader: "css-loader",
                        options: {
                            modules: true
                        }
                    },
                    'sass-loader'
                ]
            }
        ]
    }

答案 1 :(得分:0)

我发现了一个非常不错的npm软件包,可以修复我的所有问题:

https://github.com/leinelissen/laravel-mix-react-css-modules