diff --git a/packages/uni-mp-compiler/__tests__/style.spec.ts b/packages/uni-mp-compiler/__tests__/style.spec.ts index cfe8986678311d8cc5c2f6f66251e6fb468379fa..b5e7d0592deadaf896f9cbd47b13a31b34d08c84 100644 --- a/packages/uni-mp-compiler/__tests__/style.spec.ts +++ b/packages/uni-mp-compiler/__tests__/style.spec.ts @@ -67,80 +67,80 @@ describe('compiler: transform style', () => { }` ) }) - // test('v-bind:style object syntax + style', () => { - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _ctx.isRed } - // }` - // ) - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _ctx.isRed } - // }` - // ) - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _ctx.ok, b: _ctx.handle(_ctx.ok), c: _ctx.ok > 1, d: _ctx.j, e: _ctx.k, f: _ctx.l, g: _ctx.m, h: _normalizeClass({ ..._ctx.n }), i: _normalizeClass({ ...{ a: true } }), j: _normalizeClass({ ...{ b: _ctx.o } }) } - // }` - // ) - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _ctx.ok, b: _ctx.handle(_ctx.ok), c: _ctx.ok > 1, d: _ctx.j, e: _ctx.k, f: _ctx.l, g: _ctx.m, h: _normalizeClass({ ..._ctx.n }), i: _normalizeClass({ ...{ a: true } }), j: _normalizeClass({ ...{ b: _ctx.o } }) } - // }` - // ) - // }) - // test('v-bind:style array syntax', () => { - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _normalizeClass(_ctx.classA), b: _normalizeClass(_ctx.classB) } - // }` - // ) - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _normalizeClass(_ctx.classA), b: _normalizeClass(_ctx.classB), c: _normalizeClass({ classC: _ctx.isC, classD: _ctx.isD }), d: _normalizeClass(_ctx.isF ? 'classF' : ''), e: _normalizeClass(_ctx.isG && 'classG'), f: _normalizeClass([..._ctx.classH]), g: _normalizeClass([...[_ctx.classI, _ctx.classJ]]), h: _normalizeClass(_ctx.handle(_ctx.classK)) } - // }` - // ) - // }) - // test('v-bind:style array syntax + style', () => { - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _normalizeClass(_ctx.classA), b: _normalizeClass(_ctx.classB) } - // }` - // ) - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _normalizeClass(_ctx.classA), b: _normalizeClass(_ctx.classB) } - // }` - // ) - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _normalizeClass(_ctx.classA), b: _normalizeClass(_ctx.classB), c: _normalizeClass({ classC: _ctx.isC, classD: _ctx.isD }), d: _normalizeClass(_ctx.isF ? 'classF' : ''), e: _normalizeClass(_ctx.isG && 'classG'), f: _normalizeClass([..._ctx.classH]), g: _normalizeClass([...[_ctx.classI, _ctx.classJ]]), h: _normalizeClass(_ctx.handle(_ctx.classK)) } - // }` - // ) - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return { a: _normalizeClass(_ctx.classA), b: _normalizeClass(_ctx.classB), c: _normalizeClass({ classC: _ctx.isC, classD: _ctx.isD }), d: _normalizeClass(_ctx.isF ? 'classF' : ''), e: _normalizeClass(_ctx.isG && 'classG'), f: _normalizeClass([..._ctx.classH]), g: _normalizeClass([...[_ctx.classI, _ctx.classJ]]), h: _normalizeClass(_ctx.handle(_ctx.classK)) } - // }` - // ) - // }) + test('v-bind:style object syntax + style', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _ctx.handle(_ctx.bg), b: _ctx.fontWeight, c: _hyphenate(_ctx.padding), d: _ctx.box.padding, e: _normalizeStyle(_ctx.style), f: _normalizeStyle({ margin: '0px' }) } +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _ctx.handle(_ctx.bg), b: _ctx.fontWeight, c: _hyphenate(_ctx.padding), d: _ctx.box.padding, e: _normalizeStyle(_ctx.style), f: _normalizeStyle({ margin: '0px' }) } +}` + ) + }) + test('v-bind:style array syntax', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeStyle(_ctx.styleA), b: _normalizeStyle(_ctx.styleB) } +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeStyle(_ctx.styleA), b: _normalizeStyle(_ctx.styleB), c: _normalizeStyle({ color: 'red', fontSize: _ctx.fontSize }), d: _normalizeStyle(_ctx.styleC), e: _normalizeStyle([_ctx.styleD, _ctx.styleE]), f: _normalizeStyle(_ctx.handle(_ctx.styleF)) } +}` + ) + }) + test('v-bind:style array syntax + style', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeStyle(_ctx.styleA), b: _normalizeStyle(_ctx.styleB) } +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeStyle(_ctx.styleA), b: _normalizeStyle(_ctx.styleB) } +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeStyle(_ctx.styleA), b: _normalizeStyle(_ctx.styleB), c: _normalizeStyle({ color: 'red', fontSize: _ctx.fontSize }), d: _normalizeStyle(_ctx.styleC), e: _normalizeStyle([_ctx.styleD, _ctx.styleE]), f: _normalizeStyle(_ctx.handle(_ctx.styleF)) } +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _normalizeStyle(_ctx.styleA), b: _normalizeStyle(_ctx.styleB), c: _normalizeStyle({ color: 'red', fontSize: _ctx.fontSize }), d: _normalizeStyle(_ctx.styleC), e: _normalizeStyle([_ctx.styleD, _ctx.styleE]), f: _normalizeStyle(_ctx.handle(_ctx.styleF)) } +}` + ) + }) }) diff --git a/packages/uni-mp-compiler/src/transforms/transformStyle.ts b/packages/uni-mp-compiler/src/transforms/transformStyle.ts index 6a12512530c13e8da468a955992c762eda9f3eda..acc9c1ac53e9ecbd35c248311b050fabdca5b71b 100644 --- a/packages/uni-mp-compiler/src/transforms/transformStyle.ts +++ b/packages/uni-mp-compiler/src/transforms/transformStyle.ts @@ -12,6 +12,7 @@ import { isLiteral, isObjectProperty, binaryExpression, + isIdentifier, } from '@babel/types' import { DirectiveNode, @@ -118,13 +119,12 @@ function rewriteStyleArrayExpression( ) { expr.elements.forEach((prop, index) => { if (!isStringLiteral(prop)) { - const code = genBabelExpr(arrayExpression([prop])) + const code = genBabelExpr( + arrayExpression([isSpreadElement(prop) ? prop.argument : prop]) + ) expr.elements[index] = identifier( rewriteStyleExpression( - createSimpleExpression( - isSpreadElement(prop) ? code : code.slice(1, -1), - false - ), + createSimpleExpression(code.slice(1, -1), false), context ).content ) @@ -187,6 +187,18 @@ function createBinaryExpression(left: Expression, right: Expression) { function createStyleBindingByArrayExpression(expr: ArrayExpression) { let result: Expression | undefined + function concat(expr: Expression) { + if (!result) { + result = expr + } else { + result = createBinaryExpression(addSemicolon(result), expr) + } + } + expr.elements.forEach((prop) => { + if (isStringLiteral(prop) || isIdentifier(prop)) { + concat(prop) + } + }) return result } diff --git a/packages/uni-mp-vue/dist/vue.runtime.esm.js b/packages/uni-mp-vue/dist/vue.runtime.esm.js index 0bc3c7502fbf6517e75d7faf4b28a2403f020466..124d0814f8ea144cf1c62a3a441c6a0928d52b46 100644 --- a/packages/uni-mp-vue/dist/vue.runtime.esm.js +++ b/packages/uni-mp-vue/dist/vue.runtime.esm.js @@ -1,5 +1,5 @@ import { extend, isSymbol, isObject, toRawType, def, hasChanged, isArray, isFunction, NOOP, remove, toHandlerKey, camelize, capitalize, isString, normalizeClass, normalizeStyle, isOn, isPromise, EMPTY_OBJ, isSet, isMap, isPlainObject, invokeArrayFns, hasOwn, NO, isIntegerKey, toNumber, hyphenate, isReservedProp, EMPTY_ARR, makeMap, toTypeString } from '@vue/shared'; -export { camelize, normalizeClass, normalizeProps, normalizeStyle, toDisplayString, toHandlerKey } from '@vue/shared'; +export { camelize, hyphenate, normalizeClass, normalizeProps, normalizeStyle, toDisplayString, toHandlerKey } from '@vue/shared'; // lifecycle // App and Page