diff --git a/packages/vue-cli-plugin-uni/commands/build.js b/packages/vue-cli-plugin-uni/commands/build.js index d765580676b5f869abc08ab853dc8ac744197dff..8d6319c4d931ca5215bb5981f30b05d58b7c69f8 100644 --- a/packages/vue-cli-plugin-uni/commands/build.js +++ b/packages/vue-cli-plugin-uni/commands/build.js @@ -28,7 +28,8 @@ module.exports = (api, options) => { '--minimize': 'Tell webpack to minimize the bundle using the TerserPlugin.', '--auto-host': 'specify automator host', '--auto-port': 'specify automator port', - '--subpackage': 'specify subpackage' + '--subpackage': 'specify subpackage', + '--plugin': 'specify plugin' } }, async (args) => { for (const key in defaults) { @@ -42,6 +43,10 @@ module.exports = (api, options) => { process.env.UNI_SUBPACKGE = args.subpackage } + if (args.plugin && platforms.includes(process.env.UNI_PLATFORM)) { + process.env.UNI_MP_PLUGIN = args.plugin + } + require('./util').initAutomator(args) args.entry = args.entry || args._[0] @@ -190,4 +195,4 @@ async function build (args, api, options) { module.exports.defaultModes = { 'uni-build': process.env.NODE_ENV -} +} diff --git a/packages/vue-cli-plugin-uni/lib/configure-webpack.js b/packages/vue-cli-plugin-uni/lib/configure-webpack.js index 9af28c6fd9b1b8086860a1604b6a4aa29894ff61..785323abd3f2601f9809b47612a5d317f3f72f3d 100644 --- a/packages/vue-cli-plugin-uni/lib/configure-webpack.js +++ b/packages/vue-cli-plugin-uni/lib/configure-webpack.js @@ -197,7 +197,7 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt if (!isAppView) { // app-plus view不需要copy plugins.push(new CopyWebpackPlugin(getCopyWebpackPluginOptions(manifestPlatformOptions, vueOptions))) } - if (!process.env.UNI_SUBPACKGE) { + if (!process.env.UNI_SUBPACKGE || !process.env.UNI_MP_PLUGIN) { try { const automatorJson = require.resolve('@dcloudio/uni-automator/dist/automator.json') plugins.push(new CopyWebpackPlugin([{ diff --git a/packages/vue-cli-plugin-uni/lib/mp/index.js b/packages/vue-cli-plugin-uni/lib/mp/index.js index 8b9ee13c2feca688dd4ac4f895da01c01bbecdf8..c0d7c0e262621ac5dada6a40c52cccd9d59804f0 100644 --- a/packages/vue-cli-plugin-uni/lib/mp/index.js +++ b/packages/vue-cli-plugin-uni/lib/mp/index.js @@ -38,6 +38,8 @@ function getProvides () { if (process.env.UNI_USING_COMPONENTS) { if (process.env.UNI_SUBPACKGE) { provides.createApp = [uniPath, 'createSubpackageApp'] + } else if (process.env.UNI_MP_PLUGIN) { + provides.createApp = [uniPath, 'createPlugin'] } else { provides.createApp = [uniPath, 'createApp'] } @@ -126,11 +128,11 @@ class PreprocessAssetsPlugin { function initSubpackageConfig (webpackConfig, vueOptions) { if (process.env.UNI_OUTPUT_DEFAULT_DIR === process.env.UNI_OUTPUT_DIR) { // 未自定义output - process.env.UNI_OUTPUT_DIR = path.resolve(process.env.UNI_OUTPUT_DIR, process.env.UNI_SUBPACKGE) + process.env.UNI_OUTPUT_DIR = path.resolve(process.env.UNI_OUTPUT_DIR, (process.env.UNI_SUBPACKGE || process.env.UNI_MP_PLUGIN)) } vueOptions.outputDir = process.env.UNI_OUTPUT_DIR webpackConfig.output.path(process.env.UNI_OUTPUT_DIR) - webpackConfig.output.jsonpFunction('webpackJsonp_' + process.env.UNI_SUBPACKGE) + webpackConfig.output.jsonpFunction('webpackJsonp_' + (process.env.UNI_SUBPACKGE || process.env.UNI_MP_PLUGIN)) } module.exports = { @@ -162,7 +164,7 @@ module.exports = { new webpack.ProvidePlugin(getProvides()) ] - if (process.env.UNI_SUBPACKGE && process.env.UNI_SUBPACKGE !== 'main') { + if ((process.env.UNI_SUBPACKGE || process.env.UNI_MP_PLUGIN) && process.env.UNI_SUBPACKGE !== 'main') { plugins.push(new PreprocessAssetsPlugin()) } @@ -272,7 +274,7 @@ module.exports = { })) } - if (process.env.UNI_SUBPACKGE) { + if (process.env.UNI_SUBPACKGE || process.env.UNI_MP_PLUGIN) { initSubpackageConfig(webpackConfig, vueOptions) } diff --git a/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js b/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js index f29912aa5ec8d5369a1da8722cbddbe6f4050991..53e785c5136cd09e33c3e19d0744332b94ac490e 100644 --- a/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js +++ b/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js @@ -2230,17 +2230,17 @@ function updateListeners ( /* */ -// fixed by xxxxxx (mp properties) -function extractPropertiesFromVNodeData(data, Ctor, res, context) { - var propOptions = Ctor.options.mpOptions && Ctor.options.mpOptions.properties; - if (isUndef(propOptions)) { - return res +// fixed by xxxxxx (mp properties) +function extractPropertiesFromVNodeData(data, Ctor, res, context) { + var propOptions = Ctor.options.mpOptions && Ctor.options.mpOptions.properties; + if (isUndef(propOptions)) { + return res } - var externalClasses = Ctor.options.mpOptions.externalClasses || []; + var externalClasses = Ctor.options.mpOptions.externalClasses || []; var attrs = data.attrs; - var props = data.props; - if (isDef(attrs) || isDef(props)) { - for (var key in propOptions) { + var props = data.props; + if (isDef(attrs) || isDef(props)) { + for (var key in propOptions) { var altKey = hyphenate(key); var result = checkProp(res, props, key, altKey, true) || checkProp(res, attrs, key, altKey, false); @@ -2253,10 +2253,10 @@ function extractPropertiesFromVNodeData(data, Ctor, res, context) { ) { // 赋值 externalClass 真正的值(模板里 externalClass 的值可能是字符串) res[key] = context[camelize(res[key])]; - } - } - } - return res + } + } + } + return res } function extractPropsFromVNodeData ( @@ -5522,138 +5522,138 @@ function type(obj) { return Object.prototype.toString.call(obj) } -/* */ - -function flushCallbacks$1(vm) { - if (vm.__next_tick_callbacks && vm.__next_tick_callbacks.length) { - if (process.env.VUE_APP_DEBUG) { - var mpInstance = vm.$scope; - console.log('[' + (+new Date) + '][' + (mpInstance.is || mpInstance.route) + '][' + vm._uid + - ']:flushCallbacks[' + vm.__next_tick_callbacks.length + ']'); - } - var copies = vm.__next_tick_callbacks.slice(0); - vm.__next_tick_callbacks.length = 0; - for (var i = 0; i < copies.length; i++) { - copies[i](); - } - } -} - -function hasRenderWatcher(vm) { - return queue.find(function (watcher) { return vm._watcher === watcher; }) -} - -function nextTick$1(vm, cb) { - //1.nextTick 之前 已 setData 且 setData 还未回调完成 - //2.nextTick 之前存在 render watcher +/* */ + +function flushCallbacks$1(vm) { + if (vm.__next_tick_callbacks && vm.__next_tick_callbacks.length) { + if (process.env.VUE_APP_DEBUG) { + var mpInstance = vm.$scope; + console.log('[' + (+new Date) + '][' + (mpInstance.is || mpInstance.route) + '][' + vm._uid + + ']:flushCallbacks[' + vm.__next_tick_callbacks.length + ']'); + } + var copies = vm.__next_tick_callbacks.slice(0); + vm.__next_tick_callbacks.length = 0; + for (var i = 0; i < copies.length; i++) { + copies[i](); + } + } +} + +function hasRenderWatcher(vm) { + return queue.find(function (watcher) { return vm._watcher === watcher; }) +} + +function nextTick$1(vm, cb) { + //1.nextTick 之前 已 setData 且 setData 还未回调完成 + //2.nextTick 之前存在 render watcher if (!vm.__next_tick_pending && !hasRenderWatcher(vm)) { if(process.env.VUE_APP_DEBUG){ var mpInstance = vm.$scope; console.log('[' + (+new Date) + '][' + (mpInstance.is || mpInstance.route) + '][' + vm._uid + ']:nextVueTick'); - } - return nextTick(cb, vm) + } + return nextTick(cb, vm) }else{ if(process.env.VUE_APP_DEBUG){ var mpInstance$1 = vm.$scope; console.log('[' + (+new Date) + '][' + (mpInstance$1.is || mpInstance$1.route) + '][' + vm._uid + ']:nextMPTick'); } - } - var _resolve; - if (!vm.__next_tick_callbacks) { - vm.__next_tick_callbacks = []; - } - vm.__next_tick_callbacks.push(function () { - if (cb) { - try { - cb.call(vm); - } catch (e) { - handleError(e, vm, 'nextTick'); - } - } else if (_resolve) { - _resolve(vm); - } - }); - // $flow-disable-line - if (!cb && typeof Promise !== 'undefined') { - return new Promise(function (resolve) { - _resolve = resolve; - }) - } -} - -/* */ - -function cloneWithData(vm) { - // 确保当前 vm 所有数据被同步 - var ret = Object.create(null); - var dataKeys = [].concat( - Object.keys(vm._data || {}), - Object.keys(vm._computedWatchers || {})); - - dataKeys.reduce(function(ret, key) { - ret[key] = vm[key]; - return ret - }, ret); - - // vue-composition-api - var compositionApiState = vm.__composition_api_state__ || vm.__secret_vfa_state__; - var rawBindings = compositionApiState && compositionApiState.rawBindings; - if (rawBindings) { - Object.keys(rawBindings).forEach(function (key) { - ret[key] = vm[key]; - }); - } - - //TODO 需要把无用数据处理掉,比如 list=>l0 则 list 需要移除,否则多传输一份数据 - Object.assign(ret, vm.$mp.data || {}); - if ( - Array.isArray(vm.$options.behaviors) && - vm.$options.behaviors.indexOf('uni://form-field') !== -1 - ) { //form-field - ret['name'] = vm.name; - ret['value'] = vm.value; - } - - return JSON.parse(JSON.stringify(ret)) -} - + } + var _resolve; + if (!vm.__next_tick_callbacks) { + vm.__next_tick_callbacks = []; + } + vm.__next_tick_callbacks.push(function () { + if (cb) { + try { + cb.call(vm); + } catch (e) { + handleError(e, vm, 'nextTick'); + } + } else if (_resolve) { + _resolve(vm); + } + }); + // $flow-disable-line + if (!cb && typeof Promise !== 'undefined') { + return new Promise(function (resolve) { + _resolve = resolve; + }) + } +} + +/* */ + +function cloneWithData(vm) { + // 确保当前 vm 所有数据被同步 + var ret = Object.create(null); + var dataKeys = [].concat( + Object.keys(vm._data || {}), + Object.keys(vm._computedWatchers || {})); + + dataKeys.reduce(function(ret, key) { + ret[key] = vm[key]; + return ret + }, ret); + + // vue-composition-api + var compositionApiState = vm.__composition_api_state__ || vm.__secret_vfa_state__; + var rawBindings = compositionApiState && compositionApiState.rawBindings; + if (rawBindings) { + Object.keys(rawBindings).forEach(function (key) { + ret[key] = vm[key]; + }); + } + + //TODO 需要把无用数据处理掉,比如 list=>l0 则 list 需要移除,否则多传输一份数据 + Object.assign(ret, vm.$mp.data || {}); + if ( + Array.isArray(vm.$options.behaviors) && + vm.$options.behaviors.indexOf('uni://form-field') !== -1 + ) { //form-field + ret['name'] = vm.name; + ret['value'] = vm.value; + } + + return JSON.parse(JSON.stringify(ret)) +} + var patch = function(oldVnode, vnode) { var this$1 = this; - - if (vnode === null) { //destroy - return - } - if (this.mpType === 'page' || this.mpType === 'component') { - var mpInstance = this.$scope; - var data = Object.create(null); - try { - data = cloneWithData(this); - } catch (err) { - console.error(err); - } - data.__webviewId__ = mpInstance.data.__webviewId__; - var mpData = Object.create(null); - Object.keys(data).forEach(function (key) { //仅同步 data 中有的数据 - mpData[key] = mpInstance.data[key]; - }); - var diffData = this.$shouldDiffData === false ? data : diff(data, mpData); - if (Object.keys(diffData).length) { - if (process.env.VUE_APP_DEBUG) { - console.log('[' + (+new Date) + '][' + (mpInstance.is || mpInstance.route) + '][' + this._uid + - ']差量更新', - JSON.stringify(diffData)); - } - this.__next_tick_pending = true; - mpInstance.setData(diffData, function () { - this$1.__next_tick_pending = false; - flushCallbacks$1(this$1); - }); - } else { - flushCallbacks$1(this); - } - } + + if (vnode === null) { //destroy + return + } + if (this.mpType === 'page' || this.mpType === 'component') { + var mpInstance = this.$scope; + var data = Object.create(null); + try { + data = cloneWithData(this); + } catch (err) { + console.error(err); + } + data.__webviewId__ = mpInstance.data.__webviewId__; + var mpData = Object.create(null); + Object.keys(data).forEach(function (key) { //仅同步 data 中有的数据 + mpData[key] = mpInstance.data[key]; + }); + var diffData = this.$shouldDiffData === false ? data : diff(data, mpData); + if (Object.keys(diffData).length) { + if (process.env.VUE_APP_DEBUG) { + console.log('[' + (+new Date) + '][' + (mpInstance.is || mpInstance.route) + '][' + this._uid + + ']差量更新', + JSON.stringify(diffData)); + } + this.__next_tick_pending = true; + mpInstance.setData(diffData, function () { + this$1.__next_tick_pending = false; + flushCallbacks$1(this$1); + }); + } else { + flushCallbacks$1(this); + } + } }; /* */ @@ -5794,250 +5794,250 @@ function normalizeStyleBinding (bindingStyle) { return bindingStyle } -/* */ - -var MP_METHODS = ['createSelectorQuery', 'createIntersectionObserver', 'selectAllComponents', 'selectComponent']; - -function getTarget(obj, path) { - var parts = path.split('.'); - var key = parts[0]; - if (key.indexOf('__$n') === 0) { //number index - key = parseInt(key.replace('__$n', '')); - } - if (parts.length === 1) { - return obj[key] - } - return getTarget(obj[key], parts.slice(1).join('.')) -} - -function internalMixin(Vue) { - - Vue.config.errorHandler = function(err, vm, info) { - Vue.util.warn(("Error in " + info + ": \"" + (err.toString()) + "\""), vm); - console.error(err); - /* eslint-disable no-undef */ - var app = getApp(); - if (app && app.onError) { - app.onError(err); - } - }; - - var oldEmit = Vue.prototype.$emit; - - Vue.prototype.$emit = function(event) { - if (this.$scope && event) { - this.$scope['triggerEvent'](event, { - __args__: toArray(arguments, 1) - }); - } - return oldEmit.apply(this, arguments) - }; - - Vue.prototype.$nextTick = function(fn) { - return nextTick$1(this, fn) - }; - - MP_METHODS.forEach(function (method) { - Vue.prototype[method] = function(args) { - if (this.$scope && this.$scope[method]) { - return this.$scope[method](args) - } - // mp-alipay - if (typeof my === 'undefined') { - return - } - if (method === 'createSelectorQuery') { - /* eslint-disable no-undef */ - return my.createSelectorQuery(args) - } else if (method === 'createIntersectionObserver') { - /* eslint-disable no-undef */ - return my.createIntersectionObserver(args) - } - // TODO mp-alipay 暂不支持 selectAllComponents,selectComponent - }; - }); - - Vue.prototype.__init_provide = initProvide; - - Vue.prototype.__init_injections = initInjections; - - Vue.prototype.__call_hook = function(hook, args) { - var vm = this; - // #7573 disable dep collection when invoking lifecycle hooks - pushTarget(); - var handlers = vm.$options[hook]; - var info = hook + " hook"; - var ret; - if (handlers) { - for (var i = 0, j = handlers.length; i < j; i++) { - ret = invokeWithErrorHandling(handlers[i], vm, args ? [args] : null, vm, info); - } - } - if (vm._hasHookEvent) { - vm.$emit('hook:' + hook, args); - } - popTarget(); - return ret - }; - - Vue.prototype.__set_model = function(target, key, value, modifiers) { - if (Array.isArray(modifiers)) { - if (modifiers.indexOf('trim') !== -1) { - value = value.trim(); - } - if (modifiers.indexOf('number') !== -1) { - value = this._n(value); - } - } - if (!target) { - target = this; - } - target[key] = value; - }; - - Vue.prototype.__set_sync = function(target, key, value) { - if (!target) { - target = this; - } - target[key] = value; - }; - - Vue.prototype.__get_orig = function(item) { - if (isPlainObject(item)) { - return item['$orig'] || item - } - return item - }; - - Vue.prototype.__get_value = function(dataPath, target) { - return getTarget(target || this, dataPath) - }; - - - Vue.prototype.__get_class = function(dynamicClass, staticClass) { - return renderClass(staticClass, dynamicClass) - }; - - Vue.prototype.__get_style = function(dynamicStyle, staticStyle) { - if (!dynamicStyle && !staticStyle) { - return '' - } - var dynamicStyleObj = normalizeStyleBinding(dynamicStyle); - var styleObj = staticStyle ? extend(staticStyle, dynamicStyleObj) : dynamicStyleObj; - return Object.keys(styleObj).map(function (name) { return ((hyphenate(name)) + ":" + (styleObj[name])); }).join(';') - }; - - Vue.prototype.__map = function(val, iteratee) { - //TODO 暂不考虑 string - var ret, i, l, keys, key; - if (Array.isArray(val)) { - ret = new Array(val.length); - for (i = 0, l = val.length; i < l; i++) { - ret[i] = iteratee(val[i], i); - } - return ret - } else if (isObject(val)) { - keys = Object.keys(val); - ret = Object.create(null); - for (i = 0, l = keys.length; i < l; i++) { - key = keys[i]; - ret[key] = iteratee(val[key], key, i); - } - return ret - } else if (typeof val === 'number') { - ret = new Array(val); - for (i = 0, l = val; i < l; i++) { - // 第一个参数暂时仍和小程序一致 - ret[i] = iteratee(i, i); - } - return ret - } - return [] - }; - -} - -/* */ - -var LIFECYCLE_HOOKS$1 = [ - //App - 'onLaunch', - 'onShow', - 'onHide', - 'onUniNViewMessage', - 'onPageNotFound', - 'onThemeChange', - 'onError', - 'onUnhandledRejection', - //Page - 'onInit', - 'onLoad', - // 'onShow', - 'onReady', - // 'onHide', - 'onUnload', - 'onPullDownRefresh', - 'onReachBottom', - 'onTabItemTap', - 'onAddToFavorites', - 'onShareTimeline', - 'onShareAppMessage', - 'onResize', - 'onPageScroll', - 'onNavigationBarButtonTap', - 'onBackPress', - 'onNavigationBarSearchInputChanged', - 'onNavigationBarSearchInputConfirmed', - 'onNavigationBarSearchInputClicked', - //Component - // 'onReady', // 兼容旧版本,应该移除该事件 - 'onPageShow', - 'onPageHide', - 'onPageResize' -]; -function lifecycleMixin$1(Vue) { - - //fixed vue-class-component - var oldExtend = Vue.extend; - Vue.extend = function(extendOptions) { - extendOptions = extendOptions || {}; - - var methods = extendOptions.methods; - if (methods) { - Object.keys(methods).forEach(function (methodName) { - if (LIFECYCLE_HOOKS$1.indexOf(methodName)!==-1) { - extendOptions[methodName] = methods[methodName]; - delete methods[methodName]; - } - }); - } - - return oldExtend.call(this, extendOptions) - }; - - var strategies = Vue.config.optionMergeStrategies; - var mergeHook = strategies.created; - LIFECYCLE_HOOKS$1.forEach(function (hook) { - strategies[hook] = mergeHook; - }); - - Vue.prototype.__lifecycle_hooks__ = LIFECYCLE_HOOKS$1; -} - -/* */ - -// install platform patch function -Vue.prototype.__patch__ = patch; - -// public mount method -Vue.prototype.$mount = function( - el , - hydrating -) { - return mountComponent$1(this, el, hydrating) -}; - -lifecycleMixin$1(Vue); +/* */ + +var MP_METHODS = ['createSelectorQuery', 'createIntersectionObserver', 'selectAllComponents', 'selectComponent']; + +function getTarget(obj, path) { + var parts = path.split('.'); + var key = parts[0]; + if (key.indexOf('__$n') === 0) { //number index + key = parseInt(key.replace('__$n', '')); + } + if (parts.length === 1) { + return obj[key] + } + return getTarget(obj[key], parts.slice(1).join('.')) +} + +function internalMixin(Vue) { + + Vue.config.errorHandler = function(err, vm, info) { + Vue.util.warn(("Error in " + info + ": \"" + (err.toString()) + "\""), vm); + console.error(err); + /* eslint-disable no-undef */ + var app = typeof getApp === 'function' && getApp(); + if (app && app.onError) { + app.onError(err); + } + }; + + var oldEmit = Vue.prototype.$emit; + + Vue.prototype.$emit = function(event) { + if (this.$scope && event) { + this.$scope['triggerEvent'](event, { + __args__: toArray(arguments, 1) + }); + } + return oldEmit.apply(this, arguments) + }; + + Vue.prototype.$nextTick = function(fn) { + return nextTick$1(this, fn) + }; + + MP_METHODS.forEach(function (method) { + Vue.prototype[method] = function(args) { + if (this.$scope && this.$scope[method]) { + return this.$scope[method](args) + } + // mp-alipay + if (typeof my === 'undefined') { + return + } + if (method === 'createSelectorQuery') { + /* eslint-disable no-undef */ + return my.createSelectorQuery(args) + } else if (method === 'createIntersectionObserver') { + /* eslint-disable no-undef */ + return my.createIntersectionObserver(args) + } + // TODO mp-alipay 暂不支持 selectAllComponents,selectComponent + }; + }); + + Vue.prototype.__init_provide = initProvide; + + Vue.prototype.__init_injections = initInjections; + + Vue.prototype.__call_hook = function(hook, args) { + var vm = this; + // #7573 disable dep collection when invoking lifecycle hooks + pushTarget(); + var handlers = vm.$options[hook]; + var info = hook + " hook"; + var ret; + if (handlers) { + for (var i = 0, j = handlers.length; i < j; i++) { + ret = invokeWithErrorHandling(handlers[i], vm, args ? [args] : null, vm, info); + } + } + if (vm._hasHookEvent) { + vm.$emit('hook:' + hook, args); + } + popTarget(); + return ret + }; + + Vue.prototype.__set_model = function(target, key, value, modifiers) { + if (Array.isArray(modifiers)) { + if (modifiers.indexOf('trim') !== -1) { + value = value.trim(); + } + if (modifiers.indexOf('number') !== -1) { + value = this._n(value); + } + } + if (!target) { + target = this; + } + target[key] = value; + }; + + Vue.prototype.__set_sync = function(target, key, value) { + if (!target) { + target = this; + } + target[key] = value; + }; + + Vue.prototype.__get_orig = function(item) { + if (isPlainObject(item)) { + return item['$orig'] || item + } + return item + }; + + Vue.prototype.__get_value = function(dataPath, target) { + return getTarget(target || this, dataPath) + }; + + + Vue.prototype.__get_class = function(dynamicClass, staticClass) { + return renderClass(staticClass, dynamicClass) + }; + + Vue.prototype.__get_style = function(dynamicStyle, staticStyle) { + if (!dynamicStyle && !staticStyle) { + return '' + } + var dynamicStyleObj = normalizeStyleBinding(dynamicStyle); + var styleObj = staticStyle ? extend(staticStyle, dynamicStyleObj) : dynamicStyleObj; + return Object.keys(styleObj).map(function (name) { return ((hyphenate(name)) + ":" + (styleObj[name])); }).join(';') + }; + + Vue.prototype.__map = function(val, iteratee) { + //TODO 暂不考虑 string + var ret, i, l, keys, key; + if (Array.isArray(val)) { + ret = new Array(val.length); + for (i = 0, l = val.length; i < l; i++) { + ret[i] = iteratee(val[i], i); + } + return ret + } else if (isObject(val)) { + keys = Object.keys(val); + ret = Object.create(null); + for (i = 0, l = keys.length; i < l; i++) { + key = keys[i]; + ret[key] = iteratee(val[key], key, i); + } + return ret + } else if (typeof val === 'number') { + ret = new Array(val); + for (i = 0, l = val; i < l; i++) { + // 第一个参数暂时仍和小程序一致 + ret[i] = iteratee(i, i); + } + return ret + } + return [] + }; + +} + +/* */ + +var LIFECYCLE_HOOKS$1 = [ + //App + 'onLaunch', + 'onShow', + 'onHide', + 'onUniNViewMessage', + 'onPageNotFound', + 'onThemeChange', + 'onError', + 'onUnhandledRejection', + //Page + 'onInit', + 'onLoad', + // 'onShow', + 'onReady', + // 'onHide', + 'onUnload', + 'onPullDownRefresh', + 'onReachBottom', + 'onTabItemTap', + 'onAddToFavorites', + 'onShareTimeline', + 'onShareAppMessage', + 'onResize', + 'onPageScroll', + 'onNavigationBarButtonTap', + 'onBackPress', + 'onNavigationBarSearchInputChanged', + 'onNavigationBarSearchInputConfirmed', + 'onNavigationBarSearchInputClicked', + //Component + // 'onReady', // 兼容旧版本,应该移除该事件 + 'onPageShow', + 'onPageHide', + 'onPageResize' +]; +function lifecycleMixin$1(Vue) { + + //fixed vue-class-component + var oldExtend = Vue.extend; + Vue.extend = function(extendOptions) { + extendOptions = extendOptions || {}; + + var methods = extendOptions.methods; + if (methods) { + Object.keys(methods).forEach(function (methodName) { + if (LIFECYCLE_HOOKS$1.indexOf(methodName)!==-1) { + extendOptions[methodName] = methods[methodName]; + delete methods[methodName]; + } + }); + } + + return oldExtend.call(this, extendOptions) + }; + + var strategies = Vue.config.optionMergeStrategies; + var mergeHook = strategies.created; + LIFECYCLE_HOOKS$1.forEach(function (hook) { + strategies[hook] = mergeHook; + }); + + Vue.prototype.__lifecycle_hooks__ = LIFECYCLE_HOOKS$1; +} + +/* */ + +// install platform patch function +Vue.prototype.__patch__ = patch; + +// public mount method +Vue.prototype.$mount = function( + el , + hydrating +) { + return mountComponent$1(this, el, hydrating) +}; + +lifecycleMixin$1(Vue); internalMixin(Vue); /* */ diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js index b587e892080f4218bf23160e752b7bcc395ad5d9..bee315f5459b58fd5be921bbd3b040d215e490b6 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js @@ -184,7 +184,7 @@ module.exports = function generateJson (compilation) { delete jsonObj.navigationBarShadow } - if (process.env.UNI_SUBPACKGE && jsonObj.usingComponents) { + if ((process.env.UNI_SUBPACKGE || process.env.UNI_MP_PLUGIN) && jsonObj.usingComponents) { jsonObj.usingComponents = normalizeUsingComponents(name, jsonObj.usingComponents) } const source = JSON.stringify(jsonObj, null, 2) diff --git a/src/core/runtime/index.js b/src/core/runtime/index.js index 2604ddec7102ddcdd96d4c3cc125247062504d07..0f0c8f515f2ca6f130b375979b8fddf68a953ad4 100644 --- a/src/core/runtime/index.js +++ b/src/core/runtime/index.js @@ -26,8 +26,9 @@ import { import createApp from './wrapper/create-app' import createPage from './wrapper/create-page' -import createComponent from './wrapper/create-component' +import createComponent from './wrapper/create-component' import createSubpackageApp from './wrapper/create-subpackage-app' +import createPlugin from './wrapper/create-plugin' todos.forEach(todoApi => { protocols[todoApi] = false @@ -115,13 +116,15 @@ if (__PLATFORM__ === 'app-plus') { __GLOBAL__.createApp = createApp __GLOBAL__.createPage = createPage __GLOBAL__.createComponent = createComponent -__GLOBAL__.createSubpackageApp = createSubpackageApp +__GLOBAL__.createSubpackageApp = createSubpackageApp +__GLOBAL__.createPlugin = createPlugin export { createApp, createPage, - createComponent, - createSubpackageApp + createComponent, + createSubpackageApp, + createPlugin } -export default uni +export default uni diff --git a/src/core/runtime/wrapper/create-plugin.js b/src/core/runtime/wrapper/create-plugin.js new file mode 100644 index 0000000000000000000000000000000000000000..98a28c4dac3e8768d2af351af6922ab502838cc3 --- /dev/null +++ b/src/core/runtime/wrapper/create-plugin.js @@ -0,0 +1,26 @@ +import 'uni-platform/runtime/index' + +import { + isFn +} from 'uni-shared' + +import parseApp from 'uni-platform/runtime/wrapper/app-parser' + +export default function createPlugin (vm) { + const appOptions = parseApp(vm) + if (isFn(appOptions.onShow) && __GLOBAL__.onAppShow) { + __GLOBAL__.onAppShow((...args) => { + appOptions.onShow.apply(vm, args) + }) + } + if (isFn(appOptions.onHide) && __GLOBAL__.onAppHide) { + __GLOBAL__.onAppHide((...args) => { + appOptions.onHide.apply(vm, args) + }) + } + if (isFn(appOptions.onLaunch)) { + const args = __GLOBAL__.getLaunchOptionsSync && __GLOBAL__.getLaunchOptionsSync() + appOptions.onLaunch.call(vm, args) + } + return vm +} diff --git a/src/platforms/mp-weixin/runtime/index.js b/src/platforms/mp-weixin/runtime/index.js index 8d482b7f7d677263ff9c11c9cb4a9c5b71fe09aa..2e3a1fc711a600f4ccf4fa61349ed65828a38f99 100644 --- a/src/platforms/mp-weixin/runtime/index.js +++ b/src/platforms/mp-weixin/runtime/index.js @@ -14,7 +14,7 @@ const customize = cached((str) => { function initTriggerEvent (mpInstance) { if (__PLATFORM__ === 'mp-weixin' || __PLATFORM__ === 'app-plus') { - if (!wx.canIUse('nextTick')) { + if (!wx.canIUse || !wx.canIUse('nextTick')) { return } } @@ -49,4 +49,4 @@ if (!MPPage.__$wrappered) { initHook('created', options) return MPComponent(options) } -} +} diff --git a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js index 3d3acf33875bd90162d9c860e583f9e742c6ea81..5d1ee7632e3cf305036df437568f82fc82f3a858 100644 --- a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js +++ b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js @@ -69,7 +69,7 @@ export default function parseBaseApp (vm, { delete this.$options.mpType delete this.$options.mpInstance - if (this.mpType === 'page') { // hack vue-i18n + if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n const app = getApp() if (app.$vm && app.$vm.$i18n) { this._i18n = app.$vm.$i18n @@ -88,7 +88,7 @@ export default function parseBaseApp (vm, { return } if (__PLATFORM__ === 'mp-weixin' || __PLATFORM__ === 'mp-qq') { - if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 + if (wx.canIUse && !wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上') } } @@ -123,4 +123,4 @@ export default function parseBaseApp (vm, { initHooks(appOptions, hooks) return appOptions -} +}