Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yangkaifeng
uni-app
提交
a690ddd5
U
uni-app
项目概览
yangkaifeng
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
3
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,发现更多精彩内容 >>
提交
a690ddd5
编写于
11月 18, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(h5): support disable tree shaking
上级
23cbb4c5
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
56 addition
and
11 deletion
+56
-11
packages/uni-cli-shared/src/json/manifest.ts
packages/uni-cli-shared/src/json/manifest.ts
+4
-0
packages/uni-cli-shared/src/vite/plugins/inject.ts
packages/uni-cli-shared/src/vite/plugins/inject.ts
+23
-4
packages/uni-cli-shared/src/vite/utils/ast.ts
packages/uni-cli-shared/src/vite/utils/ast.ts
+5
-0
packages/uni-h5-vite/src/plugins/inject.ts
packages/uni-h5-vite/src/plugins/inject.ts
+17
-5
packages/uni-h5-vite/src/plugins/pagesJson.ts
packages/uni-h5-vite/src/plugins/pagesJson.ts
+7
-2
未找到文件。
packages/uni-cli-shared/src/json/manifest.ts
浏览文件 @
a690ddd5
...
...
@@ -72,3 +72,7 @@ export function getRouterOptions(manifestJson: Record<string, any>): {
}
{
return
extend
({},
manifestJson
.
h5
?.
router
)
}
export
function
isEnableTreeShaking
(
manifestJson
:
Record
<
string
,
any
>
)
{
return
manifestJson
.
h5
?.
optimization
?.
treeShaking
?.
enable
!==
false
}
packages/uni-cli-shared/src/vite/plugins/inject.ts
浏览文件 @
a690ddd5
...
...
@@ -16,7 +16,13 @@ import { walk } from 'estree-walker'
import
{
extend
}
from
'
@vue/shared
'
import
MagicString
from
'
magic-string
'
import
{
isProperty
,
isReference
,
isMemberExpression
,
isJsFile
}
from
'
../utils
'
import
{
isProperty
,
isReference
,
isMemberExpression
,
isJsFile
,
isAssignmentExpression
,
}
from
'
../utils
'
interface
Scope
{
parent
:
Scope
...
...
@@ -46,6 +52,7 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
delete
modules
.
sourceMap
delete
modules
.
callback
const
reassignments
=
new
Set
<
string
>
()
const
modulesMap
=
new
Map
<
string
,
string
|
[
string
,
string
]
>
()
const
namespaceModulesMap
=
new
Map
<
string
,
string
|
[
string
,
string
]
>
()
Object
.
keys
(
modules
).
forEach
((
name
)
=>
{
...
...
@@ -105,7 +112,12 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
const
newImports
=
new
Map
()
function
handleReference
(
node
:
BaseNode
,
name
:
string
,
keypath
:
string
)
{
function
handleReference
(
node
:
BaseNode
,
name
:
string
,
keypath
:
string
,
parent
?:
BaseNode
)
{
let
mod
=
modulesMap
.
get
(
keypath
)
if
(
!
mod
&&
hasNamespace
)
{
const
mods
=
keypath
.
split
(
'
.
'
)
...
...
@@ -128,8 +140,15 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
if
(
mod
&&
!
imports
.
has
(
name
)
&&
!
scope
.
contains
(
name
))
{
if
(
typeof
mod
===
'
string
'
)
mod
=
[
mod
,
'
default
'
]
if
(
mod
[
0
]
===
id
)
return
false
const
hash
=
`
${
keypath
}
:
${
mod
[
0
]}
:
${
mod
[
1
]}
`
// 当 API 被覆盖定义后,不再摇树
if
(
reassignments
.
has
(
hash
))
{
return
false
}
if
(
parent
&&
isAssignmentExpression
(
parent
))
{
reassignments
.
add
(
hash
)
return
false
}
const
importLocalName
=
name
===
keypath
?
name
:
makeLegalIdentifier
(
`$inject_
${
keypath
}
`
)
...
...
@@ -184,7 +203,7 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
if
(
isReference
(
node
,
parent
))
{
const
{
name
,
keypath
}
=
flatten
(
node
)
const
handled
=
handleReference
(
node
,
name
,
keypath
)
const
handled
=
handleReference
(
node
,
name
,
keypath
,
parent
)
if
(
handled
)
{
this
.
skip
()
}
...
...
packages/uni-cli-shared/src/vite/utils/ast.ts
浏览文件 @
a690ddd5
...
...
@@ -4,6 +4,7 @@ import type {
Property
,
Identifier
,
CallExpression
,
AssignmentExpression
,
MemberExpression
,
MethodDefinition
,
ExportSpecifier
,
...
...
@@ -25,6 +26,10 @@ export const isProperty = (node: BaseNode): node is Property =>
export
const
isIdentifier
=
(
node
:
BaseNode
):
node
is
Identifier
=>
node
.
type
===
'
Identifier
'
export
const
isAssignmentExpression
=
(
node
:
BaseNode
):
node
is
AssignmentExpression
=>
node
.
type
===
'
AssignmentExpression
'
export
const
isCallExpression
=
(
node
:
BaseNode
):
node
is
CallExpression
=>
node
.
type
===
'
CallExpression
'
...
...
packages/uni-h5-vite/src/plugins/inject.ts
浏览文件 @
a690ddd5
...
...
@@ -10,6 +10,8 @@ import {
buildInCssSet
,
uniViteInjectPlugin
,
isCombineBuiltInCss
,
isEnableTreeShaking
,
parseManifestJsonOnce
,
}
from
'
@dcloudio/uni-cli-shared
'
const
apiJson
=
require
(
path
.
resolve
(
__dirname
,
'
../../lib/api.json
'
))
...
...
@@ -49,17 +51,27 @@ export function uniInjectPlugin(): Plugin {
}
})
}
const
injectPlugin
=
uniViteInjectPlugin
(
extend
(
uniInjectPluginOptions
,
{
callback
,
})
)
let
injectPlugin
:
Plugin
return
{
name
:
'
vite:uni-h5-inject
'
,
apply
:
'
build
'
,
enforce
:
'
post
'
,
configResolved
(
config
)
{
resolvedConfig
=
config
const
enableTreeShaking
=
isEnableTreeShaking
(
parseManifestJsonOnce
(
process
.
env
.
UNI_INPUT_DIR
)
)
if
(
!
enableTreeShaking
)
{
// 不启用摇树优化,移除 wx、uni 等 API 配置
delete
uniInjectPluginOptions
[
'
wx.
'
]
delete
uniInjectPluginOptions
[
'
uni.
'
]
}
injectPlugin
=
uniViteInjectPlugin
(
extend
(
uniInjectPluginOptions
,
{
callback
,
})
)
},
transform
(
code
,
id
)
{
return
injectPlugin
.
transform
!
.
call
(
this
,
code
,
id
)
...
...
packages/uni-h5-vite/src/plugins/pagesJson.ts
浏览文件 @
a690ddd5
...
...
@@ -11,6 +11,8 @@ import {
normalizePagesRoute
,
normalizePagePath
,
normalizePath
,
isEnableTreeShaking
,
parseManifestJsonOnce
,
}
from
'
@dcloudio/uni-cli-shared
'
export
function
uniPagesJsonPlugin
():
Plugin
{
...
...
@@ -79,8 +81,11 @@ function getGlobal(ssr?: boolean) {
// 兼容 wx 对象
function
registerGlobalCode
(
config
:
ResolvedConfig
,
ssr
?:
boolean
)
{
const
name
=
getGlobal
(
ssr
)
if
(
config
.
command
===
'
build
'
&&
!
ssr
)
{
// 非SSR的发行模式,补充全局 uni 对象
const
enableTreeShaking
=
isEnableTreeShaking
(
parseManifestJsonOnce
(
process
.
env
.
UNI_INPUT_DIR
)
)
if
(
enableTreeShaking
&&
config
.
command
===
'
build
'
&&
!
ssr
)
{
// 非 SSR 的发行模式,补充全局 uni 对象
return
`
${
name
}
.uni = {};
${
name
}
.wx = {}`
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录