javascript - Webpack SCSS 到 CSS 提取不起作用

标签 javascript css reactjs sass webpack

我正在尝试使用 webpack 插件在我的 react.js 应用程序上将 SCSS 提取到 CSS:extract-text-webpack-plugin。我没有收到任何错误,但是编译时我在页面上看不到任何样式。在下面的代码中,我只是试图将屏幕上呈现的 hello world 的颜色从黑色更改为红色。这是我的文件:

webpack.config.js

var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');


module.exports = {
    devtool: 'inline-source-map',
    entry: [
        'webpack-dev-server/client?http://127.0.0.1:8080/',
        'webpack/hot/only-dev-server',
        './src/index.js',
        './sass/styles.scss'
    ],
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    resolve: {
        modules: ['node_modules', 'src'],
        extensions: ['.js', '.jsx']
    },
   module: {
            rules: [
        {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            use: ['react-hot-loader', 'babel-loader']
        },

        { // regular css files
            test: /\.css$/,
            loader: ExtractTextPlugin.extract({
              loader: 'css-loader?importLoaders=1',
            }),
        },

        { // sass / scss loader for webpack
            test: /\.(sass|scss)$/,
            loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
        }

    ]
},

plugins: [
     new webpack.HotModuleReplacementPlugin(),
     new webpack.NoEmitOnErrorsPlugin(),
     new ExtractTextPlugin("styles.css")
   ]
 };

样式.scss

$color : red;

.hello{
   font-color: $color;
   text-align: center;
}

app.js

import React from 'react';

export default class App extends React.Component {
    render() {
        return (
     <div>
        <h1 className = "hello">Hello World</h1>
      </div>);
    }
}

index.js

import React from 'react';
import { render } from 'react-dom';
import App from './components/app';

render ( <App />, document.getElementById('app'));

我错过了什么?

最佳答案

您的加载程序链中缺少 style-loader

来自 sass-loader docs :

Chain the sass-loader with the css-loader and the style-loader to immediately apply all styles to the DOM.

// webpack.config.js
module.exports = {
    ...
    module: {
        rules: [{
            test: /\.scss$/,
            use: [{
                loader: "style-loader" // creates style nodes from JS strings
           }, {
               loader: "css-loader" // translates CSS into CommonJS
           }, {
               loader: "sass-loader" // compiles Sass to CSS
            }]
        }]
    }
};

关于javascript - Webpack SCSS 到 CSS 提取不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758210/

相关文章:

html - 悬停时图像缩放效果

css - 覆盖框阴影伪元素防止悬停事件发生在 child 身上

javascript - 为什么要为 Node 捆绑 React server.js?

javascript - 加速 Ionic 应用程序中的 Levenshtein 距离计算

javascript - 是否可以在 Javascript 中将代码块分配给 switch case 两次?

javascript - 使用缓冲区和流写入数据

javascript - ReactJs 立即打印值

javascript - 获取包含 HTML 内容的 contentEditable 区域中的插入符号(光标)位置

CSS - 图像和文本的对齐问题

reactjs - React Hook useEffect 缺少依赖项(没有在 useEffect 中移动函数)