Skip to content

Commit 74b3363

Browse files
committed
refactor: to esm
1 parent f51ed5d commit 74b3363

File tree

8 files changed

+50
-53
lines changed

8 files changed

+50
-53
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"parserOptions": {
3-
"ecmaVersion": 2020
3+
"ecmaVersion": 2020,
4+
"sourceType": "module"
45
},
56
"rules": {
67
"indent": [2, 2, {"SwitchCase": 1}],

.npmignore

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ posthtml([ postcss(postcssPlugins, postcssOptions, filterType) ])
3535
.then((result) => console.log(result.html))
3636
```
3737

38-
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)).
38+
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)).
3939

4040
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.)
4141

lib/index.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
1-
const postcss = require('postcss');
2-
const postcssrc = require('postcss-load-config');
3-
4-
module.exports = function (plugins, options, filterType) {
5-
if (arguments.length === 0) {
6-
const rc = postcssrc.sync();
7-
plugins = rc.plugins;
8-
options = rc.options;
1+
import postcss from 'postcss'
2+
import postcssConfig from 'postcss-load-config'
3+
4+
const plugin = (plugins = null, options = null, filterType = null) => {
5+
if (plugins === null && options === null && filterType === null) {
6+
const rc = postcssConfig.sync()
7+
plugins = rc.plugins
8+
options = rc.options
99
}
1010

11-
plugins = [].concat(plugins).filter(Boolean);
12-
options = options || {};
11+
plugins = [].concat(plugins).filter(Boolean)
12+
options = options || {}
1313

14-
const css = postcss(plugins);
14+
const css = postcss(plugins)
1515

1616
return function (tree) {
17-
const promises = [];
17+
const promises = []
1818

1919
tree.walk(node => {
20-
let promise;
20+
let promise
2121

2222
if (node.tag === 'style' && node.content) {
23-
let meetsFilter = true;
23+
let meetsFilter = true
24+
2425
if (filterType) {
25-
const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : '';
26-
const meetsTypeAttr = filterType.test(typeAttr);
27-
const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '');
28-
const meetsOtherType = !meetsStandardType && meetsTypeAttr;
29-
meetsFilter = meetsStandardType || meetsOtherType;
26+
const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : ''
27+
const meetsTypeAttr = filterType.test(typeAttr)
28+
const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '')
29+
const meetsOtherType = !meetsStandardType && meetsTypeAttr
30+
meetsFilter = meetsStandardType || meetsOtherType
3031
}
3132

3233
if (meetsFilter) {
33-
const styles = [].concat(node.content).join('');
34-
const from = options.from || tree.options.from;
34+
const styles = [].concat(node.content).join('')
35+
const from = options.from || tree.options.from
36+
3537
promise = css.process(styles, {...options, from})
3638
.then(result => {
37-
node.content = [result.css];
38-
});
39+
node.content = [result.css]
40+
})
3941

40-
promises.push(promise);
42+
promises.push(promise)
4143
}
4244
}
4345

4446
if (node.attrs && node.attrs.style) {
4547
promise = css.process(node.attrs.style, options)
4648
.then(result => {
47-
node.attrs.style = result.css;
48-
});
49+
node.attrs.style = result.css
50+
})
4951

50-
promises.push(promise);
52+
promises.push(promise)
5153
}
5254

53-
return node;
54-
});
55+
return node
56+
})
5557

5658
return Promise.all(promises).then(() => {
57-
return tree;
58-
});
59-
};
60-
};
59+
return tree
60+
})
61+
}
62+
}
63+
64+
export default plugin

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"author": "Ivan Voischev <voischev.ivan@ya.ru>",
88
"bugs": "https://github.com/posthtml/posthtml-postcss/issues",
99
"homepage": "https://github.com/posthtml/posthtml-postcss",
10-
"main": "lib/index.js",
10+
"type": "module",
11+
"exports": "./lib/index.js",
1112
"engines": {
12-
"node": ">=16"
13+
"node": ">=18"
1314
},
1415
"scripts": {
1516
"dev": "vitest",
File renamed without changes.

vitest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {defineConfig} from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['**/*test.{js,ts}'],
6+
},
7+
})

vitest.config.mts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)