Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(uni-mp-weibo-vue2): 新增对微博小程序的支持 #4441

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ packages/uni-cli-shared/components/ad-interstitial.vue
packages/uni-cli-shared/components/ad-rewarded-video.vue
packages/uni-cli-shared/lib/uni_modules/uni_modules.js
packages/uni-cli-shared/lib/uts/uts.js
packages/uni-mp-weibo/src
4 changes: 2 additions & 2 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let name = 'index'
let filename = ''
let entry = './lib/' + process.env.UNI_PLATFORM + '/main.js'

if (process.env.UNI_PLATFORM === 'h5' && process.env.UNI_UI === 'true') {
if ((process.env.UNI_PLATFORM === 'h5' || process.env.UNI_PLATFORM === 'mp-weibo') && process.env.UNI_UI === 'true') {
entry = './lib/' + process.env.UNI_PLATFORM + '/ui.js'
}

Expand Down Expand Up @@ -63,7 +63,7 @@ service.run('build', {
process.exit(1)
})

if (process.env.UNI_PLATFORM === 'h5' && process.env.UNI_WATCH === 'false') {
if ((process.env.UNI_PLATFORM === 'h5' || process.env.UNI_PLATFORM === 'mp-weibo') && process.env.UNI_WATCH === 'false') {
const packagePath = path.join(__dirname, `../packages/uni-${process.env.UNI_PLATFORM}`)
const packageJsonPath = path.join(packagePath, 'package.json')
del(path.join(packagePath, '{lib,src}'))
Expand Down
8 changes: 4 additions & 4 deletions build/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const postCssConfig = require('../postcss.config')

let outputDir = resolve('./packages/uni-' + process.env.UNI_PLATFORM + '/dist')

if (process.env.UNI_PLATFORM === 'h5' && process.env.UNI_UI === 'true') {
if ((process.env.UNI_PLATFORM === 'h5' || process.env.UNI_PLATFORM === 'mp-weibo') && process.env.UNI_UI === 'true') {
outputDir = resolve('./packages/uni-' + process.env.UNI_PLATFORM + '-ui/dist')
}

if (process.env.UNI_PLATFORM === 'app-plus' && process.env.UNI_VIEW === 'true') {
outputDir = resolve('./packages/uni-' + process.env.UNI_PLATFORM + '/dist')
}

if (process.env.UNI_PLATFORM === 'h5') { postCssConfig.plugins.push(splitMediaPlugin) }
if (process.env.UNI_PLATFORM === 'h5' || process.env.UNI_PLATFORM === 'mp-weibo') { postCssConfig.plugins.push(splitMediaPlugin) }

module.exports = {
publicPath: '/',
Expand All @@ -30,7 +30,7 @@ module.exports = {
transpileDependencies: ['@dcloudio/uni-i18n'],
productionSourceMap: false,
configureWebpack: webpackConfig,
parallel: process.env.UNI_PLATFORM !== 'h5' || process.env.UNI_WATCH !== 'false' || process.env.UNI_UI === 'true',
parallel: (process.env.UNI_PLATFORM !== 'h5' && process.env.UNI_PLATFORM !== 'mp-weibo') || process.env.UNI_WATCH !== 'false' || process.env.UNI_UI === 'true',
chainWebpack: config => {
config.devtool('source-map')

Expand All @@ -48,7 +48,7 @@ module.exports = {
})
config.plugins.delete('hmr') // remove hot module reload

if (process.env.UNI_PLATFORM === 'h5') {
if (process.env.UNI_PLATFORM === 'h5' || process.env.UNI_PLATFORM === 'mp-weibo') {
config
.plugin('webpack-build-done')
.use(webpack.ProgressPlugin, [function (percentage, message, ...args) {
Expand Down
3 changes: 3 additions & 0 deletions lib/mp-weibo/extends-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'uni-sub-platform/service/index'
import * as api from 'uni-platform/service/api/index'
export default api
66 changes: 66 additions & 0 deletions lib/mp-weibo/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 1.导出全局对象(UniViewJSBridge,UniServiceJSBridge,uni,getApp,getCurrentPages)
* 2.引入 Vue 插件(uniVueServicePlugin,uniVueServicePlugin)
* 3.引入 Vue 组件
*/
import Vue from 'vue'
// import 'uni-platform/view/index.css'
import initVue from 'uni-core/vue'

class UniApp {
constructor () {
this._init()
}

_init () {
global.UniViewJSBridge = {
subscribe: UniViewJSBridge.subscribe,
publishHandler: UniViewJSBridge.publishHandler,
subscribeHandler: UniViewJSBridge.subscribeHandler
}

global.UniServiceJSBridge = {
subscribe: UniServiceJSBridge.subscribe,
publishHandler: UniServiceJSBridge.publishHandler,
subscribeHandler: UniServiceJSBridge.subscribeHandler
}

const {
default: uni,
getApp,
getCurrentPages
} = require('uni-platform/service/index')

// 与微信JS-SDK同名的方法
// const sameNameApis = ['setClipboardData', 'getClipboardData', 'onUserCaptureScreen', 'openBluetoothAdapter', 'startBluetoothDevicesDiscovery', 'onBluetoothDeviceFound', 'stopBluetoothDevicesDiscovery', 'onBluetoothAdapterStateChange', 'getConnectedBluetoothDevices', 'getBluetoothDevices', 'getBluetoothAdapterState', 'closeBluetoothAdapter', 'writeBLECharacteristicValue', 'readBLECharacteristicValue', 'onBLEConnectionStateChange', 'onBLECharacteristicValueChange', 'notifyBLECharacteristicValueChange', 'getBLEDeviceServices', 'getBLEDeviceCharacteristics', 'createBLEConnection', 'closeBLEConnection', 'onBeaconServiceChange', 'onBeaconUpdate', 'getBeacons', 'startBeaconDiscovery', 'stopBeaconDiscovery', 'chooseImage', 'previewImage', 'getNetworkType', 'onNetworkStatusChange', 'openLocation', 'getLocation']
// 默认会被iOS企业微信替换的方法
const sameNameApis = ['chooseImage']
sameNameApis.forEach(item => {
Object.defineProperty(uni, item, {
writable: false,
configurable: false
})
})

global.uni = uni

global.wx = global.uni

global.getApp = getApp
global.getCurrentPages = getCurrentPages

Vue.use(require('uni-service/plugins').default, {
routes: __uniRoutes
})

Vue.use(require('uni-view/plugins').default, {
routes: __uniRoutes
})

initVue(Vue)
require('uni-platform/components')
require('uni-components')
}
}
global.UniApp = UniApp
global.__uniConfig && new UniApp()
27 changes: 27 additions & 0 deletions lib/mp-weibo/ui-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
canIUse
} from 'uni-core/service/api/base/can-i-use'
import {
upx2px
} from 'uni-core/service/api/base/upx2px'
import {
getLocation
} from 'uni-platform/service/api/location/get-location'
import {
onCompassChange,
stopCompass
} from 'uni-platform/service/api/device/compass'
import {
getSystemInfoSync
} from 'uni-platform/service/api/device/get-system-info'

// TODO route

export default {
canIUse,
upx2px,
getLocation,
onCompassChange,
stopCompass,
getSystemInfoSync
}
54 changes: 54 additions & 0 deletions lib/mp-weibo/ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Vue from 'vue'

// 使用白名单过滤(前期有一批自定义组件使用了 uni-)
import tags from 'uni-helpers/tags'

import 'uni-core/view/index.css'

import uni from './ui-api.js'

import {
processEvent
} from 'uni-core/view/plugins/events'

const oldIsReservedTag = Vue.config.isReservedTag

global.uni = uni

Vue.config.isReservedTag = function (tag) {
return tags.indexOf(tag) !== -1 || oldIsReservedTag(tag)
}

Vue.config.ignoredElements = tags

const oldGetTagNamespace = Vue.config.getTagNamespace

const conflictTags = ['switch', 'image', 'text', 'view']

Vue.config.getTagNamespace = function (tag) {
if (~conflictTags.indexOf(tag)) { // svg 部分标签名称与 uni 标签冲突
return false
}
return oldGetTagNamespace(tag) || false
}

const findUniTarget = function ($event, $el) {
let target = $event.target
for (; target && target !== $el; target = target.parentNode) {
if (target.tagName && target.tagName.indexOf('UNI-') === 0) {
break
}
}
return target
}

Vue.prototype.$handleEvent = function ($event) {
if ($event instanceof Event) { // 未处理的 event 对象 需要对 target 校正及包装
// 查找 uniTarget
const target = findUniTarget($event, this.$el)
$event = processEvent.call(this, $event.type, $event, {}, target || $event.target, $event.currentTarget)
}
return $event
}

require('uni-components')
1 change: 1 addition & 0 deletions lib/mp-weibo/uni.automator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions lib/mp-weibo/uni.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const fs = require('fs')
const path = require('path')

function getTemplatePath (template) {
if (template) {
const userTemplate = path.resolve(process.env.UNI_INPUT_DIR, template)
if (fs.existsSync(userTemplate)) { return userTemplate }
}
return path.resolve(process.env.UNI_CLI_CONTEXT, 'public/index.html')
}

function transform (content, platformOptions) {
if (platformOptions.darkmode === true) {
// darkmode
try {
content += fs.readFileSync(require.resolve('@dcloudio/uni-h5/dist/index.dark.css'))
} catch (error) { }
}
if (process.env.NODE_ENV === 'production') {
return content + // shadow
'body::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)}}'
}
return content
}

function getIndexCssPath (assetsDir, template, hashKey) {
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
const VUE_APP_INDEX_CSS_HASH = process.env[hashKey]
if (VUE_APP_INDEX_CSS_HASH) {
try {
const templateContent = fs.readFileSync(getTemplatePath(template))
if (new RegExp('\\b' + hashKey + '\\b').test(templateContent)) {
return path.join(assetsDir, `[name].${VUE_APP_INDEX_CSS_HASH}${CopyWebpackPluginVersion > 7 ? '' : '.'}[ext]`)
}
} catch (e) { }
}
return assetsDir
}

module.exports = {
options: {
cssVars: {
'--status-bar-height': '0px'
},
filterTag: 'wxs',
vue: '@dcloudio/vue-cli-plugin-uni/packages/h5-vue'
},
copyWebpackOptions (platformOptions, vueOptions) {
const copyOptions = [
{
from: require.resolve('@dcloudio/uni-mp-weibo/dist/index.css'),
to: getIndexCssPath(vueOptions.assetsDir, platformOptions.template, 'VUE_APP_INDEX_CSS_HASH'),
transform (content) {
return transform(content, platformOptions)
}
},
'hybrid/html'
]
global.uniModules.forEach(module => {
copyOptions.push('uni_modules/' + module + '/hybrid/html')
})

return copyOptions
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
"release": "npm run lint:cli && lerna publish --force-publish=*",
"release:alpha": "npm run lint:cli && lerna publish --force-publish=* --npm-tag=alpha",
"release:next": "npm run lint:cli && lerna publish --force-publish=* --npm-tag=next",
"release:v3": "npm run lint:cli && lerna publish --no-git-tag-version --force-publish=* --npm-tag=v3"
"release:v3": "npm run lint:cli && lerna publish --no-git-tag-version --force-publish=* --npm-tag=v3",
"build:mp-weibo": "npm run lint && cross-env NODE_ENV=development UNI_WATCH=false UNI_PLATFORM=mp-weibo node build/build.js",
"dev:mp-weibo": "npm run lint && cross-env NODE_ENV=production UNI_WATCH=true UNI_PLATFORM=mp-weibo node build/build.js"

},
"dependencies": {
"base64-arraybuffer": "^0.2.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/uni-cli-shared/lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ function getH5Options (manifestJson) {
configureH5(h5)
})

// 处理微博小程序
if (process.env.UNI_PLATFORM === 'mp-weibo') {
h5.publicPath = './'
h5.router = {
base: './',
mode: 'hash'
}
}

return h5
}

Expand Down
2 changes: 2 additions & 0 deletions packages/uni-mp-weibo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/lib/
/src/
2 changes: 2 additions & 0 deletions packages/uni-mp-weibo/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!/lib/
!/src/
Loading