Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
776 additions
and
383 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 |
---|---|---|
|
@@ -37,6 +37,6 @@ | |
"stub": "unbuild --stub" | ||
}, | ||
"dependencies": { | ||
"@unocss/core": "^0.45.7" | ||
"@unocss/core": "^0.45.13" | ||
} | ||
} |
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
File renamed without changes.
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,37 @@ | ||
import type { UnocssPluginContext } from 'unocss' | ||
import type { TransformerAppletOptions } from './types' | ||
|
||
export async function compileApplet(body: string, ctx: UnocssPluginContext, options: TransformerAppletOptions = {}): Promise<string[]> { | ||
const { | ||
classPrefix = 'uno-', | ||
hashFn = hash, | ||
layer = 'applet_shortcuts', | ||
} = options | ||
|
||
const { uno, tokens } = ctx | ||
const replacements = [] | ||
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, { layer }]) | ||
tokens.add(className) | ||
} | ||
return replacements | ||
} | ||
|
||
export 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { SourceCodeTransformer } from 'unocss' | ||
import MagicString from 'magic-string' | ||
import type { TransformerAppletOptions } from './types' | ||
import { compileApplet } from './compile' | ||
|
||
export default function transformerApplet(options: TransformerAppletOptions = {}): SourceCodeTransformer { | ||
// Regular expression of characters to be escaped | ||
const charReg = /[.:%!#()[\/\],]/ | ||
|
||
const classRE = /:?(hover-)?class=\".*?\"/g | ||
const string1RE = /([']).*?(['])/g | ||
const string2RE = /([\`]).*?([\`])/g | ||
|
||
return { | ||
name: 'transformer-applet', | ||
enforce: 'pre', | ||
async transform(s, _, ctx) { | ||
const code = new MagicString(s.toString()) | ||
// process class | ||
const classMatches = [...code.original.matchAll(classRE)] | ||
for (const match of classMatches) { | ||
// skip `... ? ... : ...` | ||
if (/\?.*:/g.test(match[0])) | ||
continue | ||
|
||
// skip `... : ...` | ||
if (/{.+:.+}/g.test(match[0])) | ||
continue | ||
|
||
const start = match.index! | ||
const matchSplit = match[0].split('class=') | ||
|
||
const body = matchSplit[1].slice(1, -1) | ||
|
||
if (charReg.test(body)) { | ||
const replacements = await compileApplet(body, ctx, options) | ||
code.overwrite(start, start + match[0].length, `${matchSplit[0]}class="${replacements.join(' ')}"`) | ||
} | ||
} | ||
|
||
// process string1 | ||
const string1Matches = [...code.original.matchAll(string1RE)] | ||
for (const match of string1Matches) { | ||
// There may be no need | ||
// https://tailwindcss.com/docs/background-image#arbitrary-values | ||
// skip all the image formats in HTML for bg-[url('...')] | ||
if (/\.(png|jpg|jpeg|gif|svg)/g.test(match[0])) | ||
continue | ||
// skip http(s):// | ||
if (/http(s)?:\/\//g.test(match[0])) | ||
continue | ||
|
||
const start = match.index! | ||
const body = match[0].slice(1, -1) | ||
if (charReg.test(body)) { | ||
const replacements = await compileApplet(body, ctx, options) | ||
code.overwrite(start, start + match[0].length, `'${replacements.join(' ')}'`) | ||
} | ||
} | ||
|
||
// process string2 | ||
const string2Matches = [...code.original.matchAll(string2RE)] | ||
for (const match of string2Matches) { | ||
// skip `${...}` | ||
if (/\$\{.*\}/g.test(match[0])) | ||
continue | ||
|
||
const start = match.index! | ||
const body = match[0].slice(1, -1) | ||
if (charReg.test(body)) { | ||
const replacements = await compileApplet(body, ctx, options) | ||
code.overwrite(start, start + match[0].length, `'${replacements.join(' ')}'`) | ||
} | ||
} | ||
s.overwrite(0, s.original.length, code.toString()) | ||
}, | ||
} | ||
} | ||
|
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,18 @@ | ||
export interface TransformerAppletOptions { | ||
/** | ||
* Prefix for compile class name | ||
* @default 'uno-' | ||
*/ | ||
classPrefix?: string | ||
|
||
/** | ||
* Hash function | ||
*/ | ||
hashFn?: (str: string) => string | ||
|
||
/** | ||
* The layer name of generated rules | ||
* @default 'applet_shortcuts' | ||
*/ | ||
layer?: string | ||
} |
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,16 @@ | ||
# @unocss-applet/transformer-attributify | ||
|
||
Attributify Mode for [UnoCSS](https://github.com/unocss/unocss). | ||
|
||
## Instal | ||
|
||
```bash | ||
npm i @unocss-applet/transformer-attributify --save-dev # with npm | ||
yarn add @unocss-applet/transformer-attributify -D # with yarn | ||
pnpm add @unocss-applet/transformer-attributify -D # with pnpm | ||
``` | ||
|
||
|
||
## License | ||
|
||
MIT License © 2022-PRESENT [Neil Lee](https://github.com/zguolee) |
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,16 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
clean: true, | ||
declaration: true, | ||
externals: [ | ||
'magic-string', | ||
'unocss', | ||
], | ||
rollup: { | ||
emitCJS: true, | ||
}, | ||
}) |
Oops, something went wrong.