22

Using LESS with create-react-app without Ejecting

 5 years ago
source link: https://www.tuicool.com/articles/hit/ZzQvAji
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using LESS with create-react-app without Ejecting

YbYBr2I.jpg!web

If you’re a fan of create-react-app (React’s official command line interface), you probably know it should supports SASS by default. Thos feature is ready and will be included in the next release of the project. So, using SASS or SCSS with CRA should be rather simple.

But, what happens when you want to use LESS as your styling method? Will this still work? Can we make it work?

In this post I’ll teach you how to combine create-react-app with LESS without having to eject.

Why use LESS when SASS is supported?

I hope you already know the difference between SASS & LESS .

There are lots of discussions about LESS and SASS, I’m not going to dive deeply into this topic. There could be many reasons to use LESS, but my personal experience was, I wanted to use a framework written in LESS.

If you want to override some variables or mixins of a LESS framework, the best way is overriding it with your own LESS file. For this, you need to make LESS work with React.

OK, guys. Enough talking, let’s make it work! :muscle:

eYjqQvn.png!web

Less with create-react-app (CRA)

Down to business.

1. Create a basic app using CRA

I’m gonna create a simple app for this tutorial. I’ll call it react-with-less. Run this in your CLI : ´create-react-app react-with-less´

ZrARnuj.png!web
create-react-app react-with-less

Then run npm start, it will open your browser in the port 3000 http://localhost:3000/

2.Using create-react-app’s custom config solution:react-app-rewired.

In your terminal run: npm install react-app-rewired — save . After installing react-app-rewired successfully we need to change scripts field in package.json.

Open up the project with your favorite code editor. Mine is VS Code. :heart_eyes_cat:

3. Updating package.json

Change the script field in package.json from this:

to this:

4. Override Webpack config

Create a file called config-overrides.js in the root folder and add this content.

5. Using less loader, react-app-rewire-less

Back in the terminal install the rewire-less package. Run ´ npm install react-app-rewire-less ´ in the terminal.

6. Updating config-overrides.js with rewire-less

If you successfully installed react-app-rewire-less, we can start updating config-overrides.js. So, open up config-overrides.js and first import the rewire-less package:

const rewireLess = require(‘react-app-rewire-less’);

Then we can override config like this :

7. Styling with LESS 

Hi5 guys, Now you are officially a Less person. But wait. We haven’t used it yet. Let’s go back to the code editor and delete App.css file. Now open up App.js file.

In the top of the file you can see import ‘./App.css’;. Change it to

import ‘./App.less’;

Then create the less file in the same directory. Open up App.less File and add some css for testing. I’m gonna change the body background color:

body {

background: #000;

}

Go back to terminal and run npm start.

Tadaaaa! Now you can see your changes in the browser. That’s it.

Conclusion

So that’s is. If you want to use LESS with create-react-app, now you know how, so that shouldn’t stop you. Feel free to comment below and ask me anything, suggest additions or add something I might have missed.

You can check out this repo in Github:

PR’s welcome.

You can also check out my article about how to create a contact form with react + php:

That’s all guys!

Happy coding…. &$§!§!$Z/%/(!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK