Piotr Glejzer
Piotr Glejzer

Reputation: 278

Problem with styled-jsx/webpack and custom webpack.config.js

Information: I do not use NextJS in this project.

When I start my devSever I have a problem with styled-jsx/webpack as I mentioned in the title. When I try to add a new rule to CSS about scoped styled I get an error styled-jsx/css: if you are getting this error it means that yourcsstagged template literals were not transpiled.

I tried to fix my config with these topics:

https://github.com/zeit/styled-jsx/issues/537
https://github.com/zeit/styled-jsx/issues/498
https://github.com/zeit/styled-jsx/issues/469#issuecomment-423243758
https://github.com/zeit/styled-jsx/issues/434#issuecomment-415801021

but It doesn't work in my case and I don't know what I'm doing wrong.

My webpack config:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ni = require('os').networkInterfaces();
const host = Object.keys(ni)
  .map(interf => ni[interf].map(o => !o.internal && o.family === 'IPv4' && o.address))
  .reduce((a, b) => a.concat(b))
  .filter(o => o)[0];

const config = {
  mode: 'development',
  entry: './docs/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  },
  resolve: {
    alias: {
      myPackage: path.resolve('./src/index')
    }
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [
          {
            loader: require('styled-jsx/webpack').loader,
            options: {
              type: 'scoped'
            }
          }
        ]
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.css$/i,
        exclude: /\.module\.css$/i,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.module\.css$/i,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: true
            }
          }
        ]
      },

      {
        test: /\.(scss)$/,
        use: ['css-loader', 'sass-loader']
      },
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader' },

      { test: /\.(woff|woff2)$/, loader: 'url-loader?prefix=font/&limit=5000' },
      {
        test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
      },
      {
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
        issuer: {
          test: /\.jsx?$/
        },
        use: ['babel-loader', '@svgr/webpack', 'url-loader']
      },
      {
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader'
      },
      {
        test: /\.png(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader?limit=10000&mimetype=image/png'
      },
      {
        test: /\.gif(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url-loader?limit=10000&mimetype=image/gif'
      }
    ]
  },
  devServer: {
    historyApiFallback: true,
    open: true,
    compress: true,
    port: 8080,
    host,
    hot: true
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'public/index.html'
    })
  ]
};

module.exports = config;

I tried also to add this styled-jsx/webpack loader in the same rules with babel-loader ( with style-loader and css-loader too) but It doesn't work either. Like this:

  {
    test: /\.(js?|css)$/,
    // exclude: /node_modules/,
    use: [
      {
       // I tried put a style-lodaer and css-lodaer to this rule but It dosent'work
        loader: require('styled-jsx/webpack').loader,
        options: {
          type: 'scoped'
        }
      },
      'babel-loader'
    ]
  },

.babelrc

{
  "presets": ["@babel/env", "@babel/preset-react"],
  "plugins": [
    "@babel/proposal-class-properties",
    [
      "styled-jsx/babel",
      {
        "optimizeForSpeed": true
      }
    ]
  ]
}

I want to have a scoped css styles when import styles from css like a object and put them to the {styles}.

Do you have any advice about what I should do?

Upvotes: 3

Views: 1765

Answers (0)

Related Questions