Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
计算机应用开发
前端
Vuepress
提交
364571bc
V
Vuepress
项目概览
计算机应用开发
/
前端
/
Vuepress
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
Vuepress
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
364571bc
编写于
11月 28, 2019
作者:
M
meteorlxy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
style($shared-utils): lint the code
上级
bd146ec9
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
67 addition
and
74 deletion
+67
-74
packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts
...s/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts
+4
-2
packages/@vuepress/shared-utils/src/datatypes.ts
packages/@vuepress/shared-utils/src/datatypes.ts
+5
-5
packages/@vuepress/shared-utils/src/extractHeaders.ts
packages/@vuepress/shared-utils/src/extractHeaders.ts
+2
-4
packages/@vuepress/shared-utils/src/getPermalink.ts
packages/@vuepress/shared-utils/src/getPermalink.ts
+3
-3
packages/@vuepress/shared-utils/src/index.ts
packages/@vuepress/shared-utils/src/index.ts
+1
-1
packages/@vuepress/shared-utils/src/logger.ts
packages/@vuepress/shared-utils/src/logger.ts
+1
-3
packages/@vuepress/shared-utils/src/moduleLoader.ts
packages/@vuepress/shared-utils/src/moduleLoader.ts
+15
-17
packages/@vuepress/shared-utils/src/moduleResolver.ts
packages/@vuepress/shared-utils/src/moduleResolver.ts
+30
-30
packages/@vuepress/shared-utils/src/parseEmojis.ts
packages/@vuepress/shared-utils/src/parseEmojis.ts
+1
-1
packages/@vuepress/shared-utils/src/parseFrontmatter.ts
packages/@vuepress/shared-utils/src/parseFrontmatter.ts
+4
-4
packages/@vuepress/shared-utils/src/parseVueFrontmatter.ts
packages/@vuepress/shared-utils/src/parseVueFrontmatter.ts
+0
-1
packages/@vuepress/shared-utils/src/performance.ts
packages/@vuepress/shared-utils/src/performance.ts
+0
-1
packages/@vuepress/shared-utils/src/slugify.ts
packages/@vuepress/shared-utils/src/slugify.ts
+0
-1
packages/@vuepress/shared-utils/src/tryChain.ts
packages/@vuepress/shared-utils/src/tryChain.ts
+1
-1
未找到文件。
packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts
浏览文件 @
364571bc
...
...
@@ -46,10 +46,10 @@ describe('resolveScopePackage', () => {
test
(
'
incorrect format
'
,
()
=>
{
const
pkg2
=
resolveScopePackage
(
'
vuepress/plugin-a
'
)
expect
(
pkg2
).
toEqual
({
"
name
"
:
""
,
"
org
"
:
""
})
expect
(
pkg2
).
toEqual
({
'
name
'
:
''
,
'
org
'
:
''
})
const
pkg3
=
resolveScopePackage
(
'
vuepress-plugin-a
'
)
expect
(
pkg3
).
toEqual
({
"
name
"
:
""
,
"
org
"
:
""
})
expect
(
pkg3
).
toEqual
({
'
name
'
:
''
,
'
org
'
:
''
})
})
})
...
...
@@ -123,11 +123,13 @@ describe('resolvePlugin', () => {
test
(
'
relative path
'
,
()
=>
{
const
resolved
=
resolvePlugin
(
'
./plugin-a
'
)
// eslint-disable-next-line @typescript-eslint/no-var-requires
expect
(
resolved
.
entry
).
toBe
(
require
(
'
./fixtures/plugin-a
'
))
})
test
(
'
aosolute path
'
,
()
=>
{
const
resolved
=
resolvePlugin
(
path
.
resolve
(
__dirname
,
'
fixtures/plugin-a
'
))
// eslint-disable-next-line @typescript-eslint/no-var-requires
expect
(
resolved
.
entry
).
toBe
(
require
(
'
./fixtures/plugin-a
'
))
})
...
...
packages/@vuepress/shared-utils/src/datatypes.ts
浏览文件 @
364571bc
...
...
@@ -31,7 +31,7 @@ export const getType = function (fn: any) {
* ['Function', 'Object'] => 'Function or Object'
* ['Function', 'Object', 'Number'] => 'Function, Object or Number'
*/
type
Type
=
String
|
Number
|
B
oolean
|
RegExp
|
Function
|
Record
<
string
,
any
>
|
Array
<
any
>
type
Type
=
string
|
number
|
b
oolean
|
RegExp
|
Function
|
Record
<
string
,
any
>
|
Array
<
any
>
function
toNaturalMultiTypesLanguage
(
types
:
Type
[])
{
const
len
=
types
.
length
...
...
@@ -47,7 +47,7 @@ export function assertTypes (value: any, types: Type[]) {
let
valid
let
warnMsg
let
actualType
=
toRawType
(
value
)
const
expectedTypes
=
[]
const
expectedTypes
:
Type
[]
=
[]
if
(
actualType
===
'
AsyncFunction
'
)
{
actualType
=
'
Function
'
}
...
...
@@ -60,9 +60,9 @@ export function assertTypes (value: any, types: Type[]) {
}
if
(
!
valid
)
{
warnMsg
=
`expected a
${
chalk
.
green
(
toNaturalMultiTypesLanguage
(
expectedTypes
))}
`
+
`but got
${
chalk
.
yellow
(
actualType
)}
.`
warnMsg
=
`expected a
${
chalk
.
green
(
toNaturalMultiTypesLanguage
(
expectedTypes
))}
`
+
`but got
${
chalk
.
yellow
(
actualType
)}
.`
}
return
{
valid
,
warnMsg
}
...
...
packages/@vuepress/shared-utils/src/extractHeaders.ts
浏览文件 @
364571bc
...
...
@@ -12,7 +12,7 @@ import deeplyParseHeaders from './deeplyParseHeaders'
const
cache
=
new
LRU
({
max
:
1000
})
export
=
function
(
content
:
string
,
include
=
[],
md
:
any
)
{
export
=
function
(
content
:
string
,
include
:
any
[]
=
[],
md
:
any
)
{
const
key
=
content
+
include
.
join
(
'
,
'
)
const
hit
=
cache
.
get
(
key
)
if
(
hit
)
{
...
...
@@ -23,11 +23,9 @@ export = function (content: string, include = [], md: any) {
const
res
:
any
[]
=
[]
tokens
.
forEach
((
t
:
any
,
i
:
any
)
=>
{
// @ts-ignore
if
(
t
.
type
===
'
heading_open
'
&&
include
.
includes
(
t
.
tag
))
{
const
title
=
tokens
[
i
+
1
].
content
// @ts-ignore
const
slug
=
(
t
.
attrs
).
find
(([
name
])
=>
name
===
'
id
'
)[
1
]
const
slug
=
t
.
attrs
.
find
(([
name
]:
any
[])
=>
name
===
'
id
'
)[
1
]
res
.
push
({
level
:
parseInt
(
t
.
tag
.
slice
(
1
),
10
),
title
:
deeplyParseHeaders
(
title
),
...
...
packages/@vuepress/shared-utils/src/getPermalink.ts
浏览文件 @
364571bc
...
...
@@ -44,9 +44,9 @@ export = function getPermalink ({
pattern
=
removeLeadingSlash
(
pattern
)
const
link
=
localePath
+
pattern
const
link
=
localePath
+
pattern
.
replace
(
/:year/
,
String
(
year
))
.
replace
(
/:month/
,
String
(
month
))
.
replace
(
/:i_month/
,
String
(
iMonth
))
...
...
packages/@vuepress/shared-utils/src/index.ts
浏览文件 @
364571bc
...
...
@@ -68,5 +68,5 @@ export {
globby
,
hash
,
escapeHtml
,
semver
,
semver
}
packages/@vuepress/shared-utils/src/logger.ts
浏览文件 @
364571bc
...
...
@@ -7,7 +7,7 @@
import
chalk
from
'
chalk
'
interface
LoggerOptions
{
logLevel
:
number
logLevel
:
number
;
}
class
Logger
{
...
...
@@ -90,7 +90,6 @@ class Logger {
if
(
this
.
options
.
logLevel
<
3
)
{
return
}
// @ts-ignore
console
.
log
(
chalk
[
color
](
label
),
...
args
)
}
...
...
@@ -107,4 +106,3 @@ class Logger {
*/
export
=
new
Logger
()
packages/@vuepress/shared-utils/src/moduleLoader.ts
浏览文件 @
364571bc
...
...
@@ -4,6 +4,7 @@ import semver from 'semver'
import
env
from
'
./env
'
function
resolveFallback
(
request
:
string
,
options
:
{
paths
:
string
[]
})
{
// eslint-disable-next-line @typescript-eslint/no-var-requires
const
Module
=
require
(
'
module
'
)
const
isMain
=
false
const
fakeParent
=
new
Module
(
''
,
null
)
...
...
@@ -24,28 +25,37 @@ function resolveFallback (request: string, options: { paths: string[] }) {
const
filename
=
Module
.
_findPath
(
request
,
paths
,
isMain
)
if
(
!
filename
)
{
const
err
=
new
Error
(
`Cannot find module '
${
request
}
'`
)
// @ts-ignores
const
err
:
Error
&
{
code
?:
string
}
=
new
Error
(
`Cannot find module '
${
request
}
'`
)
err
.
code
=
'
MODULE_NOT_FOUND
'
throw
err
}
return
filename
}
function
clearRequireCache
(
id
:
string
,
map
=
new
Map
())
{
const
module
=
require
.
cache
[
id
]
if
(
module
)
{
map
.
set
(
id
,
true
)
// Clear children modules
module
.
children
.
forEach
((
child
:
any
)
=>
{
if
(
!
map
.
get
(
child
.
id
))
clearRequireCache
(
child
.
id
,
map
)
})
delete
require
.
cache
[
id
]
}
}
const
resolve
=
semver
.
satisfies
(
process
.
version
,
'
>=10.0.0
'
)
?
require
.
resolve
:
resolveFallback
export
function
resolveModule
(
request
:
string
,
context
:
string
):
string
{
let
resolvedPath
if
(
env
.
isTest
)
{
return
require
.
resolve
(
request
)
}
// module.paths is for globally install packages.
const
paths
=
[
context
||
process
.
cwd
(),
...
module
.
paths
]
resolvedPath
=
resolve
(
request
,
{
paths
})
const
resolvedPath
=
resolve
(
request
,
{
paths
})
return
resolvedPath
}
...
...
@@ -66,15 +76,3 @@ export function clearModule (request: string, context: string) {
clearRequireCache
(
resolvedPath
)
}
}
function
clearRequireCache
(
id
:
string
,
map
=
new
Map
())
{
const
module
=
require
.
cache
[
id
]
if
(
module
)
{
map
.
set
(
id
,
true
)
// Clear children modules
module
.
children
.
forEach
((
child
:
any
)
=>
{
if
(
!
map
.
get
(
child
.
id
))
clearRequireCache
(
child
.
id
,
map
)
})
delete
require
.
cache
[
id
]
}
}
packages/@vuepress/shared-utils/src/moduleResolver.ts
浏览文件 @
364571bc
...
...
@@ -17,8 +17,30 @@ import {
assertTypes
}
from
'
./datatypes
'
/**
* Parse info of scope package.
*/
const
SCOPE_PACKAGE_RE
=
/^@
(
.*
)\/(
.*
)
/
export
interface
ScopePackage
{
org
:
string
;
name
:
string
;
}
export
function
resolveScopePackage
(
name
:
string
)
{
if
(
SCOPE_PACKAGE_RE
.
test
(
name
))
{
return
{
org
:
RegExp
.
$1
,
name
:
RegExp
.
$2
}
}
return
{
org
:
''
,
name
:
''
}
}
/**
* Common module constructor.
*/
...
...
@@ -33,20 +55,20 @@ export class CommonModule {
)
{}
}
function
getNoopModule
(
error
?:
Error
)
{
function
getNoopModule
(
error
?:
Error
)
{
return
new
CommonModule
(
null
,
null
,
null
,
null
,
error
)
}
export
interface
NormalizedModuleRequest
{
name
:
string
|
null
shortcut
:
string
|
null
name
:
string
|
null
;
shortcut
:
string
|
null
;
}
/**
* Expose ModuleResolver.
*/
type
Type
=
String
|
Number
|
Boolean
|
RegExp
|
Function
|
Object
|
Record
<
string
,
any
>
|
Array
<
any
>
type
Type
=
string
|
number
|
boolean
|
RegExp
|
Function
|
Record
<
string
,
any
>
|
Record
<
string
,
any
>
|
Array
<
any
>
class
ModuleResolver
{
private
nonScopePrefix
:
string
...
...
@@ -162,8 +184,8 @@ class ModuleResolver {
const
{
shortcut
,
name
}
=
this
.
normalizeName
(
req
)
try
{
const
entry
=
this
.
load
?
loadModule
(
<
string
>
name
,
this
.
cwd
)
:
resolveModule
(
<
string
>
name
,
this
.
cwd
)
?
loadModule
(
name
as
string
,
this
.
cwd
)
:
resolveModule
(
name
as
string
,
this
.
cwd
)
return
new
CommonModule
(
entry
,
name
,
shortcut
,
true
/* fromDep */
)
}
catch
(
error
)
{
return
getNoopModule
(
error
)
...
...
@@ -185,8 +207,8 @@ class ModuleResolver {
*/
normalizeName
(
req
:
string
):
NormalizedModuleRequest
{
let
name
=
null
let
shortcut
=
null
let
name
:
string
|
null
=
null
let
shortcut
:
string
|
null
=
null
if
(
req
.
startsWith
(
'
@
'
))
{
const
pkg
=
resolveScopePackage
(
req
)
...
...
@@ -237,28 +259,6 @@ class ModuleResolver {
}
}
/**
* Parse info of scope package.
*/
export
interface
ScopePackage
{
org
:
string
;
name
:
string
;
}
export
function
resolveScopePackage
(
name
:
string
)
{
if
(
SCOPE_PACKAGE_RE
.
test
(
name
))
{
return
{
org
:
RegExp
.
$1
,
name
:
RegExp
.
$2
}
}
return
{
org
:
''
,
name
:
''
}
}
export
const
getMarkdownItResolver
=
(
cwd
:
string
)
=>
new
ModuleResolver
(
'
markdown-it
'
,
''
,
[
String
,
Function
],
true
/* load module */
,
cwd
)
...
...
packages/@vuepress/shared-utils/src/parseEmojis.ts
浏览文件 @
364571bc
const
emojiData
=
require
(
'
markdown-it-emoji/lib/data/full.json
'
)
import
emojiData
from
'
markdown-it-emoji/lib/data/full.json
'
export
default
(
str
:
string
)
=>
{
return
String
(
str
).
replace
(
/:
(
.+
?)
:/g
,
(
placeholder
,
key
)
=>
emojiData
[
key
]
||
placeholder
)
...
...
packages/@vuepress/shared-utils/src/parseFrontmatter.ts
浏览文件 @
364571bc
const
matter
=
require
(
'
gray-matter
'
)
const
toml
=
require
(
'
toml
'
)
import
matter
from
'
gray-matter
'
import
toml
from
'
toml
'
export
=
function
parseFrontmatter
(
content
:
string
)
{
return
matter
(
content
,
{
// eslint-disable-next-line @typescript-eslint/camelcase
excerpt_separator
:
'
<!-- more -->
'
,
engines
:
{
toml
:
toml
.
parse
.
bind
(
toml
),
excerpt
:
false
toml
:
toml
.
parse
.
bind
(
toml
)
}
})
}
packages/@vuepress/shared-utils/src/parseVueFrontmatter.ts
浏览文件 @
364571bc
// @ts-ignore
import
{
parse
as
_parse
}
from
'
@vue/component-compiler-utils
'
import
parseFrontmatter
from
'
./parseFrontmatter
'
...
...
packages/@vuepress/shared-utils/src/performance.ts
浏览文件 @
364571bc
import
os
from
'
os
'
class
Performance
{
// @ts-ignore
private
_totalMemory
:
number
private
_startFreeMemory
:
number
private
_endFreeMemory
:
number
...
...
packages/@vuepress/shared-utils/src/slugify.ts
浏览文件 @
364571bc
// string.js slugify drops non ascii chars so we have to
// use a custom implementation here
// @ts-ignore
import
{
remove
as
removeDiacritics
}
from
'
diacritics
'
// eslint-disable-next-line no-control-regex
...
...
packages/@vuepress/shared-utils/src/tryChain.ts
浏览文件 @
364571bc
...
...
@@ -13,7 +13,7 @@ export = function tryChain<T, U> (resolvers: Array<Resolver<T, U>>, arg: T): U |
continue
}
try
{
response
=
(
<
Provider
<
T
,
U
>>
provider
)(
arg
)
response
=
(
provider
as
Provider
<
T
,
U
>
)(
arg
)
return
response
}
catch
(
e
)
{
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录