简体   繁体   English

React + Webpack提取文本插件。 我究竟做错了什么? 我的样式从bundle.js工作,但是我试图将它们移到bundle.css

[英]React + Webpack Extract Text Plugin. What am I doing wrong? My styles work from bundle.js, but I am trying to move them out to bundle.css

I am writing my styles using scss, and I am using css modules so each component has its own styles. 我使用scss编写样式,并且使用css模块,因此每个组件都有自己的样式。

My webpack code is below, but here's my problem. 我的webpack代码在下面,但这是我的问题。

I would like my prod bundle to have a separate CSS bundle. 我希望我的产品捆绑包有一个单独的CSS捆绑包。 I am able to generate a separate CSS bundle.css , but none of the mapping between components to the styles. 我能够生成一个单独的CSS bundle.css ,但是没有组件之间到样式的映射。 So, it breaks bootstrap styling. 因此,它破坏了引导程序样式。

if (__PROD) {
  config = {
    devtool: 'cheap-module-source-map',
    context: appPath,
    entry: [
      'main.js'
    ],
    resolve: {
      root: appPath,
      extensions: ['', '.js', '.jsx']
    },
    module: {
      loaders: [
        {
          test: /\.js$/,
          loaders: [
            'babel'
          ],
          include: appPath
        },
        {
          test: /\.scss$/,
          // loader: ExtractTextPlugin.extract('style', 'css!postcss!sass'),
          loader: ExtractTextPlugin.extract('css!postcss!sass')
        }
      ]
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: JSON.stringify('production')
        }
      }),
      new webpack.optimize.UglifyJsPlugin({
        compressor: {
          warnings: false
        }
      }),
      new ExtractTextPlugin('bundle.css')
    ],
    postcss: [
      autoprefixer({ browsers: ['last 3 versions'] })
    ],
    output: {
      path: buildPath,
      publicPath: '/',
      filename: 'bundle.js'
    }
  };
}

Try adding the css file in the entry . 尝试在entry添加css文件。

entry: [
    'main.js',
    'yourFile.css'
]

I am using css modules 我正在使用CSS模块

You might be using them in your dev config, but you did not enable them in your production config. 您可能在开发配置中使用了它们,但是没有在生产配置中启用它们。 Add the modules option to the css-loader in the ExtractTextPlugin as well. 也将modules选项添加到ExtractTextPlugincss-loader中。

ExtractTextPlugin.extract('css?modules!postcss!sass')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM