Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
陈庄旺
uni-app
提交
f9dd77e2
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,发现更多精彩内容 >>
提交
f9dd77e2
编写于
12月 22, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: support custom scripts (#3093)
上级
cf00c403
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
109 addition
and
13 deletion
+109
-13
packages/shims-node.d.ts
packages/shims-node.d.ts
+3
-0
packages/uni-cli-shared/lib/preprocess/lib/preprocess.js
packages/uni-cli-shared/lib/preprocess/lib/preprocess.js
+7
-3
packages/uni-cli-shared/src/env/define.ts
packages/uni-cli-shared/src/env/define.ts
+15
-0
packages/uni-cli-shared/src/index.ts
packages/uni-cli-shared/src/index.ts
+1
-0
packages/uni-cli-shared/src/preprocess/context.ts
packages/uni-cli-shared/src/preprocess/context.ts
+14
-5
packages/uni-cli-shared/src/scripts.ts
packages/uni-cli-shared/src/scripts.ts
+44
-0
packages/vite-plugin-uni/src/cli/utils.ts
packages/vite-plugin-uni/src/cli/utils.ts
+24
-4
packages/vite-plugin-uni/src/index.ts
packages/vite-plugin-uni/src/index.ts
+1
-1
未找到文件。
packages/shims-node.d.ts
浏览文件 @
f9dd77e2
...
...
@@ -22,5 +22,8 @@ declare namespace NodeJS {
UNI_AUTOMATOR_WS_ENDPOINT
?:
string
UNI_H5_BASE
?:
string
UNI_H5_BROWSER
?:
'
builtin
'
UNI_CUSTOM_SCRIPT
?:
string
UNI_CUSTOM_DEFINE
?:
string
UNI_CUSTOM_CONTEXT
?:
string
}
}
packages/uni-cli-shared/lib/preprocess/lib/preprocess.js
浏览文件 @
f9dd77e2
...
...
@@ -381,9 +381,13 @@ function getTestTemplate(test) {
return
new
Function
(
"
context
"
,
"
with (context||{}){ return (
"
+
test
+
"
); }
"
);
}
function
testPasses
(
test
,
context
)
{
var
testFn
=
getTestTemplate
(
test
);
return
testFn
(
context
,
getDeepPropFromObj
);
// fixed by xxxxxx
function
testPasses
(
test
,
context
)
{
var
testFn
=
getTestTemplate
(
test
)
try
{
return
testFn
(
context
,
getDeepPropFromObj
)
}
catch
(
e
)
{}
return
false
}
function
getFileContents
(
path
,
failSilent
,
requesterPath
)
{
...
...
packages/uni-cli-shared/src/env/define.ts
浏览文件 @
f9dd77e2
...
...
@@ -5,7 +5,9 @@ export function initDefine(stringifyBoolean: boolean = false) {
const
manifestJson
=
parseManifestJsonOnce
(
process
.
env
.
UNI_INPUT_DIR
)
const
isRunByHBuilderX
=
runByHBuilderX
()
const
isDebug
=
!!
manifestJson
.
debug
return
{
...
initCustomDefine
(),
'
process.env.NODE_ENV
'
:
JSON
.
stringify
(
process
.
env
.
NODE_ENV
),
'
process.env.UNI_DEBUG
'
:
stringifyBoolean
?
JSON
.
stringify
(
isDebug
)
...
...
@@ -32,3 +34,16 @@ export function initDefine(stringifyBoolean: boolean = false) {
'
process.env.VUE_APP_PLATFORM
'
:
JSON
.
stringify
(
process
.
env
.
UNI_PLATFORM
),
}
}
function
initCustomDefine
()
{
let
define
:
Record
<
string
,
string
>
=
{}
if
(
process
.
env
.
UNI_CUSTOM_DEFINE
)
{
try
{
define
=
JSON
.
parse
(
process
.
env
.
UNI_CUSTOM_DEFINE
)
}
catch
(
e
:
any
)
{}
}
return
Object
.
keys
(
define
).
reduce
<
Record
<
string
,
string
>>
((
res
,
name
)
=>
{
res
[
'
process.env.
'
+
name
]
=
JSON
.
stringify
(
define
[
name
])
return
res
},
{})
}
packages/uni-cli-shared/src/index.ts
浏览文件 @
f9dd77e2
...
...
@@ -18,6 +18,7 @@ export * from './postcss'
export
*
from
'
./filter
'
export
*
from
'
./esbuild
'
export
*
from
'
./resolve
'
export
*
from
'
./scripts
'
export
{
M
}
from
'
./messages
'
...
...
packages/uni-cli-shared/src/preprocess/context.ts
浏览文件 @
f9dd77e2
import
{
extend
}
from
'
@vue/shared
'
import
{
extend
,
isString
,
isPlainObject
}
from
'
@vue/shared
'
const
DEFAULT_KEYS
=
[
'
APP
'
,
'
APP_NVUE
'
,
...
...
@@ -37,7 +37,7 @@ export function getPreNVueContext() {
export
function
initPreContext
(
platform
:
UniApp
.
PLATFORM
,
userPreContext
?:
Record
<
string
,
boolean
>
userPreContext
?:
Record
<
string
,
boolean
>
|
string
)
{
const
vueContext
=
Object
.
create
(
null
)
const
nvueContext
=
Object
.
create
(
null
)
...
...
@@ -67,9 +67,18 @@ export function initPreContext(
}
if
(
userPreContext
)
{
Object
.
keys
(
userPreContext
).
forEach
((
key
)
=>
{
defaultContext
[
normalizeKey
(
key
)]
=
!!
userPreContext
[
key
]
})
if
(
isString
(
userPreContext
))
{
try
{
userPreContext
=
JSON
.
parse
(
userPreContext
)
}
catch
(
e
)
{}
}
if
(
isPlainObject
(
userPreContext
))
{
Object
.
keys
(
userPreContext
).
forEach
((
key
)
=>
{
defaultContext
[
normalizeKey
(
key
)]
=
!!
(
userPreContext
as
Record
<
string
,
boolean
>
)[
key
]
})
}
}
extend
(
preVueContext
,
defaultContext
,
vueContext
)
extend
(
preNVueContext
,
defaultContext
,
nvueContext
)
...
...
packages/uni-cli-shared/src/scripts.ts
0 → 100644
浏览文件 @
f9dd77e2
import
fs
from
'
fs
'
interface
Package
{
'
uni-app
'
?:
{
scripts
?:
{
[
name
:
string
]:
{
title
?:
string
BROWSER
?:
'
Chrome
'
|
'
Firefox
'
|
'
IE
'
|
'
Edge
'
|
'
Safari
'
|
'
HBuilderX
'
env
?:
{
UNI_PLATFORM
:
UniApp
.
PLATFORM
[
name
:
string
]:
string
}
define
:
{
[
name
:
string
]:
boolean
}
}
}
}
}
export
function
parseScripts
(
name
:
string
,
pkgPath
:
string
)
{
if
(
!
fs
.
existsSync
(
pkgPath
))
{
return
}
const
pkg
=
JSON
.
parse
(
fs
.
readFileSync
(
pkgPath
,
'
utf8
'
))
as
Package
const
options
=
pkg
[
'
uni-app
'
]?.
scripts
?.[
name
]
if
(
!
options
)
{
return
}
if
(
!
options
.
env
?.
UNI_PLATFORM
)
{
console
.
error
(
`package.json->uni-app->scripts->
${
name
}
->env->UNI_PLATFORM is required`
)
process
.
exit
(
0
)
}
const
{
UNI_PLATFORM
,
...
define
}
=
options
.
env
return
{
name
:
name
,
platform
:
UNI_PLATFORM
,
define
,
context
:
options
.
define
||
{},
}
}
packages/vite-plugin-uni/src/cli/utils.ts
浏览文件 @
f9dd77e2
...
...
@@ -5,7 +5,12 @@ import chalk from 'chalk'
import
{
performance
}
from
'
perf_hooks
'
import
{
BuildOptions
,
InlineConfig
,
Logger
}
from
'
vite
'
import
{
M
,
isInHBuilderX
,
initModulePaths
}
from
'
@dcloudio/uni-cli-shared
'
import
{
M
,
isInHBuilderX
,
initModulePaths
,
parseScripts
,
}
from
'
@dcloudio/uni-cli-shared
'
import
{
CliOptions
}
from
'
.
'
import
{
initNVueEnv
}
from
'
./nvue
'
...
...
@@ -82,14 +87,15 @@ export function initEnv(type: 'dev' | 'build', options: CliOptions) {
process
.
env
.
UNI_SUB_PLATFORM
=
options
.
platform
options
.
platform
=
'
quickapp-webview
'
}
process
.
env
.
UNI_PLATFORM
=
options
.
platform
as
UniApp
.
PLATFORM
process
.
env
.
VITE_ROOT_DIR
=
process
.
env
.
UNI_INPUT_DIR
||
process
.
cwd
()
process
.
env
.
UNI_INPUT_DIR
=
process
.
env
.
UNI_INPUT_DIR
||
path
.
resolve
(
process
.
cwd
(),
'
src
'
)
initCustomScripts
(
options
)
process
.
env
.
UNI_PLATFORM
=
options
.
platform
as
UniApp
.
PLATFORM
const
hasOutputDir
=
!!
process
.
env
.
UNI_OUTPUT_DIR
if
(
hasOutputDir
)
{
;(
options
as
BuildOptions
).
outDir
=
process
.
env
.
UNI_OUTPUT_DIR
...
...
@@ -209,3 +215,17 @@ export function printStartupDuration(
)
}
}
function
initCustomScripts
(
options
:
CliOptions
)
{
const
custom
=
parseScripts
(
process
.
env
.
UNI_SCRIPT
||
options
.
platform
!
,
// process.env.UNI_SCRIPT 是 HBuilderX 传递的
path
.
join
(
process
.
env
.
VITE_ROOT_DIR
!
,
'
package.json
'
)
)
if
(
!
custom
)
{
return
}
options
.
platform
=
custom
.
platform
process
.
env
.
UNI_CUSTOM_SCRIPT
=
custom
.
name
process
.
env
.
UNI_CUSTOM_DEFINE
=
JSON
.
stringify
(
custom
.
define
)
process
.
env
.
UNI_CUSTOM_CONTEXT
=
JSON
.
stringify
(
custom
.
context
)
}
packages/vite-plugin-uni/src/index.ts
浏览文件 @
f9dd77e2
...
...
@@ -78,7 +78,7 @@ export default function uniPlugin(
options
.
platform
=
(
process
.
env
.
UNI_PLATFORM
as
UniApp
.
PLATFORM
)
||
'
h5
'
options
.
inputDir
=
process
.
env
.
UNI_INPUT_DIR
initPreContext
(
options
.
platform
)
initPreContext
(
options
.
platform
,
process
.
env
.
UNI_CUSTOM_CONTEXT
)
const
plugins
:
Plugin
[]
=
[]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录