37 lines
1.4 KiB
Plaintext
37 lines
1.4 KiB
Plaintext
import { defaultSplitRE, isValidSelector } from '@unocss/core';
|
|
|
|
const sourceMapRE = /\/\/#\s*sourceMappingURL=.*\n?/g;
|
|
function removeSourceMap(code) {
|
|
if (code.includes("sourceMappingURL="))
|
|
return code.replace(sourceMapRE, "");
|
|
return code;
|
|
}
|
|
|
|
const quotedArbitraryValuesRE = /(?:[\w&:[\]-]|\[\S{1,64}=\S{1,64}\]){1,64}\[\\?['"]?\S{1,64}?['"]\]\]?[\w:-]{0,64}/g;
|
|
const arbitraryPropertyRE = /\[(\\\W|[\w-]){1,64}:[^\s:]{0,64}?("\S{1,64}?"|'\S{1,64}?'|`\S{1,64}?`|[^\s:]{1,64}?)[^\s:]{0,64}?\)?\]/g;
|
|
const arbitraryPropertyCandidateRE = /^\[(\\\W|[\w-]){1,64}:['"]?\S{1,64}?['"]?\]$/;
|
|
function splitCodeWithArbitraryVariants(code) {
|
|
const result = [];
|
|
for (const match of code.matchAll(arbitraryPropertyRE)) {
|
|
if (match.index !== 0 && !/^[\s'"`]/.test(code[match.index - 1] ?? ""))
|
|
continue;
|
|
result.push(match[0]);
|
|
}
|
|
for (const match of code.matchAll(quotedArbitraryValuesRE))
|
|
result.push(match[0]);
|
|
code.split(defaultSplitRE).forEach((match) => {
|
|
if (isValidSelector(match) && !arbitraryPropertyCandidateRE.test(match))
|
|
result.push(match);
|
|
});
|
|
return result;
|
|
}
|
|
const extractorArbitraryVariants = {
|
|
name: "@unocss/extractor-arbitrary-variants",
|
|
order: 0,
|
|
extract({ code }) {
|
|
return splitCodeWithArbitraryVariants(removeSourceMap(code));
|
|
}
|
|
};
|
|
|
|
export { arbitraryPropertyRE, extractorArbitraryVariants as default, extractorArbitraryVariants, quotedArbitraryValuesRE, splitCodeWithArbitraryVariants };
|