PostCSS HTML Syntax
PostCSS syntax for parsing HTML (and HTML-like)
Getting Started
First thing's first, install the module:
npm install postcss-html --save-dev
If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install the corresponding module.
Use Cases
const postcss = require('postcss');
const syntax = require('postcss-html')({
scss: require('postcss-scss'),
less: require('postcss-less'),
css: require('postcss-safe-parser'),
});
postcss(plugins).process(source, { syntax: syntax }).then(function (result) {
result.content
});
If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install these module:
Advanced Use Cases
Options
const options = {
rules: [
{
test: /^postcss$/i,
lang: 'scss'
},
{
test: /^customcss$/i,
lang: 'custom'
},
],
css: 'postcss-safe-parser',
sass: require('postcss-sass'),
scss: 'postcss-scss',
less: require.resolve('./node_modules/postcss-less'),
sugarss: require('sugarss'),
custom: require('postcss-custom-syntax'),
}
const syntax = require('postcss-html')(options);
Turning PostCSS off from within your HTML
PostCSS can be temporarily turned off by using special comments in your HTML. For example:
<html>
<body>
<a style="color: red;" description="style is not parsed."></a>
<a style="color: red;" description="style is parsed."></a>
<html>
<body>
<a style="color: red;" description="style is not parsed."></a>
<a style="color: red;" description="style is not parsed."></a>
<a style="color: red;" description="style is parsed."></a>
Linting with Stylelint
The main use case of this plugin is to apply linting with Stylelint to <style>
tags and <div style="*">
property in HTML (and HTML-like).
You can use it by configuring your stylelint
config as follows:
{
"overrides": [
{
"files": ["*.html", "**/*.html"],
"customSyntax": "postcss-html"
}
]
}
You can use it more easily if you use an already configured sharable config.
{
+ "extends": [
+ "stylelint-config-html",
// If you are using Vue.
+ "stylelint-config-recommended-vue"
+ ],
- "overrides": [
- {
- "files": ["*.html", "**/*.html"],
- "customSyntax": "postcss-html"
- }
- ]
}