Compiling Sass Themes

Sass themes must be compiled to CSS understood by browsers. Compilation can be done with the Vaadin Sass Compiler, which you can run in Eclipse, Maven, or it can be run on-the-fly when the application is loaded in the browser. You can also use any other Sass compiler.

Compiling On the Fly

The easiest way to develop Sass themes is to compile them at runtime, when the page is loaded. The Vaadin servlet does this automatically if a compiled theme CSS file is not found from the theme folder. You need to have the SCSS source files placed in the theme folder. The theme is compiled when the styles.css is first requested from the server. If you edit the Sass theme, it is recompiled the next time you reload the page.

The on-the-fly compilation takes a bit time, so it is only available when the Vaadin servlet is in the development mode, as described in “Other Servlet Configuration Parameters”. Also, it requires the theme compiler and all its dependencies to be in the class path of the servlet. At least for production, you must compile the theme to CSS, as described next.

Note
If the on-the-fly compilation does not seem to work, ensure that your build script has not generated a pre-compiled CSS file. If styles.css file is found the servlet, the compilation is skipped. Delete such an existing styles.css file and disable theme compilation temporarily.

Compiling in Eclipse

If using Eclipse and the Vaadin Plugin for Eclipse, its project wizard creates a Sass theme. It includes Compile Theme command in the toolbar to compile the project theme to CSS. Another command compiles also the widget set.

eclipse theme compiler

Compiling Sass Theme

The WebContent/VAADIN/mytheme/styles.scss and any Sass sources included by it are compiled to styles.css.

Compiling with Maven

To compile the themes with Maven, you need to include the built-in themes as a dependency:

XML

  1. ...
  2. <dependencies>
  3. ...
  4. <dependency>
  5. <groupId>com.vaadin</groupId>
  6. <artifactId>vaadin-themes</artifactId>
  7. <version>${vaadin.version}</version>
  8. </dependency>
  9. </dependencies>
  10. ...

This is automatically included at least in the vaadin-archetype-application archetype for Vaadin applications. The actual theme compilation is most conveniently done by the Vaadin Maven Plugin with update-theme and compile-theme goals.

XML

  1. ...
  2. <plugin>
  3. <groupId>com.vaadin</groupId>
  4. <artifactId>vaadin-maven-plugin</artifactId>
  5. ...
  6. <executions>
  7. <execution>
  8. ...
  9. <goals>
  10. <goal>clean</goal>
  11. <goal>resources</goal>
  12. <goal>update-theme</goal>
  13. <goal>update-widgetset</goal>
  14. <goal>compile-theme</goal>
  15. <goal>compile</goal>
  16. </goals>
  17. </execution>
  18. </executions>

Once these are in place, the theme is compiled as part of relevant lifecycle phases, such as package.

  1. mvn package

You can also compile just the theme with the compile-theme goal:

  1. mvn vaadin:compile-theme

Compiling in Command-line

You can compile Sass style sheets to CSS either with the Vaadin Sass compiler or the standard one. The styles.css of a custom theme should be the compilation target. When compiled before deployment, the source files do not need to be in the theme folder.

You can run the Vaadin Sass compiler in a theme folder as follows:

  1. java -cp '../../../WEB-INF/lib/*' com.vaadin.sass.SassCompiler styles.scss styles.css

The -cp parameter should point to the class path where the Vaadin Sass Compiler and theme JARs are located. In the above example, they are assumed to be located in the WEB-INF/lib folder of the web application. If you have loaded the Vaadin libraries using Ivy, as is the case with projects created with the Vaadin Plugin for Eclipse, the Vaadin libraries are stored in Ivy’s local repository. Its folder hierarchy is somewhat scattered, so we recommend that you retrieve the libraries to a single folder. We recommend using an Ant script as is described next.

Compiling with Ant

With Apache Ant, you can easily resolve the dependencies with Ivy and compile the theme with the Theme Compiler included in Vaadin as follows. This build step can be conveniently included in a WAR build script.

Start with the following configuration:

XML

  1. <project xmlns:ivy="antlib:org.apache.ivy.ant"
  2. name="My Project" basedir="../"
  3. default="package-war">
  4. <target name="configure">
  5. <!-- Where project source files are located -->
  6. <property name="src-location" value="src" />
  7. ... other project build definitions ...
  8. <!-- Name of the theme -->
  9. <property name="theme" value="book-examples"/>
  10. <!-- Compilation result directory -->
  11. <property name="result" value="build/result"/>
  12. </target>
  13. <!-- Initialize build -->
  14. <target name="init" depends="configure">
  15. <!-- Construct and check classpath -->
  16. <path id="compile.classpath">
  17. <!-- Source code to be compiled -->
  18. <pathelement path="${src-location}" />
  19. <!-- Vaadin libraries and dependencies -->
  20. <fileset dir="${result}/lib">
  21. <include name="*.jar"/>
  22. </fileset>
  23. </path>
  24. <mkdir dir="${result}"/>
  25. </target>

You should first resolve all Vaadin libraries to a single directory, which you can use for deployment, but also for theme compilation.

  1. <target name="resolve" depends="init">
  2. <ivy:retrieve
  3. pattern="${result}/lib/[module]-[type]-[artifact]-[revision].[ext]"/>
  4. </target>

Then, you can compile the theme as follows:

  1. <!-- Compile theme -->
  2. <target name="compile-theme"
  3. depends="init, resolve">
  4. <delete dir="${result}/VAADIN/themes/${theme}"/>
  5. <mkdir dir="${result}/VAADIN/themes/${theme}"/>
  6. <java classname="com.vaadin.sass.SassCompiler"
  7. fork="true">
  8. <classpath>
  9. <path refid="compile.classpath"/>
  10. </classpath>
  11. <arg value="WebContent/VAADIN/themes/${theme}/styles.scss"/>
  12. <arg value="${result}/VAADIN/themes/${theme}/styles.css"/>
  13. </java>
  14. <!-- Copy theme resources -->
  15. <copy todir="${result}/VAADIN/themes/${theme}">
  16. <fileset dir="WebContent/VAADIN/themes/${theme}">
  17. <exclude name="**/*.scss"/>
  18. </fileset>
  19. </copy>
  20. </target>
  21. </project>