-
-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extractor-arbitrary-variants)!: make arbitrary extractor standal…
…one (#2364)
- Loading branch information
Showing
26 changed files
with
240 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: Arbitrary Variants Extractor | ||
--- | ||
|
||
# Arbitrary Variants Extractor | ||
|
||
A more complex extractor to support arbitrary variants for utilities. | ||
|
||
```html | ||
<div class="[&>*]:m-1 [&[open]]:p-2"></div> | ||
``` | ||
|
||
Will be captured with `[&>*]:m-1` and `[&[open]]:p-2` as variants. | ||
|
||
This extractor is included in [`@unocss/preset-mini`](/presets/mini) as the default extractor. Normally you don't need to install this package manually. | ||
|
||
## Installation | ||
|
||
::: code-group | ||
```bash [pnpm] | ||
pnpm add -D @unocss/extractor-arbitrary-variants | ||
``` | ||
```bash [yarn] | ||
yarn add -D @unocss/extractor-arbitrary-variants | ||
``` | ||
```bash [npm] | ||
npm install -D @unocss/extractor-arbitrary-variants | ||
``` | ||
::: | ||
|
||
```ts | ||
import { defineConfig } from 'unocss' | ||
import extractorArbitrary from '@unocss/extractor-arbitrary-variants' | ||
|
||
export default defineConfig({ | ||
extractors: [ | ||
extractorArbitrary(), | ||
], | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
export { | ||
extractorSplit, | ||
extractorSplit as extractorDefault, | ||
arbitraryPropertyRE, | ||
quotedArbitraryValuesRE, | ||
defaultSplitRE, | ||
splitWithVariantGroupRE, | ||
} from './split' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,18 @@ | ||
import type { Extractor } from '../types' | ||
import { isValidSelector } from '../utils' | ||
|
||
export const defaultSplitRE = /[\\:]?[\s'"`;{}]+/g | ||
export const splitWithVariantGroupRE = /([\\:]?[\s"'`;<>*]|:\(|\)"|\)\s)/g | ||
|
||
export const quotedArbitraryValuesRE = /(?:[\w&:[\]-]|\[\S+=\S+\])+\[\\?['"]?\S+?['"]\]\]?[\w:-]*/g | ||
export const arbitraryPropertyRE = /\[(\\\W|[\w-])+:[^\s:]*?("\S+?"|'\S+?'|`\S+?`|[^\s:]+?)[^\s:]*?\)?\]/g | ||
const arbitraryPropertyCandidateRE = /^\[(\\\W|[\w-])+:['"]?\S+?['"]?\]$/ | ||
|
||
export function splitCode(code: string) { | ||
const result = new Set<string>() | ||
|
||
for (const match of code.matchAll(arbitraryPropertyRE)) { | ||
if (!code[match.index! - 1]?.match(/^[\s'"`]/)) | ||
continue | ||
|
||
result.add(match[0]) | ||
} | ||
|
||
for (const match of code.matchAll(quotedArbitraryValuesRE)) | ||
result.add(match[0]) | ||
|
||
code | ||
.split(defaultSplitRE) | ||
.forEach((match) => { | ||
if (isValidSelector(match) && !arbitraryPropertyCandidateRE.test(match)) | ||
result.add(match) | ||
}) | ||
|
||
return [...result] | ||
return [...new Set(code.split(defaultSplitRE))] | ||
} | ||
|
||
export const extractorSplit: Extractor = { | ||
name: 'split', | ||
name: '@unocss/core/extractor-split', | ||
order: 0, | ||
extract({ code }) { | ||
return splitCode(code) | ||
}, | ||
} | ||
|
||
export { extractorSplit as extractorDefault } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# @unocss/extractor-arbitrary-variants | ||
|
||
Exactor to support arbitrary variants for utilities. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm i -D @unocss/extractor-arbitrary-variants | ||
``` | ||
|
||
```ts | ||
import extractorArbitrary from '@unocss/extractor-arbitrary-variants' | ||
|
||
UnoCSS({ | ||
extractors: [ | ||
extractorArbitrary() | ||
], | ||
}) | ||
``` | ||
|
||
## Usage | ||
|
||
```html | ||
<div class="[&>*]:m-1 [&[open]]:p-2"></div> | ||
``` | ||
|
||
Will be captured with `[&>*]:m-1` and `[&[open]]:p-2` as variants. | ||
|
||
## License | ||
|
||
MIT License © 2022-PRESENT [Anthony Fu](https://github.com/antfu) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
clean: true, | ||
declaration: true, | ||
rollup: { | ||
emitCJS: true, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@unocss/extractor-arbitrary-variants", | ||
"version": "0.50.6", | ||
"description": "Extractor arbitrary variants for utilities", | ||
"author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
"license": "MIT", | ||
"funding": "https://github.com/sponsors/antfu", | ||
"homepage": "https://github.com/unocss/unocss/tree/main/packages/extractor-arbitrary-variants#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/unocss/unocss.git", | ||
"directory": "packages/extractor-arbitrary-variants" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/unocss/unocss/issues" | ||
}, | ||
"keywords": [ | ||
"unocss", | ||
"unocss-extractor" | ||
], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"dependencies": { | ||
"@unocss/core": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Extractor } from '@unocss/core' | ||
import { defaultSplitRE, isValidSelector } from '@unocss/core' | ||
|
||
export const quotedArbitraryValuesRE = /(?:[\w&:[\]-]|\[\S+=\S+\])+\[\\?['"]?\S+?['"]\]\]?[\w:-]*/g | ||
export const arbitraryPropertyRE = /\[(\\\W|[\w-])+:[^\s:]*?("\S+?"|'\S+?'|`\S+?`|[^\s:]+?)[^\s:]*?\)?\]/g | ||
const arbitraryPropertyCandidateRE = /^\[(\\\W|[\w-])+:['"]?\S+?['"]?\]$/ | ||
|
||
export function splitCodeWithArbitraryVariants(code: string) { | ||
const result = new Set<string>() | ||
|
||
for (const match of code.matchAll(arbitraryPropertyRE)) { | ||
if (!code[match.index! - 1]?.match(/^[\s'"`]/)) | ||
continue | ||
|
||
result.add(match[0]) | ||
} | ||
|
||
for (const match of code.matchAll(quotedArbitraryValuesRE)) | ||
result.add(match[0]) | ||
|
||
code | ||
.split(defaultSplitRE) | ||
.forEach((match) => { | ||
if (isValidSelector(match) && !arbitraryPropertyCandidateRE.test(match)) | ||
result.add(match) | ||
}) | ||
|
||
return [...result] | ||
} | ||
|
||
export const extractorArbitraryVariants: Extractor = { | ||
name: '@unocss/extractor-arbitrary-variants', | ||
order: 0, | ||
extract({ code }) { | ||
return splitCodeWithArbitraryVariants(code) | ||
}, | ||
} | ||
|
||
export default extractorArbitraryVariants |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.