pabloBar
pabloBar

Reputation: 317

react styled components not working in production

I have been able to work with styled-components on webpack dev server without any problem but the styles are not getting added when building the project for production with webpack -p --progress --colors or running webpack -d --progress --colors --watch

The only style added in production is an empty style

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

in the webpack.config.js i have the following rules that is running for both dev and production:

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'],
       }
     },
    ]

},

Plugins, note that debug is used to if is production build

 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')
    })
  ],

dev server settings:

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

Upvotes: 5

Views: 6129

Answers (1)

lecstor
lecstor

Reputation: 5707

do you have a global style created with createGlobalStyle that contains a css @import? Removing the import from my site just fixed the issue for me. (sounds like the same issue).

https://github.com/styled-components/styled-components/issues/2911#issuecomment-592012166

Upvotes: 6

Related Questions