React样式组件在生产环境中无法工作

4

我曾经能够在Webpack开发服务器上使用styled-components而没有任何问题,但当使用webpack -p --progress --colors进行构建项目以用于生产环境或者运行webpack -d --progress --colors --watch时,样式并没有被添加。

唯一在生产环境中添加的样式是空样式。

<style data-styled="" data-styled-version="4.4.1"></style>

在 webpack.config.js 中,我有以下规则,适用于开发和生产环境:
module: {
    rules: [
     {
       test: /\.js?$/,
       exclude: /(node_modules|bower_components)/,
       loader: 'babel-loader',
       query: {
         presets: ['react', 'es2015', 'stage-0'],
         plugins: ['react-html-attrs', 'transform-decorators-legacy', 'transform-class-properties'],
       }
     },
    ]

},

插件注意,debug用于判断是否为生产构建

 plugins: debug ? [
      new HtmlWebpackPlugin({
        template: path.resolve(__dirname, 'template.html')
      })
  ] : [
    new webpack.DefinePlugin({
     "process.env": {
       NODE_ENV: JSON.stringify("production"),
     },
   }),
    new CleanWebpackPlugin(),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new UglifyJsPlugin(),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'template.html')
    })
  ],

开发服务器设置:

devServer: {
    contentBase: BUILD_DIR,
    historyApiFallback: true,
    watchContentBase: true,
    port: 9000
  },

没有足够的细节。请分享您的构建/开发配置。 - demkovych
@demkovych,我希望我在不混乱帖子的情况下添加了所有必要的内容。 - pabloBar
你的 styled-component babel 插件在哪里? - demkovych
@demkovych 我之前尝试安装了 'babel-plugin-styled-components' 并在 plugins: ['react-html-attrs', 'transform-decorators-legacy', 'transform-class-properties', 'babel-plugin-styled-components'] 中添加了它,但仍然无法正常工作。 - pabloBar
@demkovych 我已经尝试过了,但是它没有起作用。顺便说一下,样式组件在 webpack -d --progress --colors --watch 中无法工作,只能在 webpack dev server 中工作。 - pabloBar
显示剩余6条评论
1个回答

6

非常感谢,虽然我不太明白为什么,但是注释掉@import并重新部署后它就可以工作了。我正在Heroku上托管一个使用create-react-app构建的React应用程序。 - nkhil
嘿,同样的事情...我可能会把我的全局样式暂时移到一个CSS文件中。谢谢你的回答...是时候把所有的头发都扫起来了。 - JonathanPeel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接