Webpack配置对我的SCSS文件不起作用:(

webpack config is not working for my scss files :(

本文关键字:不起作用 文件 SCSS 配置 我的 Webpack      更新时间:2023-09-26

我正在尝试使用css模块和sass加载器。然而,这似乎不起作用。这是我的webpack配置-它没有抛出任何错误,但当我检查/static/时,我只看到bundle.js而不是style.scss。

var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');
module.exports = {
  devtool: 'source-map',
  entry: [
    'webpack-hot-middleware/client',
    'index.js'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  resolve: {
    root: path.resolve(__dirname),
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /'.jsx?$/,
        loaders: [
          'react-hot',
          'babel'
        ],
        include: path.join(__dirname, ''),
        exclude: /node_modules/
      },
      {
        test: /'.(png|jpg)$/,
        loader: 'file-loader?name=img/[name].[ext]'
      },
      {
        test: /'.scss$/,
        loader: ExtractTextPlugin.extract('style-loader', ['css-loader', 'autoprefixer-loader', 'sass-loader'])
      }
    ]
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new ExtractTextPlugin('/static/style.css', {
      allChunks: true
    })
  ]
};

您只需要添加文件名而不是文件路径(因为它是基于publicPath的)

  ...
  module: {
    loaders: [
      {
        test: /'.jsx?$/,
        loaders: [
          'react-hot',
          'babel'
        ],
        include: path.join(__dirname, ''),
        exclude: /node_modules/
      },
      {
        test: /'.(png|jpg)$/,
        loader: 'file-loader?name=img/[name].[ext]'
      },
      {
        test: /'.scss$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader', 'autoprefixer-loader', 'sass-loader')
      }
    ]
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new ExtractTextPlugin('style.css', {
      allChunks: true
    })
  ]
};