Skip to content

Commit 0992701

Browse files
typescript
1 parent c38e710 commit 0992701

11 files changed

+3183
-143
lines changed

.eslintrc.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true,
5+
"node": true
6+
},
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/recommended",
10+
"prettier"
11+
],
12+
"overrides": [
13+
],
14+
"parser": "@typescript-eslint/parser",
15+
"parserOptions": {
16+
"ecmaVersion": "latest",
17+
"sourceType": "module"
18+
},
19+
"plugins": [
20+
"@typescript-eslint",
21+
"prettier"
22+
],
23+
"rules": {
24+
"prettier/prettier": 1,
25+
"@typescript-eslint/no-explicit-any": "off"
26+
}
27+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules/
22

33
.DS_Store
4+
5+
dist/

.prettierrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"arrowParens": "always",
5+
"bracketSameLine": false,
6+
"printWidth": 120,
7+
"tabWidth": 2,
8+
"semi": true
9+
}

index.js

-50
This file was deleted.

index.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import plugin from 'tailwindcss/plugin';
2+
3+
type Options = Record<string, any>;
4+
5+
module.exports = plugin.withOptions(function (options: Options = {}) {
6+
return function ({ matchUtilities, theme, config }) {
7+
const context = {
8+
theme,
9+
config,
10+
...(options ?? {}),
11+
};
12+
matchUtilities({
13+
js: (value) => {
14+
const escape = (str: string) => {
15+
console.log(`escaping \`${str}\``);
16+
return str.replace(/_/g, '\\_').replace(/ /g, '_');
17+
};
18+
19+
const unescape = (str: string) => {
20+
console.log(`unescaping \`${str}\``);
21+
str = str.replace(/(?<!\\)_/g, ' ');
22+
str = str.replace(/\\_/g, '_');
23+
return str;
24+
};
25+
26+
const parseString = (str: string) => {
27+
console.log(`parsing \`${str}\``);
28+
return str.split(/(#{.*?})/g).map((el, i) => (i % 2 === 1 ? el.slice(2, -1) : el));
29+
};
30+
31+
const parts = parseString(escape(value));
32+
33+
const utility = parts
34+
.map((part, i) => {
35+
if (i % 2 === 0) {
36+
return part;
37+
} else {
38+
const args = Object.keys(context);
39+
const values = Object.values(context);
40+
const func = new Function(...args, `return ${unescape(part)};`);
41+
return escape(`${func(...values)}`);
42+
}
43+
})
44+
.join('');
45+
46+
return {
47+
[`@apply ${utility}`]: {},
48+
};
49+
},
50+
});
51+
};
52+
});

0 commit comments

Comments
 (0)