I want to use LESS preprocessor in my create-react-app.
React Code
ReactDOM.render(
<form>
<input type="email" data-info="Enter Email" pattern="/\S+@\S+\.\S+/" required title="Please enter value" required="required"/>
<input type="submit"/>
</form>
,document.getElementById('root'))
index.less
body{
background: red;
}
So, if we decide to use LESS stylesheets in our app, let us see how to set it up with your react app, without having to eject from create-react-app and modifying your webpack config manually. You can take a look at the completed boilerplate setup here.
Every React developer should add compression to their bundling process. One of the most impactful techniques to reduce the bundle size of a React application is compression. compression is a process in which the size of a file is reduced by re-encoding the file data to use fewer bits of storage than the original file.
Can I use Sass? If you use the create-react-app in your project, you can easily install and use Sass in your React projects. Now you are ready to include Sass files in your project!
Here is the way I am doing it
You should use node-sass-chokidar npm package:
npm install node-sass-chokidar --save-dev
Then add the following to your npm scripts in package.json :
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive"
The watcher will find every Sass file in src subdirectories, and create a corresponding CSS file next to it.
Remember to remove all css files from the source control and add src/**/*.css to your .gitignore.
Finally you might want to run watch-css automatically with npm start, and run build-css as a part of npm run build . For it, install the following npm package in order to be able to execute two scripts sequentially:
npm install --save-dev npm-run-all
Then change your npm start and build scripts in package.json file to the following:
"scripts": {
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive"
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
See this link for further information: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-a-css-preprocessor-sass-less-etc
With create-react-app 2 you can use "Create React App Configuration Override" (craco) and craco-less to configure this:
set up craco:
npm install @craco/craco
in package.json:
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "craco eject"
},
setting up craco for less:
npm install craco-less
create new file: craco.config.js
module.exports = {
plugins: [
{
plugin: require('craco-less'),
},
],
};
The simplest and painless way to add less support with reactjs app by far is to use custom-react-scripts
with create-react-app
for LESS/SASS/decorators support :
Install: npm install -g create-react-app custom-react-scripts
Create app:
create-react-app <app_name> —scripts-version custom-react-scripts
Example less file:
.myDiv{ background: gray; }
and then in your component:
import lessStyles from '<less file path>';
<div style={lessStyles.myDiv}>
...
</div>
or the regular way:
import '<less file path>';
<div className="myDiv">
...
</div>
For reference:
https://github.com/kitze/custom-react-scripts
https://github.com/fawaz-ahmed/fawaz-ahmed.github.io
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With