Skip to content

Commit

Permalink
refactor: to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Feb 26, 2024
1 parent f51ed5d commit 74b3363
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 2020
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
Expand Down
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ posthtml([ postcss(postcssPlugins, postcssOptions, filterType) ])
.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)).
If you don't pass any arguments to `posthtml-postcss`, it will try to 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.)

Expand Down
70 changes: 37 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,64 @@
const postcss = require('postcss');
const postcssrc = require('postcss-load-config');

module.exports = function (plugins, options, filterType) {
if (arguments.length === 0) {
const rc = postcssrc.sync();
plugins = rc.plugins;
options = rc.options;
import postcss from 'postcss'
import postcssConfig from 'postcss-load-config'

const plugin = (plugins = null, options = null, filterType = null) => {
if (plugins === null && options === null && filterType === null) {
const rc = postcssConfig.sync()
plugins = rc.plugins
options = rc.options
}

plugins = [].concat(plugins).filter(Boolean);
options = options || {};
plugins = [].concat(plugins).filter(Boolean)
options = options || {}

const css = postcss(plugins);
const css = postcss(plugins)

return function (tree) {
const promises = [];
const promises = []

tree.walk(node => {
let promise;
let promise

if (node.tag === 'style' && node.content) {
let meetsFilter = true;
let meetsFilter = true

if (filterType) {
const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : '';
const meetsTypeAttr = filterType.test(typeAttr);
const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '');
const meetsOtherType = !meetsStandardType && meetsTypeAttr;
meetsFilter = meetsStandardType || meetsOtherType;
const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : ''
const meetsTypeAttr = filterType.test(typeAttr)
const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '')
const meetsOtherType = !meetsStandardType && meetsTypeAttr
meetsFilter = meetsStandardType || meetsOtherType
}

if (meetsFilter) {
const styles = [].concat(node.content).join('');
const from = options.from || tree.options.from;
const styles = [].concat(node.content).join('')
const from = options.from || tree.options.from

promise = css.process(styles, {...options, from})
.then(result => {
node.content = [result.css];
});
node.content = [result.css]
})

promises.push(promise);
promises.push(promise)
}
}

if (node.attrs && node.attrs.style) {
promise = css.process(node.attrs.style, options)
.then(result => {
node.attrs.style = result.css;
});
node.attrs.style = result.css
})

promises.push(promise);
promises.push(promise)
}

return node;
});
return node
})

return Promise.all(promises).then(() => {
return tree;
});
};
};
return tree
})
}
}

export default plugin
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"author": "Ivan Voischev <voischev.ivan@ya.ru>",
"bugs": "https://github.com/posthtml/posthtml-postcss/issues",
"homepage": "https://github.com/posthtml/posthtml-postcss",
"main": "lib/index.js",
"type": "module",
"exports": "./lib/index.js",
"engines": {
"node": ">=16"
"node": ">=18"
},
"scripts": {
"dev": "vitest",
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {defineConfig} from 'vitest/config'

export default defineConfig({
test: {
include: ['**/*test.{js,ts}'],
},
})
7 changes: 0 additions & 7 deletions vitest.config.mts

This file was deleted.

0 comments on commit 74b3363

Please sign in to comment.