Skip to content

Commit

Permalink
Merge branch 'dev' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
zhetengbiji committed Apr 17, 2024
2 parents 88b1ae8 + 8078bef commit 7b38e82
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,10 @@ describe('mp:compiler-mp-alipay', () => {
'<view>{{array.length}}</view>'
)
})
it('span', () => {
assertCodegen(
'<span></span>',
'<label class="_span"></label>'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,10 @@ describe('mp:compiler-mp-baidu', () => {
'<text>{{"我是第一行3\\\\n 我的第二行2"+title}}</text>'
)
})
it('span', () => {
assertCodegen(
'<span></span>',
'<label class="_span"></label>'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ describe('mp:compiler-mp-kuaishou', () => {
}
)
})
it('span', () => {
assertCodegen(
'<span></span>',
'<label class="_span"></label>'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ describe('mp:compiler-mp-lark', () => {
'<test bind:-data-custom-hidden="{{!(shown)}}" vue-id="551070e6-1" bind:__l="__l" vue-slots="{{[\'default\']}}">hello world</test>'
)
})
it('span', () => {
assertCodegen(
'<span></span>',
'<label class="_span"></label>'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ describe('mp:compiler-mp-qq', () => {
'<text>{{"我是第一行3\\\\n 我的第二行2"+title}}</text>'
)
})

it('span', () => {
assertCodegen(
'<span></span>',
'<label class="_span"></label>'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,11 @@ describe('mp:compiler-mp-toutiao', () => {
}
)
})

it('span', () => {
assertCodegen(
'<span></span>',
'<label class="_span"></label>'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,14 @@ describe('mp:compiler-mp-weixin', () => {
)
assertCodegen( // TODO vue-id
'<span><slot v-bind:user="user">{{ user.lastName }}</slot></span>',
'<label class="_span"><block wx:if="{{$slots.default}}"><slot></slot><scoped-slots-default user="{{user}}" class="scoped-ref" bind:__l="__l"></scoped-slots-default></block><block wx:else>{{user.lastName}}</block></label>',
'<span><block wx:if="{{$slots.default}}"><slot></slot><scoped-slots-default user="{{user}}" class="scoped-ref" bind:__l="__l"></scoped-slots-default></block><block wx:else>{{user.lastName}}</block></span>',
function (res) {
expect(res.componentGenerics['scoped-slots-default']).toBe(true)
}
)
assertCodegen(
'<span><slot name="header" v-bind:user="user">{{ user.lastName }}</slot></span>',
'<label class="_span"><block wx:if="{{$slots.header}}"><slot name="header"></slot><scoped-slots-header user="{{user}}" class="scoped-ref" bind:__l="__l"></scoped-slots-header></block><block wx:else>{{user.lastName}}</block></label>',
'<span><block wx:if="{{$slots.header}}"><slot name="header"></slot><scoped-slots-header user="{{user}}" class="scoped-ref" bind:__l="__l"></scoped-slots-header></block><block wx:else>{{user.lastName}}</block></span>',
function (res) {
expect(res.componentGenerics['scoped-slots-header']).toBe(true)
}
Expand Down Expand Up @@ -517,4 +517,11 @@ describe('mp:compiler-mp-weixin', () => {
'<vertical-drag-gesture-handler onGestureEvent="handlePan" native-view="scroll-view" shouldResponseOnMove="shouldResponse" shouldAcceptGesture="shouldAccept"></vertical-drag-gesture-handler>'
)
})

it('span', () => {
assertCodegen(
'<span></span>',
'<span></span>'
)
})
})
2 changes: 1 addition & 1 deletion packages/uni-template-compiler/__tests__/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ describe('mp:compiler', () => {
// TODO vue的数字 item 是从1,小程序是从0,后续考虑抹平差异
assertCodegen(
'<div><div v-for="i in 10"><p><span></span></p></div></div>',
'<view class="_div"><block wx:for="{{10}}" wx:for-item="i" wx:for-index="__i0__"><view class="_div"><view class="_p"><label class="_span"></label></view></view></block></view>'
'<view class="_div"><block wx:for="{{10}}" wx:for-item="i" wx:for-index="__i0__"><view class="_div"><view class="_p"><span></span></view></view></block></view>'
)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/uni-template-compiler/lib/auto-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const compilerModule = {
if (process.env.UNI_PLATFORM === 'quickapp-native') {
// 排查所有标签
(options.isUnaryTag.autoComponents || (options.isUnaryTag.autoComponents = new Set())).add(el.tag)
} else if (isComponent(el.tag) && el.tag !== 'App') { // App.vue
} else if (isComponent(el.tag, options.mp.platform) && el.tag !== 'App') { // App.vue
// 挂在 isUnaryTag 上边,可以保证外部访问到
(options.isUnaryTag.autoComponents || (options.isUnaryTag.autoComponents = new Set())).add(el.tag)
}
Expand All @@ -120,4 +120,4 @@ const compilerModule = {
module.exports = {
compileTemplate,
module: compilerModule
}
}
15 changes: 13 additions & 2 deletions packages/uni-template-compiler/lib/h5.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const getCompilerOptions = require('./mp.js');
const TAGS = {
br: 'view',
hr: 'view',
Expand Down Expand Up @@ -121,7 +122,17 @@ const TAGS = {
param: 'view'
}
module.exports = {
getTagName (tagName) {
/**
* getTagName
* @param {string} tagName
* @param {string} [platform]
* @returns {boolean}
*/
getTagName (tagName, platform) {
// 排除各平台内置组件
if (platform && getCompilerOptions(platform).isNativeTag(tagName)) {
return tagName
}
return TAGS[tagName] || tagName
}
}
}
22 changes: 11 additions & 11 deletions packages/uni-template-compiler/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const generateScript = require('./script/generate')
const traverseTemplate = require('./template/traverse')
const generateTemplate = require('./template/generate')

const compilerModule = require('./module')
const compilerModule = require('./module')
const compilerModuleUniad = require('./module.uniad')

const compilerAlipayModule = require('./module-alipay')
Expand All @@ -38,10 +38,10 @@ const isWin = /^win/.test(process.platform)
const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)

module.exports = {
compile (source, options = {}) {
if (Array.isArray(options.modules)) {
options.modules.push(compilerModuleUniad)
}
compile (source, options = {}) {
if (Array.isArray(options.modules)) {
options.modules.push(compilerModuleUniad)
}

if ( // 启用摇树优化后,需要过滤内置组件
!options.autoComponentResourcePath ||
Expand All @@ -51,7 +51,7 @@ module.exports = {
}
if (!options.modules) {
options.modules = []
}
}

// transformAssetUrls
options.modules.push(require('./asset-url'))
Expand All @@ -67,7 +67,7 @@ module.exports = {
options.optimize = false // 启用 staticRenderFns
// domProps => attrs
options.mustUseProp = () => false
options.isReservedTag = (tagName) => !isComponent(tagName) // 非组件均为内置
options.isReservedTag = (tagName) => !isComponent(tagName, options.mp.platform) // 非组件均为内置
options.getTagNamespace = () => false

try {
Expand Down Expand Up @@ -96,7 +96,7 @@ module.exports = {
return compileTemplate(source, options, compile)
}

options.modules.push(compilerModule)
options.modules.push(compilerModule)

if (options.mp.platform === 'mp-alipay') {
options.modules.push(compilerAlipayModule)
Expand Down Expand Up @@ -124,9 +124,9 @@ module.exports = {
options.mp.filterModules = Object.keys(options.filterModules || {})

// (可用的原生微信小程序组件,global+scoped)
options.mp.wxComponents = options.wxComponents || Object.create(null)
Object.assign(options.mp.wxComponents, {
'uniad-plugin': 'plugin://uni-ad/ad'
options.mp.wxComponents = options.wxComponents || Object.create(null)
Object.assign(options.mp.wxComponents, {
'uniad-plugin': 'plugin://uni-ad/ad'
})

const state = {
Expand Down
15 changes: 14 additions & 1 deletion packages/uni-template-compiler/lib/mp.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,18 @@ const tags = {
'vertical-drag-gesture-handler',
'horizontal-drag-gesture-handler',
'long-press-gesture-handler',
// 其他
'draggable-sheet',
'grid-builder',
'grid-view',
'list-view',
'list-builder',
'nested-scroll-body',
'nested-scroll-header',
'open-container',
'share-element',
'snapshot',
'span',
'sticky-header',
'sticky-section'
],
Expand Down Expand Up @@ -146,7 +156,10 @@ const baseCompiler = {
* 目前 template 在前,script 在后,要做的话,就需要把 wxml 的生成机制放到 plugin 中才可以拿到真实的组件列表
*/
isComponent (tagName) {
return !tags.base.concat(tags[this.name] || []).includes(tagName)
return !this.isNativeTag(tagName)
},
isNativeTag (tagName) {
return tags.base.concat(tags[this.name] || []).includes(tagName)
},
createFilterTag (filterTag, {
content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ module.exports = {
const tagNode = path.node.arguments[0]
if (t.isStringLiteral(tagNode)) {
// 需要把标签增加到 class 样式中
const tagName = getTagName(tagNode.value)
const tagName = getTagName(tagNode.value, this.options.platform.name)
if (tagName !== tagNode.value) {
addStaticClass(path, '_' + tagNode.value)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-template-compiler/lib/template/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function traverseDataNode (dataNode, state, node) {
// 自定义组件不支持 hidden 属性
const platform = state.options.platform.name
const platforms = ['mp-weixin', 'mp-qq', 'mp-jd', 'mp-xhs', 'mp-toutiao', 'mp-lark']
if (isComponent(node.type) && platforms.includes(platform)) {
if (platforms.includes(platform) && isComponent(node.type, platform)) {
// 字节跳动|飞书小程序自定义属性不会反应在DOM上,只能使用事件格式
key = `${platform === 'mp-toutiao' || platform === 'mp-lark' ? 'bind:-' : ''}${ATTR_DATA_CUSTOM_HIDDEN}`
} else {
Expand Down
10 changes: 8 additions & 2 deletions packages/uni-template-compiler/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ const {
getTagName
} = require('./h5')

function isComponent (tagName) {
/**
* isComponent
* @param {string} tagName
* @param {string} [platform]
* @returns {boolean}
*/
function isComponent (tagName, platform) {
if (
tagName === 'block' ||
tagName === 'component' ||
Expand All @@ -237,7 +243,7 @@ function isComponent (tagName) {
return false
}
}
return !hasOwn(tags, getTagName(tagName.replace(/^v-uni-/, '')))
return !hasOwn(tags, getTagName(tagName.replace(/^v-uni-/, ''), platform))
}

function makeMap (str, expectsLowerCase) {
Expand Down
14 changes: 14 additions & 0 deletions packages/vue-cli-plugin-uni/lib/chain-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,23 @@ module.exports = function chainWebpack (platformOptions, vueOptions, api) {
.use('extract-css-loader')
.tap(options => {
options.esModule = false
// 参考 https://github.com/vuejs/vue-cli/commit/b41ed76c5ce54c618587f24b9d14c35cf31a96d4
// 修复 部分平台 css 内的资源路径不正确,如uni-icons在百度小程序上引用的字体文件路径
options.publicPath = '/'
return options
})
}
} else {
if (langRule.oneOf(type).uses.has('extract-css-loader')){
langRule.oneOf(type)
.use('extract-css-loader')
.tap(options => {
// 参考 https://github.com/vuejs/vue-cli/commit/b41ed76c5ce54c618587f24b9d14c35cf31a96d4
// 修复 部分平台 css 内的资源路径不正确,如uni-icons在百度小程序上引用的字体文件路径
if (options) options.publicPath = '/'
return options
})
}
}
langRule.oneOf(type)
.use('uniapp-preprocss')
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js

Large diffs are not rendered by default.

0 comments on commit 7b38e82

Please sign in to comment.