diff --git a/packages/uni-cli-shared/src/mp/constants.ts b/packages/uni-cli-shared/src/mp/constants.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f8fe8574d671c3705489a1db2656dacc399db94 --- /dev/null +++ b/packages/uni-cli-shared/src/mp/constants.ts @@ -0,0 +1,2 @@ +export const COMPONENT_ON_LINK = 'onVI' +export const COMPONENT_BIND_LINK = '__l' diff --git a/packages/uni-cli-shared/src/mp/index.ts b/packages/uni-cli-shared/src/mp/index.ts index 54f2ca47ac4e25ee4f9488e84156e694e507b840..8fd3c1573b2367d04847968a2b67df2639db5012 100644 --- a/packages/uni-cli-shared/src/mp/index.ts +++ b/packages/uni-cli-shared/src/mp/index.ts @@ -2,4 +2,5 @@ export * from './nvue' export * from './event' export * from './style' export * from './template' +export * from './constants' export { transformVueComponentImports } from './transformImports' diff --git a/packages/uni-cli-shared/src/utils.ts b/packages/uni-cli-shared/src/utils.ts index c066c6ab45b1b8f154a352777c5bea6f21cb7781..9651976168bc551321a1809f6dd9bdd9bdf699b9 100644 --- a/packages/uni-cli-shared/src/utils.ts +++ b/packages/uni-cli-shared/src/utils.ts @@ -7,21 +7,22 @@ export { default as hash } from 'hash-sum' import { PAGE_EXTNAME, PAGE_EXTNAME_APP } from './constants' import { SFCTemplateCompileOptions } from '@vue/compiler-sfc' -import { NodeTypes, ElementNode, Node } from '@vue/compiler-core' +import { + NodeTypes, + ElementNode, + RootNode, + TemplateChildNode, +} from '@vue/compiler-core' export const isWindows = os.platform() === 'win32' export function normalizePath(id: string): string { return isWindows ? id.replace(/\\/g, '/') : id } export function checkElementNodeTag( - node: Node | null | undefined, + node: RootNode | TemplateChildNode | null | undefined, tag: string ): node is ElementNode { - return ( - !!node && - node.type === NodeTypes.ELEMENT && - (node as ElementNode).tag === tag - ) + return !!node && node.type === NodeTypes.ELEMENT && node.tag === tag } export const resolveMainPathOnce = once((inputDir: string) => { diff --git a/packages/uni-cli-shared/src/vue/transforms/transformComponent.ts b/packages/uni-cli-shared/src/vue/transforms/transformComponent.ts index 65cb7c110fe36f3e29da776770987a08fbcc3a7f..8b65d5aec14b1ee86f5c6c8545434ff350cbdd5c 100644 --- a/packages/uni-cli-shared/src/vue/transforms/transformComponent.ts +++ b/packages/uni-cli-shared/src/vue/transforms/transformComponent.ts @@ -9,30 +9,35 @@ import { TemplateChildNode, TransformContext, } from '@vue/compiler-core' +import { COMPONENT_BIND_LINK, COMPONENT_ON_LINK } from '../../mp/constants' -export function addComponentBindLink( - node: RootNode | TemplateChildNode, - context: TransformContext +export function createTransformComponentLink( + name: typeof COMPONENT_BIND_LINK | typeof COMPONENT_ON_LINK ) { - if ( - node.type === NodeTypes.ELEMENT && - node.tagType === ElementTypes.COMPONENT + return function transformComponentLink( + node: RootNode | TemplateChildNode, + context: TransformContext ) { - const { tag } = node if ( - isComponentTag(tag) || - isCoreComponent(tag) || - context.isBuiltInComponent(tag) + node.type === NodeTypes.ELEMENT && + node.tagType === ElementTypes.COMPONENT ) { - return + const { tag } = node + if ( + isComponentTag(tag) || + isCoreComponent(tag) || + context.isBuiltInComponent(tag) + ) { + return + } + node.props.push({ + type: NodeTypes.DIRECTIVE, + name: 'on', + modifiers: [], + loc: locStub, + arg: createSimpleExpression(name, true), + exp: createSimpleExpression('__l', true), + }) } - node.props.push({ - type: NodeTypes.DIRECTIVE, - name: 'on', - modifiers: [], - loc: locStub, - arg: createSimpleExpression('__l', true), - exp: createSimpleExpression('__l', true), - }) } } diff --git a/packages/uni-mp-baidu/dist/uni.compiler.js b/packages/uni-mp-baidu/dist/uni.compiler.js index 42659955416c4f94413fe9fb5dd454453ccdd169..a079bd609392ffc03f827721e60bf8015ee1cba5 100644 --- a/packages/uni-mp-baidu/dist/uni.compiler.js +++ b/packages/uni-mp-baidu/dist/uni.compiler.js @@ -148,7 +148,7 @@ const miniProgram = { array: true, }, slot: { - fallback: false, + fallback: true, }, directive: 's-', }; diff --git a/packages/uni-mp-baidu/src/compiler/options.ts b/packages/uni-mp-baidu/src/compiler/options.ts index a5663f2d36d36c3102d11f69eda63d4aadcf9ca6..d9d5c5b2152b88a46d85eb210b32e0df0f0d782d 100644 --- a/packages/uni-mp-baidu/src/compiler/options.ts +++ b/packages/uni-mp-baidu/src/compiler/options.ts @@ -12,7 +12,7 @@ export const miniProgram: MiniProgramCompilerOptions = { array: true, }, slot: { - fallback: false, + fallback: true, }, directive: 's-', } diff --git a/packages/uni-mp-compiler/__tests__/component.spec.ts b/packages/uni-mp-compiler/__tests__/component.spec.ts index 5add1d3ec8e04d98a1dfdf69aebc0d67c876e9a2..7c80ee4955975a74e7968546146a051c710b7875 100644 --- a/packages/uni-mp-compiler/__tests__/component.spec.ts +++ b/packages/uni-mp-compiler/__tests__/component.spec.ts @@ -1,19 +1,11 @@ -import { addComponentBindLink } from '@dcloudio/uni-cli-shared' +import { + COMPONENT_BIND_LINK, + createTransformComponentLink, +} from '@dcloudio/uni-cli-shared' import { assert } from './testUtils' +const nodeTransforms = [createTransformComponentLink(COMPONENT_BIND_LINK)] describe('compiler: transform component', () => { - // test('basic', () => { - // assert( - // ``, - // ``, - // `(_ctx, _cache) => { - // return {} - // }`, - // { - // nodeTransforms: [addComponentBindLink as any], - // } - // ) - // }) test('component + component', () => { assert( ``, @@ -22,7 +14,7 @@ describe('compiler: transform component', () => { return {} }`, { - nodeTransforms: [addComponentBindLink as any], + nodeTransforms, } ) }) @@ -34,7 +26,7 @@ describe('compiler: transform component', () => { return {} }`, { - nodeTransforms: [addComponentBindLink as any], + nodeTransforms, } ) }) @@ -46,7 +38,7 @@ describe('compiler: transform component', () => { return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-0' + '-' + i0 }; }) } }`, { - nodeTransforms: [addComponentBindLink as any], + nodeTransforms, } ) assert( @@ -56,7 +48,7 @@ describe('compiler: transform component', () => { return { a: _f(_ctx.items, (item, key, index) => { return { a: '2a9ec0b0-0' + '-' + index }; }) } }`, { - nodeTransforms: [addComponentBindLink as any], + nodeTransforms, } ) }) @@ -68,7 +60,7 @@ describe('compiler: transform component', () => { return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }; }) } }`, { - nodeTransforms: [addComponentBindLink as any], + nodeTransforms, } ) }) @@ -80,7 +72,7 @@ describe('compiler: transform component', () => { return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + ('2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) } }`, { - nodeTransforms: [addComponentBindLink as any], + nodeTransforms, } ) }) @@ -92,7 +84,7 @@ describe('compiler: transform component', () => { return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.items, (item1, k1, i1) => { return { a: '2a9ec0b0-1' + '-' + i0 + '-' + i1 + ',' + ('2a9ec0b0-0' + '-' + i0) }; }), b: '2a9ec0b0-0' + '-' + i0 }; }) } }`, { - nodeTransforms: [addComponentBindLink as any], + nodeTransforms, } ) }) diff --git a/packages/uni-mp-compiler/__tests__/slot.fallback.spec.ts b/packages/uni-mp-compiler/__tests__/slot.fallback.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a9cb83c8adcd69383db7593a76cf9cc3b445e07 --- /dev/null +++ b/packages/uni-mp-compiler/__tests__/slot.fallback.spec.ts @@ -0,0 +1,52 @@ +import { assert, miniProgram } from './testUtils' + +const options = { + miniProgram: { + ...miniProgram, + slot: { + fallback: true, + }, + }, +} +describe('compiler: transform slot', () => { + test('basic', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}`, + options + ) + }) + test('fallback content', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}`, + options + ) + }) + test('names slots', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}`, + options + ) + }) + test('names slots with fallback content', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}`, + options + ) + }) +}) diff --git a/packages/uni-mp-compiler/src/template/codegen.ts b/packages/uni-mp-compiler/src/template/codegen.ts index 23b2b80c43c0ae8be3f8b0d41284f8dd1510859e..5b515c864473bcaf13ef9c02838c678249587ac7 100644 --- a/packages/uni-mp-compiler/src/template/codegen.ts +++ b/packages/uni-mp-compiler/src/template/codegen.ts @@ -114,7 +114,8 @@ function genVFor( function genSlot(node: SlotOutletNode, context: TemplateCodegenContext) { // 移除掉所有非name属性,即移除作用域插槽的绑定指令 node.props = node.props.filter((prop) => prop.name === 'name') - if (!node.children.length) { + if (!node.children.length || context.slot.fallback) { + // 无后备内容或支持后备内容 return genElement(node, context) } const children = node.children.slice() diff --git a/packages/uni-mp-qq/dist/uni.compiler.js b/packages/uni-mp-qq/dist/uni.compiler.js index 75960cb5bf86ad2c9f9e2dcbce31f255b5a41619..846f1517fb52e0bea2755d81078eb5ec7437ae0e 100644 --- a/packages/uni-mp-qq/dist/uni.compiler.js +++ b/packages/uni-mp-qq/dist/uni.compiler.js @@ -133,7 +133,7 @@ const options = { extname: '.qml', directive: 'qq:', compilerOptions: { - nodeTransforms: [uniCliShared.addComponentBindLink], + nodeTransforms: [uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_BIND_LINK)], }, }, style: { diff --git a/packages/uni-mp-qq/src/compiler/options.ts b/packages/uni-mp-qq/src/compiler/options.ts index 6a3d483de0d73dae6897efcf06551567109c557c..44a20f710a8e601bca949a81dc8ec3bf94208dba 100644 --- a/packages/uni-mp-qq/src/compiler/options.ts +++ b/packages/uni-mp-qq/src/compiler/options.ts @@ -1,6 +1,9 @@ import path from 'path' -import { addComponentBindLink } from '@dcloudio/uni-cli-shared' +import { + COMPONENT_BIND_LINK, + createTransformComponentLink, +} from '@dcloudio/uni-cli-shared' import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite' import source from './project.config.json' @@ -56,7 +59,7 @@ export const options: UniMiniProgramPluginOptions = { extname: '.qml', directive: 'qq:', compilerOptions: { - nodeTransforms: [addComponentBindLink], + nodeTransforms: [createTransformComponentLink(COMPONENT_BIND_LINK)], }, }, style: { diff --git a/packages/uni-mp-toutiao/dist/uni.compiler.js b/packages/uni-mp-toutiao/dist/uni.compiler.js index 8097d55f378e53d906251beef21f0a295e4ddba7..469fae7b5d957412a3e0f57b6928d99aa2c1b3ee 100644 --- a/packages/uni-mp-toutiao/dist/uni.compiler.js +++ b/packages/uni-mp-toutiao/dist/uni.compiler.js @@ -69,12 +69,12 @@ ${filter.code} }, }, slot: { - fallback: false, + fallback: true, }, extname: '.ttml', directive: 'tt:', compilerOptions: { - nodeTransforms: [uniCliShared.addComponentBindLink], + nodeTransforms: [uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_BIND_LINK)], }, }, style: { diff --git a/packages/uni-mp-toutiao/src/compiler/options.ts b/packages/uni-mp-toutiao/src/compiler/options.ts index a17b2d0186945e7cbb003440461e5a881c3232bb..f097f4f3bc5e7841d3ab14a20ee8d693c98f58ef 100644 --- a/packages/uni-mp-toutiao/src/compiler/options.ts +++ b/packages/uni-mp-toutiao/src/compiler/options.ts @@ -1,5 +1,8 @@ import path from 'path' -import { addComponentBindLink } from '@dcloudio/uni-cli-shared' +import { + COMPONENT_BIND_LINK, + createTransformComponentLink, +} from '@dcloudio/uni-cli-shared' import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite' import source from './project.config.json' @@ -44,12 +47,12 @@ ${filter.code} }, }, slot: { - fallback: false, + fallback: true, }, extname: '.ttml', directive: 'tt:', compilerOptions: { - nodeTransforms: [addComponentBindLink], + nodeTransforms: [createTransformComponentLink(COMPONENT_BIND_LINK)], }, }, style: { diff --git a/packages/uni-mp-vite/src/plugin/build.ts b/packages/uni-mp-vite/src/plugin/build.ts index 9c6a278b736c97c7a5a17fdb460a57ac7a05e0db..322f20747885d72c6827b4038cbd0ef3e36cde28 100644 --- a/packages/uni-mp-vite/src/plugin/build.ts +++ b/packages/uni-mp-vite/src/plugin/build.ts @@ -11,6 +11,7 @@ import { hasJsonFile, removeExt, resolveMainPathOnce, + normalizeMiniProgramFilename, } from '@dcloudio/uni-cli-shared' import { GetManualChunk, GetModuleInfo } from 'rollup' import { @@ -57,10 +58,7 @@ export function buildOptions(): UserConfig['build'] { parseVirtualComponentPath(id) ) } - const filepath = path.relative(inputDir, id) - return normalizePath( - filepath.replace(path.extname(filepath), '.js') - ) + return removeExt(normalizeMiniProgramFilename(id, inputDir)) + '.js' } return '[name].js' }, diff --git a/packages/uni-mp-weixin/dist/uni.compiler.js b/packages/uni-mp-weixin/dist/uni.compiler.js index 72b9e19d7e465e305d64ec498770b3b944a46382..fced358e12904b3518095b173bd4cc5cf11c4896 100644 --- a/packages/uni-mp-weixin/dist/uni.compiler.js +++ b/packages/uni-mp-weixin/dist/uni.compiler.js @@ -117,7 +117,10 @@ ${filter.code} extname: '.wxml', directive: 'wx:', compilerOptions: { - nodeTransforms: [uniCliShared.addComponentBindLink], + isCustomElement: (tag) => { + return ['page-meta', 'navigation-bar', 'match-media'].includes(tag); + }, + nodeTransforms: [uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_BIND_LINK)], }, }, style: { diff --git a/packages/uni-mp-weixin/src/compiler/options.ts b/packages/uni-mp-weixin/src/compiler/options.ts index 3e23b1807aaa32cf2de253df7d684ccf199d7f2b..1588e5f689da94048e153d3668792d6d57d1cb45 100644 --- a/packages/uni-mp-weixin/src/compiler/options.ts +++ b/packages/uni-mp-weixin/src/compiler/options.ts @@ -1,6 +1,9 @@ import path from 'path' -import { addComponentBindLink } from '@dcloudio/uni-cli-shared' +import { + COMPONENT_BIND_LINK, + createTransformComponentLink, +} from '@dcloudio/uni-cli-shared' import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite' import source from './project.config.json' @@ -65,7 +68,10 @@ ${filter.code} extname: '.wxml', directive: 'wx:', compilerOptions: { - nodeTransforms: [addComponentBindLink], + isCustomElement: (tag) => { + return ['page-meta', 'navigation-bar', 'match-media'].includes(tag) + }, + nodeTransforms: [createTransformComponentLink(COMPONENT_BIND_LINK)], }, }, style: {