Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
tomleePHP
uni-app
提交
cc221698
U
uni-app
项目概览
tomleePHP
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cc221698
编写于
12月 02, 2019
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(v3): uni-migration
上级
90945f7f
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
135 addition
and
40 deletion
+135
-40
packages/uni-migration/__tests__/demo.js
packages/uni-migration/__tests__/demo.js
+3
-1
packages/uni-migration/lib/index.js
packages/uni-migration/lib/index.js
+29
-4
packages/uni-migration/lib/mp-weixin/transform/file-transformer.js
...uni-migration/lib/mp-weixin/transform/file-transformer.js
+9
-4
packages/uni-migration/lib/mp-weixin/transform/index.js
packages/uni-migration/lib/mp-weixin/transform/index.js
+13
-3
packages/uni-migration/lib/mp-weixin/transform/json-transformer.js
...uni-migration/lib/mp-weixin/transform/json-transformer.js
+15
-7
packages/uni-migration/lib/mp-weixin/transform/script-transformer.js
...i-migration/lib/mp-weixin/transform/script-transformer.js
+5
-5
packages/uni-migration/lib/mp-weixin/transform/template-transformer/index.js
...ion/lib/mp-weixin/transform/template-transformer/index.js
+4
-4
packages/uni-migration/lib/mp-weixin/transform/template-transformer/transform/generate.js
...ixin/transform/template-transformer/transform/generate.js
+24
-4
packages/uni-migration/lib/mp-weixin/transform/template-transformer/transform/index.js
...-weixin/transform/template-transformer/transform/index.js
+3
-2
packages/uni-migration/lib/mp-weixin/util.js
packages/uni-migration/lib/mp-weixin/util.js
+16
-1
packages/uni-mp-weixin/dist/mp.js
packages/uni-mp-weixin/dist/mp.js
+14
-5
未找到文件。
packages/uni-migration/__tests__/demo.js
浏览文件 @
cc221698
...
...
@@ -13,5 +13,7 @@ migrate('/Users/fxy/Downloads/wa-vantui_1.1/wxcomponents/vant',
// } = require('../lib/mp-weixin/transform/template-transformer')
// console.log(transformTemplate(
// `<view wx:for="{{ isSimple(columns) ? [columns] : columns }}" wx:for-item="item1" wx:key="{{ index }}"/>`
// `<view></view><wxs module="a">aasdf</wxs>`, {
// filename: 'index'
// }
// ))
packages/uni-migration/lib/index.js
浏览文件 @
cc221698
...
...
@@ -7,6 +7,24 @@ const migraters = {
'
mp-weixin
'
:
require
(
'
./mp-weixin
'
)
}
/**
* 先简单的 hack 一下,支持 vant 的 array.wxs
* @param {Object} src
* @param {Object} dest
*/
function
hackVant
(
src
,
dest
)
{
if
(
src
.
indexOf
(
'
array.wxs
'
)
!==
-
1
)
{
fs
.
outputFileSync
(
dest
,
fs
.
readFileSync
(
src
)
.
toString
()
.
replace
(
`array.constructor === 'Array'`
,
'
Array.isArray(array)
'
)
)
return
true
}
return
false
}
module
.
exports
=
function
migrate
(
input
,
out
,
options
=
{
platform
:
'
mp-weixin
'
})
{
...
...
@@ -24,9 +42,16 @@ module.exports = function migrate(input, out, options = {
})
const
styleExtname
=
options
.
extname
.
style
assets
.
forEach
(
asset
=>
{
const
src
=
path
.
resolve
(
input
,
asset
)
const
dest
=
path
.
resolve
(
out
,
asset
.
replace
(
styleExtname
,
'
.css
'
))
console
.
log
(
`copy:
${
dest
}
`
)
fs
.
copySync
(
src
,
dest
)
if
(
typeof
asset
===
'
string
'
)
{
const
src
=
path
.
resolve
(
input
,
asset
)
const
dest
=
path
.
resolve
(
out
,
asset
.
replace
(
styleExtname
,
'
.css
'
))
console
.
log
(
`copy:
${
dest
}
`
)
if
(
!
hackVant
(
src
,
dest
))
{
fs
.
copySync
(
src
,
dest
)
}
}
else
{
console
.
log
(
`write:
${
path
.
resolve
(
out
,
asset
.
path
)}
`
)
fs
.
outputFileSync
(
path
.
resolve
(
out
,
asset
.
path
),
asset
.
content
)
}
})
}
packages/uni-migration/lib/mp-weixin/transform/file-transformer.js
浏览文件 @
cc221698
const
path
=
require
(
'
path
'
)
const
{
transformJsonFile
}
=
require
(
'
./json-transformer
'
)
...
...
@@ -23,12 +25,14 @@ module.exports = function transformFile(input, options) {
filepath
+
templateExtname
]
const
[
usingComponent
sCode
]
=
transformJsonFile
(
filepath
+
'
.json
'
,
deps
)
const
[
j
sCode
]
=
transformJsonFile
(
filepath
+
'
.json
'
,
deps
)
const
[
templateCode
,
wxsCode
=
''
]
=
transformTemplateFile
(
filepath
+
templateExtname
)
const
[
templateCode
,
wxsCode
=
''
,
wxsFiles
=
[]]
=
transformTemplateFile
(
filepath
+
templateExtname
,
{
filename
:
path
.
basename
(
filepath
)
})
const
styleCode
=
transformStyleFile
(
filepath
+
styleExtname
,
options
,
deps
)
||
''
const
scriptCode
=
transformScriptFile
(
filepath
+
'
.js
'
,
usingComponent
sCode
,
options
,
deps
)
const
scriptCode
=
transformScriptFile
(
filepath
+
'
.js
'
,
j
sCode
,
options
,
deps
)
return
[
`<template>
...
...
@@ -41,6 +45,7 @@ ${scriptCode}
<style>
${
styleCode
}
</style>`
,
deps
deps
,
wxsFiles
]
}
packages/uni-migration/lib/mp-weixin/transform/index.js
浏览文件 @
cc221698
...
...
@@ -3,11 +3,12 @@ const glob = require('glob')
const
transformFile
=
require
(
'
./file-transformer
'
)
function
generateVueFile
(
input
,
out
,
options
)
{
const
[
content
,
deps
]
=
transformFile
(
input
,
options
)
const
[
content
,
deps
,
wxsFiles
]
=
transformFile
(
input
,
options
)
return
{
path
:
path
.
resolve
(
out
,
path
.
basename
(
input
).
replace
(
options
.
extname
.
template
,
'
.vue
'
)),
content
,
deps
deps
,
wxsFiles
}
}
...
...
@@ -28,12 +29,21 @@ function generateVueFolder(input, out, options) {
)
files
.
push
(
vueFile
)
deps
.
push
(...
vueFile
.
deps
)
const
dirname
=
path
.
dirname
(
file
)
vueFile
.
wxsFiles
.
forEach
(
wxsFile
=>
{
wxsFile
.
path
=
path
.
join
(
dirname
,
wxsFile
.
path
)
assets
.
push
(
wxsFile
)
})
}
else
{
assets
.
push
(
file
)
}
})
return
[
files
,
assets
.
filter
(
asset
=>
{
return
!
deps
.
includes
(
path
.
resolve
(
input
,
asset
))
if
(
typeof
asset
===
'
string
'
)
{
return
!
deps
.
includes
(
path
.
resolve
(
input
,
asset
))
}
return
true
})]
}
...
...
packages/uni-migration/lib/mp-weixin/transform/json-transformer.js
浏览文件 @
cc221698
const
fs
=
require
(
'
fs
'
)
const
{
camelize
,
capitalize
}
=
require
(
'
../util
'
)
function
transformJson
(
content
)
{
const
{
usingComponents
}
=
JSON
.
parse
(
content
)
if
(
!
usingComponents
)
{
return
[
'
{}
'
]
return
[
''
]
}
const
usingComponentsCode
=
[]
const
importCode
=
[]
const
componentsCode
=
[]
Object
.
keys
(
usingComponents
).
forEach
(
name
=>
{
usingComponentsCode
.
push
(
`'
${
name
}
': require('
${
usingComponents
[
name
]}
.vue').default`
)
const
identifier
=
capitalize
(
camelize
(
name
))
importCode
.
push
(
`import
${
identifier
}
from '
${
usingComponents
[
name
]}
.vue'`
)
componentsCode
.
push
(
`'
${
name
}
':
${
identifier
}
`
)
})
return
[
`
{
${
usingComponentsCode
.
join
(
'
,
\n
'
)
}
}
`
]
return
[
`
${
importCode
.
join
(
'
\n
'
)}
global['__wxVueOptions'] = {components:{
${
componentsCode
.
join
(
'
,
'
)}
}
}
`
]
}
module
.
exports
=
{
transformJson
,
transformJsonFile
(
filepath
,
deps
)
{
if
(
!
fs
.
existsSync
(
filepath
))
{
return
[
'
{}
'
]
return
[
''
]
}
deps
.
push
(
filepath
)
return
transformJson
(
fs
.
readFileSync
(
filepath
,
'
utf8
'
).
toString
().
trim
())
...
...
packages/uni-migration/lib/mp-weixin/transform/script-transformer.js
浏览文件 @
cc221698
...
...
@@ -5,16 +5,16 @@ const {
normalizePath
}
=
require
(
'
../util
'
)
function
transformScript
(
content
,
route
,
usingComponentsC
ode
)
{
return
`
global['__wxRoute'].push('
${
route
}
')
global['__wx
UsingComponents'].push(
${
usingComponentsCode
}
)
function
transformScript
(
content
,
route
,
c
ode
)
{
return
`
${
code
}
global['__wx
Route'] = '
${
route
}
'
${
content
}
export default global['__wxComponents']['
${
route
}
']`
}
module
.
exports
=
{
transformScript
,
transformScriptFile
(
filepath
,
usingComponentsC
ode
,
options
,
deps
)
{
transformScriptFile
(
filepath
,
c
ode
,
options
,
deps
)
{
let
content
=
''
if
(
!
fs
.
existsSync
(
filepath
))
{
content
=
`
...
...
@@ -29,6 +29,6 @@ Component({})
route
=
normalizePath
(
path
.
relative
(
options
.
base
,
filepath
))
}
route
=
route
.
replace
(
'
.js
'
,
''
)
return
transformScript
(
content
,
route
,
usingComponentsC
ode
,
options
)
return
transformScript
(
content
,
route
,
c
ode
,
options
)
}
}
packages/uni-migration/lib/mp-weixin/transform/template-transformer/index.js
浏览文件 @
cc221698
...
...
@@ -4,13 +4,13 @@ const path = require('path')
const
parse
=
require
(
'
./parser
'
)
const
transform
=
require
(
'
./transform
'
)
function
transformTemplate
(
content
)
{
return
transform
(
parse
(
content
))
function
transformTemplate
(
content
,
options
=
{}
)
{
return
transform
(
parse
(
content
)
,
options
)
}
module
.
exports
=
{
transformTemplate
,
transformTemplateFile
(
filepath
)
{
return
transformTemplate
(
fs
.
readFileSync
(
filepath
,
'
utf8
'
).
toString
().
trim
())
transformTemplateFile
(
filepath
,
options
=
{}
)
{
return
transformTemplate
(
fs
.
readFileSync
(
filepath
,
'
utf8
'
).
toString
().
trim
()
,
options
)
}
}
packages/uni-migration/lib/mp-weixin/transform/template-transformer/transform/generate.js
浏览文件 @
cc221698
...
...
@@ -33,8 +33,28 @@ function genElement(node) {
return
''
}
function
genWxs
(
wxs
)
{
return
wxs
.
map
(
wxsNode
=>
genElement
(
wxsNode
)).
join
(
''
).
trim
()
function
genWxs
(
wxs
,
state
)
{
const
wxsCode
=
[]
const
wxsFiles
=
[]
wxs
.
forEach
(
wxsNode
=>
{
const
{
src
,
module
}
=
wxsNode
.
attribs
if
(
!
module
)
{
return
}
if
(
!
src
)
{
wxsNode
.
attribs
.
src
=
'
./
'
+
(
state
.
filename
?
(
state
.
filename
+
'
-
'
+
module
)
:
module
)
+
'
.wxs
'
wxsFiles
.
push
({
path
:
wxsNode
.
attribs
.
src
,
content
:
genChildren
(
wxsNode
)
})
}
wxsNode
.
children
.
length
=
0
wxsCode
.
push
(
genElement
(
wxsNode
))
})
return
[
wxsCode
.
join
(
''
).
trim
(),
wxsFiles
]
}
function
shouldWrapper
(
node
)
{
...
...
@@ -56,7 +76,7 @@ function shouldWrapper(node) {
module
.
exports
=
function
generate
(
node
,
state
)
{
if
(
shouldWrapper
(
node
))
{
return
[
`<view>
${
genChildren
(
node
).
trim
()}
</view>`
,
genWxs
(
state
.
wxs
)]
return
[
`<view>
${
genChildren
(
node
).
trim
()}
</view>`
,
...
genWxs
(
state
.
wxs
,
state
)]
}
return
[
genChildren
(
node
).
trim
(),
genWxs
(
state
.
wxs
)]
return
[
genChildren
(
node
).
trim
(),
...
genWxs
(
state
.
wxs
,
state
)]
}
packages/uni-migration/lib/mp-weixin/transform/template-transformer/transform/index.js
浏览文件 @
cc221698
const
traverse
=
require
(
'
./traverse
'
)
const
generate
=
require
(
'
./generate
'
)
module
.
exports
=
function
transform
(
ast
)
{
module
.
exports
=
function
transform
(
ast
,
options
)
{
const
state
=
{
wxs
:
[]
wxs
:
[],
filename
:
options
.
filename
}
return
generate
(
traverse
(
ast
,
state
),
state
)
}
packages/uni-migration/lib/mp-weixin/util.js
浏览文件 @
cc221698
const
isWin
=
/^win/
.
test
(
process
.
platform
)
const
normalizePath
=
path
=>
(
isWin
?
path
.
replace
(
/
\\
/g
,
'
/
'
)
:
path
)
const
normalizePath
=
path
=>
(
isWin
?
path
.
replace
(
/
\\
/g
,
'
/
'
)
:
path
)
const
camelizeRE
=
/-
(\w)
/g
function
camelize
(
str
)
{
return
str
.
replace
(
camelizeRE
,
(
_
,
c
)
=>
c
?
c
.
toUpperCase
()
:
''
)
}
/**
* Capitalize a string.
*/
function
capitalize
(
str
)
{
return
str
.
charAt
(
0
).
toUpperCase
()
+
str
.
slice
(
1
)
}
module
.
exports
=
{
camelize
,
capitalize
,
normalizePath
}
packages/uni-mp-weixin/dist/mp.js
浏览文件 @
cc221698
...
...
@@ -61,6 +61,10 @@ function parseProperties (properties, vueComponentOptions) {
vueComponentOptions
.
props
=
props
;
}
function
parseComponents
(
vueComponentOptions
)
{
vueComponentOptions
.
components
=
global
[
'
__wxVueOptions
'
].
components
;
}
function
parseOptions
(
options
,
vueComponentOptions
)
{
if
(
!
options
)
{
return
...
...
@@ -72,6 +76,10 @@ function parseMethods (methods, vueComponentOptions) {
if
(
!
methods
)
{
return
}
if
(
methods
.
$emit
)
{
console
.
warn
(
`Method "$emit" conflicts with an existing Vue instance method`
);
delete
methods
.
$emit
;
}
vueComponentOptions
.
methods
=
methods
;
}
...
...
@@ -233,6 +241,8 @@ function parseComponent (mpComponentOptions) {
}
};
parseComponents
(
vueComponentOptions
);
parseData
(
data
,
vueComponentOptions
);
parseOptions
(
options
,
vueComponentOptions
);
parseMethods
(
methods
,
vueComponentOptions
);
...
...
@@ -288,15 +298,14 @@ function getDate () {
return
new
(
Function
.
prototype
.
bind
.
apply
(
Date
,
args
))()
}
global
[
'
__wxRoute
'
]
=
[];
global
[
'
__wxRoute
'
]
=
''
;
global
[
'
__wxComponents
'
]
=
Object
.
create
(
null
);
global
[
'
__wxVueOptions
'
]
=
Object
.
create
(
null
);
function
Component
(
options
)
{
const
componentOptions
=
parseComponent
(
options
);
componentOptions
.
mixins
.
unshift
(
polyfill
);
if
(
!
global
[
'
__wxComponents
'
])
{
global
[
'
__wxComponents
'
]
=
Object
.
create
(
null
);
}
global
[
'
__wxComponents
'
][
global
[
'
__wxRoute
'
].
pop
()]
=
componentOptions
;
global
[
'
__wxComponents
'
][
global
[
'
__wxRoute
'
]]
=
componentOptions
;
}
function
Behavior
(
options
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录