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

feat(cli): isUnaryTag

上级 d0d1bbb4
......@@ -5,8 +5,8 @@ const {
module.exports = {
preTransformNode (el, options) {
if (isComponent(el.tag)) {
// 挂在 isReservedTag 上边,可以保证外部访问到
(options.isReservedTag.autoComponents || (options.isReservedTag.autoComponents = new Set())).add(el.tag)
// 挂在 isUnaryTag 上边,可以保证外部访问到
(options.isUnaryTag.autoComponents || (options.isUnaryTag.autoComponents = new Set())).add(el.tag)
}
}
}
......@@ -23,7 +23,8 @@ const compilerAlipayModule = require('./module-alipay')
const generateCodeFrame = require('./codeframe')
const {
isComponent
isComponent,
isUnaryTag
} = require('./util')
function compileTemplate (source, options) {
......@@ -31,7 +32,7 @@ function compileTemplate (source, options) {
console.log(options)
const {
autoComponents
} = options.isReservedTag
} = options.isUnaryTag
if (autoComponents) {
console.log('检测到的自定义组件:' + [...autoComponents])
}
......@@ -44,10 +45,12 @@ function compileTemplate (source, options) {
module.exports = {
compile (source, options = {}) {
(options.modules || (options.modules = [])).push(require('./auto-components'))
(options.modules || (options.modules = [])).push(require('./auto-components'))
options.isUnaryTag = isUnaryTag
options.preserveWhitespace = false
if (options.service) {
(options.modules || (options.modules = [])).push(require('./app/service'))
options.optimize = false // 启用 staticRenderFns
options.optimize = false // 启用 staticRenderFns
// domProps => attrs
options.mustUseProp = () => false
options.isReservedTag = (tagName) => !isComponent(tagName) // 非组件均为内置
......@@ -61,7 +64,8 @@ module.exports = {
}
} else if (options.view) {
(options.modules || (options.modules = [])).push(require('./app/view'))
options.optimize = false // 暂不启用 staticRenderFns
options.optimize = false // 暂不启用 staticRenderFns
options.isUnaryTag = isUnaryTag
options.isReservedTag = (tagName) => false // 均为组件
try {
return compileTemplate(source, options)
......
......@@ -6,9 +6,9 @@ const {
METHOD_RENDER_LIST
} = require('./constants')
function cached(fn) {
function cached (fn) {
const cache = Object.create(null)
return function cachedFn(str) {
return function cachedFn (str) {
const hit = cache[str]
return hit || (cache[str] = fn(str))
}
......@@ -21,7 +21,7 @@ const camelize = cached((str) => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
})
function getCode(node) {
function getCode (node) {
return babelGenerate(t.cloneDeep(node), {
compact: true,
jsescOption: {
......@@ -31,11 +31,11 @@ function getCode(node) {
}).code
}
function traverseKey(ast, state) {
function traverseKey (ast, state) {
let forKey = false
babelTraverse(ast, {
noScope: true,
ObjectProperty(path) {
ObjectProperty (path) {
if (forKey) {
return
}
......@@ -44,7 +44,7 @@ function traverseKey(ast, state) {
path.stop()
}
},
CallExpression(path) {
CallExpression (path) {
if (path.node.callee.name === METHOD_RENDER_LIST) {
path.stop()
}
......@@ -53,7 +53,7 @@ function traverseKey(ast, state) {
return forKey
}
function traverseFilter(ast, state) {
function traverseFilter (ast, state) {
const filterModules = state.options.filterModules
if (!filterModules.length) {
return false
......@@ -61,7 +61,7 @@ function traverseFilter(ast, state) {
let isFilter = false
babelTraverse(ast, {
noScope: true,
Identifier(path) {
Identifier (path) {
if (filterModules.includes(path.node.name)) {
const parentNode = path.parent
if ( // t.msg || t['msg']
......@@ -81,11 +81,11 @@ function traverseFilter(ast, state) {
return isFilter
}
function wrapper(code, reverse = false) {
function wrapper (code, reverse = false) {
return reverse ? `{{!(${code})}}` : `{{${code}}}`
}
function genCode(node, noWrapper = false, reverse = false, quotes = true) {
function genCode (node, noWrapper = false, reverse = false, quotes = true) {
if (t.isStringLiteral(node)) {
return reverse ? `!(${node.value})` : node.value
} else if (t.isIdentifier(node)) {
......@@ -98,11 +98,11 @@ function genCode(node, noWrapper = false, reverse = false, quotes = true) {
return noWrapper ? code : wrapper(code, reverse)
}
function getForIndexIdentifier(id) {
function getForIndexIdentifier (id) {
return `__i${id}__`
}
function getForKey(forKey, forIndex, state) {
function getForKey (forKey, forIndex, state) {
if (forKey) {
if (t.isIdentifier(forKey)) {
if (forIndex !== forKey.name) { // 非 forIndex
......@@ -121,7 +121,7 @@ function getForKey(forKey, forIndex, state) {
return ''
}
function processMemberProperty(node, state) {
function processMemberProperty (node, state) {
if (node.computed) {
const property = node.property
if (t.isNumericLiteral(property)) {
......@@ -139,7 +139,7 @@ function processMemberProperty(node, state) {
}
}
function processMemberExpression(element, state) {
function processMemberExpression (element, state) {
// item['order']=>item.order
if (t.isMemberExpression(element)) {
element = t.cloneDeep(element)
......@@ -152,19 +152,19 @@ function processMemberExpression(element, state) {
babelTraverse(element, {
noScope: true,
MemberExpression(path) {
MemberExpression (path) {
processMemberProperty(path.node, state)
}
})
babelTraverse(element, {
noScope: true,
MemberExpression(path) {
MemberExpression (path) {
if (t.isStringLiteral(path.node.property)) {
path.node.computed = false
}
},
StringLiteral(path) {
StringLiteral (path) {
path.replaceWith(t.identifier(path.node.value))
}
})
......@@ -172,7 +172,7 @@ function processMemberExpression(element, state) {
return element
}
function hasOwn(obj, key) {
function hasOwn (obj, key) {
return hasOwnProperty.call(obj, key)
}
......@@ -182,7 +182,7 @@ const {
getTagName
} = require('./h5')
function isComponent(tagName) {
function isComponent (tagName) {
if (
tagName === 'block' ||
tagName === 'component' ||
......@@ -194,15 +194,15 @@ function isComponent(tagName) {
return !hasOwn(tags, getTagName(tagName.replace('v-uni-', '')))
}
function makeMap(str, expectsLowerCase) {
function makeMap (str, expectsLowerCase) {
const map = Object.create(null)
const list = str.split(',')
for (let i = 0; i < list.length; i++) {
map[list[i]] = true
}
return expectsLowerCase ?
val => map[val.toLowerCase()] :
val => map[val]
return expectsLowerCase
? val => map[val.toLowerCase()]
: val => map[val]
}
module.exports = {
isUnaryTag: makeMap(
......
......@@ -2,10 +2,6 @@ const {
tags
} = require('@dcloudio/uni-cli-shared')
const {
isUnaryTag
} = require('@dcloudio/uni-template-compiler/lib/util')
const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/
function processEvent (expr, filterModules) {
......@@ -66,8 +62,6 @@ function fixBooleanAttribute (el) {
}
module.exports = {
isUnaryTag,
preserveWhitespace: false,
modules: [require('../format-text'), {
preTransformNode (el, options) {
fixBooleanAttribute(el)
......
......@@ -144,6 +144,11 @@ module.exports = {
loader: resolve('packages/webpack-uni-filter-loader')
}]
}]
},
resolveLoader: {
alias: {
'vue-style-loader': resolve('packages/h5-vue-style-loader')
}
},
plugins
}
......
......@@ -3,15 +3,9 @@ const {
convertStaticStyle
} = require('@dcloudio/uni-cli-shared')
const {
isUnaryTag
} = require('@dcloudio/uni-template-compiler/lib/util')
module.exports = {
isUnaryTag,
preserveWhitespace: false,
modules: [require('./format-text'), {
preTransformNode(el, {
preTransformNode (el, {
warn
}) {
if (el.attrsMap) {
......@@ -40,7 +34,7 @@ module.exports = {
})
}
},
postTransformNode(el) {
postTransformNode (el) {
if (process.env.UNI_PLATFORM === 'mp-alipay') {
if (el.tag === 'slot') {
if (!el.children.length) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册