Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
陈庄旺
uni-app
提交
04944e14
U
uni-app
项目概览
陈庄旺
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
04944e14
编写于
10月 17, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(mp): transform static import to dynamic import
上级
c83c6ceb
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
34 addition
and
25 deletion
+34
-25
packages/uni-cli-shared/package.json
packages/uni-cli-shared/package.json
+2
-0
packages/uni-cli-shared/src/mp/imports.ts
packages/uni-cli-shared/src/mp/imports.ts
+18
-2
packages/uni-mp-compiler/__tests__/transformElement.spec.ts
packages/uni-mp-compiler/__tests__/transformElement.spec.ts
+4
-9
packages/uni-mp-compiler/src/codegen.ts
packages/uni-mp-compiler/src/codegen.ts
+5
-11
packages/uni-mp-vite/src/plugins/component.ts
packages/uni-mp-vite/src/plugins/component.ts
+5
-3
未找到文件。
packages/uni-cli-shared/package.json
浏览文件 @
04944e14
...
...
@@ -19,6 +19,8 @@
"url"
:
"https://github.com/dcloudio/uni-app/issues"
},
"dependencies"
:
{
"@babel/parser"
:
"^7.15.0"
,
"@babel/types"
:
"^7.15.0"
,
"@rollup/pluginutils"
:
"^4.1.1"
,
"chokidar"
:
"^3.5.2"
,
"compare-versions"
:
"^3.6.0"
,
...
...
packages/uni-cli-shared/src/mp/imports.ts
浏览文件 @
04944e14
...
...
@@ -2,6 +2,8 @@ import path from 'path'
import
{
PluginContext
}
from
'
rollup
'
import
{
init
,
parse
as
parseImports
,
ImportSpecifier
}
from
'
es-module-lexer
'
import
{
extend
}
from
'
@vue/shared
'
import
{
isImportDeclaration
,
isImportDefaultSpecifier
}
from
'
@babel/types
'
import
{
parse
}
from
'
@babel/parser
'
import
{
EXTNAME_VUE
,
EXTNAME_VUE_RE
}
from
'
../constants
'
export
async
function
findVueComponentImports
(
...
...
@@ -24,7 +26,7 @@ export async function findVueComponentImports(
return
[]
}
const
rewriteImports
:
ImportSpecifier
[]
=
[]
const
rewriteImports
:
(
ImportSpecifier
&
{
i
:
string
})
[]
=
[]
for
(
let
i
=
0
;
i
<
imports
.
length
;
i
++
)
{
const
importSpecifier
=
imports
[
i
]
const
{
n
}
=
importSpecifier
...
...
@@ -41,7 +43,21 @@ export async function findVueComponentImports(
continue
}
if
(
EXTNAME_VUE_RE
.
test
(
res
.
id
))
{
rewriteImports
.
push
(
extend
(
importSpecifier
,
{
n
:
res
.
id
}))
const
expr
=
parse
(
source
.
slice
(
importSpecifier
.
ss
,
importSpecifier
.
se
),
{
sourceType
:
'
module
'
,
}).
program
.
body
[
0
]
if
(
isImportDeclaration
(
expr
)
&&
expr
.
specifiers
.
length
===
1
)
{
const
importDefaultSpecifier
=
expr
.
specifiers
[
0
]
if
(
!
isImportDefaultSpecifier
(
importDefaultSpecifier
))
{
continue
}
rewriteImports
.
push
(
extend
(
importSpecifier
,
{
n
:
res
.
id
,
i
:
importDefaultSpecifier
.
local
.
name
,
})
)
}
}
}
return
rewriteImports
...
...
packages/uni-mp-compiler/__tests__/transformElement.spec.ts
浏览文件 @
04944e14
...
...
@@ -25,11 +25,11 @@ function parseWithElementTransform(
describe
(
'
compiler: element transform
'
,
()
=>
{
test
(
'
import + resolve component
'
,
()
=>
{
const
{
root
,
code
}
=
parseWithElementTransform
(
`<Foo/>`
)
const
{
root
}
=
parseWithElementTransform
(
`<Foo/>`
)
expect
((
root
as
CodegenRootNode
).
bindingComponents
).
toEqual
({
Foo
:
{
name
:
'
_component_Foo
'
,
type
:
BindingComponentTypes
.
UNKNOWN
},
})
expect
(
code
).
toContain
(
`if (!Math) {Math.max.call(Max, _component_Foo)}`
)
//
expect(code).toContain(`if (!Math) {Math.max.call(Max, _component_Foo)}`)
})
test
(
'
import + resolve component multi
'
,
()
=>
{
...
...
@@ -48,21 +48,16 @@ describe('compiler: element transform', () => {
Example
:
{
name
:
'
$setup["Example"]
'
,
type
:
BindingComponentTypes
.
SETUP
},
Test
:
{
name
:
'
_component_Test
'
,
type
:
BindingComponentTypes
.
SELF
},
})
expect
(
code
).
toContain
(
`if (!Math) {Math.max.call(Max, _component_Foo, _component_Bar, $setup["Example"], _component_Test)}`
)
expect
(
code
).
toContain
(
`if (!Math) {Math.max.call(Max, $setup["Example"])}`
)
})
test
(
'
resolve implcitly self-referencing component
'
,
()
=>
{
const
{
root
,
code
}
=
parseWithElementTransform
(
`<Example/>`
,
{
const
{
root
}
=
parseWithElementTransform
(
`<Example/>`
,
{
filename
:
`/foo/bar/Example.vue?vue&type=template`
,
})
expect
((
root
as
CodegenRootNode
).
bindingComponents
).
toEqual
({
Example
:
{
name
:
'
_component_Example
'
,
type
:
BindingComponentTypes
.
SELF
},
})
expect
(
code
).
toContain
(
`if (!Math) {Math.max.call(Max, _component_Example)}`
)
})
test
(
'
resolve component from setup bindings
'
,
()
=>
{
...
...
packages/uni-mp-compiler/src/codegen.ts
浏览文件 @
04944e14
...
...
@@ -151,25 +151,21 @@ function genComponentImports(
{
push
}:
CodegenContext
)
{
const
importDeclarations
:
string
[]
=
[]
// 仅记录easycom和setup组件
const
components
:
string
[]
=
[]
Object
.
keys
(
bindingComponents
).
forEach
((
tag
)
=>
{
const
{
name
,
type
}
=
bindingComponents
[
tag
]
if
(
type
===
BindingComponentTypes
.
UNKNOWN
)
{
const
source
=
matchEasycom
(
tag
)
if
(
source
)
{
components
.
push
(
name
)
addImportDeclaration
(
importDeclarations
,
name
,
source
)
}
}
else
if
(
type
===
BindingComponentTypes
.
SETUP
)
{
components
.
push
(
name
)
}
})
importDeclarations
.
forEach
((
str
)
=>
push
(
str
))
}
function
genComponents
(
bindingComponents
:
TransformContext
[
'
bindingComponents
'
],
{
push
}:
CodegenContext
)
{
const
components
=
Object
.
keys
(
bindingComponents
).
map
(
(
tag
)
=>
bindingComponents
[
tag
].
name
)
if
(
components
.
length
)
{
push
(
`if (!Math) {`
)
push
(
`Math.max.call(Max,
${
components
.
map
((
name
)
=>
name
).
join
(
'
,
'
)}
)`
)
...
...
@@ -197,7 +193,6 @@ function genFunctionPreamble(ast: RootNode, context: CodegenContext) {
}
}
genComponentImports
(
bindingComponents
,
context
)
genComponents
(
bindingComponents
,
context
)
newline
()
push
(
`return `
)
}
...
...
@@ -216,7 +211,6 @@ function genModulePreamble(
)
}
genComponentImports
(
bindingComponents
,
context
)
genComponents
(
bindingComponents
,
context
)
newline
()
if
(
!
inline
)
{
push
(
`export `
)
...
...
packages/uni-mp-vite/src/plugins/component.ts
浏览文件 @
04944e14
...
...
@@ -29,11 +29,13 @@ export function uniComponentPlugin(): Plugin {
}
const
s
=
new
MagicString
(
code
)
const
rewriteImports
:
string
[]
=
[]
vueComponentImports
.
forEach
(({
n
,
ss
,
se
})
=>
{
vueComponentImports
.
forEach
(({
n
,
ss
,
se
,
i
})
=>
{
s
.
remove
(
ss
,
se
)
rewriteImports
.
push
(
`import('
${
virtualComponentPath
(
n
!
)}
')`
)
rewriteImports
.
push
(
`const
${
i
}
= ()=>import('
${
virtualComponentPath
(
n
!
)}
')`
)
})
s
.
prepend
(
`if(!Math){
${
rewriteImports
.
join
(
'
;
'
)}
}`
)
s
.
prepend
(
rewriteImports
.
join
(
'
;
'
)
+
'
;
\n
'
)
return
s
.
toString
()
},
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录