Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
66611642
U
uni-app
项目概览
DCloud
/
uni-app
6 个月 前同步成功
通知
751
Star
38709
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
8
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
8
Issue
8
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
66611642
编写于
1月 31, 2023
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(uts): compiler
上级
86e54ebe
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
140 addition
and
18 deletion
+140
-18
packages/uni-app-plus/dist/uni.runtime.esm.js
packages/uni-app-plus/dist/uni.runtime.esm.js
+13
-0
packages/uni-app-plus/src/service/api/index.ts
packages/uni-app-plus/src/service/api/index.ts
+2
-0
packages/uni-app-plus/src/service/api/plugin/uts.ts
packages/uni-app-plus/src/service/api/plugin/uts.ts
+16
-0
packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap
...ages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap
+21
-0
packages/uni-uts-v1/__tests__/code.spec.ts
packages/uni-uts-v1/__tests__/code.spec.ts
+15
-1
packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json
...ckage/cache/app-android/uts/utssdk/test-uts/manifest.json
+1
-1
packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json
...unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json
+1
-1
packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts
...i-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts
+7
-2
packages/uni-uts-v1/src/code.ts
packages/uni-uts-v1/src/code.ts
+52
-12
packages/uni-uts-v1/src/index.ts
packages/uni-uts-v1/src/index.ts
+12
-1
未找到文件。
packages/uni-app-plus/dist/uni.runtime.esm.js
浏览文件 @
66611642
...
...
@@ -17434,6 +17434,17 @@ function initUtsClassName(moduleName, className, is_uni_modules) {
capitalize(className));
}
return '';
}
const pluginDefines = {};
function registerUTSPlugin(name, define) {
pluginDefines[name] = define;
}
function requireUTSPlugin(name) {
const define = pluginDefines[name];
if (!define) {
console.error(`${name} is not found`);
}
return define;
}
const EventType = {
...
...
@@ -19556,6 +19567,8 @@ var uni$1 = {
initUtsIndexClassName: initUtsIndexClassName,
initUtsClassName: initUtsClassName,
initUtsPackageName: initUtsPackageName,
requireUTSPlugin: requireUTSPlugin,
registerUTSPlugin: registerUTSPlugin,
navigateTo: navigateTo,
reLaunch: reLaunch,
switchTab: switchTab,
...
...
packages/uni-app-plus/src/service/api/index.ts
浏览文件 @
66611642
...
...
@@ -90,6 +90,8 @@ export {
initUtsIndexClassName
,
initUtsClassName
,
initUtsPackageName
,
requireUTSPlugin
,
registerUTSPlugin
,
}
from
'
./plugin/uts
'
export
*
from
'
./ad/rewardedVideoAd
'
...
...
packages/uni-app-plus/src/service/api/plugin/uts.ts
浏览文件 @
66611642
...
...
@@ -387,3 +387,19 @@ export function initUtsClassName(
}
return
''
}
const
pluginDefines
:
Record
<
string
,
Record
<
string
,
unknown
>>
=
{}
export
function
registerUTSPlugin
(
name
:
string
,
define
:
Record
<
string
,
unknown
>
)
{
pluginDefines
[
name
]
=
define
}
export
function
requireUTSPlugin
(
name
:
string
)
{
const
define
=
pluginDefines
[
name
]
if
(
!
define
)
{
console
.
error
(
`
${
name
}
is not found`
)
}
return
define
}
packages/uni-uts-v1/__tests__/__snapshots__/code.spec.ts.snap
浏览文件 @
66611642
...
...
@@ -12,5 +12,26 @@ const cls = initUtsIndexClassName(name, is_uni_modules)
export const onMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarning', params: [{"name":"callback","type":"UTSCallback"}]})
export const offMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarning', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}]})
export default initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, 'User', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} ))
export const a = 1
"
`;
exports[`code genProxyCode cjs 1`] = `
"
import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app'
// const { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } = uni
const name = 'test-uts'
const errMsg = \`\`
const is_uni_modules = false
const pkg = initUtsPackageName(name, is_uni_modules)
const cls = initUtsIndexClassName(name, is_uni_modules)
Object.defineProperty(exports, '__esModule', { value: true })
exports.onMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'onMemoryWarning', params: [{"name":"callback","type":"UTSCallback"}]})
exports.offMemoryWarning = initUtsProxyFunction(false, { errMsg, main: true, package: pkg, class: cls, name: 'offMemoryWarning', params: [{"name":"callback","type":"UTSCallback","default":"UTSNull"}]})
exports.default = initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, 'User', is_uni_modules) }, {"constructor":{"params":[]},"methods":{},"staticMethods":{},"props":[],"staticProps":[]} ))
const a = 1
exports.a = a
"
`;
packages/uni-uts-v1/__tests__/code.spec.ts
浏览文件 @
66611642
import
{
resolve
}
from
'
path
'
import
{
genProxyCode
}
from
'
../src/code
'
import
{
FORMATS
,
genProxyCode
}
from
'
../src/code
'
import
{
ERR_MSG_PLACEHOLDER
}
from
'
../src/utils
'
const
pluginDir
=
resolve
(
__dirname
,
'
examples/uts/utssdk/test-uts
'
)
...
...
@@ -18,4 +18,18 @@ describe('code', () => {
).
replace
(
ERR_MSG_PLACEHOLDER
,
''
)
).
toMatchSnapshot
()
})
test
(
'
genProxyCode cjs
'
,
async
()
=>
{
expect
(
(
await
genProxyCode
(
pluginDir
,
{
id
:
'
test-uts
'
,
is_uni_modules
:
false
,
name
:
'
test-uts
'
,
namespace
:
'
uts.sdk.testUts
'
,
extname
:
'
.uts
'
,
format
:
FORMATS
.
CJS
,
})
).
replace
(
ERR_MSG_PLACEHOLDER
,
''
)
).
toMatchSnapshot
()
})
})
packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-android/uts/utssdk/test-uts/manifest.json
浏览文件 @
66611642
...
...
@@ -11,7 +11,7 @@
"md5"
:
"d41d8cd98f00b204e9800998ecf8427e"
},
"index.uts"
:
{
"md5"
:
"0
12e7488376fec3b6d104a0242a6353
5"
"md5"
:
"0
ab09e7091607dbfb059a9745160ad2
5"
},
"package.json"
:
{
"md5"
:
"9595031a0d4158abb72060cdf3a200c1"
...
...
packages/uni-uts-v1/__tests__/examples/uts/unpackage/cache/app-ios/uts/utssdk/test-uts/manifest.json
浏览文件 @
66611642
...
...
@@ -11,7 +11,7 @@
"md5"
:
"d41d8cd98f00b204e9800998ecf8427e"
},
"index.uts"
:
{
"md5"
:
"0
12e7488376fec3b6d104a0242a6353
5"
"md5"
:
"0
ab09e7091607dbfb059a9745160ad2
5"
},
"package.json"
:
{
"md5"
:
"9595031a0d4158abb72060cdf3a200c1"
...
...
packages/uni-uts-v1/__tests__/examples/uts/utssdk/test-uts/index.uts
浏览文件 @
66611642
export function onMemoryWarning(callback: (level: number) => void) {}
export function onMemoryWarning(callback: (level: number) => void) {
}
export function offMemoryWarning(
callback: ((level: number) => void) | null = null
) {}
\ No newline at end of file
) { }
export default class User {
}
export const a = 1
export let b = 2, c = 3
\ No newline at end of file
packages/uni-uts-v1/src/code.ts
浏览文件 @
66611642
...
...
@@ -13,10 +13,16 @@ import {
Param
,
TsTypeAnnotation
,
VariableDeclaration
,
VariableDeclarationKind
,
}
from
'
../types/types
'
import
{
createResolveTypeReferenceName
,
ERR_MSG_PLACEHOLDER
}
from
'
./utils
'
import
{
isInHBuilderX
}
from
'
./shared
'
import
{
camelize
,
capitalize
}
from
'
@vue/shared
'
export
const
enum
FORMATS
{
ES
=
'
es
'
,
CJS
=
'
cjs
'
,
}
interface
GenProxyCodeOptions
{
is_uni_modules
:
boolean
id
:
string
...
...
@@ -25,13 +31,14 @@ interface GenProxyCodeOptions {
namespace
:
string
androidComponents
?:
Record
<
string
,
string
>
iosComponents
?:
Record
<
string
,
string
>
format
?:
FORMATS
}
export
async
function
genProxyCode
(
module
:
string
,
options
:
GenProxyCodeOptions
)
{
const
{
name
,
is_uni_modules
}
=
options
const
{
name
,
is_uni_modules
,
format
}
=
options
return
`
import { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } from '@dcloudio/uni-app'
// const { initUtsProxyClass, initUtsProxyFunction, initUtsPackageName, initUtsIndexClassName, initUtsClassName } = uni
...
...
@@ -44,7 +51,7 @@ ${genComponentsCode(
options
.
androidComponents
||
{},
options
.
iosComponents
||
{}
)}
${
genModuleCode
(
await
parseModuleDecls
(
module
,
options
))}
${
genModuleCode
(
await
parseModuleDecls
(
module
,
options
)
,
format
)}
`
}
...
...
@@ -92,19 +99,35 @@ export function resolvePlatformIndex(
return
fs
.
existsSync
(
filename
)
&&
filename
}
function
genModuleCode
(
decls
:
ProxyDecl
[])
{
function
exportDefaultCode
(
format
:
FORMATS
)
{
return
format
===
FORMATS
.
ES
?
'
export default
'
:
'
exports.default =
'
}
function
exportVarCode
(
format
:
FORMATS
,
kind
:
VariableDeclarationKind
)
{
if
(
format
===
FORMATS
.
ES
)
{
return
`export
${
kind
}
`
}
return
`exports.`
}
function
genModuleCode
(
decls
:
ProxyDecl
[],
format
:
FORMATS
=
FORMATS
.
ES
)
{
const
codes
:
string
[]
=
[]
if
(
format
===
FORMATS
.
CJS
)
{
codes
.
push
(
`Object.defineProperty(exports, '__esModule', { value: true })`
)
}
const
exportDefault
=
exportDefaultCode
(
format
)
const
exportConst
=
exportVarCode
(
format
,
'
const
'
)
decls
.
forEach
((
decl
)
=>
{
if
(
decl
.
type
===
'
Class
'
)
{
if
(
decl
.
isDefault
)
{
codes
.
push
(
`
export default
initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '
${
`
${
exportDefault
}
initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '
${
decl
.
cls
}
', is_uni_modules) },
${
JSON
.
stringify
(
decl
.
options
)}
))`
)
}
else
{
codes
.
push
(
`
export const
${
`
${
exportConst
}
${
decl
.
cls
}
= initUtsProxyClass(Object.assign({ errMsg, package: pkg, class: initUtsClassName(name, '
${
decl
.
cls
...
...
@@ -114,7 +137,7 @@ function genModuleCode(decls: ProxyDecl[]) {
}
else
if
(
decl
.
type
===
'
FunctionDeclaration
'
)
{
if
(
decl
.
isDefault
)
{
codes
.
push
(
`
export default i
nitUtsProxyFunction(
${
`
${
exportDefault
}
nitUtsProxyFunction(
${
decl
.
async
}
, { errMsg, main: true, package: pkg, class: cls, name: '
${
decl
.
method
...
...
@@ -122,7 +145,7 @@ function genModuleCode(decls: ProxyDecl[]) {
)
}
else
{
codes
.
push
(
`
export const
${
decl
.
method
}
= initUtsProxyFunction(
${
`
${
exportConst
}
${
decl
.
method
}
= initUtsProxyFunction(
${
decl
.
async
}
, { errMsg, main: true, package: pkg, class: cls, name: '
${
decl
.
method
...
...
@@ -130,11 +153,28 @@ function genModuleCode(decls: ProxyDecl[]) {
)
}
}
else
if
(
decl
.
type
===
'
VariableDeclaration
'
)
{
codes
.
push
(
`export
${
decl
.
kind
}
${
decl
.
declarations
.
map
((
d
)
=>
`
${(
d
.
id
as
Identifier
).
value
}
=
${
genInitCode
(
d
.
init
!
)}
`
)
.
join
(
'
,
'
)}
`
)
if
(
format
===
FORMATS
.
ES
)
{
codes
.
push
(
`export
${
decl
.
kind
}
${
decl
.
declarations
.
map
(
(
d
)
=>
`
${(
d
.
id
as
Identifier
).
value
}
=
${
genInitCode
(
d
.
init
!
)}
`
)
.
join
(
'
,
'
)}
`
)
}
else
if
(
format
===
FORMATS
.
CJS
)
{
codes
.
push
(
`
${
decl
.
kind
}
${
decl
.
declarations
.
map
(
(
d
)
=>
`
${(
d
.
id
as
Identifier
).
value
}
=
${
genInitCode
(
d
.
init
!
)}
`
)
.
join
(
'
,
'
)}
`
)
const
exportVar
=
exportVarCode
(
format
,
decl
.
kind
)
decl
.
declarations
.
forEach
((
d
)
=>
{
const
name
=
(
d
.
id
as
Identifier
).
value
codes
.
push
(
`
${
exportVar
}${
name
}
=
${
name
}
`
)
})
}
}
})
return
codes
.
join
(
`\n`
)
...
...
packages/uni-uts-v1/src/index.ts
浏览文件 @
66611642
...
...
@@ -15,6 +15,7 @@ import {
}
from
'
./swift
'
import
{
FORMATS
,
genProxyCode
,
resolvePlatformIndex
,
resolvePlatformIndexFilename
,
...
...
@@ -93,7 +94,17 @@ export async function compile(pluginDir: string) {
const
deps
:
string
[]
=
[]
const
code
=
await
genProxyCode
(
pluginDir
,
extend
({
androidComponents
,
iosComponents
},
pkg
)
extend
(
{
androidComponents
,
iosComponents
,
format
:
process
.
env
.
UNI_UTS_JS_CODE_FORMAT
===
'
cjs
'
?
FORMATS
.
CJS
:
FORMATS
.
ES
,
},
pkg
)
)
let
errMsg
=
''
if
(
process
.
env
.
NODE_ENV
!==
'
development
'
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录