Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qianlong66
uni-app
提交
f620269c
U
uni-app
项目概览
qianlong66
/
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,发现更多精彩内容 >>
提交
f620269c
编写于
11月 26, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mp): support main.ts (#3019)
上级
ca8ff7b8
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
72 addition
and
4 deletion
+72
-4
packages/uni-cli-shared/__tests__/transformImports.spec.ts
packages/uni-cli-shared/__tests__/transformImports.spec.ts
+38
-0
packages/uni-cli-shared/src/constants.ts
packages/uni-cli-shared/src/constants.ts
+2
-0
packages/uni-cli-shared/src/mp/imports.ts
packages/uni-cli-shared/src/mp/imports.ts
+9
-1
packages/uni-cli-shared/src/mp/transformImports.ts
packages/uni-cli-shared/src/mp/transformImports.ts
+7
-2
packages/uni-cli-shared/src/utils.ts
packages/uni-cli-shared/src/utils.ts
+16
-1
未找到文件。
packages/uni-cli-shared/__tests__/transformImports.spec.ts
浏览文件 @
f620269c
...
...
@@ -51,6 +51,44 @@ export function createApp() {
'
component-b
'
:
'
/components/component-b
'
,
})
})
test
(
`ts`
,
async
()
=>
{
const
source
=
`
import { createSSRApp } from 'vue'
import ComponentA from './components/component-a.vue'
import ComponentB from './components/component-b.vue'
export function createApp() {
const app = createSSRApp(App)
app.provide('UseRequestConfigContext', {
requestMethod: (param: any) => {},
})
app.component('component-a',ComponentA)
app.component('component-b',ComponentB)
return {
app
}
}
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
.
replace
(
'
.js
'
,
'
.ts
'
),
{
root
,
global
:
true
,
resolve
,
dynamicImport
,
}
)
expect
(
code
).
toContain
(
`const ComponentA = ()=>import('
${
root
}
/components/component-a.vue')`
)
expect
(
code
).
toContain
(
`const ComponentB = ()=>import('
${
root
}
/components/component-b.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
'
component-a
'
:
'
/components/component-a
'
,
'
component-b
'
:
'
/components/component-b
'
,
})
})
})
describe
(
'
local
'
,
()
=>
{
const
importer
=
'
/usr/xxx/projects/test/src/pages/index/index.vue
'
...
...
packages/uni-cli-shared/src/constants.ts
浏览文件 @
f620269c
export
const
PUBLIC_DIR
=
'
static
'
export
const
EXTNAME_JS
=
[
'
.js
'
,
'
.ts
'
,
'
.jsx
'
,
'
.tsx
'
]
export
const
EXTNAME_TS
=
[
'
.ts
'
,
'
.tsx
'
]
export
const
EXTNAME_VUE
=
[
'
.vue
'
,
'
.nvue
'
]
export
const
EXTNAME_VUE_RE
=
/
\.(
vue|nvue
)
$/
export
const
EXTNAME_JS_RE
=
/
\.[
jt
]
sx
?
$/
export
const
EXTNAME_TS_RE
=
/
\.
tsx
?
$/
export
const
ASSETS_INLINE_LIMIT
=
40
*
1024
...
...
packages/uni-cli-shared/src/mp/imports.ts
浏览文件 @
f620269c
...
...
@@ -5,7 +5,14 @@ import { extend } from '@vue/shared'
import
{
isImportDeclaration
,
isImportDefaultSpecifier
}
from
'
@babel/types
'
import
{
parse
}
from
'
@babel/parser
'
import
{
EXTNAME_VUE
,
EXTNAME_VUE_RE
}
from
'
../constants
'
import
{
normalizeParsePlugins
}
from
'
../utils
'
/**
* 暂时没用
* @param source
* @param importer
* @param resolve
* @returns
*/
export
async
function
findVueComponentImports
(
source
:
string
,
importer
:
string
,
...
...
@@ -44,6 +51,7 @@ export async function findVueComponentImports(
}
if
(
EXTNAME_VUE_RE
.
test
(
res
.
id
))
{
const
expr
=
parse
(
source
.
slice
(
importSpecifier
.
ss
,
importSpecifier
.
se
),
{
plugins
:
normalizeParsePlugins
(
res
.
id
),
sourceType
:
'
module
'
,
}).
program
.
body
[
0
]
if
(
isImportDeclaration
(
expr
)
&&
expr
.
specifiers
.
length
===
1
)
{
...
...
packages/uni-cli-shared/src/mp/transformImports.ts
浏览文件 @
f620269c
...
...
@@ -20,7 +20,11 @@ import MagicString from 'magic-string'
import
{
PluginContext
}
from
'
rollup
'
import
{
M
}
from
'
../messages
'
import
{
BINDING_COMPONENTS
}
from
'
../constants
'
import
{
normalizeMiniProgramFilename
,
removeExt
}
from
'
../utils
'
import
{
normalizeMiniProgramFilename
,
normalizeParsePlugins
,
removeExt
,
}
from
'
../utils
'
import
{
addLeadingSlash
}
from
'
@dcloudio/uni-shared
'
interface
TransformVueComponentImportsOptions
{
...
...
@@ -47,9 +51,10 @@ export async function transformVueComponentImports(
if
(
!
global
&&
!
code
.
includes
(
BINDING_COMPONENTS
))
{
return
{
code
,
usingComponents
:
{}
}
}
const
s
=
new
MagicString
(
code
)
const
scriptAst
=
parse
(
code
,
{
plugins
:
[...(
babelParserPlugins
||
[])]
,
plugins
:
normalizeParsePlugins
(
importer
,
babelParserPlugins
)
,
sourceType
:
'
module
'
,
}).
program
...
...
packages/uni-cli-shared/src/utils.ts
浏览文件 @
f620269c
...
...
@@ -3,7 +3,7 @@ import os from 'os'
import
path
from
'
path
'
import
{
camelize
,
capitalize
}
from
'
@vue/shared
'
export
{
default
as
hash
}
from
'
hash-sum
'
import
{
PAGE_EXTNAME
,
PAGE_EXTNAME_APP
}
from
'
./constants
'
import
{
EXTNAME_TS_RE
,
PAGE_EXTNAME
,
PAGE_EXTNAME_APP
}
from
'
./constants
'
import
{
NodeTypes
,
...
...
@@ -11,6 +11,7 @@ import {
RootNode
,
TemplateChildNode
,
}
from
'
@vue/compiler-core
'
import
{
ParserPlugin
}
from
'
@babel/parser
'
export
let
isRunningWithYarnPnp
:
boolean
try
{
...
...
@@ -76,3 +77,17 @@ export function normalizeMiniProgramFilename(
}
return
normalizeNodeModules
(
path
.
relative
(
inputDir
,
filename
))
}
export
function
normalizeParsePlugins
(
importer
:
string
,
babelParserPlugins
?:
ParserPlugin
[]
)
{
const
isTS
=
EXTNAME_TS_RE
.
test
(
importer
.
split
(
'
?
'
)[
0
])
const
plugins
:
ParserPlugin
[]
=
[]
if
(
isTS
)
{
plugins
.
push
(
'
jsx
'
)
}
if
(
babelParserPlugins
)
plugins
.
push
(...
babelParserPlugins
)
if
(
isTS
)
plugins
.
push
(
'
typescript
'
,
'
decorators-legacy
'
)
return
plugins
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录