如何让源图为React Css模块工作?

时间:2015-12-23 11:37:43

标签: reactjs webpack css-modules webpack-hmr react-css-modules

我尝试使用react-css-moduleswebpackHot Module Replacement设置React项目。一切都像魅力一样,但我无法使CSS源图工作。

我跟着this guide让HMR工作。它涉及一个BrowserSync设置,用于在Webpack将其写入磁盘后更新css文件。

我使用(由react-css-modules建议)ExtractTextPlugin来提取所有的css:

{
    test: /\.scss$/,
    loader: ExtractTextPlugin.extract('style','css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!sass')
}

但是,如果我将其更改为源图,则按照建议here

loader: ExtractTextPlugin.extract('style', 'css?sourceMap!sass-loader outputStyle=expanded&sourceMap=true&sourceMapContents=true')

我在浏览器控制台中收到错误:"root" CSS module is undefined.

您可以找到我的示例repo here,但这里是我用于开发的完整webpack配置。

var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var WriteFilePlugin = require('write-file-webpack-plugin').default;

module.exports = {
  entry: {
    bundle: [
      'webpack/hot/dev-server',
      'webpack-hot-middleware/client',
      './index.js'
    ]
  },
  devtool: 'cheap-module-source-map',
  debug: true,
  devServer: devServer,
  context: path.resolve(__dirname, './src'),
  output: {
    path: path.resolve(__dirname, './builds'),
    filename: '[name].js',
    publicPath: '/builds/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.OldWatchingPlugin(),
    new WriteFilePlugin(),
    new ExtractTextPlugin('[name].css', {
      allChunks: true
    })
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['react-hot', 'babel-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style','css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!sass')
      }
    ]
  },
  resolve: {
    extensions: ['', '.js', '.json']
  }
};

如何使源图工作?

2 个答案:

答案 0 :(得分:8)

使用此:

ExtractTextPlugin.extract('style','css?sourceMap&modules&importLoaders=1&localI‌​dentName=[name]__[local]___[hash:base64:5]!sass?sourceMap')

即。将sourceMap参数添加到css& sass个装载机。它在sass-loader docs中这么说。

答案 1 :(得分:2)

这就是我设置css模块的方法:

'css-loader?modules&sourceMap&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!',