Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
0114e258
U
uni-app
项目概览
DCloud
/
uni-app
3 个月 前同步成功
通知
725
Star
38705
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0114e258
编写于
8月 18, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(app): console
上级
95517b9b
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
311 addition
and
18 deletion
+311
-18
packages/uni-app-vite/src/index.ts
packages/uni-app-vite/src/index.ts
+11
-0
packages/uni-cli-nvue/src/babel/plugin/ConsolePlugin.ts
packages/uni-cli-nvue/src/babel/plugin/ConsolePlugin.ts
+64
-0
packages/uni-cli-shared/__tests__/__snapshots__/console.spec.ts.snap
...i-cli-shared/__tests__/__snapshots__/console.spec.ts.snap
+21
-0
packages/uni-cli-shared/__tests__/console.spec.ts
packages/uni-cli-shared/__tests__/console.spec.ts
+51
-0
packages/uni-cli-shared/src/env/provide.ts
packages/uni-cli-shared/src/env/provide.ts
+1
-0
packages/uni-cli-shared/src/hbx/formatLog.ts
packages/uni-cli-shared/src/hbx/formatLog.ts
+63
-0
packages/uni-cli-shared/src/logs/console.ts
packages/uni-cli-shared/src/logs/console.ts
+44
-0
packages/uni-cli-shared/src/vite/plugins/console.ts
packages/uni-cli-shared/src/vite/plugins/console.ts
+22
-0
packages/uni-cli-shared/src/vite/plugins/index.ts
packages/uni-cli-shared/src/vite/plugins/index.ts
+1
-0
packages/uni-cli-shared/src/vite/plugins/inject.ts
packages/uni-cli-shared/src/vite/plugins/inject.ts
+3
-18
packages/uni-cli-shared/src/vite/utils/ast.ts
packages/uni-cli-shared/src/vite/utils/ast.ts
+15
-0
packages/uni-cli-shared/src/vite/utils/url.ts
packages/uni-cli-shared/src/vite/utils/url.ts
+15
-0
未找到文件。
packages/uni-app-vite/src/index.ts
浏览文件 @
0114e258
import
path
from
'
path
'
import
{
initProvide
,
uniViteInjectPlugin
,
uniCssScopedPlugin
,
getAppStyleIsolation
,
parseManifestJsonOnce
,
uniConsolePlugin
,
}
from
'
@dcloudio/uni-cli-shared
'
import
{
UniAppPlugin
}
from
'
./plugin
'
import
{
uniTemplatePlugin
}
from
'
./plugins/template
'
...
...
@@ -31,6 +33,15 @@ function initUniCssScopedPluginOptions() {
const
plugins
=
[
// uniResolveIdPlugin(),
uniConsolePlugin
({
filename
(
filename
)
{
filename
=
path
.
relative
(
process
.
env
.
UNI_INPUT_DIR
,
filename
)
if
(
filename
.
startsWith
(
'
.
'
))
{
return
''
}
return
filename
},
}),
uniMainJsPlugin
(),
uniManifestJsonPlugin
(),
uniPagesJsonPlugin
(),
...
...
packages/uni-cli-nvue/src/babel/plugin/ConsolePlugin.ts
0 → 100644
浏览文件 @
0114e258
import
*
as
Types
from
'
@babel/types
'
import
{
PluginObj
,
PluginPass
}
from
'
@babel/core
'
import
{
normalizePath
}
from
'
@dcloudio/uni-cli-shared
'
const
METHODS
=
[
'
error
'
,
'
warn
'
,
'
info
'
,
'
log
'
,
'
debug
'
]
const
FORMAT_LOG
=
'
__f__
'
module
.
exports
=
function
({
types
:
t
,
}:
{
types
:
typeof
Types
}):
PluginObj
<
{
opts
:
{
filename
?:
(
filename
:
string
)
=>
string
|
undefined
}
}
&
PluginPass
>
{
return
{
visitor
:
{
CallExpression
(
path
,
state
)
{
let
{
opts
,
file
:
{
opts
:
{
filename
},
},
}
=
state
if
(
!
filename
)
{
return
}
if
(
opts
&&
opts
.
filename
)
{
filename
=
opts
.
filename
(
filename
)
}
if
(
!
filename
)
{
return
}
const
{
callee
,
arguments
:
args
,
loc
}
=
path
.
node
if
(
!
t
.
isMemberExpression
(
callee
))
{
return
}
const
{
object
,
property
}
=
callee
if
(
!
t
.
isIdentifier
(
object
)
||
!
t
.
isIdentifier
(
property
))
{
return
}
if
(
object
.
name
!==
'
console
'
||
!
METHODS
.
includes
(
property
.
name
))
{
return
}
if
(
property
.
name
===
'
debug
'
)
{
property
.
name
=
'
log
'
}
const
arg
=
args
[
0
]
if
(
arg
&&
t
.
isCallExpression
(
arg
)
&&
t
.
isIdentifier
(
arg
.
callee
)
&&
arg
.
callee
.
name
===
FORMAT_LOG
)
{
return
}
args
.
unshift
({
type
:
'
StringLiteral
'
,
value
:
` at
${
normalizePath
(
filename
)}
:
${
loc
!
.
start
.
line
}
`
,
}
as
Types
.
StringLiteral
)
args
.
unshift
(
t
.
stringLiteral
(
property
.
name
))
path
.
replaceWith
(
t
.
callExpression
(
t
.
identifier
(
FORMAT_LOG
),
args
))
},
},
}
}
packages/uni-cli-shared/__tests__/__snapshots__/console.spec.ts.snap
0 → 100644
浏览文件 @
0114e258
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`console console.debug 1`] = `"__f__('info','at foo.vue:1',a,b,c);"`;
exports[`console console.error 1`] = `"__f__('info','at foo.vue:1',a,b,c);"`;
exports[`console console.info 1`] = `"__f__('info','at foo.vue:1',a,b,c);"`;
exports[`console console.log 1`] = `"const a = 1;__f__('log','at foo.vue:1',a);"`;
exports[`console console.log multiline 1`] = `
"const a = 1;
__f__('log','at foo.vue:3',a);
const b = 2
__f__('log','at foo.vue:5',a,b);
__f__('log','at foo.vue:6',a,b,c);
"
`;
exports[`console console.warn 1`] = `"__f__('info','at foo.vue:1',a,b,c);"`;
packages/uni-cli-shared/__tests__/console.spec.ts
0 → 100644
浏览文件 @
0114e258
import
{
rewriteConsoleExpr
}
from
'
../src/logs/console
'
import
{
normalizeLog
}
from
'
../src/hbx/formatLog
'
const
filename
=
'
foo.vue
'
describe
(
'
console
'
,
()
=>
{
test
(
'
console.log
'
,
()
=>
{
expect
(
rewriteConsoleExpr
(
filename
,
`const a = 1;console.log(a);`
)
).
toMatchSnapshot
()
})
test
(
'
console.log multiline
'
,
()
=>
{
expect
(
rewriteConsoleExpr
(
filename
,
`const a = 1;
console.log(a);
const b = 2
console.log(a,b);
console.log(a,b,c);
`
)
).
toMatchSnapshot
()
})
test
(
'
console.info
'
,
()
=>
{
expect
(
rewriteConsoleExpr
(
filename
,
`console.info(a,b,c);`
)
).
toMatchSnapshot
()
})
test
(
'
console.debug
'
,
()
=>
{
expect
(
rewriteConsoleExpr
(
filename
,
`console.info(a,b,c);`
)
).
toMatchSnapshot
()
})
test
(
'
console.warn
'
,
()
=>
{
expect
(
rewriteConsoleExpr
(
filename
,
`console.info(a,b,c);`
)
).
toMatchSnapshot
()
})
test
(
'
console.error
'
,
()
=>
{
expect
(
rewriteConsoleExpr
(
filename
,
`console.info(a,b,c);`
)
).
toMatchSnapshot
()
})
test
(
'
console.log format
'
,
()
=>
{
expect
(
normalizeLog
(
'
log
'
,
'
at
'
+
filename
+
'
:1
'
,
[
'
a
'
,
'
b
'
,
{
a
:
1
}])
).
toBe
(
`a---COMMA---b---COMMA------BEGIN:JSON---{"a":1}---END:JSON--- at foo.vue:1`
)
})
})
packages/uni-cli-shared/src/env/provide.ts
浏览文件 @
0114e258
...
...
@@ -3,6 +3,7 @@ const libDir = path.resolve(__dirname, '../lib')
export
function
initProvide
()
{
const
cryptoDefine
=
[
path
.
join
(
libDir
,
'
crypto.js
'
),
'
default
'
]
return
{
__f__
:
[
path
.
join
(
__dirname
,
'
../hbx/formatLog.js
'
),
'
formatLog
'
],
crypto
:
cryptoDefine
,
'
window.crypto
'
:
cryptoDefine
,
'
global.crypto
'
:
cryptoDefine
,
...
...
packages/uni-cli-shared/src/hbx/
console
.ts
→
packages/uni-cli-shared/src/hbx/
formatLog
.ts
浏览文件 @
0114e258
function
typof
(
v
:
unknown
)
{
var
s
=
Object
.
prototype
.
toString
.
call
(
v
)
return
s
.
substring
(
8
,
s
.
length
-
1
)
}
import
{
toTypeString
,
toRawType
}
from
'
@vue/shared
'
function
isDebugMode
()
{
// @ts-expect-error
return
typeof
__channelId__
===
'
string
'
&&
__channelId__
}
function
jsonStringifyReplacer
(
k
ey
:
string
,
value
:
unknown
)
{
switch
(
t
ypof
(
value
))
{
function
jsonStringifyReplacer
(
k
:
string
,
p
:
unknown
)
{
switch
(
t
oRawType
(
p
))
{
case
'
Function
'
:
return
'
function() { [native code] }
'
default
:
return
value
return
p
}
}
type
LogType
=
'
log
'
|
'
info
'
|
'
warn
'
|
'
error
'
// export function log(type: LogType) {
// for (
// var _len = arguments.length,
// args = new Array(_len > 1 ? _len - 1 : 0),
// _key = 1;
// _key < _len;
// _key++
// ) {
// args[_key - 1] = arguments[_key]
// }
// console[type].apply(console, args)
// }
export
default
function
formatLog
()
{
for
(
var
_len
=
arguments
.
length
,
args
=
new
Array
(
_len
),
_key
=
0
;
_key
<
_len
;
_key
++
)
{
args
[
_key
]
=
arguments
[
_key
]
}
var
type
=
args
.
shift
()
as
LogType
export
function
normalizeLog
(
type
:
'
log
'
|
'
info
'
|
'
debug
'
|
'
warn
'
|
'
error
'
,
filename
:
string
,
args
:
unknown
[]
)
{
if
(
isDebugMode
())
{
args
.
push
(
args
.
pop
()
.
replace
(
'
at
'
,
'
uni-app:///
'
))
args
.
push
(
filename
.
replace
(
'
at
'
,
'
uni-app:///
'
))
return
console
[
type
].
apply
(
console
,
args
)
}
var
msgs
=
args
.
map
(
function
(
v
)
{
var
type
=
Object
.
prototype
.
toString
.
call
(
v
).
toLowerCase
()
const
msgs
=
args
.
map
(
function
(
v
)
{
const
type
=
toTypeString
(
v
).
toLowerCase
()
if
(
type
===
'
[object object]
'
||
type
===
'
[object array]
'
)
{
try
{
v
=
...
...
@@ -64,8 +41,7 @@ export default function formatLog() {
}
else
if
(
v
===
undefined
)
{
v
=
'
---UNDEFINED---
'
}
else
{
var
vType
=
typof
(
v
).
toUpperCase
()
const
vType
=
toRawType
(
v
).
toUpperCase
()
if
(
vType
===
'
NUMBER
'
||
vType
===
'
BOOLEAN
'
)
{
v
=
'
---BEGIN:
'
+
vType
+
'
---
'
+
v
+
'
---END:
'
+
vType
+
'
---
'
}
else
{
...
...
@@ -73,23 +49,15 @@ export default function formatLog() {
}
}
}
return
v
})
var
msg
=
''
if
(
msgs
.
length
>
1
)
{
var
lastMsg
=
msgs
.
pop
()
msg
=
msgs
.
join
(
'
---COMMA---
'
)
if
(
lastMsg
.
indexOf
(
'
at
'
)
===
0
)
{
msg
+=
lastMsg
}
else
{
msg
+=
'
---COMMA---
'
+
lastMsg
}
}
else
{
msg
=
msgs
[
0
]
}
return
msgs
.
join
(
'
---COMMA---
'
)
+
'
'
+
filename
}
console
[
type
](
msg
)
export
function
formatLog
(
type
:
'
log
'
|
'
info
'
|
'
debug
'
|
'
warn
'
|
'
error
'
,
filename
:
string
,
...
args
:
unknown
[]
)
{
console
[
type
](
normalizeLog
(
type
,
filename
,
args
))
}
packages/uni-cli-shared/src/logs/console.ts
0 → 100644
浏览文件 @
0114e258
import
{
MagicString
}
from
'
@vue/compiler-sfc
'
const
F
=
'
__f__
'
export
function
rewriteConsoleExpr
(
filename
:
string
,
code
:
string
)
{
const
re
=
/
(
console
\.(
log|info|debug|warn|error
))\(([^
)
]
+
)\)
/g
const
locate
=
getLocator
(
code
)
const
s
=
new
MagicString
(
code
)
let
match
:
RegExpExecArray
|
null
while
((
match
=
re
.
exec
(
code
)))
{
const
[,
expr
,
type
]
=
match
s
.
overwrite
(
match
.
index
,
match
.
index
+
expr
.
length
+
1
,
F
+
`('
${
type
}
','at
${
filename
}
:
${
locate
(
match
.
index
).
line
+
1
}
',`
)
}
return
s
.
toString
()
}
function
getLocator
(
source
:
string
)
{
const
originalLines
=
source
.
split
(
'
\n
'
)
const
lineOffsets
:
number
[]
=
[]
for
(
let
i
=
0
,
pos
=
0
;
i
<
originalLines
.
length
;
i
++
)
{
lineOffsets
.
push
(
pos
)
pos
+=
originalLines
[
i
].
length
+
1
}
return
function
locate
(
index
:
number
)
{
let
i
=
0
let
j
=
lineOffsets
.
length
while
(
i
<
j
)
{
const
m
=
(
i
+
j
)
>>
1
if
(
index
<
lineOffsets
[
m
])
{
j
=
m
}
else
{
i
=
m
+
1
}
}
const
line
=
i
-
1
const
column
=
index
-
lineOffsets
[
line
]
return
{
line
,
column
}
}
}
packages/uni-cli-shared/src/vite/plugins/console.ts
浏览文件 @
0114e258
import
debug
from
'
debug
'
import
{
Plugin
}
from
'
vite
'
import
{
createFilter
,
FilterPattern
}
from
'
@rollup/pluginutils
'
import
{
isJsFile
,
parseVueRequest
}
from
'
../utils
'
import
{
rewriteConsoleExpr
}
from
'
../../logs/console
'
export
interface
ConsoleOptions
{
filename
?:
(
filename
:
string
)
=>
string
include
?:
FilterPattern
exclude
?:
FilterPattern
}
const
debugConsole
=
debug
(
'
vite:uni:console
'
)
export
function
uniConsolePlugin
(
options
:
ConsoleOptions
):
Plugin
{
const
filter
=
createFilter
(
options
.
include
,
options
.
exclude
)
return
{
name
:
'
vite:uni-app-console
'
,
enforce
:
'
pre
'
,
apply
:
'
build
'
,
transform
(
code
,
id
)
{
if
(
!
filter
(
id
))
return
null
if
(
!
isJsFile
(
id
))
return
null
let
{
filename
}
=
parseVueRequest
(
id
)
if
(
options
.
filename
)
{
filename
=
options
.
filename
(
filename
)
}
if
(
!
filename
)
{
return
null
}
if
(
!
code
.
includes
(
'
console.
'
))
{
return
null
}
debugConsole
(
id
)
return
{
code
:
rewriteConsoleExpr
(
filename
,
code
),
map
:
null
,
}
},
}
}
packages/uni-cli-shared/src/vite/plugins/index.ts
浏览文件 @
0114e258
...
...
@@ -4,6 +4,7 @@ export * from './copy'
export
*
from
'
./inject
'
export
*
from
'
./mainJs
'
export
*
from
'
./jsonJs
'
export
*
from
'
./console
'
export
{
assetPlugin
}
from
'
./vitejs/plugins/asset
'
export
{
cssPlugin
,
cssPostPlugin
}
from
'
./vitejs/plugins/css
'
packages/uni-cli-shared/src/vite/plugins/inject.ts
浏览文件 @
0114e258
import
path
,
{
sep
}
from
'
path
'
import
{
sep
}
from
'
path
'
import
debug
from
'
debug
'
import
{
Plugin
}
from
'
vite
'
...
...
@@ -16,14 +16,7 @@ import { walk } from 'estree-walker'
import
{
extend
}
from
'
@vue/shared
'
import
{
MagicString
}
from
'
@vue/compiler-sfc
'
import
{
EXTNAME_JS_RE
,
EXTNAME_VUE
}
from
'
../../constants
'
import
{
isProperty
,
isReference
,
isMemberExpression
,
parseVueRequest
,
}
from
'
../utils
'
import
{
isProperty
,
isReference
,
isMemberExpression
,
isJsFile
}
from
'
../utils
'
interface
Scope
{
parent
:
Scope
...
...
@@ -79,15 +72,7 @@ export function uniViteInjectPlugin(options: InjectOptions): Plugin {
name
:
'
vite:uni-inject
'
,
transform
(
code
,
id
)
{
if
(
!
filter
(
id
))
return
null
const
isJs
=
EXTNAME_JS_RE
.
test
(
id
)
if
(
!
isJs
)
{
const
{
filename
,
query
}
=
parseVueRequest
(
id
)
const
isVueJs
=
EXTNAME_VUE
.
includes
(
path
.
extname
(
filename
))
&&
!
query
.
vue
if
(
!
isVueJs
)
{
return
null
}
}
if
(
!
isJsFile
(
id
))
return
null
debugInjectTry
(
id
)
if
(
code
.
search
(
firstpass
)
===
-
1
)
return
null
if
(
sep
!==
'
/
'
)
id
=
id
.
split
(
sep
).
join
(
'
/
'
)
...
...
packages/uni-cli-shared/src/vite/utils/ast.ts
浏览文件 @
0114e258
...
...
@@ -63,6 +63,21 @@ export function createLiteral(value: string) {
}
as
Literal
}
export
function
createIdentifier
(
name
:
string
)
{
return
{
type
:
'
Identifier
'
,
name
,
}
as
Identifier
}
export
function
createCallExpression
(
callee
:
unknown
,
args
:
unknown
[])
{
return
{
type
:
'
CallExpression
'
,
callee
,
arguments
:
args
,
}
as
CallExpression
}
export
function
parseVue
(
code
:
string
,
errors
:
SyntaxError
[])
{
return
parse
(
code
,
{
isNativeTag
:
()
=>
true
,
...
...
packages/uni-cli-shared/src/vite/utils/url.ts
浏览文件 @
0114e258
import
path
from
'
path
'
import
qs
from
'
querystring
'
import
{
EXTNAME_JS_RE
,
EXTNAME_VUE
}
from
'
../../constants
'
export
interface
VueQuery
{
vue
?:
boolean
...
...
@@ -39,3 +41,16 @@ export const hashRE = /#.*$/
export
const
cleanUrl
=
(
url
:
string
)
=>
url
.
replace
(
hashRE
,
''
).
replace
(
queryRE
,
''
)
export
function
isJsFile
(
id
:
string
)
{
const
isJs
=
EXTNAME_JS_RE
.
test
(
id
)
if
(
isJs
)
{
return
true
}
const
{
filename
,
query
}
=
parseVueRequest
(
id
)
const
isVueJs
=
EXTNAME_VUE
.
includes
(
path
.
extname
(
filename
))
&&
!
query
.
vue
if
(
isVueJs
)
{
return
true
}
return
false
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录