Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Stylus support in a React.js application?

I want the classes in my React.js application to be available for export from .styl-files in the same way as it can be done from CSS Modules, but I can't find any ready-made solution to this problem.

I found a guide to setting up CSS Modules in an application created with Create React App.
I understand that you need to run npm run eject and somehow rewrite configuration files,
but how – I don't understand.

like image 920
Artyom Ionash Avatar asked Aug 31 '18 08:08

Artyom Ionash


People also ask

Can I use Deno With React?

Aleph is a JavaScript framework that offers you the best developer experience in building modern web applications with React while using Deno for server-side operations. Aleph allows you to build React application that uses server-side rendering by default.

How use external scripts in React?

Installation: Open a terminal inside your ReactJS project folder and write the following code to install react-script-tag Package. Import 'ScriptTag' component: Import the built-in 'ScriptTag' component from the react-script-tag library at the top of the file where we want to add the script tag.

How do you access a variable outside a function in React?

It's to do with scoping and variable accessibility. If you put your variable outside of the render() function, you will need to use the keyword this to access it.

How do I connect my phone to React?

Simply pull up your device's browser and type in the IPv4 Address you copied down earlier followed by a colon and then the port number. The format should look something like the following: 555.55. 55.555:1234 once you hit enter, you should see your React App live on your mobile device!


2 Answers

You need to install next npm-packages in your project:

  • stylus
  • stylus-loader
  • css-loader

In webpack.config, in section module you need to add next points:

{
  test: /\.styl$/,
  use: [
    'style-loader',
    'css-loader?modules&camelCase&localIdentName=[path]__[name]__[local]--[hash:base64:5]',
    'stylus-loader',
  ],
},
{
  test: /\.css$/,
  use: [
    'style-loader',
    'css-loader',
  ],
},

Then you can import your styles from .styl files in your React components like this:

import style from './СomponentStyle.styl'; 

and you can use style by CSS name for example:

className={style.container} 

where container - it is name of CSS but without dot. For complicated names like: .container-btn-green you need write next code: style.containerBtnGreen or style['container-btn-green']

like image 83
Denis Bubnov Avatar answered Nov 09 '22 23:11

Denis Bubnov


Run-time solution ( without eject )

install react-rewired and customize-cra to have an option to update config on run-time

npm i react-app-rewired
npm i customize-cra

create file config-overrides.js in the package.json folder with content :

const {
    override,
    addWebpackModuleRule
  } = require("customize-cra");

module.exports = override(
    addWebpackModuleRule({
      test: /\.styl$/,
      exclude: /(node_modules)/,
      loaders: [
        'style-loader',
        {
          loader: 'css-loader',
          options: {url: false}
        },
        {
          loader: 'postcss-loader',
          options: {
            ident: 'postcss',
            plugins: (loader) => [require('autoprefixer')()]
          }
        },
        'stylus-loader'
      ]
    }),
    addWebpackModuleRule({
      test: /\.css$/,
      use: [
        'style-loader',
        'css-loader',
      ]
    })
);

install stylus

npm i stylus
npm i stylus-loader
npm i css-loader 

change your start / build scripts to use react-rewired

"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
...

Then you can import your styles from .styl files in your React components

require('./style/index.styl');

OR

import style from './СomponentStyle.styl'; --> className={style.container} 

This all :) Additional thanks to Denis Bubnov in current topic - you helped me much to implement current solution!

like image 45
Konstantin Eletskiy Avatar answered Nov 10 '22 00:11

Konstantin Eletskiy