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

feat(cli): isUnaryTag

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