diff --git a/packages/uni-components/build.json b/packages/uni-components/build.json new file mode 100644 index 0000000000000000000000000000000000000000..c160aa3d62865300197eca2428220f5651a8dc6b --- /dev/null +++ b/packages/uni-components/build.json @@ -0,0 +1,7 @@ +[ + { + "input": { + "src/nvue/bridge.ts": ["dist/bridge.js"] + } + } +] diff --git a/packages/uni-components/dist/bridge.js b/packages/uni-components/dist/bridge.js new file mode 100644 index 0000000000000000000000000000000000000000..5c3dbad49d30589eb5cc74bbe1eacb01a0d89475 --- /dev/null +++ b/packages/uni-components/dist/bridge.js @@ -0,0 +1,81 @@ +const E = function () { + // Keep this empty so it's easier to inherit from + // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) +}; +E.prototype = { + on: function (name, callback, ctx) { + var e = this.e || (this.e = {}); + (e[name] || (e[name] = [])).push({ + fn: callback, + ctx: ctx, + }); + return this; + }, + once: function (name, callback, ctx) { + var self = this; + function listener() { + self.off(name, listener); + callback.apply(ctx, arguments); + } + listener._ = callback; + return this.on(name, listener, ctx); + }, + emit: function (name) { + var data = [].slice.call(arguments, 1); + var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); + var i = 0; + var len = evtArr.length; + for (i; i < len; i++) { + evtArr[i].fn.apply(evtArr[i].ctx, data); + } + return this; + }, + off: function (name, callback) { + var e = this.e || (this.e = {}); + var evts = e[name]; + var liveEvents = []; + if (evts && callback) { + for (var i = 0, len = evts.length; i < len; i++) { + if (evts[i].fn !== callback && evts[i].fn._ !== callback) + liveEvents.push(evts[i]); + } + } + // Remove event from queue to prevent memory leak + // Suggested by https://github.com/lazd + // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 + liveEvents.length ? (e[name] = liveEvents) : delete e[name]; + return this; + }, +}; +var E$1 = E; + +function initBridge(subscribeNamespace) { + const emitter = new E$1(); + return { + on(event, callback) { + return emitter.on(event, callback); + }, + once(event, callback) { + return emitter.once(event, callback); + }, + off(event, callback) { + return emitter.off(event, callback); + }, + emit(event, ...args) { + return emitter.emit(event, ...args); + }, + subscribe(event, callback, once = false) { + emitter[once ? 'once' : 'on'](`${subscribeNamespace}.${event}`, callback); + }, + unsubscribe(event, callback) { + emitter.off(`${subscribeNamespace}.${event}`, callback); + }, + subscribeHandler(event, args, pageId) { + emitter.emit(`${subscribeNamespace}.${event}`, args, pageId); + }, + }; +} + +const UniViewJSBridge = initBridge('nvue'); + +export { UniViewJSBridge }; diff --git a/packages/uni-components/dist/components.js b/packages/uni-components/dist/components.js index 4eed33a784a807ce06512c646a1e053fc869341c..f907fd16479a3210a201e5a5e8d1cd6b71213264 100644 --- a/packages/uni-components/dist/components.js +++ b/packages/uni-components/dist/components.js @@ -1,5 +1,5 @@ -export function initComponents(uni, Vue, weex) { - var components = function(vue) { +export function initComponents({ uni, Vue, weex, plus, BroadcastChannel }) { + var components = function(vue, shared) { "use strict"; const OPEN_TYPES = [ "navigate", @@ -81,18 +81,16 @@ export function initComponents(uni, Vue, weex) { } }; } - const hasOwnProperty = Object.prototype.hasOwnProperty; - const hasOwn = (val, key) => hasOwnProperty.call(val, key); function useHoverClass(props) { if (props.hoverClass && props.hoverClass !== "none") { const hoverAttrs = { hoverClass: props.hoverClass }; - if (hasOwn(props, "hoverStartTime")) { + if (shared.hasOwn(props, "hoverStartTime")) { hoverAttrs.hoverStartTime = props.hoverStartTime; } - if (hasOwn(props, "hoverStayTime")) { + if (shared.hasOwn(props, "hoverStayTime")) { hoverAttrs.hoverStayTime = props.hoverStayTime; } - if (hasOwn(props, "hoverStopPropagation")) { + if (shared.hasOwn(props, "hoverStopPropagation")) { hoverAttrs.hoverStopPropagation = props.hoverStopPropagation; } return hoverAttrs; @@ -124,6 +122,6 @@ export function initComponents(uni, Vue, weex) { Navigator }; return index; - }(Vue); + }(Vue, VueShared); return components; } diff --git a/packages/uni-components/src/nvue/bridge.ts b/packages/uni-components/src/nvue/bridge.ts new file mode 100644 index 0000000000000000000000000000000000000000..96131b785b5135017c2ba251f7b9a4978e3be168 --- /dev/null +++ b/packages/uni-components/src/nvue/bridge.ts @@ -0,0 +1,2 @@ +import { initBridge } from '@dcloudio/uni-core' +export const UniViewJSBridge = initBridge('nvue') diff --git a/packages/uni-components/vite.config.ts b/packages/uni-components/vite.config.ts index 320c4ba68ff771dc208fed33a3b1770cd7108f93..657eb245091097a335538df2773432e44dea6776 100644 --- a/packages/uni-components/vite.config.ts +++ b/packages/uni-components/vite.config.ts @@ -2,8 +2,21 @@ import path from 'path' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' + +function resolve(file: string) { + return path.resolve(__dirname, file) +} + export default defineConfig({ root: __dirname, + resolve: { + alias: [ + { + find: '@dcloudio/uni-core', + replacement: resolve('../uni-core/src'), + }, + ], + }, build: { minify: false, lib: { @@ -12,15 +25,17 @@ export default defineConfig({ formats: ['iife'], }, rollupOptions: { - external: ['uni', 'vue', 'weex'], + external: ['uni', 'vue', 'weex', '@vue/shared'], output: { - banner: 'export function initComponents(uni, Vue, weex) {', + banner: + 'export function initComponents({uni,Vue,weex,plus,BroadcastChannel}) {', footer: 'return components\n}', entryFileNames: 'components.js', globals: { uni: 'uni', vue: 'Vue', weex: 'weex', + '@vue/shared': 'VueShared', }, }, }, diff --git a/packages/uni-core/src/helpers/bridge.ts b/packages/uni-core/src/helpers/bridge.ts index 1842078e76d54eaa96bfc54decf09785f95890f8..33ecece152a3ce155c5ce45f58b0cc5c7ddedacd 100644 --- a/packages/uni-core/src/helpers/bridge.ts +++ b/packages/uni-core/src/helpers/bridge.ts @@ -1,8 +1,7 @@ -// TODO 等待 vue3 的兼容模式自带emitter import E from './TinyEmitter' export function initBridge( - subscribeNamespace: 'service' | 'view' + subscribeNamespace: 'service' | 'view' | 'nvue' ): Omit< UniApp.UniServiceJSBridge, | 'invokeOnCallback' @@ -10,7 +9,6 @@ export function initBridge( | 'invokeViewMethodKeepAlive' | 'publishHandler' > { - // TODO vue3 compatibility builds const emitter = new E() return { on(event: string, callback: UniApp.CallbackFunction) { diff --git a/packages/uni-core/src/index.ts b/packages/uni-core/src/index.ts index 62b66ecef5d341ad12e6e63545ef18833e8a4be0..3ed84de5782b87c850272835ce9c027bb6ab5e83 100644 --- a/packages/uni-core/src/index.ts +++ b/packages/uni-core/src/index.ts @@ -3,3 +3,4 @@ export * from './view' export * from './service' export * from './helpers' export * from './constants' +export { initBridge } from './helpers/bridge'