Class CssBundler
For internal use only. May be renamed or removed in a future release.
- Since:
- 25.1
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringCreates the message that explains why the given value is not a valid CSS cascade layer name.static StringcreateLayerImport(String url, String layer) Creates a CSS@importrule that loads the style sheet at the given URL into the given cascade layer.static StringinlineImports(File themeFolder, File cssFile, tools.jackson.databind.JsonNode themeJson, File nodeModulesFolder) Recurse over CSS import and inlines all of them into a single CSS block.static StringinlineImportsForPublicResources(File baseFolder, File cssFile, String contextPath) Inlines imports for CSS files located under public static resources (e.g.static StringinlineImportsForStaticResourcesRelative(File baseFolder, File cssFile, File nodeModulesFolder) Inlines imports for CSS files located under public static resources (e.g.static StringinlineImportsForThemes(File themeFolder, File cssFile, tools.jackson.databind.JsonNode themeJson) Recurse over CSS import and inlines all ot them into a single CSS block in themes folder undersrc/main/frontend/themes/<theme-name>.static booleanisValidLayerName(String layer) Returns whether the given value is a valid CSS cascade layer name: one or more CSS identifiers separated by dots, such asthemeortheme.base.static StringMinify CSS content by removing comments and unnecessary whitespace.static voidvalidateLayerName(String layer) Checks that the given value is a valid CSS cascade layer name: one or more CSS identifiers separated by dots, such asthemeortheme.base.static StringwrapInLayer(String css, String layer) Wraps the given style sheet content into a@layerblock so that it applies in the given cascade layer.
-
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 undersrc/main/frontend/themes/<theme-name>.Unresolvable imports are put on the top of the resulting code, because
@importstatements 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
@importstatements 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 tocssFile- the CSS file to processcontextPath- 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
-
inlineImportsForStaticResourcesRelative
public static String inlineImportsForStaticResourcesRelative(File baseFolder, File cssFile, File nodeModulesFolder) throws IOException Inlines imports for CSS files located under public static resources (e.g. META-INF/resources) at build time, rewriting relativeurl(...)references so they are expressed relative to the entry CSS file's folder.Unlike
inlineImportsForPublicResources(File, File, String), this variant does not prepend a servlet context path. The resulting URLs are purely relative, which is required when the CSS is processed at build time and the deployment context path is not yet known.- Parameters:
baseFolder- folder of the entry CSS file; inlinedurl(...)references are rewritten to paths relative to this folder.cssFile- the CSS file to process.nodeModulesFolder- the node_modules folder for resolving npm package imports, ornullif node_modules resolution is not needed.- Returns:
- the processed stylesheet content, with inlined imports and rewritten URLs.
- Throws:
IOException- if filesystem resources cannot be read.- Since:
- 25.1.4
-
minifyCss
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
-
isValidLayerName
Returns whether the given value is a valid CSS cascade layer name: one or more CSS identifiers separated by dots, such asthemeortheme.base.- Parameters:
layer- the layer name to check, notnull- Returns:
trueif the value is a valid layer name
-
validateLayerName
Checks that the given value is a valid CSS cascade layer name: one or more CSS identifiers separated by dots, such asthemeortheme.base.- Parameters:
layer- the layer name to check, notnull- Throws:
IllegalArgumentException- if the value is not a valid layer name
-
createInvalidLayerNameMessage
Creates the message that explains why the given value is not a valid CSS cascade layer name.- Parameters:
layer- the invalid layer name- Returns:
- the error message
-
createLayerImport
Creates a CSS@importrule that loads the style sheet at the given URL into the given cascade layer.A
<link rel="stylesheet">cannot put a style sheet into a layer, so a layered style sheet is loaded through this rule in a<style>element instead.- Parameters:
url- the URL of the style sheet, notnulllayer- a valid cascade layer name, seevalidateLayerName(String)- Returns:
- the
@importrule
-
wrapInLayer
Wraps the given style sheet content into a@layerblock so that it applies in the given cascade layer.The content must not contain
@importrules, as those are not allowed inside a block and a browser would silently drop them.- Parameters:
css- the style sheet content, notnulllayer- a valid cascade layer name, seevalidateLayerName(String)- Returns:
- the content wrapped into a
@layerblock - Throws:
IllegalArgumentException- if the layer name is not valid or the content contains an@importrule
-