Sass Integration

Backoffice provides support for Syntactically Awesome Style Sheets (Sass) scripting language, a CSS preprocessor that gives better experience in styling your application.

Prerequisites

Backoffice copies SCSS files from backoffice extension, Backoffice Framework JAR files, and the registered backoffice extensions to the resources/backoffice/generated directory and then compiles them. The compiled CSS files are located in the same directory as the copied SCSS files.

Backoffice uses jruby-complete and sass-gem in order to compile SCSS files.

Basic Configuration

In Backoffice, the backoffice.sass.enabled is set to true by default in the local.properties:

backoffice.sass.enabled=true

To disable Sass, change the value of this property to false.

By default, the resources are cached by Platform. Therefore, any change related to resources (SCSS or CSS files) requires restarting SAP Commerce Cloud Server. You can avoid this by disabling the resourcecache in your local.properties file:
backoffice.cockpitng.resourceloader.resourcecache.enabled=false
For the purposes of debugging Sass files in a browser, you can use source maps. Information about an original file is lost while processing a Sass file. To connect a Sass file with the generated CSS file, you can set the following property in the local.properties file to true:
backoffice.sass.source.map.enabled=true
By default, the property is set to false.

Variables Files

Backoffice contains a few files related to Sass variables:
  • cockpitng-globals.scss located in the cockpit-base/cockpitframework/src/main/resources/cockpitng/cng/scss directory − this file contains variables connected with Backoffice Framework.

  • backoffice-variables.scss located in the backoffice/resources/backoffice/scss directory − this file contains variables connected with Backoffice, and these variables may override variables from cockpitng-variables.scss.

  • <EXTENSION>-variables.scss located in the <EXTENSION>/backoffice/resources/scss directory − this file contains variables connected with a given extension, and these variables may override variables from the cockpitng-variables.scss and the backoffice-variables.scss.

  • *-variables.scss located anywhere − file with the highest priority, which can override variables from the cockpitng-variables.scss, backoffice-variables.scss and <EXTENSION>-variables.scss files. Assuming that the file is called custom-variables.scss and it has been created within the backoffice/resources/cockpitng/cng/scss path, the next step is adding the following properties to the local.properties file:

    backoffice.sass.preferred.extension=customBackofficeExtension
    backoffice.sass.preferred.variables.file=pathToScssFile

    For example:

    backoffice.sass.preferred.extension=customExtension
    backoffice.sass.preferred.variables.file=backoffice/resources/scss/custom-variables.scss

The above variables files are included automatically to every SCSS file during the copying process. If you want to have SCSS file which shouldn't have automatic imports, then the path to such file should match the pattern **/globals/**/__*.scss - which means that the path to the directory needs to have a globals directory somewhere in the path and the file's name needs to start with __ and end with .scss - for example, myextbackoffice/backoffice/resources/cng/scss/globals/__myfile.scss.

Registering backoffice Extension with Sass Files

To register extension as the extension with Sass files, just add the following callback to your buildcallbacks.xml. See the example for the platformbackoffice extension:

<macrodef name="platformbackoffice_before_build">
    <sequential>
        <register_sass_extension extensionname="platformbackoffice"/>
        <register_sass_extension extensionname="platformbackoffice" resources="resources" destination=""/>
    </sequential>
</macrodef>

The <extensionname> parameter indicates the name of extension to provide Sass support.

The <resources> parameter points to the path where SCSS files are located.

The <destination> parameter allows you to place the generated files in the subdirectories of the resources/backoffice/generated directory.

Notice that two register records have been declared. The first one is called the default registration. The default resources attribute points to the backoffice/resources path and the destination is equal to cockpitng. The second one is called custom registration and the resources and the destination attributes are provided by a user.

Copying Sass Files Process

During the copying process, the whole structure of directories is preserved.

See the example:

For the following registration in the buildcallbacks.xml:

<register_sass_extension extensionname="platformbackoffice" />

and the SCSS file located in:

platformbackoffice/backoffice/resources/widgets/charts/genericChart/genericChart.scss

the copy of SCSS file would be placed in the following path:

resources/backoffice/generated/cockpitng/widgets/charts/genericChart/genericChart.scss
Platform build process invokes backoffice Sass callbacks in the following order (at the backoffice_after_build_all_extensions stage):
  1. <backoffice_sass_copy_sass_from_registered_extensions/>

    It copies SCSS files from the registered extension to the resources/backoffice/generated directory.

  2. <backoffice_sass_copy_sass_from_backoffice/>

    It copies SCSS files from backoffice to the resources/backoffice/generated directory.

  3. <backoffice_sass_copy_sass_from_cockpitng/>

    It unzips and copies SCSS files from Backoffice Framework JARs to the resources/backoffice/generated directory.

  4. <backoffice_sass_copy_scss_from_additional_resource_loader/>
    It copies SCSS files from Backoffice Framework sources.
  5. After copying process, all required imports are added into SCSS files and compiling process is invoked.

To start the build process, call ant all in the <HYBRIS_BIN_DIR>/platform directory.

Imports

By convention, each extension that contains SASS files should have the variables located in the <EXTENSION_NAME>/backoffice/resources/scss/<EXTENSION_NAME>-variables.scss directory. Backoffice adds this import automatically for each SCSS file for the given extension.

During the copying process, backoffice automatically adds a few @import statements to SCSS files (excluding files located in **/globals/**/__*.scss):

  • @import cockpitng-globals.scss

  • @import backoffice-variables.scss

  • @import <EXTENSION_NAME>-variables.scss

  • Other @import statements located within SCSS files

  • @import specified in backoffice.sass.preferred.extension

Hot Deployment of SCSS Files

Backoffice supports hot deployment of SCSS files. To redeploy modified SCSS files, just invoke ant sasscompile (without the need to stop SAP Commerce Cloud Server).

The hot redeployment process consists of the same steps as "Copying Sass Files Process"

Build Lifecycle

The backoffice copies and compiles Sass files in two cases: ant all and ant sasscompile.

In backoffice, we distinguish the following build cycles:
  • ant clean
    • Description: Removes all SCSS and CSS files from the resources/backoffice/generated directory.
    • Steps:
      1. Removes the generated directory.
  • ant sassclean
    • Description: Removes all SCSS and CSS files from the resources/backoffice/generated directory.
    • Steps:
      1. Removes the generated directory.
  • ant all
    • Description: Removes old registrations, copies fresh SCSS files from backoffice, backoffice extensions, and Backoffice Framework and compiles them.
    • Steps:
      1. Removes the sassExtensions file from backoffice.
      2. Creates a new sassExtensions file in backoffice. New records are added to this file by each extension that is registered as Sass extension.
      3. Checks whether any SCSS file in backoffice, registered extension, or property, which is in path pointed in the backoffice.cockpitng.additionalResourceLoader.path file, has been changed. If nothing was changed, the next steps are skipped.
      4. Copies Sass files from registered extensions.
      5. Copies Sass files from backoffice
      6. Check whether .sasscompiled file exists. If not, the files from Backoffice Framework are taken into account (JAR files would be unzipped, Sass files would be copied to the generated directory).
      7. Compiles all Sass files.
  • ant sasscompile
    • Description: Hot redeployment. It does not require restarting SAP Commerce Cloud Server. Copies fresh SCSS files from backoffice and backoffice-based extensions and compiles them.
    • Steps:
      1. Copies Sass files from registered extensions.
      2. Copies Sass files from backoffice.
      3. Compiles all Sass files.

yBackoffice

yBackoffice has been adapted to support Sass files as well. A new question has been added while generating new extension: 'Register as a Sass extension?'. All CSS files are replaced by SCSS files in case of positive answer. Moreover, new registration is added to buildcallbacks.xml automatically:
<sequential>
   <register_sass_extension extensionname="EXTENSIONNAME"/>
   <register_sass_extension extensionname="EXTENSIONNAME" resources="resources" destination=""/>
</sequential>