Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
76e85b8b
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看板
提交
76e85b8b
编写于
10月 25, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(mp): support global registration
上级
ae7ec06d
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
351 addition
and
192 deletion
+351
-192
packages/uni-cli-shared/__tests__/transformImports.spec.ts
packages/uni-cli-shared/__tests__/transformImports.spec.ts
+204
-163
packages/uni-cli-shared/src/messages/index.ts
packages/uni-cli-shared/src/messages/index.ts
+2
-0
packages/uni-cli-shared/src/mp/transformImports.ts
packages/uni-cli-shared/src/mp/transformImports.ts
+50
-2
packages/uni-mp-compiler/__tests__/test.spec.ts
packages/uni-mp-compiler/__tests__/test.spec.ts
+3
-0
packages/uni-mp-compiler/__tests__/testUtils.ts
packages/uni-mp-compiler/__tests__/testUtils.ts
+3
-0
packages/uni-mp-compiler/__tests__/transformElement.spec.ts
packages/uni-mp-compiler/__tests__/transformElement.spec.ts
+11
-2
packages/uni-mp-compiler/__tests__/vBind.spec.ts
packages/uni-mp-compiler/__tests__/vBind.spec.ts
+6
-1
packages/uni-mp-compiler/__tests__/vFor.spec.ts
packages/uni-mp-compiler/__tests__/vFor.spec.ts
+6
-1
packages/uni-mp-compiler/__tests__/vIf.spec.ts
packages/uni-mp-compiler/__tests__/vIf.spec.ts
+6
-1
packages/uni-mp-compiler/__tests__/vOn.mp.spec.ts
packages/uni-mp-compiler/__tests__/vOn.mp.spec.ts
+6
-1
packages/uni-mp-compiler/__tests__/vOn.spec.ts
packages/uni-mp-compiler/__tests__/vOn.spec.ts
+6
-1
packages/uni-mp-compiler/src/ast.ts
packages/uni-mp-compiler/src/ast.ts
+3
-1
packages/uni-mp-compiler/src/codegen.ts
packages/uni-mp-compiler/src/codegen.ts
+9
-10
packages/uni-mp-compiler/src/options.ts
packages/uni-mp-compiler/src/options.ts
+2
-0
packages/uni-mp-vite/src/index.ts
packages/uni-mp-vite/src/index.ts
+5
-1
packages/uni-mp-vite/src/plugins/mainJs.ts
packages/uni-mp-vite/src/plugins/mainJs.ts
+28
-7
packages/uni-mp-vite/src/plugins/usingComponents.ts
packages/uni-mp-vite/src/plugins/usingComponents.ts
+1
-1
未找到文件。
packages/uni-cli-shared/__tests__/transformImports.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -2,7 +2,7 @@ import path from 'path'
import
{
ResolvedId
}
from
'
rollup
'
import
{
transformVueComponentImports
}
from
'
../src/mp/transformImports
'
const
root
=
'
/usr/xxx/projects/test/src
'
const
importer
=
'
/usr/xxx/projects/test/src/pages/index/index.vue
'
async
function
resolve
(
id
:
string
,
importer
?:
string
)
{
return
{
id
:
importer
?
path
.
resolve
(
path
.
dirname
(
importer
),
id
)
:
id
,
...
...
@@ -14,181 +14,222 @@ function dynamicImport(name: string, source: string) {
}
describe
(
'
transformVueComponentImports
'
,
()
=>
{
test
(
`basic`
,
async
()
=>
{
const
source
=
`import test1 from "
${
root
}
/components/test1.vue";
const _sfc_main = {
components: {
test1
}
};
const __BINDING_COMPONENTS__ = '{"test1":{"name":"_component_test1","type":"unknown"}}';
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {};
}
import "
${
importer
}
?vue&type=style&index=0&lang.css";
import _export_sfc from "plugin-vue:export-helper";
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
describe
(
'
global
'
,
()
=>
{
const
importer
=
'
/usr/xxx/projects/test/src/main.js
'
test
(
`basic`
,
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.component('component-a',ComponentA)
app.component('component-b',ComponentB)
return {
app
}
}
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
}
)
expect
(
code
).
toContain
(
`const test1 = ()=>import('
${
root
}
/components/test1.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test1
:
'
/components/test1
'
})
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
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
'
,
})
})
})
test
(
`easycom`
,
async
()
=>
{
const
source
=
`import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = {
components: {
test1,
MyComponentName
}
};
const __BINDING_COMPONENTS__ = '{"test":{"name":"_easycom_test","type":"unknown"},"test1":{"name":"_component_test1","type":"unknown"},"MyComponentName":{"name":"_component_MyComponentName","type":"unknown"},"my-component-name":{"name":"_component_my_component_name","type":"unknown"}}';
import _easycom_test from "
${
root
}
/components/test/test.vue";
if (!Math) {
Math.max.call(Max, _easycom_test);
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {};
}
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
import _export_sfc from "plugin-vue:export-helper";
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
describe
(
'
local
'
,
()
=>
{
const
importer
=
'
/usr/xxx/projects/test/src/pages/index/index.vue
'
test
(
`basic`
,
async
()
=>
{
const
source
=
`import test1 from "
${
root
}
/components/test1.vue";
const _sfc_main = {
components: {
test1
}
};
const __BINDING_COMPONENTS__ = '{"test1":{"name":"_component_test1","type":"unknown"}}';
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {};
}
)
expect
(
code
).
toContain
(
`const _easycom_test = ()=>import('
${
root
}
/components/test/test.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test
:
'
/components/test/test
'
,
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
import "
${
importer
}
?vue&type=style&index=0&lang.css";
import _export_sfc from "plugin-vue:export-helper";
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
}
)
expect
(
code
).
toContain
(
`const test1 = ()=>import('
${
root
}
/components/test1.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test1
:
'
/components/test1
'
})
})
})
test
(
`PascalCase`
,
async
()
=>
{
const
source
=
`import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = {
components: {
test1,
MyComponentName
test
(
`easycom`
,
async
()
=>
{
const
source
=
`import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = {
components: {
test1,
MyComponentName
}
};
const __BINDING_COMPONENTS__ = '{"test":{"name":"_easycom_test","type":"unknown"},"test1":{"name":"_component_test1","type":"unknown"},"MyComponentName":{"name":"_component_MyComponentName","type":"unknown"},"my-component-name":{"name":"_component_my_component_name","type":"unknown"}}';
import _easycom_test from "
${
root
}
/components/test/test.vue";
if (!Math) {
Math.max.call(Max, _easycom_test);
}
};
const __BINDING_COMPONENTS__ = '{"test1":{"name":"_component_test1","type":"unknown"},"MyComponentName":{"name":"_component_MyComponentName","type":"unknown"},"my-component-name":{"name":"_component_my_component_name","type":"unknown"}}';
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {};
}
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
import _export_sfc from "plugin-vue:export-helper";
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {};
}
)
expect
(
code
).
toContain
(
`const MyComponentName = ()=>import('
${
root
}
/components/test1.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
import _export_sfc from "plugin-vue:export-helper";
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
}
)
expect
(
code
).
toContain
(
`const _easycom_test = ()=>import('
${
root
}
/components/test/test.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test
:
'
/components/test/test
'
,
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
})
})
})
test
(
`setup`
,
async
()
=>
{
const
source
=
`import { defineComponent as _defineComponent } from "vue";
const __BINDING_COMPONENTS__ = '{"test1":{"name":"test1","type":"setup"},"MyComponentName":{"name":"MyComponentName","type":"setup"},"my-component-name":{"name":"MyComponentName","type":"setup"}}';
if (!Math) {
Math.max.call(Max, test1, MyComponentName, MyComponentName);
}
import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = /* @__PURE__ */ _defineComponent({
setup(__props) {
return (_ctx, _cache) => {
return {};
};
}
});
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
export default _sfc_main;
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
test
(
`PascalCase`
,
async
()
=>
{
const
source
=
`import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = {
components: {
test1,
MyComponentName
}
};
const __BINDING_COMPONENTS__ = '{"test1":{"name":"_component_test1","type":"unknown"},"MyComponentName":{"name":"_component_MyComponentName","type":"unknown"},"my-component-name":{"name":"_component_my_component_name","type":"unknown"}}';
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {};
}
)
expect
(
code
).
toContain
(
`const MyComponentName = ()=>import('
${
root
}
/components/test1.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
import _export_sfc from "plugin-vue:export-helper";
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
}
)
expect
(
code
).
toContain
(
`const MyComponentName = ()=>import('
${
root
}
/components/test1.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
})
})
})
test
(
`setup with easycom`
,
async
()
=>
{
const
source
=
`import { defineComponent as _defineComponent } from "vue";
const __BINDING_COMPONENTS__ = '{"test":{"name":"_easycom_test","type":"unknown"},"test1":{"name":"test1","type":"setup"},"MyComponentName":{"name":"MyComponentName","type":"setup"},"my-component-name":{"name":"MyComponentName","type":"setup"}}';
import _easycom_test from "
${
root
}
/components/test/test.vue";
if (!Math) {
Math.max.call(Max, _easycom_test, test1, MyComponentName, MyComponentName);
}
import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = /* @__PURE__ */ _defineComponent({
setup(__props) {
return (_ctx, _cache) => {
return {};
};
test
(
`setup`
,
async
()
=>
{
const
source
=
`import { defineComponent as _defineComponent } from "vue";
const __BINDING_COMPONENTS__ = '{"test1":{"name":"test1","type":"setup"},"MyComponentName":{"name":"MyComponentName","type":"setup"},"my-component-name":{"name":"MyComponentName","type":"setup"}}';
if (!Math) {
Math.max.call(Max, test1, MyComponentName, MyComponentName);
}
});
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
export default _sfc_main;
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = /* @__PURE__ */ _defineComponent({
setup(__props) {
return (_ctx, _cache) => {
return {};
};
}
});
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
export default _sfc_main;
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
}
)
expect
(
code
).
toContain
(
`const MyComponentName = ()=>import('
${
root
}
/components/test1.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
})
})
test
(
`setup with easycom`
,
async
()
=>
{
const
source
=
`import { defineComponent as _defineComponent } from "vue";
const __BINDING_COMPONENTS__ = '{"test":{"name":"_easycom_test","type":"unknown"},"test1":{"name":"test1","type":"setup"},"MyComponentName":{"name":"MyComponentName","type":"setup"},"my-component-name":{"name":"MyComponentName","type":"setup"}}';
import _easycom_test from "
${
root
}
/components/test/test.vue";
if (!Math) {
Math.max.call(Max, _easycom_test, test1, MyComponentName, MyComponentName);
}
)
expect
(
code
).
toContain
(
`const _easycom_test = ()=>import('
${
root
}
/components/test/test.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test
:
'
/components/test/test
'
,
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
import test1 from "../../components/test1.vue";
import MyComponentName from "../../components/test1.vue";
const _sfc_main = /* @__PURE__ */ _defineComponent({
setup(__props) {
return (_ctx, _cache) => {
return {};
};
}
});
import "
${
root
}
/pages/index/index.vue?vue&type=style&index=0&lang.css";
export default _sfc_main;
`
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
importer
,
{
root
,
resolve
,
dynamicImport
,
}
)
expect
(
code
).
toContain
(
`const _easycom_test = ()=>import('
${
root
}
/components/test/test.vue')`
)
expect
(
usingComponents
).
toMatchObject
({
test
:
'
/components/test/test
'
,
test1
:
'
/components/test1
'
,
'
my-component-name
'
:
'
/components/test1
'
,
})
})
})
})
packages/uni-cli-shared/src/messages/index.ts
浏览文件 @
76e85b8b
...
...
@@ -18,4 +18,6 @@ export const M = {
'
i18n.fallbackLocale.missing
'
:
'
当前应用配置的 fallbackLocale 或 locale 为:{locale},但 locale 目录缺少该语言文件
'
,
'
easycom.conflict
'
:
'
easycom组件冲突:
'
,
'
mp.component.args[0]
'
:
'
{0}的第一个参数必须为静态字符串
'
,
'
mp.component.args[1]
'
:
'
{0}需要两个参数
'
,
}
packages/uni-cli-shared/src/mp/transformImports.ts
浏览文件 @
76e85b8b
import
{
parse
,
ParserPlugin
}
from
'
@babel/parser
'
import
{
ImportDeclaration
,
isCallExpression
,
isIdentifier
,
isImportDeclaration
,
isMemberExpression
,
isObjectExpression
,
isObjectProperty
,
isStringLiteral
,
...
...
@@ -16,11 +18,13 @@ import { camelize, capitalize, hyphenate } from '@vue/shared'
import
{
walk
}
from
'
estree-walker
'
import
MagicString
from
'
magic-string
'
import
{
PluginContext
}
from
'
rollup
'
import
{
M
}
from
'
../messages
'
import
{
BINDING_COMPONENTS
}
from
'
../constants
'
import
{
normalizeMiniProgramFilename
,
removeExt
}
from
'
../utils
'
interface
TransformVueComponentImportsOptions
{
root
:
string
global
?:
boolean
resolve
:
PluginContext
[
'
resolve
'
]
dynamicImport
:
(
name
:
string
,
source
:
string
)
=>
string
babelParserPlugins
?:
ParserPlugin
[]
...
...
@@ -31,6 +35,7 @@ export async function transformVueComponentImports(
{
root
,
resolve
,
global
,
dynamicImport
,
babelParserPlugins
,
}:
TransformVueComponentImportsOptions
...
...
@@ -38,7 +43,7 @@ export async function transformVueComponentImports(
code
:
string
usingComponents
:
Record
<
string
,
string
>
}
>
{
if
(
!
code
.
includes
(
BINDING_COMPONENTS
))
{
if
(
!
global
&&
!
code
.
includes
(
BINDING_COMPONENTS
))
{
return
{
code
,
usingComponents
:
{}
}
}
const
s
=
new
MagicString
(
code
)
...
...
@@ -49,7 +54,9 @@ export async function transformVueComponentImports(
const
imports
=
findVueComponentImports
(
scriptAst
.
body
,
parseComponents
(
scriptAst
,
findBindingComponents
(
scriptAst
.
body
))
global
?
parseGlobalComponents
(
scriptAst
)
:
parseComponents
(
scriptAst
,
findBindingComponents
(
scriptAst
.
body
))
)
const
usingComponents
:
Record
<
string
,
string
>
=
{}
for
(
let
i
=
0
;
i
<
imports
.
length
;
i
++
)
{
...
...
@@ -116,6 +123,47 @@ function findBindingComponents(ast: Statement[]): BindingComponents {
}
return
{}
}
/**
* 查找全局组件定义:app.component('component-a',{})
* @param ast
* @returns
*/
function
parseGlobalComponents
(
ast
:
Program
)
{
const
bindingComponents
:
BindingComponents
=
{}
;(
walk
as
any
)(
ast
,
{
enter
(
child
:
Node
)
{
if
(
!
isCallExpression
(
child
))
{
return
}
const
{
callee
}
=
child
// .component
if
(
!
isMemberExpression
(
callee
)
||
!
isIdentifier
(
callee
.
property
)
||
callee
.
property
.
name
!==
'
component
'
)
{
return
}
// .component('component-a',{})
const
args
=
child
.
arguments
if
(
args
.
length
!==
2
)
{
return
}
const
[
name
,
value
]
=
args
if
(
!
isStringLiteral
(
name
))
{
return
console
.
warn
(
M
[
'
mp.component.args[0]
'
])
}
if
(
!
isIdentifier
(
value
))
{
return
console
.
warn
(
M
[
'
mp.component.args[1]
'
])
}
bindingComponents
[
value
.
name
]
=
{
tag
:
name
.
value
,
type
:
'
unknown
'
,
}
},
})
return
bindingComponents
}
/**
* 从 components 中查找定义的组件,修改 bindingComponents
* @param ast
...
...
packages/uni-mp-compiler/__tests__/test.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -13,6 +13,9 @@ function assert(
filename
:
'
foo.vue
'
,
prefixIdentifiers
:
true
,
inline
:
true
,
generatorOpts
:
{
concise
:
true
,
},
miniProgram
:
{
slot
:
{
fallback
:
false
,
...
...
packages/uni-mp-compiler/__tests__/testUtils.ts
浏览文件 @
76e85b8b
...
...
@@ -26,6 +26,9 @@ export function assert(
inline
:
true
,
isNativeTag
,
isCustomElement
,
generatorOpts
:
{
concise
:
true
,
},
miniProgram
:
{
slot
:
{
fallback
:
false
,
...
...
packages/uni-mp-compiler/__tests__/transformElement.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -16,7 +16,12 @@ function parseWithElementTransform(
root
:
RootNode
node
:
ElementNode
}
{
const
{
ast
,
code
,
preamble
}
=
compile
(
`<div>
${
template
}
</div>`
,
options
)
const
{
ast
,
code
,
preamble
}
=
compile
(
`<div>
${
template
}
</div>`
,
{
generatorOpts
:
{
concise
:
true
,
},
...
options
,
})
const
node
=
(
ast
as
any
).
children
[
0
].
children
[
0
]
return
{
code
,
...
...
@@ -32,10 +37,14 @@ describe('compiler: element transform', () => {
source
:
`<image src="/static/logo.png"/>`
,
filename
:
'
foo.vue
'
,
id
:
'
foo
'
,
compiler
:
MPCompiler
as
unknown
as
TemplateCompiler
,
compilerOptions
:
{
mode
:
'
module
'
,
},
generatorOpts
:
{
concise
:
true
,
},
}
as
any
,
transformAssetUrls
:
{
includeAbsolute
:
true
,
...(
createUniVueTransformAssetUrls
(
'
/
'
)
as
Record
<
string
,
any
>
),
...
...
packages/uni-mp-compiler/__tests__/vBind.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -5,7 +5,12 @@ import { CompilerOptions } from '../src/options'
import
{
assert
,
miniProgram
}
from
'
./testUtils
'
function
parseWithVBind
(
template
:
string
,
options
:
CompilerOptions
=
{})
{
const
{
ast
,
code
}
=
compile
(
template
,
options
)
const
{
ast
,
code
}
=
compile
(
template
,
{
generatorOpts
:
{
concise
:
true
,
},
...
options
,
})
return
{
code
,
node
:
ast
.
children
[
0
]
as
ElementNode
,
...
...
packages/uni-mp-compiler/__tests__/vFor.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -14,7 +14,12 @@ function parseWithForTransform(
template
:
string
,
options
:
CompilerOptions
=
{}
)
{
const
{
ast
}
=
compile
(
template
,
options
)
const
{
ast
}
=
compile
(
template
,
{
generatorOpts
:
{
concise
:
true
,
},
...
options
,
})
return
{
root
:
ast
,
...
...
packages/uni-mp-compiler/__tests__/vIf.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -9,7 +9,12 @@ function compileWithIfTransform(
returnIndex
:
number
=
0
,
childrenLen
:
number
=
1
)
{
const
{
ast
}
=
compile
(
template
,
options
)
const
{
ast
}
=
compile
(
template
,
{
generatorOpts
:
{
concise
:
true
,
},
...
options
,
})
if
(
!
options
.
onError
)
{
expect
(
ast
.
children
.
length
).
toBe
(
childrenLen
)
for
(
let
i
=
0
;
i
<
childrenLen
;
i
++
)
{
...
...
packages/uni-mp-compiler/__tests__/vOn.mp.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -5,7 +5,12 @@ import { CompilerOptions } from '../src/options'
import
{
assert
}
from
'
./testUtils
'
function
parseWithVOn
(
template
:
string
,
options
:
CompilerOptions
=
{})
{
const
{
ast
}
=
compile
(
template
,
options
)
const
{
ast
}
=
compile
(
template
,
{
generatorOpts
:
{
concise
:
true
,
},
...
options
,
})
return
{
root
:
ast
,
node
:
ast
.
children
[
0
]
as
ElementNode
,
...
...
packages/uni-mp-compiler/__tests__/vOn.spec.ts
浏览文件 @
76e85b8b
...
...
@@ -4,7 +4,12 @@ import { CompilerOptions } from '../src/options'
import
{
assert
}
from
'
./testUtils
'
function
parseWithVOn
(
template
:
string
,
options
:
CompilerOptions
=
{})
{
const
{
ast
}
=
compile
(
template
,
options
)
const
{
ast
}
=
compile
(
template
,
{
generatorOpts
:
{
concise
:
true
,
},
...
options
,
})
return
{
root
:
ast
,
node
:
ast
.
children
[
0
]
as
ElementNode
,
...
...
packages/uni-mp-compiler/src/ast.ts
浏览文件 @
76e85b8b
...
...
@@ -94,7 +94,9 @@ export function parseExpr(
code
=
genExpr
(
code
)
}
try
{
return
parseExpression
(
code
)
return
parseExpression
(
code
,
{
plugins
:
context
.
expressionPlugins
,
})
}
catch
(
e
:
any
)
{
context
.
onError
(
createCompilerError
(
...
...
packages/uni-mp-compiler/src/codegen.ts
浏览文件 @
76e85b8b
import
{
isString
,
isSymbol
}
from
'
@vue/shared
'
import
{
isString
,
isSymbol
,
hasOwn
}
from
'
@vue/shared
'
import
{
CodegenResult
,
CompoundExpressionNode
,
...
...
@@ -11,7 +11,7 @@ import {
TO_DISPLAY_STRING
,
}
from
'
@vue/compiler-core
'
import
{
Expression
}
from
'
@babel/types
'
import
{
default
as
babelGenerate
}
from
'
@babel/generator
'
import
{
default
as
babelGenerate
,
GeneratorOptions
}
from
'
@babel/generator
'
import
{
addImportDeclaration
,
matchEasycom
}
from
'
@dcloudio/uni-cli-shared
'
import
{
CodegenOptions
,
CodegenRootNode
}
from
'
./options
'
...
...
@@ -88,7 +88,7 @@ export function generate(
}
push
(
`return `
)
push
(
genBabelExpr
(
ast
.
renderData
))
push
(
genBabelExpr
(
ast
.
renderData
,
options
.
generatorOpts
))
if
(
useWithBlock
)
{
deindent
()
push
(
`}`
)
...
...
@@ -283,13 +283,12 @@ function createGenNodeContext() {
return
context
}
export
function
genBabelExpr
(
expr
:
Expression
)
{
return
babelGenerate
(
expr
,
{
concise
:
true
,
jsescOption
:
{
quotes
:
'
single
'
,
},
}).
code
export
function
genBabelExpr
(
expr
:
Expression
,
opts
:
GeneratorOptions
=
{})
{
if
(
!
hasOwn
(
opts
,
'
jsescOption
'
))
{
opts
.
jsescOption
=
{}
}
opts
.
jsescOption
!
.
quotes
=
'
single
'
return
babelGenerate
(
expr
,
opts
).
code
}
export
function
genExpr
(
...
...
packages/uni-mp-compiler/src/options.ts
浏览文件 @
76e85b8b
import
{
ParserPlugin
}
from
'
@babel/parser
'
import
{
GeneratorOptions
}
from
'
@babel/generator
'
import
{
CallExpression
,
Expression
,
...
...
@@ -90,6 +91,7 @@ export interface CodegenOptions extends SharedTransformCodegenOptions {
scopeId
?:
string
|
null
runtimeModuleName
?:
string
runtimeGlobalName
?:
string
generatorOpts
?:
GeneratorOptions
miniProgram
?:
MiniProgramCompilerOptions
}
...
...
packages/uni-mp-vite/src/index.ts
浏览文件 @
76e85b8b
...
...
@@ -12,7 +12,11 @@ import { uniRenderjsPlugin } from './plugins/renderjs'
export
{
UniMiniProgramPluginOptions
}
from
'
./plugin
'
export
default
(
options
:
UniMiniProgramPluginOptions
)
=>
{
return
[
uniMainJsPlugin
(
options
),
(
options
:
{
vueOptions
?:
{
script
?:
Partial
<
SFCScriptCompileOptions
>
}
})
=>
{
return
uniMainJsPlugin
(
options
.
vueOptions
?.
script
)
},
uniManifestJsonPlugin
(
options
),
uniPagesJsonPlugin
(
options
),
uniEntryPlugin
(
options
),
...
...
packages/uni-mp-vite/src/plugins/mainJs.ts
浏览文件 @
76e85b8b
import
{
defineUniMainJsPlugin
}
from
'
@dcloudio/uni-cli-shared
'
import
{
UniMiniProgramPluginOptions
}
from
'
../plugin
'
import
{
addMiniProgramUsingComponents
,
defineUniMainJsPlugin
,
transformVueComponentImports
,
}
from
'
@dcloudio/uni-cli-shared
'
import
{
SFCScriptCompileOptions
}
from
'
@vue/compiler-sfc
'
import
{
dynamicImport
}
from
'
./usingComponents
'
export
function
uniMainJsPlugin
(
options
:
UniMiniProgramPluginOptions
)
{
export
function
uniMainJsPlugin
(
options
:
Partial
<
SFCScriptCompileOptions
>
=
{}
)
{
return
defineUniMainJsPlugin
((
opts
)
=>
{
return
{
name
:
'
vite:uni-mp-main-js
'
,
enforce
:
'
pre
'
,
transform
(
cod
e
,
id
)
{
async
transform
(
sourc
e
,
id
)
{
if
(
opts
.
filter
(
id
))
{
code
=
code
.
includes
(
'
createSSRApp
'
)
?
createApp
(
code
)
:
createLegacyApp
(
code
)
source
=
source
.
includes
(
'
createSSRApp
'
)
?
createApp
(
source
)
:
createLegacyApp
(
source
)
const
inputDir
=
process
.
env
.
UNI_INPUT_DIR
const
{
code
,
usingComponents
}
=
await
transformVueComponentImports
(
source
,
id
,
{
root
:
inputDir
,
global
:
true
,
resolve
:
this
.
resolve
,
dynamicImport
,
babelParserPlugins
:
options
.
babelParserPlugins
,
}
)
addMiniProgramUsingComponents
(
'
app
'
,
usingComponents
)
return
{
code
:
`import 'plugin-vue:export-helper';import 'uni-mp-runtime';import './pages.json.js';`
+
...
...
packages/uni-mp-vite/src/plugins/usingComponents.ts
浏览文件 @
76e85b8b
...
...
@@ -49,6 +49,6 @@ export function uniUsingComponentsPlugin(
}
}
function
dynamicImport
(
name
:
string
,
value
:
string
)
{
export
function
dynamicImport
(
name
:
string
,
value
:
string
)
{
return
`const
${
name
}
= ()=>import('
${
virtualComponentPath
(
value
)}
')`
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录