Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
genary
uni-app
提交
95f62d71
U
uni-app
项目概览
genary
/
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,发现更多精彩内容 >>
提交
95f62d71
编写于
3月 09, 2022
作者:
fxy060608
浏览文件
操作
浏览文件
下载
差异文件
chore: merge
上级
afc63786
58e04d0a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
4 addition
and
172 deletion
+4
-172
packages/uni-cli-shared/src/vue/parse.ts
packages/uni-cli-shared/src/vue/parse.ts
+2
-2
packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts
...ages/vite-plugin-uni/src/configResolved/plugins/preVue.ts
+2
-170
未找到文件。
packages/uni-cli-shared/src/vue/parse.ts
浏览文件 @
95f62d71
...
...
@@ -13,7 +13,7 @@ const BLOCK_RE = /<\/block>/
const
WXS_LANG_RE
=
/lang=
[
"|'
](
renderjs|wxs
)[
"|'
]
/
const
WXS_ATTRS
=
[
'
wxs
'
,
'
renderjs
'
]
export
function
parseVueCode
(
code
:
string
)
{
export
function
parseVueCode
(
code
:
string
,
isNVue
=
false
)
{
const
hasBlock
=
BLOCK_RE
.
test
(
code
)
const
hasWxs
=
WXS_LANG_RE
.
test
(
code
)
if
(
!
hasBlock
&&
!
hasWxs
)
{
...
...
@@ -27,7 +27,7 @@ export function parseVueCode(code: string) {
// 重新解析新的 code
ast
=
parseVue
(
code
,
errors
)
}
if
(
hasWxs
)
{
if
(
!
isNVue
&&
hasWxs
)
{
const
wxsNodes
=
parseWxsNodes
(
ast
)
code
=
parseWxsCode
(
wxsNodes
,
code
)
// add watch
...
...
packages/vite-plugin-uni/src/configResolved/plugins/preVue.ts
浏览文件 @
95f62d71
import
path
from
'
path
'
import
debug
from
'
debug
'
import
{
Plugin
}
from
'
vite
'
import
{
RootNode
,
NodeTypes
,
ParentNode
,
ElementNode
,
AttributeNode
,
TemplateChildNode
,
}
from
'
@vue/compiler-core
'
import
{
MagicString
}
from
'
@vue/compiler-sfc
'
import
{
clearMiniProgramTemplateFilter
,
EXTNAME_VUE
,
normalizeMiniProgramFilename
,
parseVueRequest
,
removeExt
,
isElementNode
,
parseVue
,
parseVueCode
,
}
from
'
@dcloudio/uni-cli-shared
'
const
debugPreVue
=
debug
(
'
uni:pre-vue
'
)
const
BLOCK_RE
=
/<
\/
block>/
const
WXS_LANG_RE
=
/lang=
[
"|'
](
renderjs|wxs
)[
"|'
]
/
const
WXS_ATTRS
=
[
'
wxs
'
,
'
renderjs
'
]
export
function
uniPreVuePlugin
():
Plugin
{
let
isNVue
=
false
return
{
...
...
@@ -48,159 +29,10 @@ export function uniPreVuePlugin(): Plugin {
clearMiniProgramTemplateFilter
(
removeExt
(
normalizeMiniProgramFilename
(
id
,
process
.
env
.
UNI_INPUT_DIR
))
)
const
hasBlock
=
BLOCK_RE
.
test
(
code
)
// nvue 不支持 renderjs,wxs
const
hasWxs
=
!
isNVue
&&
WXS_LANG_RE
.
test
(
code
)
if
(
!
hasBlock
&&
!
hasWxs
)
{
return
}
debugPreVue
(
id
)
const
watchFiles
:
string
[]
=
[]
const
errors
:
SyntaxError
[]
=
[]
let
ast
=
parseVue
(
code
,
errors
)
if
(
hasBlock
)
{
code
=
parseBlockCode
(
ast
,
code
)
}
if
(
hasWxs
)
{
if
(
hasBlock
)
{
// 重新解析新的 code
ast
=
parseVue
(
code
,
errors
)
}
const
wxsNodes
=
parseWxsNodes
(
ast
)
code
=
parseWxsCode
(
wxsNodes
,
code
)
// add watch
for
(
const
wxsNode
of
wxsNodes
)
{
const
srcProp
=
wxsNode
.
props
.
find
(
(
prop
)
=>
prop
.
type
===
NodeTypes
.
ATTRIBUTE
&&
prop
.
name
===
'
src
'
)
as
AttributeNode
|
undefined
if
(
srcProp
&&
srcProp
.
value
)
{
const
resolveId
=
await
this
.
resolve
(
srcProp
.
value
.
content
,
id
)
if
(
resolveId
)
{
watchFiles
.
push
(
resolveId
.
id
)
}
}
}
}
// if (errors.length) {
// this.error(errors.join('\n'))
// }
watchFiles
.
forEach
((
file
)
=>
this
.
addWatchFile
(
file
))
return
{
code
,
// 暂不提供sourcemap,意义不大
code
:
parseVueCode
(
code
,
isNVue
).
code
,
// 暂不提供sourcemap,意义不大
map
:
null
,
}
},
}
}
function
traverseChildren
({
children
}:
ParentNode
,
blockNodes
:
ElementNode
[])
{
children
.
forEach
((
node
)
=>
traverseNode
(
node
,
blockNodes
))
}
function
traverseNode
(
node
:
RootNode
|
TemplateChildNode
,
blockNodes
:
ElementNode
[]
)
{
if
(
isElementNode
(
node
)
&&
node
.
tag
===
'
block
'
)
{
blockNodes
.
push
(
node
)
}
if
(
node
.
type
===
NodeTypes
.
IF_BRANCH
||
node
.
type
===
NodeTypes
.
FOR
||
node
.
type
===
NodeTypes
.
ELEMENT
||
node
.
type
===
NodeTypes
.
ROOT
)
{
traverseChildren
(
node
,
blockNodes
)
}
}
export
function
parseBlockCode
(
ast
:
RootNode
,
code
:
string
)
{
const
blockNodes
:
ElementNode
[]
=
[]
traverseNode
(
ast
,
blockNodes
)
if
(
blockNodes
.
length
)
{
return
parseBlockNode
(
code
,
blockNodes
)
}
return
code
}
const
BLOCK_END_LEN
=
'
</block>
'
.
length
const
BLOCK_START_LEN
=
'
<block
'
.
length
function
parseBlockNode
(
code
:
string
,
blocks
:
ElementNode
[])
{
const
magicString
=
new
MagicString
(
code
)
blocks
.
forEach
(({
loc
})
=>
{
const
startOffset
=
loc
.
start
.
offset
const
endOffset
=
loc
.
end
.
offset
magicString
.
overwrite
(
startOffset
,
startOffset
+
BLOCK_START_LEN
,
'
<template
'
)
magicString
.
overwrite
(
endOffset
-
BLOCK_END_LEN
,
endOffset
,
'
</template>
'
)
})
return
magicString
.
toString
()
}
export
function
parseWxsNodes
(
ast
:
RootNode
)
{
return
ast
.
children
.
filter
(
(
node
)
=>
node
.
type
===
NodeTypes
.
ELEMENT
&&
node
.
tag
===
'
script
'
&&
node
.
props
.
find
(
(
prop
)
=>
prop
.
name
===
'
lang
'
&&
prop
.
type
===
NodeTypes
.
ATTRIBUTE
&&
prop
.
value
&&
WXS_ATTRS
.
includes
(
prop
.
value
.
content
)
)
)
as
ElementNode
[]
}
export
function
parseWxsCode
(
wxsNodes
:
ElementNode
[],
code
:
string
)
{
if
(
wxsNodes
.
length
)
{
code
=
parseWxsNode
(
code
,
wxsNodes
)
}
return
code
}
const
SCRIPT_END_LEN
=
'
</script>
'
.
length
const
SCRIPT_START_LEN
=
'
<script
'
.
length
function
parseWxsNode
(
code
:
string
,
nodes
:
ElementNode
[])
{
const
magicString
=
new
MagicString
(
code
)
nodes
.
forEach
(({
loc
,
props
})
=>
{
const
langAttr
=
props
.
find
((
prop
)
=>
prop
.
name
===
'
lang
'
)
as
AttributeNode
const
moduleAttr
=
props
.
find
(
(
prop
)
=>
prop
.
name
===
'
module
'
)
as
AttributeNode
const
startOffset
=
loc
.
start
.
offset
const
endOffset
=
loc
.
end
.
offset
const
lang
=
langAttr
.
value
!
.
content
const
langStartOffset
=
langAttr
.
loc
.
start
.
offset
magicString
.
overwrite
(
startOffset
,
startOffset
+
SCRIPT_START_LEN
,
'
<
'
+
lang
)
// <renderjs or <wxs
magicString
.
overwrite
(
langStartOffset
,
langStartOffset
+
(
'
lang="
'
+
lang
+
'
"
'
).
length
,
''
)
// remove lang="renderjs" or lang="wxs"
magicString
.
overwrite
(
endOffset
-
SCRIPT_END_LEN
,
endOffset
,
'
</
'
+
lang
+
'
>
'
)
//</renderjs> or </wxs>
if
(
moduleAttr
)
{
const
moduleStartOffset
=
moduleAttr
.
loc
.
start
.
offset
magicString
.
overwrite
(
moduleStartOffset
,
moduleStartOffset
+
'
module
'
.
length
,
'
name
'
)
// module="echarts" => name="echarts"
}
})
return
magicString
.
toString
()
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录