提交 c134b5f2 编写于 作者: Q qiang

feat(mp): config add mergeVirtualHostAttributes

上级 ea808591
...@@ -6,12 +6,9 @@ const { ...@@ -6,12 +6,9 @@ const {
} = require('../../../constants') } = require('../../../constants')
const { const {
getCode getCode,
} = require('../../../util')
const {
isRootElement isRootElement
} = require('./util') } = require('../../../util')
function processClassArrayExpressionElements (classArrayExpression) { function processClassArrayExpressionElements (classArrayExpression) {
let binaryExpression let binaryExpression
...@@ -84,8 +81,7 @@ function processClassArrayExpression (classValuePath) { ...@@ -84,8 +81,7 @@ function processClassArrayExpression (classValuePath) {
module.exports = function processClass (paths, path, state) { module.exports = function processClass (paths, path, state) {
const classPath = paths.class const classPath = paths.class
const staticClassPath = paths.staticClass const staticClassPath = paths.staticClass
const platformName = state.options.platform.name const mergeVirtualHostAttributes = state.options.mergeVirtualHostAttributes
const virtualHost = platformName === 'mp-weixin' || platformName === 'mp-alipay'
let classArrayExpression let classArrayExpression
if (classPath) { if (classPath) {
const classValuePath = classPath.get('value') const classValuePath = classPath.get('value')
...@@ -128,7 +124,7 @@ module.exports = function processClass (paths, path, state) { ...@@ -128,7 +124,7 @@ module.exports = function processClass (paths, path, state) {
state.errors.add(':class' + uniI18n.__('templateCompiler.noSupportSyntax', { 0: getCode(classValuePath.node) })) state.errors.add(':class' + uniI18n.__('templateCompiler.noSupportSyntax', { 0: getCode(classValuePath.node) }))
} }
} }
if (virtualHost && isRootElement(path.parentPath)) { if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
const virtualHostClass = t.identifier(VIRTUAL_HOST_CLASS) const virtualHostClass = t.identifier(VIRTUAL_HOST_CLASS)
if (classArrayExpression) { if (classArrayExpression) {
classArrayExpression.elements.push(virtualHostClass) classArrayExpression.elements.push(virtualHostClass)
......
...@@ -8,12 +8,9 @@ const { ...@@ -8,12 +8,9 @@ const {
const { const {
getCode, getCode,
hyphenate hyphenate,
} = require('../../../util')
const {
isRootElement isRootElement
} = require('./util') } = require('../../../util')
const getMemberExpr = require('../member-expr') const getMemberExpr = require('../member-expr')
...@@ -125,8 +122,7 @@ function generateGetStyle (stylePath, styleValuePath, staticStylePath, state) { ...@@ -125,8 +122,7 @@ function generateGetStyle (stylePath, styleValuePath, staticStylePath, state) {
module.exports = function processStyle (paths, path, state) { module.exports = function processStyle (paths, path, state) {
const stylePath = paths.style const stylePath = paths.style
const staticStylePath = paths.staticStyle const staticStylePath = paths.staticStyle
const platformName = state.options.platform.name const mergeVirtualHostAttributes = state.options.mergeVirtualHostAttributes
const virtualHost = platformName === 'mp-weixin' || platformName === 'mp-alipay'
if (stylePath) { if (stylePath) {
const styleValuePath = stylePath.get('value') const styleValuePath = stylePath.get('value')
if (styleValuePath.isObjectExpression()) { if (styleValuePath.isObjectExpression()) {
...@@ -177,18 +173,22 @@ module.exports = function processStyle (paths, path, state) { ...@@ -177,18 +173,22 @@ module.exports = function processStyle (paths, path, state) {
} else { } else {
state.errors.add(`:style 不支持 ${getCode(styleValuePath.node)} 语法`) state.errors.add(`:style 不支持 ${getCode(styleValuePath.node)} 语法`)
} }
if (virtualHost && isRootElement(path.parentPath)) { if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
styleValuePath.replaceWith(t.binaryExpression('+', styleValuePath.node, t.identifier(VIRTUAL_HOST_STYLE))) styleValuePath.replaceWith(t.binaryExpression('+', styleValuePath.node, t.identifier(VIRTUAL_HOST_STYLE)))
} }
} else if (staticStylePath) { } else if (staticStylePath) {
if (virtualHost && isRootElement(path.parentPath)) { if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
const styleNode = processStaticStyle([t.identifier(VIRTUAL_HOST_STYLE)], staticStylePath, state) const styleNode = processStaticStyle([t.identifier(VIRTUAL_HOST_STYLE)], staticStylePath, state)
const property = t.objectProperty(t.identifier('style'), styleNode) const property = t.objectProperty(t.identifier('style'), styleNode)
path.node.properties.push(property) path.node.properties.push(property)
return [] return []
} }
staticStylePath.get('value').replaceWith(getStaticStyleStringLiteral(staticStylePath, state)) staticStylePath.get('value').replaceWith(getStaticStyleStringLiteral(staticStylePath, state))
} else {
if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
const property = t.objectProperty(t.identifier('style'), t.identifier(VIRTUAL_HOST_STYLE))
path.node.properties.push(property)
}
} }
return [] return []
} }
...@@ -2,8 +2,7 @@ const t = require('@babel/types') ...@@ -2,8 +2,7 @@ const t = require('@babel/types')
const babelTraverse = require('@babel/traverse').default const babelTraverse = require('@babel/traverse').default
const { const {
INTERNAL_SET_MODEL, INTERNAL_SET_MODEL
METHOD_CREATE_ELEMENT
} = require('../../../constants') } = require('../../../constants')
module.exports = { module.exports = {
...@@ -44,9 +43,5 @@ module.exports = { ...@@ -44,9 +43,5 @@ module.exports = {
] ]
) )
) )
},
isRootElement (path) {
const result = path.findParent(path => (path.isCallExpression() && path.get('callee').isIdentifier({ name: METHOD_CREATE_ELEMENT })) || path.isReturnStatement())
return result.isReturnStatement()
} }
} }
...@@ -24,7 +24,8 @@ const { ...@@ -24,7 +24,8 @@ const {
hyphenate, hyphenate,
traverseFilter, traverseFilter,
getComponentName, getComponentName,
hasEscapeQuote hasEscapeQuote,
isRootElement
} = require('../../util') } = require('../../util')
const traverseData = require('./data') const traverseData = require('./data')
...@@ -182,7 +183,10 @@ module.exports = { ...@@ -182,7 +183,10 @@ module.exports = {
if (this.options.scopeId) { if (this.options.scopeId) {
addStaticClass(path, this.options.scopeId) addStaticClass(path, this.options.scopeId)
} }
// 根节点无 attrs 时添加空对象,方便后续合并外层 attrs
if (this.options.mergeVirtualHostAttributes && !t.isObjectExpression(path.node.arguments[1]) && isRootElement(path)) {
path.node.arguments.splice(1, 0, t.objectExpression([]))
}
const dataPath = path.get('arguments.1') const dataPath = path.get('arguments.1')
dataPath && dataPath.isObjectExpression() && traverseData(dataPath, this, tagNode.value) dataPath && dataPath.isObjectExpression() && traverseData(dataPath, this, tagNode.value)
} }
......
...@@ -13,7 +13,7 @@ const uniI18n = require('@dcloudio/uni-cli-i18n') ...@@ -13,7 +13,7 @@ const uniI18n = require('@dcloudio/uni-cli-i18n')
function processElement (ast, state, isRoot) { function processElement (ast, state, isRoot) {
const platform = state.options.platform const platform = state.options.platform
const platformName = platform.name const platformName = platform.name
const virtualHost = platformName === 'mp-weixin' || platformName === 'mp-alipay' const mergeVirtualHostAttributes = state.options.mergeVirtualHostAttributes
// <template slot="f"></template> // <template slot="f"></template>
if (ast.type === 'template' && hasOwn(ast.attr, 'slot')) { if (ast.type === 'template' && hasOwn(ast.attr, 'slot')) {
ast.type = 'view' ast.type = 'view'
...@@ -47,13 +47,16 @@ function processElement (ast, state, isRoot) { ...@@ -47,13 +47,16 @@ function processElement (ast, state, isRoot) {
ast.attr['bind:' + INTERNAL_EVENT_LINK] = INTERNAL_EVENT_LINK ast.attr['bind:' + INTERNAL_EVENT_LINK] = INTERNAL_EVENT_LINK
} }
if (virtualHost && platform.isComponent(ast.type)) { if (mergeVirtualHostAttributes && platform.isComponent(ast.type)) {
const obj = { const obj = {
style: VIRTUAL_HOST_STYLE, style: VIRTUAL_HOST_STYLE,
class: VIRTUAL_HOST_CLASS class: VIRTUAL_HOST_CLASS
} }
Object.keys(obj).forEach(key => { Object.keys(obj).forEach(key => {
ast.attr[obj[key]] = ast.attr[key] if (key in ast.attr) {
ast.attr[obj[key]] = ast.attr[key]
}
// 支付宝小程序自定义组件外部属性始终无效
if (platformName === 'mp-alipay') { if (platformName === 'mp-alipay') {
delete ast.attr[key] delete ast.attr[key]
} }
......
...@@ -5,7 +5,8 @@ const uniI18n = require('@dcloudio/uni-cli-i18n') ...@@ -5,7 +5,8 @@ const uniI18n = require('@dcloudio/uni-cli-i18n')
const { const {
METHOD_RENDER_LIST, METHOD_RENDER_LIST,
METHOD_RESOLVE_SCOPED_SLOTS METHOD_RESOLVE_SCOPED_SLOTS,
METHOD_CREATE_ELEMENT
} = require('./constants') } = require('./constants')
function cached (fn) { function cached (fn) {
...@@ -270,6 +271,11 @@ function hasEscapeQuote (path) { ...@@ -270,6 +271,11 @@ function hasEscapeQuote (path) {
return has return has
} }
function isRootElement (path) {
const result = path.findParent(path => (path.isCallExpression() && path.get('callee').isIdentifier({ name: METHOD_CREATE_ELEMENT })) || path.isReturnStatement())
return result.isReturnStatement()
}
module.exports = { module.exports = {
hasOwn, hasOwn,
isUnaryTag: makeMap( isUnaryTag: makeMap(
...@@ -301,5 +307,6 @@ module.exports = { ...@@ -301,5 +307,6 @@ module.exports = {
processMemberExpression, processMemberExpression,
getForIndexIdentifier, getForIndexIdentifier,
isSimpleObjectExpression, isSimpleObjectExpression,
hasEscapeQuote hasEscapeQuote,
isRootElement
} }
...@@ -319,6 +319,8 @@ if ((process.env.UNI_PLATFORM === 'mp-kuaishou' || process.env.UNI_PLATFORM === ...@@ -319,6 +319,8 @@ if ((process.env.UNI_PLATFORM === 'mp-kuaishou' || process.env.UNI_PLATFORM ===
process.env.SCOPED_SLOTS_COMPILER = modes[2] process.env.SCOPED_SLOTS_COMPILER = modes[2]
} }
process.env.MERGE_VIRTUAL_HOST_ATTRIBUTES = (!!platformOptions.mergeVirtualHostAttributes).toString()
process.env.UNI_STAT_UNI_CLOUD = '' process.env.UNI_STAT_UNI_CLOUD = ''
process.env.UNI_STAT_DEBUG = '' process.env.UNI_STAT_DEBUG = ''
if ( if (
......
...@@ -66,7 +66,8 @@ module.exports = function (content, map) { ...@@ -66,7 +66,8 @@ module.exports = function (content, map) {
Object.assign(vueLoaderOptions.options.compilerOptions, { Object.assign(vueLoaderOptions.options.compilerOptions, {
mp: { mp: {
platform: process.env.UNI_PLATFORM, platform: process.env.UNI_PLATFORM,
scopedSlotsCompiler: process.env.SCOPED_SLOTS_COMPILER scopedSlotsCompiler: process.env.SCOPED_SLOTS_COMPILER,
mergeVirtualHostAttributes: process.env.MERGE_VIRTUAL_HOST_ATTRIBUTES === 'true'
}, },
filterModules, filterModules,
filterTagName, filterTagName,
......
...@@ -136,7 +136,8 @@ const NON_APP_JSON_KEYS = [ ...@@ -136,7 +136,8 @@ const NON_APP_JSON_KEYS = [
'optimization', 'optimization',
'scopedSlotsCompiler', 'scopedSlotsCompiler',
'usingComponents', 'usingComponents',
'uniStatistics' 'uniStatistics',
'mergeVirtualHostAttributes'
] ]
module.exports = { module.exports = {
hasOwn, hasOwn,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册