-
Notifications
You must be signed in to change notification settings - Fork 0
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
12 changed files
with
401 additions
and
82 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
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
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,6 +1,10 @@ | ||
import { positions, positionShort } from './position' | ||
import { position, positionShort } from './position' | ||
import type { PropsAtomicMap } from '../types' | ||
|
||
export const border = [ | ||
['border', 'border'], | ||
['b', 'b'], | ||
export const border: PropsAtomicMap[] = [ | ||
['border', ['border', 'b']], | ||
[/^border-(top|bottom|left|right)(?:-\w+)?$/, ([, p]) => { | ||
const index = position.indexOf(p) | ||
return [`border-${positionShort[index]}`, `b-${positionShort[index]}`] | ||
}], | ||
] |
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,9 @@ | ||
import { border } from './border' | ||
import { margin } from './margin' | ||
import { positions } from './position' | ||
|
||
export const maps = [ | ||
border, | ||
positions, | ||
margin, | ||
].flat(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,10 @@ | ||
import { position, positionShort } from './position' | ||
import type { PropsAtomicMap } from '../types' | ||
|
||
export const margin: PropsAtomicMap[] = [ | ||
['margin', ['margin', 'm']], | ||
[/^margin-(top|bottom|left|right)(?:-\w+)?$/, ([, p]) => { | ||
const index = position.indexOf(p) | ||
return [`margin-${positionShort[index]}`, `m-${positionShort[index]}`] | ||
}], | ||
] |
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,2 +1,9 @@ | ||
export const positions = ['top', 'bottom', 'left', 'right'] | ||
import type { PropsAtomicMap } from '../types' | ||
|
||
export const position = ['top', 'bottom', 'left', 'right'] | ||
export const positionShort = ['t', 'b', 'l', 'r'] | ||
|
||
export const positions: PropsAtomicMap[] = position.map((p, i) => [ | ||
p, | ||
[p, positionShort[i]], | ||
]) |
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,71 @@ | ||
import type { CssNode, Declaration, Dimension, List, Url } from 'css-tree' | ||
import { toArray } from '../utils' | ||
import type { CssValueParsed, CssValueParsedMeta } from '../types' | ||
|
||
export function parseDeclarationNode(node: Declaration): CssValueParsed | undefined { | ||
if (node.type !== 'Declaration') { | ||
return | ||
} | ||
|
||
const prop = node.property | ||
let meta: CssValueParsedMeta[] | ||
|
||
if (node.value.type === 'Raw') { | ||
meta = toArray<CssValueParsedMeta>({ value: node.value.value }) | ||
Check failure on line 14 in src/parser/declaration.ts GitHub Actions / test (lts/*, ubuntu-latest)
Check failure on line 14 in src/parser/declaration.ts GitHub Actions / test (lts/*, windows-latest)
|
||
} | ||
else { | ||
meta = node.value.children.map(child => parseChildNode(child)) as unknown as CssValueParsedMeta[] | ||
} | ||
|
||
return { | ||
prop, | ||
meta, | ||
} | ||
} | ||
|
||
function parseChildNode(child: CssNode): CssValueParsedMeta | undefined { | ||
switch (child.type) { | ||
case 'Dimension': | ||
case 'Number': | ||
case 'String': | ||
case 'Percentage': | ||
case 'Hash': | ||
case 'Url': | ||
case 'Raw': | ||
case 'Operator': { | ||
let _v: any = child.value | ||
if (child.type === 'Hash') { | ||
_v = `#${child.value}` | ||
} | ||
else if (child.type === 'Percentage') { | ||
_v = `${child.value}%` | ||
} | ||
|
||
const meta: CssValueParsedMeta = { value: _v, type: child.type } | ||
|
||
if ((child as Dimension).unit) | ||
meta.unit = (child as Dimension).unit | ||
|
||
if ((child as Url).type === 'Url') | ||
meta.fname = 'url' | ||
|
||
return meta | ||
} | ||
|
||
case 'Identifier': | ||
return { | ||
value: child.name, | ||
type: child.type, | ||
} | ||
|
||
case 'Function': | ||
return { | ||
value: child.children.map(child => parseChildNode(child)!).toArray(), | ||
fname: child.name, | ||
type: child.type, | ||
} | ||
|
||
default: | ||
return undefined | ||
} | ||
} |
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,93 @@ | ||
import { maps } from '../maps' | ||
import { toArray } from '../utils' | ||
import type { AtomicComposed, CssValueParsed, CssValueParsedMeta, DynamicPropAtomicMap, StaticPropAtomicMap, TransfromOptions } from '../types' | ||
|
||
const atomicCache: Record<string, string[]> = {} | ||
|
||
const nonTransfromPxProps = [ | ||
'border', | ||
] | ||
|
||
/** | ||
Check warning on line 11 in src/transfromer/index.ts GitHub Actions / lint
|
||
* 将 CssValueParsedMeta[] 转换为 atomic css | ||
* @param meta CssValueParsedMeta[] | ||
Check warning on line 13 in src/transfromer/index.ts GitHub Actions / lint
|
||
* @returns | ||
Check warning on line 14 in src/transfromer/index.ts GitHub Actions / lint
|
||
*/ | ||
export function transfrom(metas: CssValueParsedMeta[], options: TransfromOptions = {}): string[] { | ||
const atomics: string[] = [] | ||
} | ||
|
||
export function transfromParsed(parsed: CssValueParsed, options: TransfromOptions = {}) { | ||
let key: string | undefined | ||
|
||
const { | ||
shortify = false, | ||
} = options | ||
const { prop, meta } = parsed | ||
|
||
for (const map of maps) { | ||
let atomics: AtomicComposed | undefined | ||
|
||
if (typeof map[0] === 'string') { | ||
if (prop === map[0]) { | ||
atomics = toArray((map as StaticPropAtomicMap)[1]) as AtomicComposed | ||
} | ||
} | ||
else { | ||
const match = prop.match(map[0]) | ||
if (match) { | ||
const matched = (map as DynamicPropAtomicMap)[1](match) | ||
if (matched) { | ||
atomics = toArray(matched) as AtomicComposed | ||
} | ||
} | ||
} | ||
|
||
if (atomics) { | ||
if (shortify) { | ||
key = atomics[1] || atomics[0] | ||
} | ||
else { | ||
key = atomics[0] | ||
} | ||
break | ||
} | ||
} | ||
|
||
if (!key) { | ||
return | ||
} | ||
|
||
return analyzeMeta({ | ||
prop, | ||
meta, | ||
key, | ||
}) | ||
} | ||
|
||
function analyzeMeta(bundle: { | ||
meta: CssValueParsed['meta'] | ||
key: string | ||
prop: string | ||
}): string[] { | ||
const { meta, key, prop } = bundle | ||
const atomics: string[] = [] | ||
|
||
for (const m of meta) { | ||
if (Array.isArray(m)) { | ||
|
||
} | ||
else { | ||
if (m.unit === 'px' && /^\d+$/.test(m.value as string)) { | ||
if (nonTransfromPxProps.some(p => prop.includes(p))) { | ||
atomics.push(`${key}-[${m.value}${m.unit}]`) | ||
} | ||
else { | ||
atomics.push(`${key}-${Number(m.value) / 4}`) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return atomics | ||
} |
Oops, something went wrong.