Web development and Tech news Blog site. WEBISFREE.com

HOME > css

Preprocessor Sass/SCSS to CSS compilation transformation

Last Modified : 04 Mar, 2023 / Created : 04 Mar, 2023
735
View Count
Let's briefly take a look at how to convert sass or scss into css and use it in the NodeJS environment.




When is conversion necessary?
In general situations, it is not necessary as it can be automatically converted during the transpiling or bundling process in most building processes. However, there may be rare situations where manual building is required, or when code from a previously worked on CSS preprocessor file needs to be converted to CSS.


One. Installing sass module.
To perform a conversion, you basically need to use the sass command. To use sass as a cli, you install the node package globally.
npm i -g sass

Once the installation is complete, try entering "sass" on the command line. Verify if the message below appears.
$ sass
Compile Sass to CSS.

Usage: sass <input.scss> [output.css]
sass <input.scss>:<output.css> <input/>:<output/> <dir/>
...


Two. Using commands in command line.
Once the installation is complete, checking the conversion or CSS code is simple. First, let's briefly learn about a few options as shown below.

-w // If the file has been changed, automatically detect and convert it.
-c // Using color in terminal or Unicode.
-h // Check for help.

There are options like above. In the case of watch, it is very useful as the file that is automatically updated every time the file changes.


! View example code for SASS conversion.


Let's look at a brief example. First, here is the sample code.

Filename : test.scss
$color1: red;
$color2: blue;

div {
  color: $color1;

  span {
    color: $color2;
  }
}

@ Check for only code
The method to only check for code changes made in CSS is to enter the SCSS or SASS command and file name in the CLI.
> sass test.scss

// Result
div {
  color: red;
}
div span {
  color: blue;
}

If we check the converted result, we can confirm that it has been converted properly. This time, let's try creating a new file called test2.css..


@ Create a new css file
Creating a new file is simple. You just need to add a new file name after the existing file name.
> sass test.scss test2.css

This time, a new file named test2.css has been created without being displayed. Let's check its content.
> cat test2.css

div {
color: red;
}
div span {
color: blue;
}

/*# sourceMappingURL=test2.css.map */

Upon examining the generated files, they are identical. However, it can be noted that there are comments added at the bottom.


We have learned the method for converting sass/scss files to css in a brief manner up to this point.
Perhaps you're looking for the following text as well?

    Previous

    Resolving the issue of using "position: fixed" CSS property with a parent element that has "position: relative"

    Previous

    Learn how to implement text outline in CSS