Skip to content

Commit 278ff4b

Browse files
committed
Support both cjs and es modules
1 parent 10183a9 commit 278ff4b

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@
99
"postcss": "^8.4.20",
1010
"postcss-value-parser": "^4.2.0"
1111
},
12-
"version": "1.0.0",
13-
"main": "index.js",
12+
"version": "0.0.1",
13+
"main": "src/index.js",
14+
"exports": {
15+
".": {
16+
"import": "./src/index.js",
17+
"require": "./src/index.cjs",
18+
"default": "./src/index.cjs"
19+
},
20+
"./package.json": "./package.json"
21+
},
1422
"directories": {
1523
"test": "test"
1624
},
17-
"author": "",
25+
"author": "yuanchuan",
1826
"license": "MIT",
19-
"description": ""
27+
"description": "PostCSS plugin to use generators from css-doodle with the same syntax"
2028
}

src/index.cjs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const valueParser = require('postcss-value-parser');
2+
3+
const KEYWORDS = ['@svg', '@shape'];
4+
5+
module.exports = plugin;
6+
plugin.postcss = true;
7+
8+
function plugin() {
9+
return {
10+
postcssPlugin: "postcss-doodle",
11+
async Declaration(decl) {
12+
try {
13+
let parsed = valueParser(decl.value);
14+
parsed.nodes = await transformNodes(parsed.nodes);
15+
decl.value = parsed.toString();
16+
} catch (e) {
17+
// fail silently
18+
}
19+
}
20+
}
21+
}
22+
23+
async function transformNodes(nodes) {
24+
let { svg, shape} = await import('css-doodle/generator');
25+
return nodes.map(node => {
26+
if (node.type === 'function' && KEYWORDS.includes(node.value)) {
27+
const { result, args } = getInput(node.nodes);
28+
node.type = 'word';
29+
if (node.value === '@svg') {
30+
node.value = wrapSVG(svg(result));
31+
}
32+
if (node.value === '@shape') {
33+
node.value = shape(...args);
34+
}
35+
}
36+
return node;
37+
});
38+
}
39+
40+
function getInput(nodes) {
41+
let result = '', argResult = '';
42+
let args = [];
43+
for (let node of nodes) {
44+
let nested = (node.type === 'function' && Array.isArray(node.nodes));
45+
if (nested) {
46+
let value = `${node.value}(${getInput(node.nodes).result})`;
47+
result += value;
48+
argResult += value;
49+
} else {
50+
if (node.type === 'div' && node.value === ',') {
51+
args.push(argResult);
52+
argResult = '';
53+
} else {
54+
argResult += node.value;
55+
}
56+
result += node.value;
57+
}
58+
}
59+
if (argResult.length) {
60+
args.push(argResult);
61+
}
62+
return { result, args };
63+
}
64+
65+
function wrapSVG(input) {
66+
return `url(data:image/svg+xml;utf8,${ encodeURIComponent(input) })`;
67+
}
68+

index.js renamed to src/index.js

File renamed without changes.

test/compare.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert';
22
import postcss from 'postcss';
3-
import plugin from '../index.js';
3+
import plugin from '../src/index.js';
44

55
async function render(input) {
66
const { css } = await postcss()

0 commit comments

Comments
 (0)