Urara-Blog/node_modules/.pnpm-store/v3/files/47/51ea96ee9dc6c43bdc035e9edc89c1f19c9739eaf0dbee5521766f2586a5140657867de0079ad6e30a2337d945e2ff22a06c79683a122a7bc35f84f031582e
2022-08-14 01:14:53 +08:00

59 lines
1.9 KiB
Text

import { toArray } from '@unocss/core';
function createFilter(include, exclude) {
const includePattern = toArray(include || []);
const excludePattern = toArray(exclude || []);
return (id) => {
if (excludePattern.some((p) => id.match(p)))
return false;
return includePattern.some((p) => id.match(p));
};
}
const elementRE = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/gs;
const attributeRE = /([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g;
function transformerAttributifyJsx(options = {}) {
const {
blocklist = []
} = options;
const isBlocked = (matchedRule) => {
for (const blockedRule of blocklist) {
if (blockedRule instanceof RegExp) {
if (blockedRule.test(matchedRule))
return true;
} else if (matchedRule === blockedRule) {
return true;
}
}
return false;
};
const idFilter = createFilter(
options.include || [/\.[jt]sx$/, /\.mdx$/],
options.exclude || []
);
return {
name: "transformer-jsx",
enforce: "pre",
idFilter,
async transform(code, _, { uno }) {
const tasks = [];
for (const item of Array.from(code.original.matchAll(elementRE))) {
for (const attr of item[3].matchAll(attributeRE)) {
const matchedRule = attr[0];
if (matchedRule.includes("=") || isBlocked(matchedRule))
continue;
tasks.push(uno.parseToken(matchedRule).then((matched) => {
if (matched) {
const tag = item[2];
const startIdx = (item.index || 0) + (attr.index || 0) + tag.length + 1;
const endIdx = startIdx + matchedRule.length;
code.overwrite(startIdx, endIdx, `${matchedRule}=""`);
}
}));
}
}
await Promise.all(tasks);
}
};
}
export { transformerAttributifyJsx as default };