Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Metz
uni-app
提交
aae9ab34
U
uni-app
项目概览
Metz
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
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,发现更多精彩内容 >>
提交
aae9ab34
编写于
5月 07, 2022
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(uts): add log
上级
5b1eb4f3
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
326 addition
and
123 deletion
+326
-123
packages/uts-darwin-arm64/uts.darwin-arm64.node
packages/uts-darwin-arm64/uts.darwin-arm64.node
+0
-0
packages/uts-win32-x64-msvc/uts.win32-x64-msvc.node
packages/uts-win32-x64-msvc/uts.win32-x64-msvc.node
+0
-0
packages/uts/dist/index.d.ts
packages/uts/dist/index.d.ts
+11
-2
packages/uts/dist/index.js
packages/uts/dist/index.js
+94
-41
packages/uts/dist/types.d.ts
packages/uts/dist/types.d.ts
+1
-5
packages/uts/package.json
packages/uts/package.json
+2
-1
packages/uts/src/api.ts
packages/uts/src/api.ts
+6
-2
packages/uts/src/cli.ts
packages/uts/src/cli.ts
+9
-4
packages/uts/src/index.ts
packages/uts/src/index.ts
+151
-52
packages/uts/src/types.ts
packages/uts/src/types.ts
+1
-16
packages/uts/src/utils.ts
packages/uts/src/utils.ts
+46
-0
pnpm-lock.yaml
pnpm-lock.yaml
+5
-0
未找到文件。
packages/uts-darwin-arm64/uts.darwin-arm64.node
浏览文件 @
aae9ab34
无法预览此类型文件
packages/uts-win32-x64-msvc/uts.win32-x64-msvc.node
浏览文件 @
aae9ab34
无法预览此类型文件
packages/uts/dist/index.d.ts
浏览文件 @
aae9ab34
export
declare
const
enum
UtsTarget
{
KOTLIN
=
"
kotlin
"
,
SWIFT
=
"
swift
"
}
export
declare
type
UtsMode
=
'
dev
'
|
'
build
'
;
export
interface
ToOptions
{
export
interface
ToOptions
{
/**
* 为 true 时,禁用日志输出,默认为 false
*/
silent
?:
boolean
;
input
:
{
input
:
{
/**
/**
* 插件根目录
* 插件根目录
...
@@ -24,5 +33,5 @@ export interface ToOptions {
...
@@ -24,5 +33,5 @@ export interface ToOptions {
inlineSourcesContent
?:
boolean
;
inlineSourcesContent
?:
boolean
;
};
};
}
}
export
declare
function
runDev
(
target
:
'
kotlin
'
|
'
swift
'
,
opts
:
ToOptions
):
void
;
export
declare
function
runDev
(
target
:
UtsTarget
,
opts
:
ToOptions
):
void
;
export
declare
function
runBuild
(
target
:
'
kotlin
'
|
'
swift
'
,
opts
:
ToOptions
):
void
|
Promise
<
void
>
;
export
declare
function
runBuild
(
target
:
UtsTarget
,
opts
:
ToOptions
):
void
|
Promise
<
import
(
"
./types
"
).
UtsResult
[]
>
;
packages/uts/dist/index.js
浏览文件 @
aae9ab34
...
@@ -10,16 +10,30 @@ const fast_glob_1 = __importDefault(require("fast-glob"));
...
@@ -10,16 +10,30 @@ const fast_glob_1 = __importDefault(require("fast-glob"));
const
chokidar_1
=
require
(
"
chokidar
"
);
const
chokidar_1
=
require
(
"
chokidar
"
);
const
api_1
=
require
(
"
./api
"
);
const
api_1
=
require
(
"
./api
"
);
const
utils_1
=
require
(
"
./utils
"
);
const
utils_1
=
require
(
"
./utils
"
);
const
targetDirs
=
{
[
"
kotlin
"
/* KOTLIN */
]:
'
android
'
,
[
"
swift
"
/* SWIFT */
]:
'
ios
'
,
};
function
resolveDefaultOutputDir
(
mode
,
inputDir
)
{
function
resolveDefaultOutputDir
(
mode
,
inputDir
)
{
return
path_1
.
default
.
resolve
(
inputDir
,
'
../dist/
'
+
mode
+
'
/kotlin
'
);
return
path_1
.
default
.
resolve
(
inputDir
,
'
../dist/
'
+
mode
);
}
}
function
parseOptions
(
mode
,
opts
)
{
function
parseOptions
(
mode
,
target
,
opts
)
{
const
{
input
}
=
opts
;
const
{
input
}
=
opts
;
if
(
!
(
input
===
null
||
input
===
void
0
?
void
0
:
input
.
dir
))
{
if
(
!
(
input
===
null
||
input
===
void
0
?
void
0
:
input
.
dir
))
{
throw
new
Error
(
`input.dir is required`
);
throw
new
Error
(
`input.dir is required
.
`
);
}
}
if
(
!
fs_extra_1
.
default
.
existsSync
(
input
.
dir
))
{
if
(
!
fs_extra_1
.
default
.
existsSync
(
input
.
dir
))
{
throw
new
Error
(
`
${
input
}
is not found`
);
throw
new
Error
(
`
${
input
}
is not found.`
);
}
let
inputSrcDir
=
''
;
if
(
target
===
"
kotlin
"
/* KOTLIN */
)
{
inputSrcDir
=
resolveKotlinSrcDir
(
input
.
dir
);
}
else
{
throw
new
Error
(
`
${
target
}
is unsupported.`
);
}
if
(
!
fs_extra_1
.
default
.
existsSync
(
inputSrcDir
))
{
throw
new
Error
(
`
${
inputSrcDir
}
is not found.`
);
}
}
if
(
!
opts
.
output
)
{
if
(
!
opts
.
output
)
{
opts
.
output
=
{
opts
.
output
=
{
...
@@ -30,66 +44,102 @@ function parseOptions(mode, opts) {
...
@@ -30,66 +44,102 @@ function parseOptions(mode, opts) {
if
(
!
opts
.
output
.
dir
)
{
if
(
!
opts
.
output
.
dir
)
{
opts
.
output
.
dir
=
resolveDefaultOutputDir
(
mode
,
input
.
dir
);
opts
.
output
.
dir
=
resolveDefaultOutputDir
(
mode
,
input
.
dir
);
}
}
opts
.
silent
=
opts
.
silent
===
true
;
return
opts
;
return
opts
;
}
}
const
EXTNAME
=
'
.uts
'
;
const
EXTNAME
=
'
.uts
'
;
function
watchSwift
(
_
)
{
}
function
resolveKotlinSrcDir
(
dir
)
{
function
buildSwift
(
_
)
{
}
return
path_1
.
default
.
join
(
dir
,
targetDirs
[
"
kotlin
"
/* KOTLIN */
]
+
'
/src
'
);
function
watchKotlin
({
input
:
{
dir
:
inputDir
,
extname
},
output
:
{
dir
:
outputDir
,
sourceMap
},
})
{
}
const
input
=
{
function
initInputKotlinOptions
(
root
)
{
root
:
inputDir
,
return
{
root
,
filename
:
''
,
filename
:
''
,
};
};
const
output
=
{
}
outDir
:
outputDir
,
function
initOutputKotlinOptions
(
outDir
,
sourceMap
,
inlineSourcesContent
)
{
return
{
outDir
,
sourceMap
,
sourceMap
,
inlineSourcesContent
,
};
};
}
function
watchSwift
(
_
)
{
}
function
buildSwift
(
_
)
{
}
function
watchKotlin
({
silent
,
input
:
{
dir
:
inputDir
,
extname
},
output
:
{
dir
:
outputDir
,
sourceMap
,
inlineSourcesContent
},
})
{
fs_extra_1
.
default
.
emptyDirSync
(
outputDir
);
extname
=
extname
||
EXTNAME
;
extname
=
extname
||
EXTNAME
;
const
inputSrcDir
=
resolveKotlinSrcDir
(
inputDir
);
const
outputSrcDir
=
resolveKotlinSrcDir
(
outputDir
);
const
input
=
initInputKotlinOptions
(
inputSrcDir
);
const
output
=
initOutputKotlinOptions
(
outputSrcDir
,
sourceMap
,
!!
inlineSourcesContent
);
(
0
,
chokidar_1
.
watch
)(
'
**/*
'
+
extname
,
{
(
0
,
chokidar_1
.
watch
)(
'
**/*
'
+
extname
,
{
cwd
:
inputDir
,
cwd
:
input
Src
Dir
,
ignored
:
[
'
**/*.d
'
+
extname
],
ignored
:
[
'
**/*.d
'
+
extname
],
})
})
.
on
(
'
add
'
,
(
filename
)
=>
buildKotlinFile
(
path_1
.
default
.
resolve
(
inputDir
,
filename
),
input
,
output
))
.
on
(
'
add
'
,
(
filename
)
=>
{
.
on
(
'
change
'
,
(
filename
)
=>
buildKotlinFile
(
path_1
.
default
.
resolve
(
inputDir
,
filename
),
input
,
output
))
buildKotlinFile
(
path_1
.
default
.
resolve
(
inputSrcDir
,
filename
),
input
,
output
).
then
((
res
)
=>
{
!
silent
&&
(
0
,
utils_1
.
printUtsResult
)(
res
);
});
})
.
on
(
'
change
'
,
(
filename
)
=>
{
buildKotlinFile
(
path_1
.
default
.
resolve
(
inputSrcDir
,
filename
),
input
,
output
).
then
((
res
)
=>
{
!
silent
&&
(
0
,
utils_1
.
printUtsResult
)(
res
);
});
})
.
on
(
'
unlink
'
,
(
filename
)
=>
{
.
on
(
'
unlink
'
,
(
filename
)
=>
{
try
{
try
{
fs_extra_1
.
default
.
unlinkSync
(
path_1
.
default
.
resolve
(
outputDir
,
filename
));
fs_extra_1
.
default
.
unlinkSync
(
path_1
.
default
.
resolve
(
output
Src
Dir
,
filename
));
}
}
catch
(
e
)
{
}
catch
(
e
)
{
}
})
})
.
on
(
'
ready
'
,
()
=>
{
.
on
(
'
ready
'
,
()
=>
{
copyAssets
(
inputDir
,
outputDir
,
extname
);
copyAssets
(
"
kotlin
"
/* KOTLIN */
,
inputDir
,
outputDir
,
extname
);
});
});
}
}
function
buildKotlin
({
input
:
{
dir
:
inputDir
,
extname
},
output
:
{
dir
:
outputDir
,
sourceMap
,
inlineSourcesContent
},
})
{
function
buildKotlin
({
silent
,
input
:
{
dir
:
inputDir
,
extname
},
output
:
{
dir
:
outputDir
,
sourceMap
,
inlineSourcesContent
},
})
{
fs_extra_1
.
default
.
emptyDirSync
(
outputDir
);
extname
=
extname
||
EXTNAME
;
extname
=
extname
||
EXTNAME
;
const
inputSrcDir
=
resolveKotlinSrcDir
(
inputDir
);
const
outputSrcDir
=
resolveKotlinSrcDir
(
outputDir
);
const
input
=
initInputKotlinOptions
(
inputSrcDir
);
const
output
=
initOutputKotlinOptions
(
outputSrcDir
,
sourceMap
,
!!
inlineSourcesContent
);
const
files
=
fast_glob_1
.
default
.
sync
(
'
**/*
'
+
extname
,
{
const
files
=
fast_glob_1
.
default
.
sync
(
'
**/*
'
+
extname
,
{
absolute
:
true
,
absolute
:
true
,
cwd
:
inputDir
,
cwd
:
input
Src
Dir
,
ignore
:
[
'
**/*.d
'
+
extname
],
ignore
:
[
'
**/*.d
'
+
extname
],
});
});
const
input
=
{
return
Promise
.
all
(
files
.
map
((
filename
)
=>
buildKotlinFile
(
filename
,
input
,
output
)))
root
:
inputDir
,
.
then
((
res
)
=>
{
filename
:
''
,
return
copyAssets
(
"
kotlin
"
/* KOTLIN */
,
inputDir
,
outputDir
,
extname
).
then
(()
=>
res
);
};
})
const
output
=
{
.
then
((
res
)
=>
{
outDir
:
outputDir
,
!
silent
&&
(
0
,
utils_1
.
printUtsResults
)(
res
);
sourceMap
,
return
res
;
inlineSourcesContent
:
!!
inlineSourcesContent
,
};
return
Promise
.
all
(
files
.
map
((
filename
)
=>
buildKotlinFile
(
filename
,
input
,
output
))).
then
(()
=>
{
return
copyAssets
(
inputDir
,
outputDir
,
extname
);
});
});
}
}
function
copyAssets
(
inputDir
,
outputDir
,
extname
)
{
function
copyAssets
(
target
,
inputDir
,
outputDir
,
extname
)
{
inputDir
=
path_1
.
default
.
resolve
(
inputDir
);
outputDir
=
path_1
.
default
.
resolve
(
outputDir
);
const
kotlinRootDir
=
path_1
.
default
.
join
(
inputDir
,
targetDirs
[
"
kotlin
"
/* KOTLIN */
]);
const
swiftRootDir
=
path_1
.
default
.
join
(
inputDir
,
targetDirs
[
"
swift
"
/* SWIFT */
]);
return
fs_extra_1
.
default
.
copy
(
inputDir
,
outputDir
,
{
return
fs_extra_1
.
default
.
copy
(
inputDir
,
outputDir
,
{
filter
(
src
)
{
filter
(
src
)
{
return
path_1
.
default
.
extname
(
src
)
!==
extname
;
if
(
target
===
"
kotlin
"
/* KOTLIN */
)
{
if
(
src
===
swiftRootDir
)
{
return
false
;
}
}
else
if
(
target
===
"
swift
"
/* SWIFT */
)
{
if
(
src
===
kotlinRootDir
)
{
return
false
;
}
}
return
!
[
extname
,
'
.ts
'
].
includes
(
path_1
.
default
.
extname
(
src
));
},
},
});
});
}
}
function
buildKotlinFile
(
filename
,
input
,
output
)
{
function
buildKotlinFile
(
filename
,
input
,
output
)
{
const
label
=
(
0
,
utils_1
.
normalizePath
)(
path_1
.
default
.
relative
(
input
.
root
,
filename
));
const
toKotlinOptions
=
{
const
toKotlinOptions
=
{
input
:
{
input
:
{
...
input
,
...
input
,
...
@@ -100,27 +150,30 @@ function buildKotlinFile(filename, input, output) {
...
@@ -100,27 +150,30 @@ function buildKotlinFile(filename, input, output) {
...
output
,
...
output
,
},
},
};
};
console
.
time
(
label
);
const
start
=
process
.
hrtime
();
return
(
0
,
api_1
.
toKotlin
)(
toKotlinOptions
).
then
(()
=>
{
return
(
0
,
api_1
.
toKotlin
)(
toKotlinOptions
).
then
((
res
)
=>
{
console
.
timeEnd
(
label
);
res
.
time
=
(
0
,
utils_1
.
timeEnd
)(
start
);
return
res
;
});
});
}
}
function
runDev
(
target
,
opts
)
{
function
runDev
(
target
,
opts
)
{
opts
=
parseOptions
(
'
dev
'
,
opts
);
opts
=
parseOptions
(
'
dev
'
,
target
,
opts
);
!
opts
.
silent
&&
(
0
,
utils_1
.
printStartup
)(
target
,
'
development
'
);
switch
(
target
)
{
switch
(
target
)
{
case
'
kotlin
'
:
case
"
kotlin
"
/* KOTLIN */
:
return
watchKotlin
(
opts
);
return
watchKotlin
(
opts
);
case
'
swift
'
:
case
"
swift
"
/* SWIFT */
:
return
watchSwift
(
opts
);
return
watchSwift
(
opts
);
}
}
}
}
exports
.
runDev
=
runDev
;
exports
.
runDev
=
runDev
;
function
runBuild
(
target
,
opts
)
{
function
runBuild
(
target
,
opts
)
{
opts
=
parseOptions
(
'
build
'
,
opts
);
opts
=
parseOptions
(
'
build
'
,
target
,
opts
);
!
opts
.
silent
&&
(
0
,
utils_1
.
printStartup
)(
target
,
'
production
'
);
switch
(
target
)
{
switch
(
target
)
{
case
'
kotlin
'
:
case
"
kotlin
"
/* KOTLIN */
:
return
buildKotlin
(
opts
);
return
buildKotlin
(
opts
);
case
'
swift
'
:
case
"
swift
"
/* SWIFT */
:
return
buildSwift
(
opts
);
return
buildSwift
(
opts
);
}
}
}
}
...
...
packages/uts/dist/types.d.ts
浏览文件 @
aae9ab34
export
declare
type
UtsTarget
=
'
es3
'
|
'
es5
'
|
'
es2015
'
|
'
es2016
'
|
'
es2017
'
|
'
es2018
'
|
'
es2019
'
|
'
es2020
'
|
'
es2021
'
|
'
es2022
'
;
export
interface
UtsParserConfig
{
export
interface
UtsParserConfig
{
/**
/**
* Defaults to `false`.
* Defaults to `false`.
...
@@ -16,10 +15,6 @@ export interface UtsParserConfig {
...
@@ -16,10 +15,6 @@ export interface UtsParserConfig {
export
declare
type
UtsParseOptions
=
UtsParserConfig
&
{
export
declare
type
UtsParseOptions
=
UtsParserConfig
&
{
filename
?:
string
;
filename
?:
string
;
comments
?:
boolean
;
comments
?:
boolean
;
/**
* default es2022
*/
target
?:
UtsTarget
;
};
};
export
declare
type
InputKotlinOptions
=
UtsParseOptions
&
{
export
declare
type
InputKotlinOptions
=
UtsParseOptions
&
{
root
:
string
;
root
:
string
;
...
@@ -43,4 +38,5 @@ export interface UtsSwiftOptions {
...
@@ -43,4 +38,5 @@ export interface UtsSwiftOptions {
}
}
export
interface
UtsResult
{
export
interface
UtsResult
{
filename
?:
string
;
filename
?:
string
;
time
?:
number
;
}
}
packages/uts/package.json
浏览文件 @
aae9ab34
...
@@ -20,7 +20,8 @@
...
@@ -20,7 +20,8 @@
"cac"
:
"6.7.9"
,
"cac"
:
"6.7.9"
,
"chokidar"
:
"^3.5.3"
,
"chokidar"
:
"^3.5.3"
,
"fast-glob"
:
"^3.2.11"
,
"fast-glob"
:
"^3.2.11"
,
"fs-extra"
:
"^10.0.0"
"fs-extra"
:
"^10.0.0"
,
"picocolors"
:
"^1.0.0"
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"@types/fs-extra"
:
"^9.0.13"
"@types/fs-extra"
:
"^9.0.13"
...
...
packages/uts/src/api.ts
浏览文件 @
aae9ab34
...
@@ -25,11 +25,15 @@ export function toKotlin(options: UtsKotlinOptions): Promise<UtsResult> {
...
@@ -25,11 +25,15 @@ export function toKotlin(options: UtsKotlinOptions): Promise<UtsResult> {
}
else
if
(
output
.
sourceMap
===
false
)
{
}
else
if
(
output
.
sourceMap
===
false
)
{
output
.
sourceMap
=
''
output
.
sourceMap
=
''
}
}
return
bindings
.
toKotlin
(
toBuffer
(
options
))
return
bindings
.
toKotlin
(
toBuffer
(
options
))
.
then
((
res
:
string
)
=>
JSON
.
parse
(
res
))
}
}
export
function
toSwift
(
options
:
UtsSwiftOptions
):
Promise
<
UtsResult
>
{
export
function
toSwift
(
options
:
UtsSwiftOptions
):
Promise
<
UtsResult
>
{
return
bindings
.
toSwift
(
toBuffer
(
options
))
return
bindings
.
toSwift
(
toBuffer
(
options
))
.
then
((
res
:
string
)
=>
JSON
.
parse
(
res
))
}
}
function
toBuffer
(
t
:
any
):
Buffer
{
function
toBuffer
(
t
:
any
):
Buffer
{
...
...
packages/uts/src/cli.ts
浏览文件 @
aae9ab34
import
{
cac
}
from
'
cac
'
import
{
cac
}
from
'
cac
'
import
{
runBuild
,
runDev
,
ToOptions
}
from
'
.
'
import
{
runBuild
,
runDev
,
ToOptions
,
UtsTarget
}
from
'
.
'
const
cli
=
cac
(
'
uts
'
)
const
cli
=
cac
(
'
uts
'
)
export
interface
CliOptions
{
export
interface
CliOptions
{
target
:
'
kotlin
'
|
'
swift
'
target
:
UtsTarget
silent
:
boolean
sourceMap
:
boolean
sourceMap
:
boolean
inlineSourcesContent
:
boolean
inlineSourcesContent
:
boolean
watch
:
boolean
watch
:
boolean
...
@@ -14,9 +15,9 @@ export interface CliOptions {
...
@@ -14,9 +15,9 @@ export interface CliOptions {
cli
cli
.
command
(
'
<input> [output]
'
)
.
command
(
'
<input> [output]
'
)
.
option
(
'
-t, --target <target>
'
,
'
[string] kotlin | swift
'
,
{
.
option
(
'
-t, --target <target>
'
,
'
[string] kotlin | swift
'
,
{
default
:
'
kotlin
'
,
default
:
UtsTarget
.
KOTLIN
,
})
})
.
option
(
'
-s, --sourceMap [sourceMap]
'
,
`[boolean] output sourceMap`
,
{
.
option
(
'
-s, --sourceMap [sourceMap]
'
,
`[boolean
|string
] output sourceMap`
,
{
default
:
false
,
default
:
false
,
})
})
.
option
(
.
option
(
...
@@ -32,8 +33,12 @@ cli
...
@@ -32,8 +33,12 @@ cli
.
option
(
'
-e, --extname [extname]
'
,
`[string] extname`
,
{
.
option
(
'
-e, --extname [extname]
'
,
`[string] extname`
,
{
default
:
'
.uts
'
,
default
:
'
.uts
'
,
})
})
.
option
(
'
--silent
'
,
`[boolean] disable log`
,
{
default
:
false
,
})
.
action
((
input
,
output
,
opts
:
CliOptions
)
=>
{
.
action
((
input
,
output
,
opts
:
CliOptions
)
=>
{
const
toOptions
:
ToOptions
=
{
const
toOptions
:
ToOptions
=
{
silent
:
opts
.
silent
,
input
:
{
input
:
{
dir
:
input
,
dir
:
input
,
extname
:
opts
.
extname
,
extname
:
opts
.
extname
,
...
...
packages/uts/src/index.ts
浏览文件 @
aae9ab34
...
@@ -6,9 +6,24 @@ import { watch } from 'chokidar'
...
@@ -6,9 +6,24 @@ import { watch } from 'chokidar'
import
{
InputKotlinOptions
,
toKotlin
}
from
'
./api
'
import
{
InputKotlinOptions
,
toKotlin
}
from
'
./api
'
import
{
OutputKotlinOptions
,
UtsKotlinOptions
}
from
'
./types
'
import
{
OutputKotlinOptions
,
UtsKotlinOptions
}
from
'
./types
'
import
{
normalizePath
}
from
'
./utils
'
import
{
printStartup
,
printUtsResult
,
printUtsResults
,
timeEnd
}
from
'
./utils
'
export
const
enum
UtsTarget
{
KOTLIN
=
'
kotlin
'
,
SWIFT
=
'
swift
'
,
}
export
type
UtsMode
=
'
dev
'
|
'
build
'
const
targetDirs
=
{
[
UtsTarget
.
KOTLIN
]:
'
android
'
,
[
UtsTarget
.
SWIFT
]:
'
ios
'
,
}
as
const
export
interface
ToOptions
{
export
interface
ToOptions
{
/**
* 为 true 时,禁用日志输出,默认为 false
*/
silent
?:
boolean
input
:
{
input
:
{
/**
/**
* 插件根目录
* 插件根目录
...
@@ -38,18 +53,32 @@ export interface ToOptions {
...
@@ -38,18 +53,32 @@ export interface ToOptions {
interface
ToKotlinOptions
extends
ToOptions
{}
interface
ToKotlinOptions
extends
ToOptions
{}
interface
ToSwiftOptions
extends
ToOptions
{}
interface
ToSwiftOptions
extends
ToOptions
{}
function
resolveDefaultOutputDir
(
mode
:
'
dev
'
|
'
build
'
,
inputDir
:
string
)
{
function
resolveDefaultOutputDir
(
mode
:
UtsMode
,
inputDir
:
string
)
{
return
path
.
resolve
(
inputDir
,
'
../dist/
'
+
mode
+
'
/kotlin
'
)
return
path
.
resolve
(
inputDir
,
'
../dist/
'
+
mode
)
}
}
function
parseOptions
(
mode
:
'
dev
'
|
'
build
'
,
opts
:
Partial
<
ToKotlinOptions
>
)
{
function
parseOptions
(
mode
:
UtsMode
,
target
:
UtsTarget
,
opts
:
Partial
<
ToKotlinOptions
>
)
{
const
{
input
}
=
opts
const
{
input
}
=
opts
if
(
!
input
?.
dir
)
{
if
(
!
input
?.
dir
)
{
throw
new
Error
(
`input.dir is required`
)
throw
new
Error
(
`input.dir is required
.
`
)
}
}
if
(
!
fs
.
existsSync
(
input
.
dir
))
{
if
(
!
fs
.
existsSync
(
input
.
dir
))
{
throw
new
Error
(
`
${
input
}
is not found`
)
throw
new
Error
(
`
${
input
}
is not found.`
)
}
let
inputSrcDir
:
string
=
''
if
(
target
===
UtsTarget
.
KOTLIN
)
{
inputSrcDir
=
resolveKotlinSrcDir
(
input
.
dir
)
}
else
{
throw
new
Error
(
`
${
target
}
is unsupported.`
)
}
}
if
(
!
fs
.
existsSync
(
inputSrcDir
))
{
throw
new
Error
(
`
${
inputSrcDir
}
is not found.`
)
}
if
(
!
opts
.
output
)
{
if
(
!
opts
.
output
)
{
opts
.
output
=
{
opts
.
output
=
{
dir
:
''
,
dir
:
''
,
...
@@ -59,75 +88,143 @@ function parseOptions(mode: 'dev' | 'build', opts: Partial<ToKotlinOptions>) {
...
@@ -59,75 +88,143 @@ function parseOptions(mode: 'dev' | 'build', opts: Partial<ToKotlinOptions>) {
if
(
!
opts
.
output
.
dir
)
{
if
(
!
opts
.
output
.
dir
)
{
opts
.
output
.
dir
=
resolveDefaultOutputDir
(
mode
,
input
.
dir
)
opts
.
output
.
dir
=
resolveDefaultOutputDir
(
mode
,
input
.
dir
)
}
}
opts
.
silent
=
opts
.
silent
===
true
return
opts
as
ToKotlinOptions
return
opts
as
ToKotlinOptions
}
}
const
EXTNAME
=
'
.uts
'
const
EXTNAME
=
'
.uts
'
function
resolveKotlinSrcDir
(
dir
:
string
)
{
return
path
.
join
(
dir
,
targetDirs
[
UtsTarget
.
KOTLIN
]
+
'
/src
'
)
}
function
initInputKotlinOptions
(
root
:
string
):
InputKotlinOptions
{
return
{
root
,
filename
:
''
,
}
}
function
initOutputKotlinOptions
(
outDir
:
string
,
sourceMap
:
string
|
boolean
,
inlineSourcesContent
:
boolean
):
OutputKotlinOptions
{
return
{
outDir
,
sourceMap
,
inlineSourcesContent
,
}
}
function
watchSwift
(
_
:
ToSwiftOptions
)
{}
function
watchSwift
(
_
:
ToSwiftOptions
)
{}
function
buildSwift
(
_
:
ToSwiftOptions
)
{}
function
buildSwift
(
_
:
ToSwiftOptions
)
{}
function
watchKotlin
({
function
watchKotlin
({
silent
,
input
:
{
dir
:
inputDir
,
extname
},
input
:
{
dir
:
inputDir
,
extname
},
output
:
{
dir
:
outputDir
,
sourceMap
},
output
:
{
dir
:
outputDir
,
sourceMap
,
inlineSourcesContent
},
}:
ToKotlinOptions
)
{
}:
ToKotlinOptions
)
{
const
input
:
InputKotlinOptions
=
{
fs
.
emptyDirSync
(
outputDir
)
root
:
inputDir
,
filename
:
''
,
}
const
output
:
OutputKotlinOptions
=
{
outDir
:
outputDir
,
sourceMap
,
}
extname
=
extname
||
EXTNAME
extname
=
extname
||
EXTNAME
const
inputSrcDir
=
resolveKotlinSrcDir
(
inputDir
)
const
outputSrcDir
=
resolveKotlinSrcDir
(
outputDir
)
const
input
=
initInputKotlinOptions
(
inputSrcDir
)
const
output
=
initOutputKotlinOptions
(
outputSrcDir
,
sourceMap
,
!!
inlineSourcesContent
)
watch
(
'
**/*
'
+
extname
,
{
watch
(
'
**/*
'
+
extname
,
{
cwd
:
inputDir
,
cwd
:
input
Src
Dir
,
ignored
:
[
'
**/*.d
'
+
extname
],
ignored
:
[
'
**/*.d
'
+
extname
],
})
})
.
on
(
'
add
'
,
(
filename
)
=>
.
on
(
'
add
'
,
(
filename
)
=>
{
buildKotlinFile
(
path
.
resolve
(
inputDir
,
filename
),
input
,
output
)
buildKotlinFile
(
path
.
resolve
(
inputSrcDir
,
filename
),
input
,
output
).
then
(
)
(
res
)
=>
{
.
on
(
'
change
'
,
(
filename
)
=>
!
silent
&&
printUtsResult
(
res
)
buildKotlinFile
(
path
.
resolve
(
inputDir
,
filename
),
input
,
output
)
}
)
)
})
.
on
(
'
change
'
,
(
filename
)
=>
{
buildKotlinFile
(
path
.
resolve
(
inputSrcDir
,
filename
),
input
,
output
).
then
(
(
res
)
=>
{
!
silent
&&
printUtsResult
(
res
)
}
)
})
.
on
(
'
unlink
'
,
(
filename
)
=>
{
.
on
(
'
unlink
'
,
(
filename
)
=>
{
try
{
try
{
fs
.
unlinkSync
(
path
.
resolve
(
outputDir
,
filename
))
fs
.
unlinkSync
(
path
.
resolve
(
output
Src
Dir
,
filename
))
}
catch
(
e
)
{}
}
catch
(
e
)
{}
})
})
.
on
(
'
ready
'
,
()
=>
{
.
on
(
'
ready
'
,
()
=>
{
copyAssets
(
inputDir
,
outputDir
,
extname
!
)
copyAssets
(
UtsTarget
.
KOTLIN
,
inputDir
,
outputDir
,
extname
!
)
})
})
}
}
function
buildKotlin
({
function
buildKotlin
({
silent
,
input
:
{
dir
:
inputDir
,
extname
},
input
:
{
dir
:
inputDir
,
extname
},
output
:
{
dir
:
outputDir
,
sourceMap
,
inlineSourcesContent
},
output
:
{
dir
:
outputDir
,
sourceMap
,
inlineSourcesContent
},
}:
ToKotlinOptions
)
{
}:
ToKotlinOptions
)
{
fs
.
emptyDirSync
(
outputDir
)
extname
=
extname
||
EXTNAME
extname
=
extname
||
EXTNAME
const
inputSrcDir
=
resolveKotlinSrcDir
(
inputDir
)
const
outputSrcDir
=
resolveKotlinSrcDir
(
outputDir
)
const
input
=
initInputKotlinOptions
(
inputSrcDir
)
const
output
=
initOutputKotlinOptions
(
outputSrcDir
,
sourceMap
,
!!
inlineSourcesContent
)
const
files
=
glob
.
sync
(
'
**/*
'
+
extname
,
{
const
files
=
glob
.
sync
(
'
**/*
'
+
extname
,
{
absolute
:
true
,
absolute
:
true
,
cwd
:
inputDir
,
cwd
:
input
Src
Dir
,
ignore
:
[
'
**/*.d
'
+
extname
],
ignore
:
[
'
**/*.d
'
+
extname
],
})
})
const
input
:
InputKotlinOptions
=
{
root
:
inputDir
,
filename
:
''
,
}
const
output
:
OutputKotlinOptions
=
{
outDir
:
outputDir
,
sourceMap
,
inlineSourcesContent
:
!!
inlineSourcesContent
,
}
return
Promise
.
all
(
return
Promise
.
all
(
files
.
map
((
filename
)
=>
buildKotlinFile
(
filename
,
input
,
output
))
files
.
map
((
filename
)
=>
buildKotlinFile
(
filename
,
input
,
output
))
).
then
(()
=>
{
)
return
copyAssets
(
inputDir
,
outputDir
,
extname
!
)
.
then
((
res
)
=>
{
})
return
copyAssets
(
UtsTarget
.
KOTLIN
,
inputDir
,
outputDir
,
extname
!
).
then
(
()
=>
res
)
})
.
then
((
res
)
=>
{
!
silent
&&
printUtsResults
(
res
)
return
res
})
}
}
function
copyAssets
(
inputDir
:
string
,
outputDir
:
string
,
extname
:
string
)
{
function
copyAssets
(
target
:
UtsTarget
,
inputDir
:
string
,
outputDir
:
string
,
extname
:
string
)
{
inputDir
=
path
.
resolve
(
inputDir
)
outputDir
=
path
.
resolve
(
outputDir
)
const
kotlinRootDir
=
path
.
join
(
inputDir
,
targetDirs
[
UtsTarget
.
KOTLIN
])
const
swiftRootDir
=
path
.
join
(
inputDir
,
targetDirs
[
UtsTarget
.
SWIFT
])
return
fs
.
copy
(
inputDir
,
outputDir
,
{
return
fs
.
copy
(
inputDir
,
outputDir
,
{
filter
(
src
)
{
filter
(
src
)
{
return
path
.
extname
(
src
)
!==
extname
if
(
target
===
UtsTarget
.
KOTLIN
)
{
if
(
src
===
swiftRootDir
)
{
return
false
}
}
else
if
(
target
===
UtsTarget
.
SWIFT
)
{
if
(
src
===
kotlinRootDir
)
{
return
false
}
}
return
!
[
extname
,
'
.ts
'
].
includes
(
path
.
extname
(
src
))
},
},
})
})
}
}
...
@@ -137,7 +234,6 @@ function buildKotlinFile(
...
@@ -137,7 +234,6 @@ function buildKotlinFile(
input
:
InputKotlinOptions
,
input
:
InputKotlinOptions
,
output
:
OutputKotlinOptions
output
:
OutputKotlinOptions
)
{
)
{
const
label
=
normalizePath
(
path
.
relative
(
input
.
root
,
filename
))
const
toKotlinOptions
:
UtsKotlinOptions
=
{
const
toKotlinOptions
:
UtsKotlinOptions
=
{
input
:
{
input
:
{
...
input
,
...
input
,
...
@@ -148,28 +244,31 @@ function buildKotlinFile(
...
@@ -148,28 +244,31 @@ function buildKotlinFile(
...
output
,
...
output
,
},
},
}
}
console
.
time
(
label
)
const
start
=
process
.
hrtime
()
return
toKotlin
(
toKotlinOptions
).
then
(()
=>
{
return
toKotlin
(
toKotlinOptions
).
then
((
res
)
=>
{
console
.
timeEnd
(
label
)
res
.
time
=
timeEnd
(
start
)
return
res
})
})
}
}
export
function
runDev
(
target
:
'
kotlin
'
|
'
swift
'
,
opts
:
ToOptions
)
{
export
function
runDev
(
target
:
UtsTarget
,
opts
:
ToOptions
)
{
opts
=
parseOptions
(
'
dev
'
,
opts
)
opts
=
parseOptions
(
'
dev
'
,
target
,
opts
)
!
opts
.
silent
&&
printStartup
(
target
,
'
development
'
)
switch
(
target
)
{
switch
(
target
)
{
case
'
kotlin
'
:
case
UtsTarget
.
KOTLIN
:
return
watchKotlin
(
opts
)
return
watchKotlin
(
opts
)
case
'
swift
'
:
case
UtsTarget
.
SWIFT
:
return
watchSwift
(
opts
)
return
watchSwift
(
opts
)
}
}
}
}
export
function
runBuild
(
target
:
'
kotlin
'
|
'
swift
'
,
opts
:
ToOptions
)
{
export
function
runBuild
(
target
:
UtsTarget
,
opts
:
ToOptions
)
{
opts
=
parseOptions
(
'
build
'
,
opts
)
opts
=
parseOptions
(
'
build
'
,
target
,
opts
)
!
opts
.
silent
&&
printStartup
(
target
,
'
production
'
)
switch
(
target
)
{
switch
(
target
)
{
case
'
kotlin
'
:
case
UtsTarget
.
KOTLIN
:
return
buildKotlin
(
opts
)
return
buildKotlin
(
opts
)
case
'
swift
'
:
case
UtsTarget
.
SWIFT
:
return
buildSwift
(
opts
)
return
buildSwift
(
opts
)
}
}
}
}
packages/uts/src/types.ts
浏览文件 @
aae9ab34
export
type
UtsTarget
=
|
'
es3
'
|
'
es5
'
|
'
es2015
'
|
'
es2016
'
|
'
es2017
'
|
'
es2018
'
|
'
es2019
'
|
'
es2020
'
|
'
es2021
'
|
'
es2022
'
export
interface
UtsParserConfig
{
export
interface
UtsParserConfig
{
/**
/**
* Defaults to `false`.
* Defaults to `false`.
...
@@ -28,10 +16,6 @@ export interface UtsParserConfig {
...
@@ -28,10 +16,6 @@ export interface UtsParserConfig {
export
type
UtsParseOptions
=
UtsParserConfig
&
{
export
type
UtsParseOptions
=
UtsParserConfig
&
{
filename
?:
string
filename
?:
string
comments
?:
boolean
comments
?:
boolean
/**
* default es2022
*/
target
?:
UtsTarget
}
}
export
type
InputKotlinOptions
=
UtsParseOptions
&
{
export
type
InputKotlinOptions
=
UtsParseOptions
&
{
...
@@ -60,4 +44,5 @@ export interface UtsSwiftOptions {
...
@@ -60,4 +44,5 @@ export interface UtsSwiftOptions {
export
interface
UtsResult
{
export
interface
UtsResult
{
filename
?:
string
filename
?:
string
time
?:
number
}
}
packages/uts/src/utils.ts
浏览文件 @
aae9ab34
import
os
from
'
os
'
import
os
from
'
os
'
import
colors
from
'
picocolors
'
import
type
{
UtsTarget
}
from
'
.
'
import
{
UtsResult
}
from
'
./types
'
export
const
isWindows
=
os
.
platform
()
===
'
win32
'
export
const
isWindows
=
os
.
platform
()
===
'
win32
'
export
function
normalizePath
(
id
:
string
):
string
{
export
function
normalizePath
(
id
:
string
):
string
{
return
isWindows
?
id
.
replace
(
/
\\
/g
,
'
/
'
)
:
id
return
isWindows
?
id
.
replace
(
/
\\
/g
,
'
/
'
)
:
id
}
}
const
round
=
(
number
:
number
)
=>
Number
(
number
.
toFixed
(
2
))
const
hrTimeToMilliseconds
=
(
hrtime
:
[
number
,
number
])
=>
hrtime
[
0
]
*
1
e3
+
hrtime
[
1
]
/
1
e6
export
function
timeEnd
(
start
:
[
number
,
number
])
{
return
round
(
hrTimeToMilliseconds
(
process
.
hrtime
())
-
hrTimeToMilliseconds
(
start
)
)
}
export
function
printStartup
(
target
:
UtsTarget
,
mode
:
string
)
{
console
.
log
(
colors
.
cyan
(
`uts v
${
require
(
'
../package.json
'
).
version
}
${
colors
.
green
(
`building
${
target
}
for
${
mode
}
...`
)}
`
)
)
}
export
function
printUtsResults
(
results
:
UtsResult
[])
{
let
longest
=
0
for
(
const
result
of
results
)
{
const
l
=
result
.
filename
!
.
length
if
(
l
>
longest
)
longest
=
l
}
console
.
log
(
colors
.
dim
(
`
${
colors
.
green
(
`✓`
)}
${
results
.
length
}
files transformed.`
)
)
results
.
forEach
((
result
)
=>
{
printUtsResult
(
result
,
longest
)
})
}
export
function
printUtsResult
(
result
:
UtsResult
,
maxLength
=
0
)
{
console
.
log
(
colors
.
green
(
result
.
filename
!
.
padEnd
(
maxLength
+
2
))
+
'
'
+
colors
.
dim
(
result
.
time
+
'
ms
'
)
)
}
pnpm-lock.yaml
浏览文件 @
aae9ab34
...
@@ -773,11 +773,13 @@ importers:
...
@@ -773,11 +773,13 @@ importers:
chokidar
:
^3.5.3
chokidar
:
^3.5.3
fast-glob
:
^3.2.11
fast-glob
:
^3.2.11
fs-extra
:
^10.0.0
fs-extra
:
^10.0.0
picocolors
:
^1.0.0
dependencies
:
dependencies
:
cac
:
6.7.9
cac
:
6.7.9
chokidar
:
3.5.3
chokidar
:
3.5.3
fast-glob
:
3.2.11
fast-glob
:
3.2.11
fs-extra
:
10.1.0
fs-extra
:
10.1.0
picocolors
:
1.0.0
optionalDependencies
:
optionalDependencies
:
'
@dcloudio/uts-darwin-arm64'
:
link:../uts-darwin-arm64
'
@dcloudio/uts-darwin-arm64'
:
link:../uts-darwin-arm64
devDependencies
:
devDependencies
:
...
@@ -786,6 +788,9 @@ importers:
...
@@ -786,6 +788,9 @@ importers:
packages/uts-darwin-arm64
:
packages/uts-darwin-arm64
:
specifiers
:
{}
specifiers
:
{}
packages/uts-win32-x64-msvc
:
specifiers
:
{}
packages/vite-plugin-uni
:
packages/vite-plugin-uni
:
specifiers
:
specifiers
:
'
@babel/core'
:
^7.17.9
'
@babel/core'
:
^7.17.9
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录