diff --git a/packages/uni-cli-shared/src/mp/constants.ts b/packages/uni-cli-shared/src/mp/constants.ts index 4f8fe8574d671c3705489a1db2656dacc399db94..a5ea2c3f210d822ae8a776c0f7b768277b828505 100644 --- a/packages/uni-cli-shared/src/mp/constants.ts +++ b/packages/uni-cli-shared/src/mp/constants.ts @@ -1,2 +1,4 @@ export const COMPONENT_ON_LINK = 'onVI' export const COMPONENT_BIND_LINK = '__l' +export const COMPONENT_CUSTOM_HIDDEN = 'data-c-h' +export const COMPONENT_CUSTOM_HIDDEN_BIND = 'bind:-' + COMPONENT_CUSTOM_HIDDEN diff --git a/packages/uni-cli-shared/src/mp/template.ts b/packages/uni-cli-shared/src/mp/template.ts index 23a29a0e5bddd5900983eafc0cb4f2a8c06f3da3..a43087f28446ea573a34a069d065fda9a0d51e64 100644 --- a/packages/uni-cli-shared/src/mp/template.ts +++ b/packages/uni-cli-shared/src/mp/template.ts @@ -39,6 +39,12 @@ export interface MiniProgramCompilerOptions { filter?: { lang: string } + component?: { + /** + * 自定义组件自定义 hidden 属性用于实现 v-show + */ + vShow: string + } directive: string emitFile?: (emittedFile: EmittedAsset) => string } diff --git a/packages/uni-cli-shared/src/vue/utils.ts b/packages/uni-cli-shared/src/vue/utils.ts index 808f766afe34981188eb4768fb181d63385252e4..78f993436768ab283fa1092d46389aeb11bd5087 100644 --- a/packages/uni-cli-shared/src/vue/utils.ts +++ b/packages/uni-cli-shared/src/vue/utils.ts @@ -23,7 +23,9 @@ export const VUE_REF_IN_FOR = 'r-i-f' export function isUserComponent( node: RootNode | TemplateChildNode, - context: TransformContext + context: { + isBuiltInComponent: TransformContext['isBuiltInComponent'] + } ): node is ComponentNode { return ( node.type === NodeTypes.ELEMENT && diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 15258134d9d7b8164a85fbb56ded9bd450df9c62..92ce766d1135a99d575fe418cfab6aebdfe9dca6 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -16784,10 +16784,9 @@ const chooseFile = /* @__PURE__ */ defineAsyncApi(API_CHOOSE_FILE, ({ }; resolve(res); }); - if (getInteractStatus()) { - fileInput.click(); - } else { - reject(t2("uni.chooseFile.notUserActivation")); + fileInput.click(); + if (!getInteractStatus()) { + console.warn(t2("uni.chooseFile.notUserActivation")); } }, ChooseFileProtocol, ChooseFileOptions); let imageInput = null; @@ -16835,10 +16834,9 @@ const chooseImage = /* @__PURE__ */ defineAsyncApi(API_CHOOSE_IMAGE, ({ }; resolve(res); }); - if (getInteractStatus()) { - imageInput.click(); - } else { - reject(t2("uni.chooseFile.notUserActivation")); + imageInput.click(); + if (!getInteractStatus()) { + console.warn(t2("uni.chooseFile.notUserActivation")); } }, ChooseImageProtocol, ChooseImageOptions); let index$c = 0; @@ -17135,10 +17133,9 @@ const chooseVideo = /* @__PURE__ */ defineAsyncApi(API_CHOOSE_VIDEO, ({ sourceTy resolve(callbackResult); } }); - if (getInteractStatus()) { - videoInput.click(); - } else { - reject(t2("uni.chooseFile.notUserActivation")); + videoInput.click(); + if (!getInteractStatus()) { + console.warn(t2("uni.chooseFile.notUserActivation")); } }, ChooseVideoProtocol, ChooseVideoOptions); const request = /* @__PURE__ */ defineTaskApi(API_REQUEST, ({ diff --git a/packages/uni-mp-compiler/src/compile.ts b/packages/uni-mp-compiler/src/compile.ts index e6678dbe54dbc2eff8b7cf031b080d8647fcabd2..c4491832b2632175b9ecab22109a1aa81a969f0b 100644 --- a/packages/uni-mp-compiler/src/compile.ts +++ b/packages/uni-mp-compiler/src/compile.ts @@ -100,6 +100,7 @@ export function baseCompile(template: string, options: CompilerOptions = {}) { event, slot, lazyElement, + component, } = options.miniProgram genTemplate(ast, { class: clazz, @@ -110,6 +111,8 @@ export function baseCompile(template: string, options: CompilerOptions = {}) { event, slot, lazyElement, + component, + isBuiltInComponent: context.isBuiltInComponent, }) } diff --git a/packages/uni-mp-compiler/src/options.ts b/packages/uni-mp-compiler/src/options.ts index d40479695ddb4338d10742039021d486419f34e7..e56bbf69ae355196b91a58e0f647cb75ff037d6b 100644 --- a/packages/uni-mp-compiler/src/options.ts +++ b/packages/uni-mp-compiler/src/options.ts @@ -100,6 +100,7 @@ export interface TemplateCodegenOptions extends Omit { scopeId?: string | null filename: string + isBuiltInComponent: Required['isBuiltInComponent'] } export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions diff --git a/packages/uni-mp-compiler/src/template/codegen.ts b/packages/uni-mp-compiler/src/template/codegen.ts index 5dd9f888ecc71b8934aa9a266b8a7f5367662134..825ff365307382ff70ce41899bf7bc72e234dbfc 100644 --- a/packages/uni-mp-compiler/src/template/codegen.ts +++ b/packages/uni-mp-compiler/src/template/codegen.ts @@ -3,6 +3,7 @@ import { SLOT_DEFAULT_NAME, dynamicSlotName } from '@dcloudio/uni-shared' import { formatMiniProgramEvent, isElementNode, + isUserComponent, MiniProgramCompilerOptions, } from '@dcloudio/uni-cli-shared' import { @@ -25,6 +26,7 @@ import { genExpr } from '../codegen' import { ForElementNode, isForElementNode } from '../transforms/vFor' import { IfElementNode, isIfElementNode } from '../transforms/vIf' import { findSlotName } from '../transforms/vSlot' +import { TransformContext } from '../transform' interface TemplateCodegenContext { code: string directive: string @@ -32,6 +34,8 @@ interface TemplateCodegenContext { event: MiniProgramCompilerOptions['event'] slot: MiniProgramCompilerOptions['slot'] lazyElement: MiniProgramCompilerOptions['lazyElement'] + component: MiniProgramCompilerOptions['component'] + isBuiltInComponent: TransformContext['isBuiltInComponent'] push(code: string): void } @@ -45,6 +49,8 @@ export function generate( filename, directive, lazyElement, + isBuiltInComponent, + component, }: TemplateCodegenOptions ) { const context: TemplateCodegenContext = { @@ -54,6 +60,8 @@ export function generate( scopeId, directive, lazyElement, + component, + isBuiltInComponent, push(code) { context.code += code }, @@ -266,7 +274,7 @@ function genElement(node: ElementNode, context: TemplateCodegenContext) { genNode(node, context) }) } - if (node.tagType === ElementTypes.COMPONENT) { + if (isUserComponent(node, context)) { tag = hyphenate(tag) } const { push } = context @@ -333,7 +341,7 @@ export function genElementProps( if (name === 'on') { genOn(prop, node, context) } else { - genDirectiveNode(prop, context) + genDirectiveNode(prop, node, context) } } }) @@ -341,7 +349,7 @@ export function genElementProps( function genOn( prop: DirectiveNode, node: ElementNode, - { push, event }: TemplateCodegenContext + { push, event, isBuiltInComponent }: TemplateCodegenContext ) { const arg = (prop.arg as SimpleExpressionNode).content const exp = prop.exp as SimpleExpressionNode @@ -349,7 +357,7 @@ function genOn( const name = (event?.format || formatMiniProgramEvent)(arg, { isCatch: modifiers.includes('stop') || modifiers.includes('prevent'), isCapture: modifiers.includes('capture'), - isComponent: node.tagType === ElementTypes.COMPONENT, + isComponent: isUserComponent(node, { isBuiltInComponent }), }) if (exp.isStatic) { push(` ${name}="${exp.content}"`) @@ -360,8 +368,10 @@ function genOn( function genDirectiveNode( prop: DirectiveNode, - { push }: TemplateCodegenContext + node: ElementNode, + context: TemplateCodegenContext ) { + const { push, component } = context if (prop.name === 'slot') { if (prop.arg) { const arg = prop.arg as SimpleExpressionNode @@ -375,7 +385,13 @@ function genDirectiveNode( ) } } else if (prop.name === 'show') { - push(` hidden="{{!${(prop.exp as SimpleExpressionNode).content}}}"`) + let hiddenPropName = 'hidden' + if (isUserComponent(node, context) && component && component.vShow) { + hiddenPropName = component.vShow + } + push( + ` ${hiddenPropName}="{{!${(prop.exp as SimpleExpressionNode).content}}}"` + ) } else if (prop.arg && prop.exp) { const arg = (prop.arg as SimpleExpressionNode).content const exp = (prop.exp as SimpleExpressionNode).content diff --git a/packages/uni-mp-lark/dist/uni.compiler.js b/packages/uni-mp-lark/dist/uni.compiler.js index 79614577d5114f78a7004a33034e14329043acce..fc17d2173e6cfc37970f784f8a02cdacf0439f22 100644 --- a/packages/uni-mp-lark/dist/uni.compiler.js +++ b/packages/uni-mp-lark/dist/uni.compiler.js @@ -81,6 +81,9 @@ const miniProgram = { dynamicSlotNames: true, }, directive: 'tt:', + component: { + vShow: uniCliShared.COMPONENT_CUSTOM_HIDDEN_BIND, + }, }; const options = { vite: { diff --git a/packages/uni-mp-qq/__tests__/component.spec.ts b/packages/uni-mp-qq/__tests__/component.spec.ts index a8ee3584aa560d954e778fd616ed6ae481953d72..e12567f0e049eada208a60279ad9aa48f5388635 100644 --- a/packages/uni-mp-qq/__tests__/component.spec.ts +++ b/packages/uni-mp-qq/__tests__/component.spec.ts @@ -1,6 +1,15 @@ import { assert } from './testUtils' describe('mp-baidu: transform component', () => { + test(`component with v-show`, () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _ctx.ok } +}` + ) + }) test(`match-media`, () => { assert( ``, diff --git a/packages/uni-mp-qq/dist/uni.compiler.js b/packages/uni-mp-qq/dist/uni.compiler.js index 729835b820992c9b4d779a5a57a3f4ee9a58133f..e11cbd81d981adbb66cdf5f06996029dac319a1b 100644 --- a/packages/uni-mp-qq/dist/uni.compiler.js +++ b/packages/uni-mp-qq/dist/uni.compiler.js @@ -99,6 +99,9 @@ const miniProgram = { dynamicSlotNames: true, }, directive: 'qq:', + component: { + vShow: uniCliShared.COMPONENT_CUSTOM_HIDDEN, + }, }; const options = { vite: { diff --git a/packages/uni-mp-qq/src/compiler/options.ts b/packages/uni-mp-qq/src/compiler/options.ts index 9c5825f6fe87aafa39bf82f3fdabdb6b943808af..26c049d0e23134b9bc65855cf04fe9da50f7124d 100644 --- a/packages/uni-mp-qq/src/compiler/options.ts +++ b/packages/uni-mp-qq/src/compiler/options.ts @@ -1,6 +1,7 @@ import path from 'path' import type { CompilerOptions } from '@vue/compiler-core' import { + COMPONENT_CUSTOM_HIDDEN, MiniProgramCompilerOptions, transformComponentLink, transformMatchMedia, @@ -29,6 +30,9 @@ export const miniProgram: MiniProgramCompilerOptions = { dynamicSlotNames: true, }, directive: 'qq:', + component: { + vShow: COMPONENT_CUSTOM_HIDDEN, + }, } export const options: UniMiniProgramPluginOptions = { diff --git a/packages/uni-mp-toutiao/__tests__/component.spec.ts b/packages/uni-mp-toutiao/__tests__/component.spec.ts index a8ee3584aa560d954e778fd616ed6ae481953d72..8b302534173bd792da631c8daeb838a51e2cb10d 100644 --- a/packages/uni-mp-toutiao/__tests__/component.spec.ts +++ b/packages/uni-mp-toutiao/__tests__/component.spec.ts @@ -1,6 +1,15 @@ import { assert } from './testUtils' describe('mp-baidu: transform component', () => { + test(`component with v-show`, () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _ctx.ok } +}` + ) + }) test(`match-media`, () => { assert( ``, diff --git a/packages/uni-mp-toutiao/dist/uni.compiler.js b/packages/uni-mp-toutiao/dist/uni.compiler.js index 67211ae3c853078bfb536b97fb18de5b2b92fbd6..a86e2b454861ef357d98f0ccd47ac7324c0f4783 100644 --- a/packages/uni-mp-toutiao/dist/uni.compiler.js +++ b/packages/uni-mp-toutiao/dist/uni.compiler.js @@ -81,6 +81,9 @@ const miniProgram = { dynamicSlotNames: true, }, directive: 'tt:', + component: { + vShow: uniCliShared.COMPONENT_CUSTOM_HIDDEN_BIND, + }, }; const options = { vite: { diff --git a/packages/uni-mp-toutiao/src/compiler/options.ts b/packages/uni-mp-toutiao/src/compiler/options.ts index 302463db2b4bc02ed3b3f64b6a83554b79c0a28a..425b1a8842516cbbd965c141f6186ec128c5f78a 100644 --- a/packages/uni-mp-toutiao/src/compiler/options.ts +++ b/packages/uni-mp-toutiao/src/compiler/options.ts @@ -1,6 +1,7 @@ import path from 'path' import type { CompilerOptions } from '@vue/compiler-core' import { + COMPONENT_CUSTOM_HIDDEN_BIND, MiniProgramCompilerOptions, transformComponentLink, transformMatchMedia, @@ -32,6 +33,9 @@ export const miniProgram: MiniProgramCompilerOptions = { dynamicSlotNames: true, }, directive: 'tt:', + component: { + vShow: COMPONENT_CUSTOM_HIDDEN_BIND, + }, } export const options: UniMiniProgramPluginOptions = { diff --git a/packages/uni-mp-vite/src/plugin/configResolved.ts b/packages/uni-mp-vite/src/plugin/configResolved.ts index c611c262ba6724df1ba0bfc3b8eb51638aa49245..f2e7c99be448e25633ba0a81f2c32f6ea3f2789a 100644 --- a/packages/uni-mp-vite/src/plugin/configResolved.ts +++ b/packages/uni-mp-vite/src/plugin/configResolved.ts @@ -24,9 +24,12 @@ import { const debugNVueCss = debug('vite:uni:nvue-css') const cssVars = `page{--status-bar-height:25px;--top-window-height:0px;--window-top:0px;--window-bottom:0px;--window-left:0px;--window-right:0px;--window-magin:0px}` const shadowCss = `page::after{position:fixed;content:'';left:-1000px;top:-1000px;-webkit-animation:shadow-preload .1s;-webkit-animation-delay:3s;animation:shadow-preload .1s;animation-delay:3s}@-webkit-keyframes shadow-preload{0%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}100%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}}@keyframes shadow-preload{0%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}100%{background-image:url(https://cdn.dcloud.net.cn/img/shadow-grey.png)}}` +const genComponentCustomHiddenCss = (name: string) => + `[${name.replace(':', '')}="true"]{display: none !important;}` export function createConfigResolved({ style: { extname }, + template: { component }, }: UniMiniProgramPluginOptions): Plugin['configResolved'] { function normalizeCssChunkFilename(id: string, extname: string) { return ( @@ -54,10 +57,15 @@ export function createConfigResolved({ chunkCssCode(filename, cssCode) { cssCode = transformScopedCss(cssCode) if (filename === 'app' + extname) { + const componentCustomHiddenCss = + (component && + component.vShow && + genComponentCustomHiddenCss(component.vShow)) || + '' if (config.isProduction) { - return cssCode + shadowCss + cssVars + return cssCode + shadowCss + cssVars + componentCustomHiddenCss } else { - return cssCode + cssVars + return cssCode + cssVars + componentCustomHiddenCss } } diff --git a/packages/uni-mp-vite/src/plugin/index.ts b/packages/uni-mp-vite/src/plugin/index.ts index d77ba7ad067bfce04563d3a23f2d5e4824954a99..e561c099bf26749b2e8457fef888eef33fdf1391 100644 --- a/packages/uni-mp-vite/src/plugin/index.ts +++ b/packages/uni-mp-vite/src/plugin/index.ts @@ -57,6 +57,7 @@ export interface UniMiniProgramPluginOptions { class: MiniProgramCompilerOptions['class'] slot: MiniProgramCompilerOptions['slot'] lazyElement?: MiniProgramCompilerOptions['lazyElement'] + component?: MiniProgramCompilerOptions['component'] customElements?: string[] filter?: { lang: string @@ -96,6 +97,7 @@ export function uniMiniProgramPlugin( filter: template.filter ? { lang: template.filter.lang } : undefined, directive: template.directive, lazyElement: template.lazyElement, + component: template.component, emitFile, slot: template.slot, }, diff --git a/packages/uni-mp-weixin/__tests__/component.spec.ts b/packages/uni-mp-weixin/__tests__/component.spec.ts index 6f8b8703ce3e9a2676bc158393438556ae665696..d40f88d1e073f4abef8044b31848949965a3406d 100644 --- a/packages/uni-mp-weixin/__tests__/component.spec.ts +++ b/packages/uni-mp-weixin/__tests__/component.spec.ts @@ -1,6 +1,15 @@ import { assert } from './testUtils' import { customElements } from '../src/compiler/options' describe('mp-weixin: transform component', () => { + test(`component with v-show`, () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _ctx.ok } +}` + ) + }) test(`built-in component`, () => { const code = customElements.map((tag) => `<${tag}/>`).join('') assert( diff --git a/packages/uni-mp-weixin/dist/uni.compiler.js b/packages/uni-mp-weixin/dist/uni.compiler.js index bea40735c2ad6b5b6fd7d5bcd27cd521cf3f8b40..13fbfeef5d52f08f1dfb83abf7ef633e40d99bcb 100644 --- a/packages/uni-mp-weixin/dist/uni.compiler.js +++ b/packages/uni-mp-weixin/dist/uni.compiler.js @@ -75,6 +75,9 @@ const miniProgram = { canvas: [{ name: 'bind', arg: ['canvas-id', 'id'] }], editor: [{ name: 'on', arg: ['ready'] }], }, + component: { + vShow: uniCliShared.COMPONENT_CUSTOM_HIDDEN, + }, }; const projectConfigFilename = 'project.config.json'; const options = { diff --git a/packages/uni-mp-weixin/src/compiler/options.ts b/packages/uni-mp-weixin/src/compiler/options.ts index c0e2730e70839434114a6b2a2119c6efa83afb37..f01c682c51f8c41d302f6d78adcc9bf1b61fed23 100644 --- a/packages/uni-mp-weixin/src/compiler/options.ts +++ b/packages/uni-mp-weixin/src/compiler/options.ts @@ -1,6 +1,7 @@ import path from 'path' import type { CompilerOptions } from '@vue/compiler-core' import { + COMPONENT_CUSTOM_HIDDEN, MiniProgramCompilerOptions, transformComponentLink, transformRef, @@ -28,6 +29,9 @@ export const miniProgram: MiniProgramCompilerOptions = { canvas: [{ name: 'bind', arg: ['canvas-id', 'id'] }], editor: [{ name: 'on', arg: ['ready'] }], }, + component: { + vShow: COMPONENT_CUSTOM_HIDDEN, + }, } const projectConfigFilename = 'project.config.json'