Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding CSS to React SSR Components

I am trying to add CSS to my component built in React using SSR, but I am unable to do so.

Things I've looked at:

  • https://www.npmjs.com/package/isomorphic-style-loader
  • https://cssinjs.org/server-side-rendering/?v=v10.0.0-alpha.22
  • webpack loaders

But in none the process is simple or clearly mentioned. The one which I tried a lot was isomorphic loader which seemed promising, but then it gave some vague errors after setting it in my CSS files:

Unexpected token (1:0) You may need an appropriate loader to handle this file type.

This is my boilerplate package - https://github.com/alexnm/react-ssr

How do I add styles to my React SSR code.

Update

const dev = process.env.NODE_ENV !== "production";
const path = require( "path" );
const { BundleAnalyzerPlugin } = require( "webpack-bundle-analyzer" );
const FriendlyErrorsWebpackPlugin = require( "friendly-errors-webpack-plugin" );

const plugins = [
    new FriendlyErrorsWebpackPlugin(),
];

if ( !dev ) {
    plugins.push( new BundleAnalyzerPlugin( {
        analyzerMode: "static",
        reportFilename: "webpack-report.html",
        openAnalyzer: false,
    } ) );
}

module.exports = {
    mode: dev ? "development" : "production",
    context: path.join( __dirname, "src" ),
    devtool: dev ? "none" : "source-map",
    entry: {
        app: "./client.js",
    },
    resolve: {
        modules: [
            path.resolve( "./src" ),
            "node_modules",
        ],
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: "babel-loader",
            },
        ],
    },
    output: {
        path: path.resolve( __dirname, "dist" ),
        filename: "[name].bundle.js",
    },
    plugins,
};
like image 303
Rahul Singh Avatar asked Nov 17 '25 23:11

Rahul Singh


1 Answers

Below configuration made CSS work
Packages installed:
babel-plugin-dynamic-import-node, babel-plugin-css-modules-transform, mini-css-extract-plugin, css-loader, style-loader

index.js

require( "babel-register" )( {
presets: [ "env" ],
plugins: [
    [
        "css-modules-transform",
        {
            camelCase: true,
            extensions: [ ".css", ".scss" ],
        }
    ],
    "dynamic-import-node"
],
} );
require( "./src/server" );

webpack.config.js

rules: [
        {
            test: /\.jsx?$/,
            exclude: /(node_modules|bower_components)/,
            loader: "babel-loader",
        },{
            test: /\.css$/,
            use: [
                {
                    loader: MiniCssExtractPlugin.loader,
                },
                'css-loader'
            ],
        },
    ]

In webpack config, added following plugin

new MiniCssExtractPlugin({
    filename: "styles.css",
}),

In server.js, Added the following code inside head in htmlTemplate.

<link rel="stylesheet" type="text/css" href="./styles.css" />

Usage

import  "./../App.css";

<h2 className="wrapper">F1 2018 Season Calendar</h2>
like image 120
Ajay Varghese Avatar answered Nov 20 '25 15:11

Ajay Varghese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!