Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compile-class): new transformer (#950)
* feat(compile-class): new transformer * chore: update * chore: update * chore: update
- Loading branch information
Showing
17 changed files
with
303 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# @unocss/transformer-compile-class | ||
|
||
<!-- @unocss-ignore --> | ||
|
||
Compile group of classes into one class. Inspried by [WindiCSS's compilation mode](https://windicss.org/posts/modes.html#compilation-mode) and [#948](https://github.com/unocss/unocss/issues/948) by [@UltraCakeBakery](https://github.com/UltraCakeBakery). | ||
|
||
## Install | ||
|
||
```bash | ||
npm i -D @unocss/transformer-compile-class | ||
``` | ||
|
||
```ts | ||
// uno.config.js | ||
import { defineConfig } from 'unocss' | ||
import transformerCompileClass from '@unocss/transformer-compile-class' | ||
|
||
export default defineConfig({ | ||
// ... | ||
transformers: [ | ||
transformerCompileClass(), | ||
], | ||
}) | ||
``` | ||
|
||
## Usage | ||
|
||
At the begin of your class strings, **add `:uno:` at the begin of the strings** to mark them for compilation. For example: | ||
|
||
```html | ||
<div class=":uno: text-center sm:text-left"> | ||
<div class=":uno: text-sm font-bold hover:text-red"/> | ||
</div> | ||
``` | ||
|
||
Will be compiled to: | ||
|
||
```html | ||
<div class="uno-qlmcrp"> | ||
<div class="uno-0qw2gr"/> | ||
</div> | ||
``` | ||
|
||
```css | ||
.uno-qlmcrp { | ||
text-align: center; | ||
} | ||
.uno-0qw2gr { | ||
font-size: 0.875rem; | ||
line-height: 1.25rem; | ||
font-weight: 700; | ||
} | ||
.uno-0qw2gr:hover { | ||
--un-text-opacity: 1; | ||
color: rgba(248, 113, 113, var(--un-text-opacity)); | ||
} | ||
@media (min-width: 640px) { | ||
.uno-qlmcrp { | ||
text-align: left; | ||
} | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
You can config the trigger string and prefix for compile class with the options. Refers to [the types](https://github.com/antfu/unocss/blob/main/packages/transformer-compile-class/src/index.ts#L4) for details. | ||
|
||
## License | ||
|
||
MIT License © 2021-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,15 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
clean: true, | ||
declaration: true, | ||
externals: [ | ||
'magic-string', | ||
], | ||
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,44 @@ | ||
{ | ||
"name": "@unocss/transformer-compile-class", | ||
"version": "0.33.1", | ||
"description": "Compile group of classes into one class", | ||
"keywords": [ | ||
"unocss", | ||
"unocss-transformer" | ||
], | ||
"homepage": "https://github.com/unocss/unocss/tree/main/packages/transformer-compile-class#readme", | ||
"bugs": { | ||
"url": "https://github.com/unocss/unocss/issues" | ||
}, | ||
"license": "MIT", | ||
"author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/unocss/unocss.git", | ||
"directory": "packages/transformer-compile-class" | ||
}, | ||
"funding": "https://github.com/sponsors/antfu", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"dependencies": { | ||
"@unocss/core": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"magic-string": "^0.26.1" | ||
} | ||
} |
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,79 @@ | ||
import type { SourceCodeTransformer } from '@unocss/core' | ||
import { escapeRegExp } from '@unocss/core' | ||
|
||
export interface CompileClassOptions { | ||
/** | ||
* Trigger string | ||
* @default ':uno:' | ||
*/ | ||
trigger?: string | ||
|
||
/** | ||
* Prefix for compile class name | ||
* @default 'uno-' | ||
*/ | ||
classPrefix?: string | ||
|
||
/** | ||
* Hash function | ||
*/ | ||
hashFn?: (str: string) => string | ||
|
||
/** | ||
* Left unknown classes inside the string | ||
* | ||
* @default true | ||
*/ | ||
keepUnknown?: boolean | ||
} | ||
|
||
export default function transformerCompileClass(options: CompileClassOptions = {}): SourceCodeTransformer { | ||
const { | ||
trigger = ':uno:', | ||
classPrefix = 'uno-', | ||
hashFn = hash, | ||
keepUnknown = true, | ||
} = options | ||
const regex = new RegExp(`(["'\`])${escapeRegExp(trigger)}\\s([^\\1]*?)\\1`, 'g') | ||
|
||
return { | ||
name: 'compile-class', | ||
enforce: 'pre', | ||
async transform(s, _, { uno }) { | ||
const matches = [...s.original.matchAll(regex)] | ||
if (!matches.length) | ||
return | ||
|
||
for (const match of matches) { | ||
let body = match[2].trim() | ||
const start = match.index! | ||
const replacements = [] | ||
if (keepUnknown) { | ||
const result = await Promise.all(body.split(/\s+/).filter(Boolean).map(async i => [i, !!await uno.parseToken(i)] as const)) | ||
const known = result.filter(([, matched]) => matched).map(([i]) => i) | ||
const unknown = result.filter(([, matched]) => !matched).map(([i]) => i) | ||
replacements.push(...unknown) | ||
body = known.join(' ') | ||
} | ||
if (body) { | ||
const hash = hashFn(body) | ||
const className = `${classPrefix}${hash}` | ||
replacements.unshift(className) | ||
uno.config.shortcuts.push([className, body]) | ||
} | ||
s.overwrite(start + 1, start + match[0].length - 1, replacements.join(' ')) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
function hash(str: string) { | ||
let i; let l | ||
let hval = 0x811C9DC5 | ||
|
||
for (i = 0, l = str.length; i < l; i++) { | ||
hval ^= str.charCodeAt(i) | ||
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24) | ||
} | ||
return (`00000${(hval >>> 0).toString(36)}`).slice(-6) | ||
} |
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
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,5 +1,5 @@ | ||
<template> | ||
<div font-sans leading-1em> | ||
<div class=":uno: font-sans leading-1em"> | ||
<Playground /> | ||
</div> | ||
</template> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.