Skip to content

Commit

Permalink
Pass "from" option to PostCSS
Browse files Browse the repository at this point in the history
This results in better syntax error messages.
  • Loading branch information
silvenon committed Sep 27, 2020
1 parent 8325e51 commit 03d9a5d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -31,10 +31,14 @@ const filterType = /^text\/css$/
const html = readFileSync('./index.html', 'utf8')

posthtml([ postcss(postcssPlugins, postcssOptions, filterType) ])
.process(html)
.process(html, { from: `${__dirname}/index.html` })
.then((result) => console.log(result.html))
```

If you don't pass arguments to `posthtml-postcss`, it will use your project's PostCSS configuration (see [`postcss-load-config`](https://www.npmjs.com/package/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.)

<h2 align="center">Example</h2>

```js
Expand Down
14 changes: 13 additions & 1 deletion index.js
@@ -1,6 +1,17 @@
var postcss = require('postcss')
var postcssrc = require('postcss-load-config')

module.exports = function (plugins, options, filterType) {
if (Array.from(arguments).length === 0) {
try {
var rc = postcssrc.sync()
plugins = rc.plugins
options = rc.options
} catch (err) {
// noop
}
}

plugins = [].concat(plugins).filter(plugin => typeof plugin === 'function')
options = options || {}

Expand All @@ -24,7 +35,8 @@ module.exports = function (plugins, options, filterType) {

if (meetsFilter) {
var styles = [].concat(node.content).join('')
promise = css.process(styles, options)
var from = options.from || tree.options.from
promise = css.process(styles, Object.assign({}, options, { from: from }))
.then(function (result) {
node.content = [result.css]
})
Expand Down
123 changes: 117 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -3,7 +3,9 @@
"version": "0.3.0",
"description": "PostHTML-PostCSS plugin. Use PostCSS in HTML document.",
"main": "index.js",
"files": ["index.js"],
"files": [
"index.js"
],
"scripts": {
"test": "npm run lint && npm run coverage",
"lint": "eslint {test/*,*}.js",
Expand Down Expand Up @@ -50,7 +52,8 @@
"posthtml": "^0.12.0"
},
"dependencies": {
"postcss": "^7.0.18"
"postcss": "^7.0.18",
"postcss-load-config": "^2.1.2"
},
"eslintConfig": {
"env": {
Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Expand Up @@ -126,4 +126,8 @@ describe('use postcss', function () {

test(html, expected, {}, null, [plugin], done)
})

it('doesn\'t crash when no PostCSS configuration is found', function () {
expect(function () { posthtml([css()]) }).to.not.throw(Error)
})
})

0 comments on commit 03d9a5d

Please sign in to comment.