Install
npm i -D posthtml-postcss
Usage
import {dirname} from 'node:path'
import {readFileSync} from 'node:fs'
import {fileURLToPath} from 'node:url'
import posthtml from 'posthtml'
import postcss from 'posthtml-postcss'
const postcssPlugins = []
const postcssOptions = {}
const filterType = /^text\/css$/
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const filePath = `${__dirname}/index.html`
const html = readFileSync(filePath, 'utf8')
posthtml([
postcss(postcssPlugins, postcssOptions, filterType)
])
.process(html, {from: filePath})
.then((result) => console.log(result.html))
If you don't pass any arguments to posthtml-postcss
, it will try to use your project's PostCSS configuration (see postcss-load-config
).
Notice that we're setting the option from
when calling process
. posthtml-postcss
forwards this to PostCSS, which is useful for syntax error messages. (postcss-cli
and gulp-posthtml
are setting from
automatically.)
Example
import posthtml from 'posthtml'
import postcss from 'posthtml-postcss'
import autoprefixer from 'autoprefixer'
const postcssPlugins = [
autoprefixer({ browsers: ['last 2 versions'] })
]
const postcssOptions = {}
const filterType = /^text\/css$/
const html = `
<style>div { display: flex; }</style>
<div style="display: flex;">Text</div>
`
posthtml([
postcss(postcssPlugins, postcssOptions, filterType)
])
.process(html)
.then(result => console.log(result.html))
Output:
<style>
div { display: -webkit-flex;display: -ms-flexbox;display: flex; }
</style>
<div style="display: -webkit-flex;display: -ms-flexbox;display: flex;">
Text
</div>
<small>1.0.3 (2024-12-16)</small>
- 1.0.3 (cf4233b)
- build(deps-dev): bump @biomejs/biome from 1.8.3 to 1.9.3 (3ffc705)
- build(deps-dev): bump @vitest/coverage-v8 from 2.0.2 to 2.1.8 (480fb5e)
- build(deps-dev): bump autoprefixer from 10.4.19 to 10.4.20 (f202601)
- build(deps): bump nanoid from 3.3.7 to 3.3.8 (8bcb0bc)
- build(deps): bump postcss from 8.4.39 to 8.4.47 (2689a04)
- build(deps): bump vite from 5.3.3 to 5.4.11 (405e6eb)
- chore: update changelog.md (1e86313)