postcss-html

PostCSS syntax for parsing HTML (and HTML-like)

MIT 60 个版本
安装
npm install postcss-html
yarn add postcss-html
pnpm add postcss-html
bun add postcss-html
README

PostCSS HTML Syntax

NPM license NPM version NPM downloads NPM downloads NPM downloads Build Status

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')({
    // syntax for parse scss (non-required options)
    scss: require('postcss-scss'),
    // syntax for parse less (non-required options)
    less: require('postcss-less'),
    // syntax for parse css blocks (non-required options)
    css: require('postcss-safe-parser'),
});
postcss(plugins).process(source, { syntax: syntax }).then(function (result) {
    // An alias for the result.css property. Use it with syntaxes that generate non-CSS output.
    result.content
});

If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install these module:

Advanced Use Cases

Options

const options = {
    rules: [
        {
            // custom language
            test: /^postcss$/i,
            lang: 'scss'
        },
        {
            // custom language
            test: /^customcss$/i,
            lang: 'custom'
        },
    ],

    // custom parser for CSS (using `postcss-safe-parser`)
    css: 'postcss-safe-parser',
    // custom parser for SASS (PostCSS-compatible syntax.)
    sass: require('postcss-sass'),
    // custom parser for SCSS (by module name)
    scss: 'postcss-scss',
    // custom parser for LESS (by module path)
    less: require.resolve('./node_modules/postcss-less'),
    // custom parser for SugarSS
    sugarss: require('sugarss'),
    // custom parser for custom language
    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>
<!-- postcss-ignore -->
<a style="color: red;" description="style is not parsed."></a>

<a style="color: red;" description="style is parsed."></a>
<html>
<body>
<!-- postcss-disable -->
<a style="color: red;" description="style is not parsed."></a>
<a style="color: red;" description="style is not parsed."></a>
<!-- postcss-enable -->

<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"
-        }
-    ]
}
版本列表
1.8.1 2026-01-13
1.8.0 2025-01-14
1.7.0 2024-05-08
1.6.0 2024-01-12
1.5.0 2022-07-07
1.4.1 2022-04-15
1.4.0 2022-04-15
1.3.1 2022-04-13
1.3.0 2021-11-24
1.2.0 2021-10-29
1.1.1 2021-10-26
1.1.0 2021-10-24
1.0.2 2021-10-24
1.0.1 2021-10-22
1.0.0-alpha.4 2021-10-21
1.0.0-alpha.3 2021-10-19
1.0.0-alpha.2 2021-10-16
1.0.0 2021-10-22
0.36.0 2019-01-02
0.34.0 2018-09-18
0.33.0 2018-08-15
0.32.0 2018-07-27
0.31.0 2018-06-29
0.30.0 2018-06-20
0.28.0 2018-06-11
0.27.0 2018-06-07
0.26.0 2018-05-28
0.25.0 2018-05-25
0.24.0 2018-05-24
0.23.7 2018-05-18
0.23.6 2018-05-08
0.23.5 2018-05-08
0.23.4 2018-05-07
0.23.3 2018-05-04
0.23.2 2018-05-04
0.23.1 2018-05-04
0.23.0 2018-05-03
0.22.0 2018-04-28
0.21.0 2018-04-13
0.20.1 2018-04-09
0.20.0 2018-04-09
0.19.0 2018-04-03
0.18.0 2018-03-28
0.17.0 2018-03-23
0.16.0 2018-03-23
0.15.0 2018-03-14
0.14.0 2018-03-09
0.13.0 2018-03-07
0.12.0 2017-12-13
0.11.0 2017-11-10
0.10.0 2017-10-19
0.9.0 2017-07-25
0.8.0 2017-07-12
0.7.0 2017-06-22
0.6.0 2017-06-08
0.5.0 2017-06-02
0.4.0 2017-05-31
0.3.0 2017-05-27
0.2.0 2017-05-26
0.1.0 2017-05-23