A CSS preprocessor is a program that lets you generate CSS from the preprocessor’s own unique syntax. The benefit of the CSS preprocessor is to avoid duplication of the code by adding global variables and those variables can be useful anywhere if you want in CSS by converting into CSS by the compiler. In this blog, we will look at adding the Sass preprocessor.
Step 1:- Install the dependency sass compiler by composer
Step 2: –Create a module in the app/code folder by creating registration.php in app/code/Webkul/CssPreprocessor/ and module.xml in the app/code/Webkul/CssPreprocessor/etc.
registration.php
module.xml
And finally, enable the module and run php bin/magento setup:upgrade
Step 3:- After creating the module, you have to create Processor.php in the following path app/code/Webkul/CssPreprocessor/Preprocessor/Adapter/Scss/ and add the following code to tell the Magento to compile the following sass file to CSS at the time of static site deploy .
Step 4: – Create a di.xml in the app/code/Webkul/CssPreprocessor/etc/ to pass the newly created class as an argument.
Step 5: – create a test.scss file in the app/code/Webkul/CssPreprocessor/view/frontend/web/css/
Step 6:- Run the following commands
Hence once you run this command the CSS will be generated in the pub/static/frontend/Magento/your_theme/Webkul_CssPreprocessor/css/test.css
Hence In this code, we have compiled the variable declared into the regular CSS by using Leafo/ScssPhp of SCSS. Basically, this is one example you can add any other CSS preprocessor as per your requirement to make Frontend development fast and reliable.
The following benefits of adding CSS Preprocessor : –
- Reduced the development time of the Developer by creating a global variable and using it anywhere need instead of repeating the code
- Adding the nested, mixin syntax.
For more details, you can refer https://developer.adobe.com/commerce/frontend-core/guide/css/custom-preprocessor/
I hope you understand the blog, Happy Coding