Transpilation
Lightning CSS includes support for transpiling modern CSS syntax to support older browsers, including vendor prefixing and syntax lowering.
Browser targets
By default Lightning CSS does not perform any transpilation of CSS syntax for older browsers. This means that if you write your code using modern syntax or without vendor prefixes, that’s what Lightning CSS will output. You can declare your app’s supported browsers using the targets option. When this is declared, Lightning CSS will transpile your code accordingly to ensure compatibility with your supported browsers.
Targets are defined using an object that specifies the minimum version of each browser you want to support. The easiest way to build a targets object is to use browserslist. This lets you use a query that automatically updates over time as new browser versions are released, market share changes, etc. The following example will return a targets object listing browsers with >= 0.25% market share.
import browserslist from 'browserslist';
import { transform, browserslistToTargets } from 'lightningcss';
// Call this once per build.
let targets = browserslistToTargets(browserslist('>= 0.25%'));
// Use `targets` for each file you transform.
let { code, map } = transform({
// ...
targets
});
For the best performance, you should call browserslist once for your whole build process, and reuse the same targets object when calling transform for each file.
Under the hood, targets are represented using an object that maps browser names to minimum versions. Version numbers are represented using a single 24-bit number, with one semver component (major, minor, patch) per byte. For example, this targets object would represent Safari 13.2.0.
let targets = {
safari: (13 << 16) | (2 << 8)
};
CLI
When using the CLI, targets can be provided by passing a browserslist query to the --targets option. Alternatively, if the --browserslist option is provided, then lightningcss finds browserslist configuration, selects queries by environment and loads the resulting queries as targets.
Configuration discovery and targets resolution is modeled after the original browserslist Node package. The configuration is resolved in the following order:
- If a
BROWSERSLISTenvironment variable is present, then load targets from its value. - If a
BROWSERSLIST_CONFIGenvironment variable is present, then load the browserslist configuration from the file at the provided path. - If none of the above apply, then find, parse and use targets from the first
browserslist,.browserslistrc, orpackage.jsonconfiguration file in any parent directory.
Browserslist configuration files may contain sections denoted by square brackets. Use these to specify different targets for different environments. Targets which are not placed in a section are added to defaults and used if no section matches the environment.
# Defaults, applied when no other section matches the provided environment.
firefox ESR
# Targets applied only to the staging environment.
[staging]
samsung >= 4
When using parsed configuration from browserslist, .browserslistrc, or package.json configuration files, the environment is determined by:
- the
BROWSERSLIST_ENVenvironment variable if present - the
NODE_ENVenvironment variable if present - otherwise
"production"is used.
If no targets are found for the resulting environment, then the defaults configuration section is used.
Feature flags
In most cases, setting the targets option and letting Lightning CSS automatically compile your CSS works great. However, in other cases you might need a little more control over exactly what features are compiled and which are not. That's where the include and exclude options come in.
The include and exclude options allow you to explicitly turn on or off certain features. These override the defaults based on the provided browser targets. For example, you might want to only compile colors, and handle auto prefixing or other features with another tool. Or you may want to handle everything except vendor prefixing with Lightning CSS. These options make that possible.
The include and exclude options are configured using the Features enum, which can be imported from lightningcss. You can bitwise OR multiple flags together to turn them on or off.
import { transform, Features } from 'lightningcss';
let { code, map } = transform({
// ...
targets,
// Always compile colors and CSS nesting, regardless of browser targets.
include: Features.Colors | Features.Nesting,
// Never add any vendor prefixes, regardless of targets.
exclude: Features.VendorPrefixes
});
Here is a full list of available flags, described in the sections below:
NestingNotSelectorListDirSelectorLangSelectorListIsSelectorTextDecorationThicknessPercentMediaIntervalSyntaxMediaRangeSyntaxCustomMediaQueriesClampFunctionColorFunctionOklabColorsLabColorsP3ColorsHexAlphaColorsSpaceSeparatedColorNotationLightDarkFontFamilySystemUiDoublePositionGradientsVendorPrefixesLogicalPropertiesSelectors– shorthand forNesting | NotSelectorList | DirSelector | LangSelectorList | IsSelectorMediaQueries– shorthand forMediaIntervalSyntax | MediaRangeSyntax | CustomMediaQueriesColors– shorthand forColorFunction | OklabColors | LabColors | P3Colors | HexAlphaColors | SpaceSeparatedColorNotation | LightDark
Vendor prefixing
Based on your configured browser targets, Lightning CSS automatically adds vendor prefixed fallbacks for many CSS features. For example, when using the image-set() function, Lightning CSS will output a fallback -webkit-image-set() value as well, since Chrome does not yet support the unprefixed value.
.logo {
background: image-set(url(logo.png) 2x, url(logo.png) 1x);
}
compiles to:
.logo {
background: -webkit-image-set(url(logo.png) 2x, url(logo.png) 1x);
background: image-set("logo.png" 2x, "logo.png"