WebPack Sass-Loader不生成类选择器样式



我正在将scss用于我的react应用程序中。当我为诸如H1,H2,p,ul,li等常数选择器编写CSS时,我得到的问题是被接受的。。但是,当我为类或ID选择器编写CSS时,样式不应用。

工作案例

h1, h2 {
  padding:0px;
}

不工作案例

.main {
 padding:10px;
}

我也有同样的问题。我的webpack.config.js现在看起来像这样:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const config = {
    entry: './app/index.js',
    module: {
    rules: [
        {
            test: /(.css|.sass|.scss)$/,
            exclude: /node_modules/,
            use: ['css-hot-loader'].concat(ExtractTextPlugin.extract({
                use: [
                    {
                        loader: "style-loader" // creates style nodes from JS strings
                    },
                    {
                        loader: 'css-loader' // translates CSS into CommonJS
                    },
                    {
                        loader : 'sass-loader',
                        options: {
                            sourceMap: true
                        }
                    },
                    {
                        loader : 'postcss-loader',
                        options: {
                            plugins: function () {
                                return [
                                    require("autoprefixer")
                                ];
                            }
                        }
                    }
                ]
            })),
        },
        {
            test: /.(js)$/,
            exclude: /(node_modules|bower_components)/,
            use : ['babel-loader']
        }
    ]
},
devtool: 'source-map',
output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/' // avoid requesting server route instead of client route when hitting refresh /Cannot GET /route
},
plugins : [
    new HtmlWebpackPlugin({
        template: 'app/index.html'
    }),
    new ExtractTextPlugin({ filename: 'css/style.css', disable: true, allChunks: true }), // this means dist/css/style.css
]
};

if(process.env.NODE_ENV === "production"){ // 'production ready'
    config.plugins.push(
      new webpack.DefinePlugin({
        'process.env' : {
            'NODE_ENV' : JSON.stringify(process.env.NODE_ENV)
        }
      }),
      new webpack.optimize.UglifyJsPlugin({ sourceMap: true, minimize: 
 true })
  )
 }
module.exports = config;

我之前有一个fallbackLoader: 'style-loader'属性,并且在查询属性中有一些未使用的CSS-Loader选项。也许这有帮助。上面的配置按预期工作。

最新更新