不导出文件时,Webpack无法正确呈现sass

时间:2019-03-13 21:06:53

标签: node.js reactjs twitter-bootstrap webpack sass

我正在尝试将Webpack 4配置为将Sass用于MERN项目。当我使用 mini-css-extract-plugin 并导出CSS文件,然后将其链接到index.html文件中时,它将起作用。但是,当我尝试将css与bundle.js文件捆绑在一起时,它不会呈现css,奇怪的是,它不会呈现其中的一部分。我尝试遵循官方的引导程序webpack安装,但未按预期工作。

以下是文件:

webpack.config.js:

const path = require('path');
// const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
  entry: './client/src/index.js',
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.s?css$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              localIdentName: '[name]__[local]__[hash:base64:5]',
              modules: true,
              importLoaders: 1,
              sourceMap: true,
            },
          },
          {
            loader: 'sass-loader'
          },
        ],
      },
        {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: "babel-loader",
            options: {
                presets: ['@babel/preset-env']
            }
        },
        {
          test: /\.jsx$/,
          exclude: /node_modules/,
          loader: "babel-loader",
          options: {
              presets: ['@babel/preset-env']
          }
        }
      ]
  },
  output: {
    path: path.resolve(__dirname, 'client/public'),
    filename: 'bundle.js'
  }
}

./ client / src / index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';

import 'bootstrap';
import '../sass/style.scss'
import App from './components/App';

ReactDOM.hydrate(
    <BrowserRouter>
      <App />
    </BrowserRouter>,
    document.getElementById('root')
)

1 个答案:

答案 0 :(得分:0)

我相信webpack 4支持MiniCssExtractPlugin,您尝试使用的其他加载器无法正常工作。

检查de Docs并尝试:

https://webpack.js.org/plugins/mini-css-extract-plugin/