Class CssBundler

java.lang.Object
com.vaadin.flow.server.frontend.CssBundler

public class CssBundler extends Object
Utility methods to handle application theme CSS content.

For internal use only. May be renamed or removed in a future release.

  • Constructor Details

    • CssBundler

      public CssBundler()
  • Method Details

    • inlineImportsForThemes

      public static String inlineImportsForThemes(File themeFolder, File cssFile, tools.jackson.databind.JsonNode themeJson) throws IOException
      Recurse over CSS import and inlines all ot them into a single CSS block in themes folder under src/main/frontend/themes/<theme-name>.

      Unresolvable imports are put on the top of the resulting code, because @import statements must come before any other CSS instruction, otherwise the import is ignored by the browser.

      Along with import resolution and code inline, URLs (url('image.png') referencing theme resources or assets are rewritten to be correctly served by Vaadin at runtime.

      Parameters:
      themeFolder - location of theme folder on the filesystem.
      cssFile - the CSS file to process.
      themeJson - the theme configuration, usually stored in theme.json file.
      Returns:
      the processed stylesheet content, with inlined imports and rewritten URLs.
      Throws:
      IOException - if filesystem resources can not be read.
    • inlineImports

      public static String inlineImports(File themeFolder, File cssFile, tools.jackson.databind.JsonNode themeJson, File nodeModulesFolder) throws IOException
      Recurse over CSS import and inlines all of them into a single CSS block.

      Unresolvable imports are put on the top of the resulting code, because @import statements must come before any other CSS instruction, otherwise the import is ignored by the browser.

      This overload supports resolving imports from node_modules in addition to relative paths.

      Parameters:
      themeFolder - location of theme folder on the filesystem. May be null if not processing theme files.
      cssFile - the CSS file to process.
      themeJson - the theme configuration, usually stored in theme.json file. May be null.
      nodeModulesFolder - the node_modules folder for resolving npm package imports. May be null if node_modules resolution is not needed.
      Returns:
      the processed stylesheet content, with inlined imports and rewritten URLs.
      Throws:
      IOException - if filesystem resources can not be read.
    • inlineImportsForPublicResources

      public static String inlineImportsForPublicResources(File baseFolder, File cssFile, String contextPath) throws IOException
      Inlines imports for CSS files located under public static resources (e.g. META-INF/resources).
      Parameters:
      baseFolder - base folder the imports and url() references are relative to
      cssFile - the CSS file to process
      contextPath - that url() are rewritten to and rebased onto
      Returns:
      the processed stylesheet content with inlined imports only
      Throws:
      IOException - if filesystem resources cannot be read
    • minifyCss

      public static String minifyCss(String css)
      Minify CSS content by removing comments and unnecessary whitespace.

      This method performs basic CSS minification:

      • Remove CSS comments
      • Collapse multiple whitespace characters
      • Remove whitespace around special characters like braces and colons
      • Remove trailing semicolons before closing braces
      Parameters:
      css - the CSS content to minify
      Returns:
      the minified CSS content