Skip to content

Add TypeScript support #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"overrides": [
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"prettier"
],
"rules": {
"prettier/prettier": 1,
"@typescript-eslint/no-explicit-any": "off"
}
}
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "always",
"bracketSameLine": false,
"printWidth": 120,
"tabWidth": 2,
"semi": true
}
1 change: 1 addition & 0 deletions dist/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
50 changes: 50 additions & 0 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const plugin_1 = __importDefault(require("tailwindcss/plugin"));
module.exports = plugin_1.default.withOptions(function (options = {}) {
return function ({ matchUtilities, theme, config }) {
const context = {
theme,
config,
...(options !== null && options !== void 0 ? options : {}),
};
matchUtilities({
js: (value) => {
const escape = (str) => {
console.log(`escaping \`${str}\``);
return str.replace(/_/g, '\\_').replace(/ /g, '_');
};
const unescape = (str) => {
console.log(`unescaping \`${str}\``);
str = str.replace(/(?<!\\)_/g, ' ');
str = str.replace(/\\_/g, '_');
return str;
};
const parseString = (str) => {
console.log(`parsing \`${str}\``);
return str.split(/(#{.*?})/g).map((el, i) => (i % 2 === 1 ? el.slice(2, -1) : el));
};
const parts = parseString(escape(value));
const utility = parts
.map((part, i) => {
if (i % 2 === 0) {
return part;
}
else {
const args = Object.keys(context);
const values = Object.values(context);
const func = new Function(...args, `return ${unescape(part)};`);
return escape(`${func(...values)}`);
}
})
.join('');
return {
[`@apply ${utility}`]: {},
};
},
});
};
});
1 change: 1 addition & 0 deletions dist/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
45 changes: 45 additions & 0 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import plugin from 'tailwindcss/plugin';
module.exports = plugin.withOptions(function (options = {}) {
return function ({ matchUtilities, theme, config }) {
const context = {
theme,
config,
...(options !== null && options !== void 0 ? options : {}),
};
matchUtilities({
js: (value) => {
const escape = (str) => {
console.log(`escaping \`${str}\``);
return str.replace(/_/g, '\\_').replace(/ /g, '_');
};
const unescape = (str) => {
console.log(`unescaping \`${str}\``);
str = str.replace(/(?<!\\)_/g, ' ');
str = str.replace(/\\_/g, '_');
return str;
};
const parseString = (str) => {
console.log(`parsing \`${str}\``);
return str.split(/(#{.*?})/g).map((el, i) => (i % 2 === 1 ? el.slice(2, -1) : el));
};
const parts = parseString(escape(value));
const utility = parts
.map((part, i) => {
if (i % 2 === 0) {
return part;
}
else {
const args = Object.keys(context);
const values = Object.values(context);
const func = new Function(...args, `return ${unescape(part)};`);
return escape(`${func(...values)}`);
}
})
.join('');
return {
[`@apply ${utility}`]: {},
};
},
});
};
});
1 change: 1 addition & 0 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
50 changes: 0 additions & 50 deletions index.js

This file was deleted.

52 changes: 52 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import plugin from 'tailwindcss/plugin';

type Options = Record<string, any>;

module.exports = plugin.withOptions(function (options: Options = {}) {
return function ({ matchUtilities, theme, config }) {
const context = {
theme,
config,
...(options ?? {}),
};
matchUtilities({
js: (value) => {
const escape = (str: string) => {
console.log(`escaping \`${str}\``);
return str.replace(/_/g, '\\_').replace(/ /g, '_');
};

const unescape = (str: string) => {
console.log(`unescaping \`${str}\``);
str = str.replace(/(?<!\\)_/g, ' ');
str = str.replace(/\\_/g, '_');
return str;
};

const parseString = (str: string) => {
console.log(`parsing \`${str}\``);
return str.split(/(#{.*?})/g).map((el, i) => (i % 2 === 1 ? el.slice(2, -1) : el));
};

const parts = parseString(escape(value));

const utility = parts
.map((part, i) => {
if (i % 2 === 0) {
return part;
} else {
const args = Object.keys(context);
const values = Object.values(context);
const func = new Function(...args, `return ${unescape(part)};`);
return escape(`${func(...values)}`);
}
})
.join('');

return {
[`@apply ${utility}`]: {},
};
},
});
};
});
Loading