Accept syntax object in customSyntax
option
#4839
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This overloads the
customSyntax
option to also accept a syntax object. The option also maintains its previous behaviour of accepting a string. This should be a fully backwards compatible change.The signature of this option has changed from
string
tostring | { parse: Function }
. This follows the convention of theformatter
option, which has similar behaviour.#3935.
This is based on a section of #4796. This is the only part of that PR that changes existing functionality.
The change itself is fairly straightforward, but it's probably worth detailing some of the why.
Why?
This API will be used to load non-standard CSS syntaxes (
scss
,less
,css-in-js
etc) when running in the browser.What is a syntax?
A syntax is typically an object containing two functions,
parse
andstringify
. Stylelint uses a syntax to transform your CSS-like code into a structure that can be linted, and then back again.Why do you need a custom syntax?
stylelint ships with syntaxes for linting the various flavours of CSS-like code that people use. These syntaxes won't be included in the default browser bundle as they are quite large - several MB for all of stylelint's included syntaxes.
What's wrong with using a string, anyway?
The
customSyntax
option uses Node.JS'srequire
function to load the syntax. It turns a string likemy-excellent-syntax
into a 'live' JS module.require
is not available in the browser.Allowing the
customSyntax
option to accept a syntax object means the host environment (whether a browser, Node.JS or something else) can deal with how to load the syntax, and then pass the object in for stylelint to use.