You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

themes-compiling.asciidoc 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ---
  2. title: Compiling Sass Themes
  3. order: 4
  4. layout: page
  5. ---
  6. [[themes.compiling]]
  7. = Compiling Sass Themes
  8. Sass themes must be compiled to CSS understood by browsers. Compilation can be
  9. done with the Vaadin Sass Compiler, which you can run in Eclipse, Maven, or it
  10. can be run on-the-fly when the application is loaded in the browser. You can
  11. also use any other Sass compiler.
  12. [[themes.compiling.on-the-fly]]
  13. == Compiling On the Fly
  14. The easiest way to develop Sass themes is to compile them at runtime, when the page is loaded.
  15. The Vaadin servlet does this automatically if a compiled theme CSS file is not found from the theme folder.
  16. You need to have the SCSS source files placed in the theme folder.
  17. The theme is compiled when the [filename]#styles.css# is first requested from the server.
  18. If you edit the Sass theme, it is recompiled the next time you reload the page.
  19. The on-the-fly compilation takes a bit time, so it is only available when the
  20. Vaadin servlet is in the development mode, as described in
  21. <<../application/application-environment#application.environment.parameters,"Other
  22. Servlet Configuration Parameters">>. Also, it requires the theme compiler and
  23. all its dependencies to be in the class path of the servlet. At least for
  24. production, you must compile the theme to CSS, as described next.
  25. 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.
  26. If a [filename]#styles.css# file is found by the servlet, the compilation is skipped.
  27. Disable theme compilation in your local development environment and delete the existing
  28. [filename]#styles.css# file to enable on-the-fly compilation. Note that
  29. theme compilation should not be disabled for production builds.
  30. [[themes.compiling.eclipse]]
  31. == Compiling in Eclipse
  32. If using Eclipse and the Vaadin Plugin for Eclipse, its project wizard creates a
  33. Sass theme. It includes [menuchoice]#Compile Theme# command in the toolbar to
  34. compile the project theme to CSS. Another command compiles also the widget set.
  35. [[figure.themes.compiling.eclipse]]
  36. .Compiling Sass Theme
  37. image::img/eclipse-theme-compiler.png[]
  38. The [filename]#WebContent/VAADIN/mytheme/styles.scss# and any Sass sources
  39. included by it are compiled to [filename]#styles.css#.
  40. [[themes.compiling.maven]]
  41. == Compiling with Maven
  42. To compile the themes with Maven, you need to include the built-in themes as a
  43. dependency:
  44. [source, xml]
  45. ----
  46. ...
  47. <dependencies>
  48. ...
  49. <dependency>
  50. <groupId>com.vaadin</groupId>
  51. <artifactId>vaadin-themes</artifactId>
  52. <version>${vaadin.version}</version>
  53. </dependency>
  54. </dependencies>
  55. ...
  56. ----
  57. This is automatically included at least in the
  58. [literal]#++vaadin-archetype-application++# archetype for Vaadin applications.
  59. The actual theme compilation is most conveniently done by the Vaadin Maven
  60. Plugin with [literal]#++update-theme++# and [literal]#++compile-theme++# goals.
  61. [source, xml]
  62. ----
  63. ...
  64. <plugin>
  65. <groupId>com.vaadin</groupId>
  66. <artifactId>vaadin-maven-plugin</artifactId>
  67. ...
  68. <executions>
  69. <execution>
  70. ...
  71. <goals>
  72. <goal>clean</goal>
  73. <goal>resources</goal>
  74. <goal>update-theme</goal>
  75. <goal>update-widgetset</goal>
  76. <goal>compile-theme</goal>
  77. <goal>compile</goal>
  78. </goals>
  79. </execution>
  80. </executions>
  81. ----
  82. Once these are in place, the theme is compiled as part of relevant lifecycle
  83. phases, such as [literal]#++package++#.
  84. [subs="normal"]
  85. ----
  86. [command]#mvn# [parameter]#package#
  87. ----
  88. You can also compile just the theme with the [package]#compile-theme# goal:
  89. [subs="normal"]
  90. ----
  91. [command]#mvn# [parameter]#vaadin:compile-theme#
  92. ----
  93. ifdef::web[]
  94. [[themes.compiling.command-line]]
  95. == Compiling in Command-line
  96. You can compile Sass style sheets to CSS either with the Vaadin Sass compiler or
  97. the standard one. The [filename]#styles.css# of a custom theme should be the
  98. compilation target. When compiled before deployment, the source files do not
  99. need to be in the theme folder.
  100. You can run the Vaadin Sass compiler in a theme folder as follows:
  101. [subs="normal"]
  102. ----
  103. [command]#java# [parameter]#-cp# [replaceable]#'../../../WEB-INF/lib/*'# com.vaadin.sass.SassCompiler styles.scss styles.css
  104. ----
  105. The [parameter]#-cp# parameter should point to the class path where the Vaadin
  106. Sass Compiler and theme JARs are located. In the above example, they are assumed
  107. to be located in the [filename]#WEB-INF/lib# folder of the web application. If
  108. you have loaded the Vaadin libraries using Ivy, as is the case with projects
  109. created with the Vaadin Plugin for Eclipse, the Vaadin libraries are stored in
  110. Ivy's local repository. Its folder hierarchy is somewhat scattered, so we
  111. recommend that you retrieve the libraries to a single folder. We recommend using
  112. an Ant script as is described next.
  113. endif::web[]
  114. [[themes.compiling.ant]]
  115. == Compiling with Ant
  116. With Apache Ant, you can easily resolve the dependencies with Ivy and compile
  117. the theme with the Theme Compiler included in Vaadin as follows. This build step
  118. can be conveniently included in a WAR build script.
  119. Start with the following configuration:
  120. [source, xml]
  121. ----
  122. <project xmlns:ivy="antlib:org.apache.ivy.ant"
  123. name="My Project" basedir="../"
  124. default="package-war">
  125. <target name="configure">
  126. <!-- Where project source files are located -->
  127. <property name="src-location" value="src" />
  128. ... other project build definitions ...
  129. <!-- Name of the theme -->
  130. <property name="theme" value="mytheme"/>
  131. <!-- Compilation result directory -->
  132. <property name="result" value="build/result"/>
  133. </target>
  134. <!-- Initialize build -->
  135. <target name="init" depends="configure">
  136. <!-- Construct and check classpath -->
  137. <path id="compile.classpath">
  138. <!-- Source code to be compiled -->
  139. <pathelement path="${src-location}" />
  140. <!-- Vaadin libraries and dependencies -->
  141. <fileset dir="${result}/lib">
  142. <include name="*.jar"/>
  143. </fileset>
  144. </path>
  145. <mkdir dir="${result}"/>
  146. </target>
  147. ----
  148. You should first resolve all Vaadin libraries to a single directory, which you
  149. can use for deployment, but also for theme compilation.
  150. ----
  151. <target name="resolve" depends="init">
  152. <ivy:retrieve
  153. pattern="${result}/lib/[module]-[type]-[artifact]-[revision].[ext]"/>
  154. </target>
  155. ----
  156. Then, you can compile the theme as follows:
  157. ----
  158. <!-- Compile theme -->
  159. <target name="compile-theme"
  160. depends="init, resolve">
  161. <delete dir="${result}/VAADIN/themes/${theme}"/>
  162. <mkdir dir="${result}/VAADIN/themes/${theme}"/>
  163. <java classname="com.vaadin.sass.SassCompiler"
  164. fork="true">
  165. <classpath>
  166. <path refid="compile.classpath"/>
  167. </classpath>
  168. <arg value="WebContent/VAADIN/themes/${theme}/styles.scss"/>
  169. <arg value="${result}/VAADIN/themes/${theme}/styles.css"/>
  170. </java>
  171. <!-- Copy theme resources -->
  172. <copy todir="${result}/VAADIN/themes/${theme}">
  173. <fileset dir="WebContent/VAADIN/themes/${theme}">
  174. <exclude name="**/*.scss"/>
  175. </fileset>
  176. </copy>
  177. </target>
  178. </project>
  179. ----