提交 10c4a910 编写于 作者: fxy060608's avatar fxy060608

feat(v3): uni-migration

上级 22e7f76d
......@@ -17,6 +17,7 @@ module.exports = function migrate(input, out, options = {
}
migrater.transform(input, out, options).forEach(file => {
console.log(`写入: ${file.path}`)
fs.outputFileSync(file.path, file.content)
console.log(`${file.content}`)
// fs.outputFileSync(file.path, file.content)
})
}
const fs = require('fs')
const path = require('path')
const parse = require('./parser')
const transform = require('./transform')
module.exports = function transformWxml(filepath) {
return [fs.readFileSync(filepath, 'utf8')]
return transform(parse(fs.readFileSync(filepath, 'utf8')))
}
const {
Parser,
DomHandler
} = require('stricter-htmlparser2')
module.exports = function parse(sourceCode) {
const handler = new DomHandler()
new Parser(handler, {
xmlMode: false,
lowerCaseAttributeNames: false,
recognizeSelfClosing: true,
lowerCaseTags: false
}).end(sourceCode)
return {
type: 'tag',
name: 'root',
attribs: {},
children: Array.isArray(handler.dom) ? handler.dom : [handler.dom]
}
}
function genAttrs(node) {
const attribs = node.attribs
const attribsArr = Object.keys(attribs).map(name => `${name}="${attribs[name]}"`)
if (!attribsArr.length) {
return ''
}
return ' ' + attribsArr.join(' ')
}
function genChildren(node) {
if (!node.children) {
return ''
}
return node.children.map(childNode => genElement(childNode)).join('')
}
function genElement(node) {
if (node.type === 'text') {
return node.data
} else if (node.type === 'tag') {
const name = node.name
return `<${name}${genAttrs(node)}>${genChildren(node)}</${name}>`
}
return ''
}
function genWxs(wxs) {
return wxs.map(wxsNode => genElement(wxsNode)).join('')
}
module.exports = function generate(node, state) {
if (node.children.length > 1) {
return [genChildren({
type: 'tag',
name: 'view',
attribs: {},
children: node.children
}), genWxs(state.wxs)]
}
return [genChildren(node), genWxs(state.wxs)]
}
const traverse = require('./traverse')
const generate = require('./generate')
module.exports = function transform(ast) {
const state = {
wxs: []
}
return generate(traverse(ast, state), state)
}
const {
parse
} = require('mustache')
const ATTRS = {
'wx:if': 'v-if',
'wx:elif': 'v-else-if',
'wx:else': 'v-else'
}
function parseMustache(expr) {
const tokens = parse(expr)
const isIdentifier = tokens.length === 1
return tokens.map(token => {
if (token[0] === 'text') {
return `'${token[1]}'`
} else if (token[0] === 'name') {
if (isIdentifier) {
return token[1]
}
return `(${token[1]})`
}
}).join('+')
}
function transformDirective(name, value, attribs) {
if (ATTRS[name]) {
attribs[ATTRS[name]] = parseMustache(value)
return true
}
}
const bindRE = /bind:?/
const catchRE = /catch:?/
const captureBindRE = /capture-bind:?/
const captureCatchRE = /capture-catch:?/
function transformEvent(name, value, attribs) {
let event = name
if (name.indexOf('bind') === 0) {
event = name.replace(bindRE, '@')
} else if (name.indexOf('catch') === 0) {
event = name.replace(catchRE, '@') + '.stop'
} else if (name.indexOf('capture-bind') === 0) {
event = name.replace(captureBindRE, '@') + '.capture'
} else if (name.indexOf('capture-catch') === 0) {
event = name.replace(captureCatchRE, '@') + '.stop.capture'
}
if (event !== name) {
attribs[event] = value
return true
}
}
function transformAttr(name, value, attribs) {
delete attribs[name]
if (transformDirective(name, value, attribs)) {
return
}
if (transformEvent(name, value, attribs)) {
return
}
if (value.indexOf('{{') === -1) {
attribs[name] = value
return
}
attribs[':' + name] = parseMustache(value)
}
function transformAttrs(node, state) {
const attribs = node.attribs
if (!attribs) {
return
}
Object.keys(attribs).forEach(name => {
transformAttr(name, attribs[name], attribs)
})
}
function transformChildren(node, state) {
node.children = node.children.filter(childNode => transformNode(childNode, state))
}
function transformNode(node, state) {
if (node.name === 'wxs') {
state.wxs.push(node)
return false
}
if (node.type === 'tag') {
transformAttrs(node, state)
transformChildren(node, state)
}
return true
}
module.exports = function traverse(node, state) {
transformNode(node, state)
return node
}
......@@ -16,5 +16,9 @@
},
"author": "fxy060608",
"license": "Apache-2.0",
"gitHead": "6b0d55e296028761e3de4b561c1ad7c5fb7a23e2"
"gitHead": "6b0d55e296028761e3de4b561c1ad7c5fb7a23e2",
"dependencies": {
"mustache": "^3.1.0",
"stricter-htmlparser2": "^3.9.6"
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册