Skip to main content
Version: 1.0.x

stylelint

Configures Stylelint to run as a Bazel aspect

First, all CSS sources must be the srcs of some Bazel rule. You can use a filegroup with lint-with-stylelint in the tags:

filegroup(
name = "css",
srcs = glob(["*.css"]),
tags = ["lint-with-stylelint"],
)

See the filegroup_tags and rule_kinds attributes below to customize this behavior.

Usage

Add stylelint as a devDependency in your package.json, and declare a binary target for Bazel to execute it.

For example in tools/lint/BUILD.bazel:

load("@npm//:stylelint/package_json.bzl", stylelint_bin = "bin")
stylelint_bin.stylelint_binary(name = "stylelint")

Then declare the linter aspect, typically in tools/lint/linters.bzl:

load("@aspect_rules_lint//lint:stylelint.bzl", "lint_stylelint_aspect")
stylelint = lint_stylelint_aspect(
binary = "@@//tools/lint:stylelint",
config = "@@//:stylelintrc",
)

Finally, register the aspect with your linting workflow, such as in .aspect/cli/config.yaml for aspect lint.

Macros and Functions

stylelint_action

Spawn stylelint as a Bazel action

Example usage (generated):

load("@aspect_rules_lint//lint:stylelint.bzl", "stylelint_action")

stylelint_action(
# an action context OR aspect context
ctx = None,
# struct with an _stylelint field
executable = None,
# list of file objects to lint
srcs = [],
# output file containing the stderr or --output-file of stylelint
stderr = None,
)

ctx

Required.

an action context OR aspect context

executable

Required.

struct with an _stylelint field

srcs

Required.

list of file objects to lint

stderr

Required.

output file containing the stderr or --output-file of stylelint

exit_code

Optional. Default: None

output file containing the exit code of stylelint. If None, then fail the build when stylelint exits non-zero. Exit codes may be: 1 - fatal error 2 - lint problem 64 - invalid CLI usage 78 - invalid configuration file

env

Optional. Default: {}

environment variables for stylelint

options

Optional. Default: []

additional command-line arguments

stylelint_fix

Create a Bazel Action that spawns stylelint with --fix.

Example usage (generated):

load("@aspect_rules_lint//lint:stylelint.bzl", "stylelint_fix")

stylelint_fix(
# an action context OR aspect context
ctx = None,
# struct with a _stylelint field
executable = None,
# list of file objects to lint
srcs = [],
# output file containing the applied fixes that can be applied with the patch(1) command.
patch = None,
# output file containing the stderr or --output-file of stylelint
stderr = None,
# output file containing the exit code of stylelint
exit_code = None,
)

ctx

Required.

an action context OR aspect context

executable

Required.

struct with a _stylelint field

srcs

Required.

list of file objects to lint

patch

Required.

output file containing the applied fixes that can be applied with the patch(1) command.

stderr

Required.

output file containing the stderr or --output-file of stylelint

exit_code

Required.

output file containing the exit code of stylelint

env

Optional. Default: {}

environment variables for stylelint

options

Optional. Default: []

additional command line options

lint_stylelint_aspect

A factory function to create a linter aspect.

Example usage (generated):

load("@aspect_rules_lint//lint:stylelint.bzl", "lint_stylelint_aspect")

lint_stylelint_aspect(
# the stylelint binary, typically a rule like
binary = None,
# label(s) of the stylelint config file
config = None,
)

binary

Required.

the stylelint binary, typically a rule like

load("@npm//:stylelint/package_json.bzl", stylelint_bin = "bin")
stylelint_bin.stylelint_binary(name = "stylelint")

config

Required.

label(s) of the stylelint config file

rule_kinds

Optional. Default: ["css_library"]

which kinds of rules should be visited by the aspect

filegroup_tags

Optional. Default: ["lint-with-stylelint"]

which tags on a filegroup indicate that it should be visited by the aspect