Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小浣熊撸代码
uni-app
提交
4cf23077
U
uni-app
项目概览
小浣熊撸代码
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4cf23077
编写于
2月 16, 2022
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(app): nvue styler
上级
2c277aaf
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
94 addition
and
27 deletion
+94
-27
packages/uni-app-vite/src/nvue/plugin/index.ts
packages/uni-app-vite/src/nvue/plugin/index.ts
+1
-1
packages/uni-nvue-styler/__tests__/combinators.spec.ts
packages/uni-nvue-styler/__tests__/combinators.spec.ts
+37
-0
packages/uni-nvue-styler/src/normalize/index.ts
packages/uni-nvue-styler/src/normalize/index.ts
+12
-8
packages/uni-nvue-styler/src/objectifier.ts
packages/uni-nvue-styler/src/objectifier.ts
+44
-18
未找到文件。
packages/uni-app-vite/src/nvue/plugin/index.ts
浏览文件 @
4cf23077
...
...
@@ -106,8 +106,8 @@ export function uniAppNVuePlugin({
}
const
{
code
,
messages
}
=
await
parse
(
source
,
{
filename
,
descendant
:
false
,
logLevel
:
'
WARNING
'
,
combinators
:
false
,
})
messages
.
forEach
((
message
)
=>
{
if
(
message
.
type
===
'
warning
'
)
{
...
...
packages/uni-nvue-styler/__tests__/combinators.spec.ts
0 → 100644
浏览文件 @
4cf23077
import
{
parse
}
from
'
../src
'
async
function
objectifierRule
(
input
:
string
)
{
const
{
code
,
messages
}
=
await
parse
(
input
,
{
combinators
:
true
,
logLevel
:
'
NOTE
'
,
})
return
{
json
:
JSON
.
parse
(
code
),
messages
,
}
}
describe
(
'
nvue-styler: combinators
'
,
()
=>
{
test
(
'
descendant
'
,
async
()
=>
{
const
{
json
,
messages
}
=
await
objectifierRule
(
` .bar {left:5;}.foo .bar {left: 0;}.foo .bar{left:5;right:10;}.bar .bar{left:2}.foo .bar .foobar{left:1}`
)
console
.
log
(
json
,
messages
)
expect
(
json
).
toEqual
({
bar
:
{
left
:
5
,
},
'
.bar
'
:
{
'
.foo
'
:
{
left
:
5
,
right
:
10
,
},
'
.bar
'
:
{
left
:
2
,
},
},
'
.foobar
'
:
{
'
.foo .bar
'
:
{
left
:
1
}
},
})
expect
(
messages
.
length
).
toBe
(
0
)
})
})
packages/uni-nvue-styler/src/normalize/index.ts
浏览文件 @
4cf23077
...
...
@@ -11,13 +11,13 @@ import { normalizeMap } from './map'
const
normalized
=
Symbol
(
'
normalized
'
)
export
interface
NormalizeOptions
{
descendant
?:
boolean
combinators
?:
boolean
logLevel
?:
'
NOTE
'
|
'
WARNING
'
|
'
ERROR
'
}
export
function
normalize
(
opts
:
NormalizeOptions
=
{}):
Plugin
{
if
(
!
hasOwn
(
opts
,
'
descendant
'
))
{
opts
.
descendant
=
false
if
(
!
hasOwn
(
opts
,
'
combinators
'
))
{
opts
.
combinators
=
false
}
if
(
!
hasOwn
(
opts
,
'
logLevel
'
))
{
opts
.
logLevel
=
'
WARNING
'
...
...
@@ -32,19 +32,22 @@ export function normalize(opts: NormalizeOptions = {}): Plugin {
return
plugin
}
function
createRuleProcessor
({
descendant
}:
NormalizeOptions
)
{
function
createRuleProcessor
({
combinators
}:
NormalizeOptions
)
{
return
(
rule
:
Rule
,
helper
:
Helpers
)
=>
{
if
((
rule
as
any
)[
normalized
])
{
return
}
const
regx
=
descendant
const
regx
=
combinators
?
/^
((?:(?:\.[
A-Za-z0-9_
\-]
+
)
+
[\+\~\>
])
*
)((?:\.[
A-Za-z0-9_
\-\:]
+
)
+
)
$/
:
/^
(\.)([
A-Za-z0-9_
\-
:
]
+
)
$/
rule
.
selector
=
rule
.
selectors
.
filter
((
selector
)
=>
{
.
map
((
selector
)
=>
{
selector
=
selector
.
replace
(
/
\s
*
([\+\~\>])\s
*/g
,
'
$1
'
)
.
replace
(
/
\s
+/
,
'
'
)
if
(
regx
.
test
(
selector
))
{
return
true
return
selector
}
rule
.
warn
(
helper
.
result
,
...
...
@@ -52,8 +55,9 @@ function createRuleProcessor({ descendant }: NormalizeOptions) {
selector
+
'
` is not supported. nvue only support classname selector
'
)
return
false
return
''
})
.
filter
(
Boolean
)
.
join
(
'
,
'
)
if
(
!
rule
.
selector
)
{
rule
.
remove
()
...
...
packages/uni-nvue-styler/src/objectifier.ts
浏览文件 @
4cf23077
...
...
@@ -10,7 +10,10 @@ export function objectifier(node: Root | Document | Container | null) {
if
(
!
node
)
{
return
{}
}
const
context
:
ObjectifierContext
=
{
'
FONT-FACE
'
:
[],
TRANSITION
:
{}
}
const
context
:
ObjectifierContext
=
{
'
FONT-FACE
'
:
[],
TRANSITION
:
{},
}
const
result
=
transform
(
node
,
context
)
if
(
context
[
'
FONT-FACE
'
].
length
)
{
result
[
'
@FONT-FACE
'
]
=
context
[
'
FONT-FACE
'
]
...
...
@@ -37,23 +40,7 @@ function transform(
}
else
if
(
child
.
type
===
'
rule
'
)
{
const
body
=
transform
(
child
,
context
)
child
.
selectors
.
forEach
((
selector
)
=>
{
let
className
=
selector
.
slice
(
1
)
const
pseudoIndex
=
className
.
indexOf
(
'
:
'
)
if
(
pseudoIndex
>
-
1
)
{
const
pseudoClass
=
className
.
slice
(
pseudoIndex
)
className
=
className
.
slice
(
0
,
pseudoIndex
)
Object
.
keys
(
body
).
forEach
(
function
(
name
)
{
body
[
name
+
pseudoClass
]
=
body
[
name
]
delete
body
[
name
]
})
}
transition
(
className
,
body
,
context
)
if
(
result
[
className
])
{
// clone
result
[
className
]
=
extend
({},
result
[
className
],
body
)
}
else
{
result
[
className
]
=
body
}
transformSelector
(
selector
,
body
,
result
,
context
)
})
}
else
if
(
child
.
type
===
'
decl
'
)
{
result
[
child
.
prop
]
=
child
.
value
...
...
@@ -62,6 +49,45 @@ function transform(
return
result
}
function
transformSelector
(
selector
:
string
,
body
:
Record
<
string
,
unknown
>
,
result
:
Record
<
string
,
unknown
|
Record
<
string
,
unknown
>>
,
context
:
ObjectifierContext
)
{
let
className
=
selector
.
slice
(
1
)
let
isCombinators
=
false
const
lastDotIndex
=
className
.
lastIndexOf
(
'
.
'
)
if
(
lastDotIndex
>
0
)
{
isCombinators
=
true
className
=
className
.
substring
(
lastDotIndex
+
1
)
}
const
pseudoIndex
=
className
.
indexOf
(
'
:
'
)
if
(
pseudoIndex
>
-
1
)
{
const
pseudoClass
=
className
.
slice
(
pseudoIndex
)
className
=
className
.
slice
(
0
,
pseudoIndex
)
Object
.
keys
(
body
).
forEach
(
function
(
name
)
{
body
[
name
+
pseudoClass
]
=
body
[
name
]
delete
body
[
name
]
})
}
transition
(
className
,
body
,
context
)
if
(
isCombinators
)
{
className
=
'
.
'
+
className
result
=
(
result
[
className
]
||
(
result
[
className
]
=
{}))
as
Record
<
string
,
unknown
>
className
=
selector
.
replace
(
className
,
''
).
trim
()
}
if
(
result
[
className
])
{
// clone
result
[
className
]
=
extend
({},
result
[
className
],
body
)
}
else
{
result
[
className
]
=
body
}
}
function
transition
(
className
:
string
,
body
:
Record
<
string
,
unknown
>
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录