From 19e2473fe1ff9a878a0e06587279f1fa9216ecf2 Mon Sep 17 00:00:00 2001 From: LeoniePhiline <22329650+LeoniePhiline@users.noreply.github.com> Date: Sat, 29 Oct 2022 02:52:23 +0200 Subject: [PATCH 1/3] feat(cli): Load browserslist configuration if no `--targets` supplied. --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 ++- src/targets.rs | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a9ad502c..2d80c0b8 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,46 @@ To see all of the available options, use the `--help` argument: npx lightningcss --help ``` +#### Browserslist configuration + +If no `--targets` option is provided, then `lightningcss` finds browserslist configuration, +selects queries by environment and loads the resulting queries as targets. + +Configuration resolution is modeled after the original `browserslist` nodeJS package. +The configuration is resolved in the following order: + +- If a `BROWSERSLIST` environment variable is present, then load targets from its value. This is analog to the `--targets` CLI option. + _Example:_ `BROWSERSLIST="firefox ESR" lightningcss [OPTIONS] ` +- If a `BROWSERSLIST_CONFIG` environment variable is present, then resolve the file at the provided path. + Then parse and use targets from `package.json` or any browserslist configuration file pointed to by the environment variable. + _Example:_ `BROWSERSLIST_CONFIG="../config/browserslist" lightningcss [OPTIONS] ` +- If none of the above apply, then find, parse and use targets from the first `browserslist`, `package.json` + or `.browserslistrc` configuration file in any parent directory. + +Browserslist configuration files may contain sections denoted by angular 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 applies matches the environment. + +_Example:_ + +``` +# Defaults, applied when no other section matches the provided environment. +firefox ESR + +[staging] +# Targets applied only to the staging environment. +samsung >= 4 +``` + +When using parsed configuration from `browserslist`, `package.json` or `.browserslistrc` configuration files, +the environment determined by + +- the `BROWSERSLIST_ENV` environment variable if present, +- otherwise the `NODE_ENV` environment variable if present, +- otherwise `production` is used. + +If no targets are found for the resulting environment, then the `defaults` configuration section is used. + ### Error recovery By default, Lightning CSS is strict, and will error when parsing an invalid rule or declaration. However, sometimes you may encounter a third party library that you can't easily modify, which unintentionally contains invalid syntax, or IE-specific hacks. In these cases, you can enable the `errorRecovery` option (or `--error-recovery` CLI flag). This will skip over invalid rules and declarations, omitting them in the output, and producing a warning instead of an error. You should also open an issue or PR to fix the issue in the library if possible. diff --git a/src/main.rs b/src/main.rs index e62f588c..fdabb0bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,10 +125,11 @@ pub fn main() -> Result<(), std::io::Error> { }; let targets = if cli_args.targets.is_empty() { - None + Browsers::load_browserslist().unwrap() } else { Browsers::from_browserslist(cli_args.targets).unwrap() }; + stylesheet .minify(MinifyOptions { targets, diff --git a/src/targets.rs b/src/targets.rs index cddfc1ad..26c6649c 100644 --- a/src/targets.rs +++ b/src/targets.rs @@ -42,11 +42,40 @@ impl Browsers { ) -> Result