解决Webpack模块中的SCSS文件

时间:2019-07-03 18:23:58

标签: javascript css reactjs webpack sass

webpack.config.js

const styles = path.join(__dirname, "client/styles.scss");

module.exports = {
       resolve: {
          modules: [..., styles]
       }
    }

file.js

import base from "styles"; 

<div styleName="base.x">
...

</div>

错误:

Module build failed (from ./node_modules/babel-loader/lib/index.js):                                                                             
Error: CSS module import does not exist: base                                                                                                    
    at getClassNameForNamespacedStyleName (\node_modules\babel-plugin-react-css-modules\dist\getClassN
ame.js:36:13)                                                                                                                                    
    at styleNameValue.split.filter.map.styleName (\node_modules\babel-plugin-react-css-modules\dist\ge
tClassName.js:68:14)                                                                                                                             
    at Array.map (<anonymous>)                                                                                                                   
    at exports.default (\node_modules\babel-plugin-react-css-modules\dist\getClassName.js:66:6)       
    at exports.default (\node_modules\babel-plugin-react-css-modules\dist\resolveStringLiteral.js:23:5
6)                                                                                                                                               
    at PluginPass.JSXElement (\node_modules\babel-plugin-react-css-modules\dist\index.js:193:48)      
    at newFn (\node_modules\@babel\traverse\lib\visitors.js:193:21)                                   
    at NodePath._call (\node_modules\@babel\traverse\lib\path\context.js:53:20)                       
    at NodePath.call (\node_modules\@babel\traverse\lib\path\context.js:40:17)                        
    at NodePath.visit (\node_modules\@babel\traverse\lib\path\context.js:88:12)                       
 @ ./client/routes.js 1:385-429 1:1128-1133                                                                                                      
 @ ./client/index.js                                                                                                                             

1 个答案:

答案 0 :(得分:0)

以下代码摘录自我的一个webpack项目。它基本上可以编译/监视任何scsscss文件。

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const process = require('process');


module.exports = {
    module: {
        rules: [
            // ... more
            {
                test: /\.(scss|css)$/,
                use: [
                    process.env.NODE_ENV !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            modules: true,
                        }
                    }, 
                    {
                        loader: "sass-loader",
                        options: {
                            data: "$env: " + process.env.NODE_ENV + ";"
                        }
                    }
                ],
            },

            // ... more
        ]
    }
}

添加您的解析配置,您应该一切顺利。但是请确保将样式的Babel转换(如果使用的话)与css-loader的localIdentName s保持同步。